blob: e6f1fc64dd3048f686ce2f1a8451d0296d555655 [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);
Willy Tarreau2542b532012-08-31 16:01:23 +020074
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020075 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020076 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020077
78 /* minimum stream initialization required for an embryonic stream is
79 * fairly low. We need very little to execute L4 ACLs, then we need a
80 * task to make the client-side connection live on its own.
81 * - flags
82 * - stick-entry tracking
83 */
84 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020085 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020086 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020087 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
88 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
89 tv_zero(&s->logs.tv_request);
90 s->logs.t_queue = -1;
91 s->logs.t_connect = -1;
92 s->logs.t_data = -1;
93 s->logs.t_close = 0;
94 s->logs.bytes_in = s->logs.bytes_out = 0;
95 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
96 s->logs.srv_queue_size = 0; /* we will get this number soon */
97
98 /* default logging function */
99 s->do_log = strm_log;
100
101 /* default error reporting function, may be changed by analysers */
102 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200103
104 /* Initialise the current rule list pointer to NULL. We are sure that
105 * any rulelist match the NULL pointer.
106 */
107 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200108 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200109
110 memset(s->stkctr, 0, sizeof(s->stkctr));
111
112 s->sess = sess;
113 s->si[0].flags = SI_FL_NONE;
114 s->si[1].flags = SI_FL_ISBACK;
115
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200116 s->uniq_id = global.req_count++;
117
Willy Tarreau87b09662015-04-03 00:22:06 +0200118 /* OK, we're keeping the stream, so let's properly initialize the stream */
119 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200120 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100121 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200122
Willy Tarreaue7dff022015-04-03 01:14:29 +0200123 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200124 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200125
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200126 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200127 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200128 t->context = s;
129 t->expire = TICK_ETERNITY;
130
Willy Tarreau87b09662015-04-03 00:22:06 +0200131 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200132 * This changes later when switching rules are executed or
133 * when the default backend is assigned.
134 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200135 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200136 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100137 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200138 s->req_cap = NULL;
139 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200140
Willy Tarreauebcd4842015-06-19 11:59:02 +0200141 /* Initialise all the variables contexts even if not used.
142 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200143 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200144 vars_init(&s->vars_txn, SCOPE_TXN);
145 vars_init(&s->vars_reqres, SCOPE_REQ);
146
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200147 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100148 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200149 si_set_state(&s->si[0], SI_ST_EST);
150
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200151 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200152 if (conn)
153 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200154 else if (appctx)
155 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200156
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200157 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200158 s->si[0].flags |= SI_FL_INDEP_STR;
159
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200160 /* pre-initialize the other side's stream interface to an INIT state. The
161 * callbacks will be initialized before attempting to connect.
162 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100163 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200164
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200165 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200166 s->si[1].flags |= SI_FL_INDEP_STR;
167
Willy Tarreau87b09662015-04-03 00:22:06 +0200168 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200169 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200170 s->pend_pos = NULL;
171
172 /* init store persistence */
173 s->store_count = 0;
174
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100175 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100176 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200177 s->req.analysers = 0;
178 channel_auto_connect(&s->req); /* don't wait to establish connection */
179 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200180
181 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100182 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100183 s->req.rex = TICK_ETERNITY;
184 s->req.wex = TICK_ETERNITY;
185 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200186
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100187 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100188 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100189 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200191 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100192 s->req.flags |= CF_NEVER_WAIT;
193 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200194 }
195
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200196 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100197 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100198 s->res.rex = TICK_ETERNITY;
199 s->res.wex = TICK_ETERNITY;
200 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200201
Willy Tarreaueee5b512015-04-03 23:46:31 +0200202 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100203
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100204 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100205
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200206 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200207 if (conn)
208 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200209 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200210 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200211
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200212 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200213 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200214
215 /* it is important not to call the wakeup function directly but to
216 * pass through task_wakeup(), because this one knows how to apply
217 * priorities to tasks.
218 */
219 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200220 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200221
222 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200223 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100224 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200225 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200226 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200227}
228
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200230 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200232static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200234 struct session *sess = strm_sess(s);
235 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100236 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200237 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200238 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100239
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240 if (s->pend_pos)
241 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100242
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100243 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200244 if (s->flags & SF_CURR_SESS) {
245 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100246 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100247 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100248 if (may_dequeue_tasks(objt_server(s->target), s->be))
249 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100250 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100251
Willy Tarreau7c669d72008-06-20 15:04:11 +0200252 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200253 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200254 * it should normally be only the same as the one above,
255 * so this should not happen in fact.
256 */
257 sess_change_server(s, NULL);
258 }
259
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100260 if (s->req.pipe)
261 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100262
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100263 if (s->res.pipe)
264 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100265
Willy Tarreaubf883e02014-11-25 21:10:35 +0100266 /* We may still be present in the buffer wait queue */
267 if (!LIST_ISEMPTY(&s->buffer_wait)) {
268 LIST_DEL(&s->buffer_wait);
269 LIST_INIT(&s->buffer_wait);
270 }
271
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100272 b_drop(&s->req.buf);
273 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100274 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200275 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200276
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100277 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200278 if (s->txn)
279 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100280
Willy Tarreau1e954912012-10-12 17:50:05 +0200281 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200282 if (cli_conn)
283 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200284
Willy Tarreaua4cda672010-06-06 18:28:49 +0200285 for (i = 0; i < s->store_count; i++) {
286 if (!s->store[i].ts)
287 continue;
288 stksess_free(s->store[i].table, s->store[i].ts);
289 s->store[i].ts = NULL;
290 }
291
Willy Tarreaueee5b512015-04-03 23:46:31 +0200292 if (s->txn) {
293 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
294 pool_free2(pool2_http_txn, s->txn);
295 s->txn = NULL;
296 }
297
Willy Tarreau92fb9832007-10-16 17:34:28 +0200298 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200299 pool_free2(fe->rsp_cap_pool, s->res_cap);
300 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100302
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200303 /* Cleanup all variable contexts. */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200304 vars_prune(&s->vars_txn, s);
305 vars_prune(&s->vars_reqres, s);
306
Willy Tarreau87b09662015-04-03 00:22:06 +0200307 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200308
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100309 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100310 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200311 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100312 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100313 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100314 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200315 if (s->list.n != &streams)
316 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100317 bref->ref = s->list.n;
318 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100319 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200320 si_release_endpoint(&s->si[1]);
321 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200322
323 /* FIXME: for now we have a 1:1 relation between stream and session so
324 * the stream must free the session.
325 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200326 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200327 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200328
329 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200330 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200331 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200332 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200333 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200334 pool_flush2(pool2_requri);
335 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200336 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200337 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100338 pool_flush2(pool2_connection);
339 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200340 pool_flush2(fe->req_cap_pool);
341 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200342 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200343}
344
Willy Tarreau78955f42014-12-28 13:09:02 +0100345/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
346 * that it's not the last available buffer or it's the response buffer. Unless
347 * the buffer is the response buffer, an extra control is made so that we always
348 * keep <tune.buffers.reserved> buffers available after this allocation. To be
349 * called at the beginning of recv() callbacks to ensure that the required
350 * buffers are properly allocated. Returns 0 in case of failure, non-zero
351 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100352 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200353int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100354{
Willy Tarreau87b09662015-04-03 00:22:06 +0200355 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100356 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100357 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100358
Willy Tarreau78955f42014-12-28 13:09:02 +0100359 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100360 margin = global.tune.reserved_bufs;
361
Willy Tarreau78955f42014-12-28 13:09:02 +0100362 s = chn_sess(chn);
363
364 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100365 if (b)
366 return 1;
367
Willy Tarreaubf883e02014-11-25 21:10:35 +0100368 if (LIST_ISEMPTY(&s->buffer_wait))
369 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100370 return 0;
371}
372
Willy Tarreau87b09662015-04-03 00:22:06 +0200373/* Allocates a work buffer for stream <s>. It is meant to be called inside
374 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200375 * to work fine, which is the response buffer so that an error message may be
376 * built and returned. Response buffers may be allocated from the reserve, this
377 * is critical to ensure that a response may always flow and will never block a
378 * server from releasing a connection. Returns 0 in case of failure, non-zero
379 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100380 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200381int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100382{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100383 if (!LIST_ISEMPTY(&s->buffer_wait)) {
384 LIST_DEL(&s->buffer_wait);
385 LIST_INIT(&s->buffer_wait);
386 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100387
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200388 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100389 return 1;
390
Willy Tarreaubf883e02014-11-25 21:10:35 +0100391 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100392 return 0;
393}
394
395/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100396 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200397 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100398 * a single buffer, so it makes sense to try to wake two up when two buffers
399 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100400 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200401void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100402{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100403 if (s->req.buf->size && buffer_empty(s->req.buf))
404 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100405
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100406 if (s->res.buf->size && buffer_empty(s->res.buf))
407 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100408
Willy Tarreaubf883e02014-11-25 21:10:35 +0100409 /* if we're certain to have at least 1 buffer available, and there is
410 * someone waiting, we can wake up a waiter and offer them.
411 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100412 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200413 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100414}
415
Willy Tarreau87b09662015-04-03 00:22:06 +0200416/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100417 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200418 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100419 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200420void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100421{
Willy Tarreau87b09662015-04-03 00:22:06 +0200422 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423
424 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100425 if (rqlimit <= run_queue)
426 break;
427
Willy Tarreaubf883e02014-11-25 21:10:35 +0100428 if (sess->task->state & TASK_RUNNING)
429 continue;
430
431 LIST_DEL(&sess->buffer_wait);
432 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100433 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100434 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100435}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200436
437/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200438int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200439{
Willy Tarreau87b09662015-04-03 00:22:06 +0200440 LIST_INIT(&streams);
441 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
442 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200443}
444
Willy Tarreau87b09662015-04-03 00:22:06 +0200445void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100446{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200447 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100448 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200449 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100450 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100451
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100452 bytes = s->req.total - s->logs.bytes_in;
453 s->logs.bytes_in = s->req.total;
454 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200455 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100456
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100457 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100458
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100459 if (objt_server(s->target))
460 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200461
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200462 if (sess->listener && sess->listener->counters)
463 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200464
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100465 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200466 struct stkctr *stkctr = &s->stkctr[i];
467
468 if (!stkctr_entry(stkctr)) {
469 stkctr = &sess->stkctr[i];
470 if (!stkctr_entry(stkctr))
471 continue;
472 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200473
Emeric Brun57056f02015-06-15 18:29:57 +0200474 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
475 if (ptr1)
476 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200477
Emeric Brun57056f02015-06-15 18:29:57 +0200478 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
479 if (ptr2)
480 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200481 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200482
483 /* If data was modified, we need to touch to re-schedule sync */
484 if (ptr1 || ptr2)
485 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100486 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100487 }
488
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100489 bytes = s->res.total - s->logs.bytes_out;
490 s->logs.bytes_out = s->res.total;
491 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200492 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100493
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100494 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100495
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100496 if (objt_server(s->target))
497 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200498
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200499 if (sess->listener && sess->listener->counters)
500 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200501
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100502 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200503 struct stkctr *stkctr = &s->stkctr[i];
504
505 if (!stkctr_entry(stkctr)) {
506 stkctr = &sess->stkctr[i];
507 if (!stkctr_entry(stkctr))
508 continue;
509 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200510
Emeric Brun57056f02015-06-15 18:29:57 +0200511 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
512 if (ptr1)
513 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200514
Emeric Brun57056f02015-06-15 18:29:57 +0200515 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
516 if (ptr2)
517 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200518 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200519
520 /* If data was modified, we need to touch to re-schedule sync */
521 if (ptr1 || ptr2)
522 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100523 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100524 }
525}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200526
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100527/* This function is called with (si->state == SI_ST_CON) meaning that a
528 * connection was attempted and that the file descriptor is already allocated.
529 * We must check for establishment, error and abort. Possible output states
530 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
531 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200532 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100533 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200534static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100536 struct stream_interface *si = &s->si[1];
537 struct channel *req = &s->req;
538 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200539 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100540
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541 /* If we got an error, or if nothing happened and the connection timed
542 * out, we must give up. The CER state handler will take care of retry
543 * attempts and error reports.
544 */
545 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100546 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100547 /* Some data were sent past the connection establishment,
548 * so we need to pretend we're established to log correctly
549 * and let later states handle the failure.
550 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100551 si->state = SI_ST_EST;
552 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100553 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100554 return 1;
555 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100556 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100557 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100558
Willy Tarreauf79c8172013-10-21 16:30:56 +0200559 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560
561 if (si->err_type)
562 return 0;
563
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100564 if (si->flags & SI_FL_ERR)
565 si->err_type = SI_ET_CONN_ERR;
566 else
567 si->err_type = SI_ET_CONN_TO;
568 return 0;
569 }
570
571 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100572 if (!(req->flags & CF_WRITE_PARTIAL) &&
573 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200574 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
575 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100576 s->be->options & PR_O_ABRT_CLOSE)))) {
577 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200578 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100579 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100580 if (s->srv_error)
581 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100582 return 1;
583 }
584
585 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200586 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 return 1;
588
589 /* OK, this means that a connection succeeded. The caller will be
590 * responsible for handling the transition from CON to EST.
591 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100592 si->state = SI_ST_EST;
593 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100594 return 1;
595}
596
597/* This function is called with (si->state == SI_ST_CER) meaning that a
598 * previous connection attempt has failed and that the file descriptor
599 * has already been released. Possible causes include asynchronous error
600 * notification and time out. Possible output states are SI_ST_CLO when
601 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
602 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
603 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
604 * marked as in error state. It returns 0.
605 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200606static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100608 struct stream_interface *si = &s->si[1];
609
Willy Tarreau87b09662015-04-03 00:22:06 +0200610 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100611 if (objt_server(s->target)) {
612 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100613
Willy Tarreaue7dff022015-04-03 01:14:29 +0200614 if (s->flags & SF_CURR_SESS) {
615 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100616 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100618 }
619
620 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200621 si->conn_retries--;
622 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100623 if (!si->err_type) {
624 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625 }
626
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100627 if (objt_server(s->target))
628 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100629 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100630 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100631 if (may_dequeue_tasks(objt_server(s->target), s->be))
632 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100633
634 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200635 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100636 s->req.flags |= CF_WRITE_ERROR;
637 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638
639 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100640 if (s->srv_error)
641 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 return 0;
643 }
644
645 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700646 * retry on another server. By default this redispatch occurs on the
647 * last retry, but if configured we allow redispatches to occur on
648 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200649 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100650 * bit to ignore any persistence cookie. We won't count a retry nor a
651 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200652 * If the connection is not persistent, the balancing algorithm is not
653 * determinist (round robin) and there is more than one active server,
654 * we accept to perform an immediate redispatch without waiting since
655 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100656 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200657 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700658 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
659 ((((s->be->redispatch_after > 0) &&
660 ((s->be->conn_retries - si->conn_retries) %
661 s->be->redispatch_after == 0)) ||
662 ((s->be->redispatch_after < 0) &&
663 ((s->be->conn_retries - si->conn_retries) %
664 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200665 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700666 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100667 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100668 if (may_dequeue_tasks(objt_server(s->target), s->be))
669 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100670
Willy Tarreaue7dff022015-04-03 01:14:29 +0200671 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100672 si->state = SI_ST_REQ;
673 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100674 if (objt_server(s->target))
675 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100676 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100677 si->state = SI_ST_ASS;
678 }
679
680 if (si->flags & SI_FL_ERR) {
681 /* The error was an asynchronous connection error, and we will
682 * likely have to retry connecting to the same server, most
683 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200684 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100685 */
686
Willy Tarreaub0290662014-06-13 17:04:44 +0200687 int delay = 1000;
688
689 if (s->be->timeout.connect && s->be->timeout.connect < delay)
690 delay = s->be->timeout.connect;
691
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 if (!si->err_type)
693 si->err_type = SI_ET_CONN_ERR;
694
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200695 /* only wait when we're retrying on the same server */
696 if (si->state == SI_ST_ASS ||
697 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
698 (s->be->srv_act <= 1)) {
699 si->state = SI_ST_TAR;
700 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
701 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100702 return 0;
703 }
704 return 0;
705}
706
707/*
708 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200709 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200710 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200712static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100713{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100714 struct stream_interface *si = &s->si[1];
715 struct channel *req = &s->req;
716 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100717
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100718 /* First, centralize the timers information */
719 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
720 si->exp = TICK_ETERNITY;
721
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100722 if (objt_server(s->target))
723 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100724
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100725 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 /* if the user wants to log as soon as possible, without counting
727 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200728 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100729 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100730 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100731 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732 }
733 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100734 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 }
736
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200737 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200738 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200739 if (req->flags & CF_WAKE_CONNECT) {
740 req->flags |= CF_WAKE_ONCE;
741 req->flags &= ~CF_WAKE_CONNECT;
742 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200743 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200744 /* real connections have timeouts */
745 req->wto = s->be->timeout.server;
746 rep->rto = s->be->timeout.server;
747 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748 req->wex = TICK_ETERNITY;
749}
750
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100751/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
752 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100753 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
754 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
755 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100756 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200757static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100759 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100760 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100761 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100762
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100763 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 +0100764 now_ms, __FUNCTION__,
765 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400766 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100767 req->rex, s->res.wex,
768 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400769 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 +0100770
771 if (si->state == SI_ST_ASS) {
772 /* Server assigned to connection request, we have to try to connect now */
773 int conn_err;
774
775 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100776 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100777
Willy Tarreaue7dff022015-04-03 01:14:29 +0200778 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100779 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100780 if (srv)
781 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500782 if (srv)
783 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100784 return;
785 }
786
787 /* We have received a synchronous error. We might have to
788 * abort, retry immediately or redispatch.
789 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200790 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100791 if (!si->err_type) {
792 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100793 }
794
Willy Tarreau827aee92011-03-10 16:55:02 +0100795 if (srv)
796 srv_inc_sess_ctr(srv);
797 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500798 srv_set_sess_last(srv);
799 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100800 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100801 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100802
Willy Tarreau87b09662015-04-03 00:22:06 +0200803 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100804 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100805 if (may_dequeue_tasks(srv, s->be))
806 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100807
808 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200809 si_shutr(si);
810 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100811 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100812
813 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
814
Willy Tarreau87b09662015-04-03 00:22:06 +0200815 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100817 if (s->srv_error)
818 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100819 return;
820 }
821
822 /* We are facing a retryable error, but we don't want to run a
823 * turn-around now, as the problem is likely a source port
824 * allocation problem, so we want to retry now.
825 */
826 si->state = SI_ST_CER;
827 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100828 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
830 return;
831 }
832 else if (si->state == SI_ST_QUE) {
833 /* connection request was queued, check for any update */
834 if (!s->pend_pos) {
835 /* The connection is not in the queue anymore. Either
836 * we have a server connection slot available and we
837 * go directly to the assigned state, or we need to
838 * load-balance first and go to the INI state.
839 */
840 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200841 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 si->state = SI_ST_REQ;
843 else {
844 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
845 si->state = SI_ST_ASS;
846 }
847 return;
848 }
849
850 /* Connection request still in queue... */
851 if (si->flags & SI_FL_EXP) {
852 /* ... and timeout expired */
853 si->exp = TICK_ETERNITY;
854 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100855 if (srv)
856 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100857 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200858 si_shutr(si);
859 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100860 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100861 if (!si->err_type)
862 si->err_type = SI_ET_QUEUE_TO;
863 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100864 if (s->srv_error)
865 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866 return;
867 }
868
869 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100870 if ((req->flags & (CF_READ_ERROR)) ||
871 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
872 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100873 /* give up */
874 si->exp = TICK_ETERNITY;
875 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200876 si_shutr(si);
877 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100878 si->err_type |= SI_ET_QUEUE_ABRT;
879 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100880 if (s->srv_error)
881 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100882 return;
883 }
884
885 /* Nothing changed */
886 return;
887 }
888 else if (si->state == SI_ST_TAR) {
889 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100890 if ((req->flags & (CF_READ_ERROR)) ||
891 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
892 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100893 /* give up */
894 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200895 si_shutr(si);
896 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100897 si->err_type |= SI_ET_CONN_ABRT;
898 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100899 if (s->srv_error)
900 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100901 return;
902 }
903
904 if (!(si->flags & SI_FL_EXP))
905 return; /* still in turn-around */
906
907 si->exp = TICK_ETERNITY;
908
Willy Tarreau87b09662015-04-03 00:22:06 +0200909 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100910 * marked "assigned".
911 * FIXME: Should we force a redispatch attempt when the server is down ?
912 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200913 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100914 si->state = SI_ST_ASS;
915 else
916 si->state = SI_ST_REQ;
917 return;
918 }
919}
920
Willy Tarreau87b09662015-04-03 00:22:06 +0200921/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900922 * also counts a failed request if the server state has not reached the request
923 * stage.
924 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200925static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900926{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200927 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900928 if (s->si[1].state < SI_ST_REQ) {
929
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200930 strm_fe(s)->fe_counters.failed_req++;
931 if (strm_li(s)->counters)
932 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900933
Willy Tarreaue7dff022015-04-03 01:14:29 +0200934 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900935 }
936 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200937 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900938 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200939 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900940 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200941 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900942 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900944 }
945}
946
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100947/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100948 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
949 * a real connection to a server, indicating that a server has been assigned,
950 * or SI_ST_EST for a successful connection to an applet. It may also return
951 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100952 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200953static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100954{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100955 struct stream_interface *si = &s->si[1];
956
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100957 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 +0100958 now_ms, __FUNCTION__,
959 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400960 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100961 s->req.rex, s->res.wex,
962 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400963 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 +0100964
965 if (si->state != SI_ST_REQ)
966 return;
967
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100968 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
969 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100970 struct appctx *appctx = objt_appctx(si->end);
971
972 if (!appctx || appctx->applet != __objt_applet(s->target))
973 appctx = stream_int_register_handler(si, objt_applet(s->target));
974
975 if (!appctx) {
976 /* No more memory, let's immediately abort. Force the
977 * error code to ignore the ERR_LOCAL which is not a
978 * real error.
979 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100981
982 si_shutr(si);
983 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100984 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100985 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100986 si->state = SI_ST_CLO;
987 if (s->srv_error)
988 s->srv_error(s, si);
989 return;
990 }
991
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100992 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100993 si->state = SI_ST_EST;
994 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +0200995 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +0100996 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100997 return;
998 }
999
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001000 /* Try to assign a server */
1001 if (srv_redispatch_connect(s) != 0) {
1002 /* We did not get a server. Either we queued the
1003 * connection request, or we encountered an error.
1004 */
1005 if (si->state == SI_ST_QUE)
1006 return;
1007
1008 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001009 si_shutr(si);
1010 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001011 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001012 if (!si->err_type)
1013 si->err_type = SI_ET_CONN_OTHER;
1014 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001015 if (s->srv_error)
1016 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001017 return;
1018 }
1019
1020 /* The server is assigned */
1021 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1022 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001023 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001024}
1025
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001026/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001027 * if appropriate. The default_backend rule is also considered, then the
1028 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001029 * It returns 1 if the processing can continue on next analysers, or zero if it
1030 * either needs more data or wants to immediately abort the request.
1031 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001032static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001033{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001034 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001035 struct session *sess = s->sess;
1036 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001037
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001038 req->analysers &= ~an_bit;
1039 req->analyse_exp = TICK_ETERNITY;
1040
Willy Tarreau87b09662015-04-03 00:22:06 +02001041 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 +02001042 now_ms, __FUNCTION__,
1043 s,
1044 req,
1045 req->rex, req->wex,
1046 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001047 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001048 req->analysers);
1049
1050 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001051 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001052 struct switching_rule *rule;
1053
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001054 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001055 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001056
Willy Tarreauf51658d2014-04-23 01:21:56 +02001057 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001058 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001059 ret = acl_pass(ret);
1060 if (rule->cond->pol == ACL_COND_UNLESS)
1061 ret = !ret;
1062 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001063
1064 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001065 /* If the backend name is dynamic, try to resolve the name.
1066 * If we can't resolve the name, or if any error occurs, break
1067 * the loop and fallback to the default backend.
1068 */
1069 struct proxy *backend;
1070
1071 if (rule->dynamic) {
1072 struct chunk *tmp = get_trash_chunk();
1073 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1074 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001075 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001076 if (!backend)
1077 break;
1078 }
1079 else
1080 backend = rule->be.backend;
1081
Willy Tarreau87b09662015-04-03 00:22:06 +02001082 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001083 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001084 break;
1085 }
1086 }
1087
1088 /* To ensure correct connection accounting on the backend, we
1089 * have to assign one if it was not set (eg: a listen). This
1090 * measure also takes care of correctly setting the default
1091 * backend if any.
1092 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001093 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001094 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001095 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001096 }
1097
Willy Tarreaufb356202010-08-03 14:02:05 +02001098 /* 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 +02001099 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001100 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1101 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001102 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001103
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001104 /* 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 +02001105 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001106 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001107 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001108 int ret = 1;
1109
1110 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001111 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 +01001112 ret = acl_pass(ret);
1113 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1114 ret = !ret;
1115 }
1116
1117 if (ret) {
1118 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001119 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001120 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001121 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001122 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001123 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001124 break;
1125 }
1126 }
1127
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001128 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001129
1130 sw_failed:
1131 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001132 channel_abort(&s->req);
1133 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001134
Willy Tarreaue7dff022015-04-03 01:14:29 +02001135 if (!(s->flags & SF_ERR_MASK))
1136 s->flags |= SF_ERR_RESOURCE;
1137 if (!(s->flags & SF_FINST_MASK))
1138 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001139
Willy Tarreaueee5b512015-04-03 23:46:31 +02001140 if (s->txn)
1141 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001142 s->req.analysers = 0;
1143 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001144 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001145}
1146
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001147/* This stream analyser works on a request. It applies all use-server rules on
1148 * it then returns 1. The data must already be present in the buffer otherwise
1149 * they won't match. It always returns 1.
1150 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001151static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001152{
1153 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001154 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001155 struct server_rule *rule;
1156
Willy Tarreau87b09662015-04-03 00:22:06 +02001157 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 +02001158 now_ms, __FUNCTION__,
1159 s,
1160 req,
1161 req->rex, req->wex,
1162 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001163 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001164 req->analysers);
1165
Willy Tarreaue7dff022015-04-03 01:14:29 +02001166 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001167 list_for_each_entry(rule, &px->server_rules, list) {
1168 int ret;
1169
Willy Tarreau192252e2015-04-04 01:47:55 +02001170 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001171 ret = acl_pass(ret);
1172 if (rule->cond->pol == ACL_COND_UNLESS)
1173 ret = !ret;
1174
1175 if (ret) {
1176 struct server *srv = rule->srv.ptr;
1177
Willy Tarreau892337c2014-05-13 23:41:20 +02001178 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001179 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001180 (s->flags & SF_FORCE_PRST)) {
1181 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001182 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001183 break;
1184 }
1185 /* if the server is not UP, let's go on with next rules
1186 * just in case another one is suited.
1187 */
1188 }
1189 }
1190 }
1191
1192 req->analysers &= ~an_bit;
1193 req->analyse_exp = TICK_ETERNITY;
1194 return 1;
1195}
1196
Emeric Brun1d33b292010-01-04 15:47:17 +01001197/* This stream analyser works on a request. It applies all sticking rules on
1198 * it then returns 1. The data must already be present in the buffer otherwise
1199 * they won't match. It always returns 1.
1200 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001201static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001202{
1203 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001204 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001205 struct sticking_rule *rule;
1206
Willy Tarreau87b09662015-04-03 00:22:06 +02001207 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 +01001208 now_ms, __FUNCTION__,
1209 s,
1210 req,
1211 req->rex, req->wex,
1212 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001213 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001214 req->analysers);
1215
1216 list_for_each_entry(rule, &px->sticking_rules, list) {
1217 int ret = 1 ;
1218 int i;
1219
Willy Tarreau9667a802013-12-09 12:52:13 +01001220 /* Only the first stick store-request of each table is applied
1221 * and other ones are ignored. The purpose is to allow complex
1222 * configurations which look for multiple entries by decreasing
1223 * order of precision and to stop at the first which matches.
1224 * An example could be a store of the IP address from an HTTP
1225 * header first, then from the source if not found.
1226 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001227 for (i = 0; i < s->store_count; i++) {
1228 if (rule->table.t == s->store[i].table)
1229 break;
1230 }
1231
1232 if (i != s->store_count)
1233 continue;
1234
1235 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001236 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001237 ret = acl_pass(ret);
1238 if (rule->cond->pol == ACL_COND_UNLESS)
1239 ret = !ret;
1240 }
1241
1242 if (ret) {
1243 struct stktable_key *key;
1244
Willy Tarreau192252e2015-04-04 01:47:55 +02001245 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 +01001246 if (!key)
1247 continue;
1248
1249 if (rule->flags & STK_IS_MATCH) {
1250 struct stksess *ts;
1251
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001252 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001253 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001254 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001255 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001256
1257 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001258 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1259 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001260 if (node) {
1261 struct server *srv;
1262
1263 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001264 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001265 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001266 (s->flags & SF_FORCE_PRST)) {
1267 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001268 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001269 }
1270 }
1271 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001272 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001273 }
1274 }
1275 if (rule->flags & STK_IS_STORE) {
1276 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1277 struct stksess *ts;
1278
1279 ts = stksess_new(rule->table.t, key);
1280 if (ts) {
1281 s->store[s->store_count].table = rule->table.t;
1282 s->store[s->store_count++].ts = ts;
1283 }
1284 }
1285 }
1286 }
1287 }
1288
1289 req->analysers &= ~an_bit;
1290 req->analyse_exp = TICK_ETERNITY;
1291 return 1;
1292}
1293
1294/* This stream analyser works on a response. It applies all store rules on it
1295 * then returns 1. The data must already be present in the buffer otherwise
1296 * they won't match. It always returns 1.
1297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001298static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001299{
1300 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001301 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001302 struct sticking_rule *rule;
1303 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001304 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001305
Willy Tarreau87b09662015-04-03 00:22:06 +02001306 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 +01001307 now_ms, __FUNCTION__,
1308 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001309 rep,
1310 rep->rex, rep->wex,
1311 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001312 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001313 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001314
1315 list_for_each_entry(rule, &px->storersp_rules, list) {
1316 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001317
Willy Tarreau9667a802013-12-09 12:52:13 +01001318 /* Only the first stick store-response of each table is applied
1319 * and other ones are ignored. The purpose is to allow complex
1320 * configurations which look for multiple entries by decreasing
1321 * order of precision and to stop at the first which matches.
1322 * An example could be a store of a set-cookie value, with a
1323 * fallback to a parameter found in a 302 redirect.
1324 *
1325 * The store-response rules are not allowed to override the
1326 * store-request rules for the same table, but they may coexist.
1327 * Thus we can have up to one store-request entry and one store-
1328 * response entry for the same table at any time.
1329 */
1330 for (i = nbreq; i < s->store_count; i++) {
1331 if (rule->table.t == s->store[i].table)
1332 break;
1333 }
1334
1335 /* skip existing entries for this table */
1336 if (i < s->store_count)
1337 continue;
1338
Emeric Brun1d33b292010-01-04 15:47:17 +01001339 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001340 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001341 ret = acl_pass(ret);
1342 if (rule->cond->pol == ACL_COND_UNLESS)
1343 ret = !ret;
1344 }
1345
1346 if (ret) {
1347 struct stktable_key *key;
1348
Willy Tarreau192252e2015-04-04 01:47:55 +02001349 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 +01001350 if (!key)
1351 continue;
1352
Willy Tarreau37e340c2013-12-06 23:05:21 +01001353 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001354 struct stksess *ts;
1355
1356 ts = stksess_new(rule->table.t, key);
1357 if (ts) {
1358 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001359 s->store[s->store_count++].ts = ts;
1360 }
1361 }
1362 }
1363 }
1364
1365 /* process store request and store response */
1366 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001367 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001368 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001369
Willy Tarreauc93cd162014-05-13 15:54:22 +02001370 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001371 stksess_free(s->store[i].table, s->store[i].ts);
1372 s->store[i].ts = NULL;
1373 continue;
1374 }
1375
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001376 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1377 if (ts) {
1378 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001379 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001380 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001381 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001382 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001383 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001384
1385 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001386 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001387 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001388 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001389 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001390
1391 rep->analysers &= ~an_bit;
1392 rep->analyse_exp = TICK_ETERNITY;
1393 return 1;
1394}
1395
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001396/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001397 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001398 */
1399#define UPDATE_ANALYSERS(real, list, back, flag) { \
1400 list = (((list) & ~(flag)) | ~(back)) & (real); \
1401 back = real; \
1402 if (!(list)) \
1403 break; \
1404 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1405 continue; \
1406}
1407
Willy Tarreau87b09662015-04-03 00:22:06 +02001408/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001409 * then puts it back to the wait queue in a clean state, or cleans up its
1410 * resources if it must be deleted. Returns in <next> the date the task wants
1411 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1412 * nothing too many times, the request and response buffers flags are monitored
1413 * and each function is called only if at least another function has changed at
1414 * least one flag it is interested in.
1415 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001416struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001417{
Willy Tarreau827aee92011-03-10 16:55:02 +01001418 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001419 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001420 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001421 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001422 unsigned int rq_prod_last, rq_cons_last;
1423 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001424 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001425 struct channel *req, *res;
1426 struct stream_interface *si_f, *si_b;
1427
1428 req = &s->req;
1429 res = &s->res;
1430
1431 si_f = &s->si[0];
1432 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001433
1434 //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 +01001435 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001436
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001437 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001438 if (s->txn)
1439 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001440
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001441 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001442 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1443 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001444
1445 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001446 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1447 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001448
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001449 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001450 si_f->flags |= SI_FL_DONT_WAKE;
1451 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001452
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001453 /* 1a: Check for low level timeouts if needed. We just set a flag on
1454 * stream interfaces when their timeouts have expired.
1455 */
1456 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001457 stream_int_check_timeouts(si_f);
1458 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001459
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001460 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001461 * for future reads or writes. Note: this will also concern upper layers
1462 * but we do not touch any other flag. We must be careful and correctly
1463 * detect state changes when calling them.
1464 */
1465
Willy Tarreau8f128b42014-11-28 15:07:47 +01001466 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001467
Willy Tarreau8f128b42014-11-28 15:07:47 +01001468 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1469 si_b->flags |= SI_FL_NOLINGER;
1470 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001471 }
1472
Willy Tarreau8f128b42014-11-28 15:07:47 +01001473 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1474 if (si_f->flags & SI_FL_NOHALF)
1475 si_f->flags |= SI_FL_NOLINGER;
1476 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001477 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001478
Willy Tarreau8f128b42014-11-28 15:07:47 +01001479 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001480
Willy Tarreau8f128b42014-11-28 15:07:47 +01001481 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1482 si_f->flags |= SI_FL_NOLINGER;
1483 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001484 }
1485
Willy Tarreau8f128b42014-11-28 15:07:47 +01001486 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1487 if (si_b->flags & SI_FL_NOHALF)
1488 si_b->flags |= SI_FL_NOLINGER;
1489 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001490 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001491
1492 /* Once in a while we're woken up because the task expires. But
1493 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001494 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001495 * timeout needs to be refreshed.
1496 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001497 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001498 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1499 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001500 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001501 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1502 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001503 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001504
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001505 /* below we may emit error messages so we have to ensure that we have
1506 * our buffers properly allocated.
1507 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001508 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001509 /* No buffer available, we've been subscribed to the list of
1510 * buffer waiters, let's wait for our turn.
1511 */
1512 goto update_exp_and_leave;
1513 }
1514
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001515 /* 1b: check for low-level errors reported at the stream interface.
1516 * First we check if it's a retryable error (in which case we don't
1517 * want to tell the buffer). Otherwise we report the error one level
1518 * upper by setting flags into the buffers. Note that the side towards
1519 * the client cannot have connect (hence retryable) errors. Also, the
1520 * connection setup code must be able to deal with any type of abort.
1521 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001522 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001523 if (unlikely(si_f->flags & SI_FL_ERR)) {
1524 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1525 si_shutr(si_f);
1526 si_shutw(si_f);
1527 stream_int_report_error(si_f);
1528 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001529 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001530 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001531 if (srv)
1532 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001533 if (!(s->flags & SF_ERR_MASK))
1534 s->flags |= SF_ERR_CLICL;
1535 if (!(s->flags & SF_FINST_MASK))
1536 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001537 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001538 }
1539 }
1540
Willy Tarreau8f128b42014-11-28 15:07:47 +01001541 if (unlikely(si_b->flags & SI_FL_ERR)) {
1542 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1543 si_shutr(si_b);
1544 si_shutw(si_b);
1545 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001546 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001547 if (srv)
1548 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001549 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001550 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001551 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001552 if (srv)
1553 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001554 if (!(s->flags & SF_ERR_MASK))
1555 s->flags |= SF_ERR_SRVCL;
1556 if (!(s->flags & SF_FINST_MASK))
1557 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001558 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001559 }
1560 /* note: maybe we should process connection errors here ? */
1561 }
1562
Willy Tarreau8f128b42014-11-28 15:07:47 +01001563 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001564 /* we were trying to establish a connection on the server side,
1565 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1566 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001567 if (unlikely(!sess_update_st_con_tcp(s)))
1568 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001569 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001570 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001571
1572 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1573 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1574 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1575 */
1576 }
1577
Willy Tarreau8f128b42014-11-28 15:07:47 +01001578 rq_prod_last = si_f->state;
1579 rq_cons_last = si_b->state;
1580 rp_cons_last = si_f->state;
1581 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001582
1583 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001584 /* Check for connection closure */
1585
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001587 "[%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 +01001588 now_ms, __FUNCTION__, __LINE__,
1589 t,
1590 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001591 req, res,
1592 req->rex, res->wex,
1593 req->flags, res->flags,
1594 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1595 si_f->err_type, si_b->err_type,
1596 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001597
1598 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001599 if (unlikely(si_f->state == SI_ST_DIS))
1600 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001601
1602 /* When a server-side connection is released, we have to count it and
1603 * check for pending connections on this server.
1604 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001605 if (unlikely(si_b->state == SI_ST_DIS)) {
1606 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001607 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001608 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001609 if (s->flags & SF_CURR_SESS) {
1610 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001611 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001612 }
1613 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001614 if (may_dequeue_tasks(srv, s->be))
1615 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001616 }
1617 }
1618
1619 /*
1620 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1621 * at this point.
1622 */
1623
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001624 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001625 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001626 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1627 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1628 si_f->state != rq_prod_last ||
1629 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001630 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001631 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001632
Willy Tarreau8f128b42014-11-28 15:07:47 +01001633 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001634 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001635 unsigned int ana_list;
1636 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001637
Willy Tarreau90deb182010-01-07 00:20:41 +01001638 /* it's up to the analysers to stop new connections,
1639 * disable reading or closing. Note: if an analyser
1640 * disables any of these bits, it is responsible for
1641 * enabling them again when it disables itself, so
1642 * that other analysers are called in similar conditions.
1643 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001644 channel_auto_read(req);
1645 channel_auto_connect(req);
1646 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001647
1648 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001649 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001650 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001651 * the list when not needed. Any analyser may return 0
1652 * to break out of the loop, either because of missing
1653 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001654 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001655 * analyser, and we may loop again if other analysers
1656 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001657 *
1658 * We build a list of analysers to run. We evaluate all
1659 * of these analysers in the order of the lower bit to
1660 * the higher bit. This ordering is very important.
1661 * An analyser will often add/remove other analysers,
1662 * including itself. Any changes to itself have no effect
1663 * on the loop. If it removes any other analysers, we
1664 * want those analysers not to be called anymore during
1665 * this loop. If it adds an analyser that is located
1666 * after itself, we want it to be scheduled for being
1667 * processed during the loop. If it adds an analyser
1668 * which is located before it, we want it to switch to
1669 * it immediately, even if it has already been called
1670 * once but removed since.
1671 *
1672 * In order to achieve this, we compare the analyser
1673 * list after the call with a copy of it before the
1674 * call. The work list is fed with analyser bits that
1675 * appeared during the call. Then we compare previous
1676 * work list with the new one, and check the bits that
1677 * appeared. If the lowest of these bits is lower than
1678 * the current bit, it means we have enabled a previous
1679 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001680 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001681
Willy Tarreau8f128b42014-11-28 15:07:47 +01001682 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001683 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001684 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001685
Willy Tarreaufb356202010-08-03 14:02:05 +02001686 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001687 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001688 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001689 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001690 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001691
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001692 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001693 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001694 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001695 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001696 }
1697
Willy Tarreau748179e2015-05-01 21:52:31 +02001698 if (ana_list & AN_REQ_HTTP_BODY) {
1699 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1700 break;
1701 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1702 }
1703
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001704 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001705 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001706 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001707 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001708 }
1709
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001710 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001712 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001713 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001714 }
1715
Willy Tarreaufb356202010-08-03 14:02:05 +02001716 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001718 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001719 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001720 }
1721
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001722 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001723 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001724 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001725 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001726 }
1727
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001728 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001729 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001730 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001731 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001732 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001733
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001734 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001735 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001736 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001737 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001738 }
1739
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001740 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001742 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001743 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001744 }
1745
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001746 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001747 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001748 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001749 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001750 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001751
Emeric Brun1d33b292010-01-04 15:47:17 +01001752 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001753 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001754 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001755 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001756 }
1757
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001758 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001760 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001762 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001763 break;
1764 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001765 }
Willy Tarreau84455332009-03-15 22:34:05 +01001766
Willy Tarreau8f128b42014-11-28 15:07:47 +01001767 rq_prod_last = si_f->state;
1768 rq_cons_last = si_b->state;
1769 req->flags &= ~CF_WAKE_ONCE;
1770 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001771
Willy Tarreau8f128b42014-11-28 15:07:47 +01001772 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001773 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001774 }
1775
Willy Tarreau576507f2010-01-07 00:09:04 +01001776 /* we'll monitor the request analysers while parsing the response,
1777 * because some response analysers may indirectly enable new request
1778 * analysers (eg: HTTP keep-alive).
1779 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001780 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001781
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001782 resync_response:
1783 /* Analyse response */
1784
Willy Tarreau8f128b42014-11-28 15:07:47 +01001785 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1786 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1787 si_f->state != rp_cons_last ||
1788 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001789 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001790 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001791
Willy Tarreau8f128b42014-11-28 15:07:47 +01001792 if ((res->flags & CF_MASK_ANALYSER) &&
1793 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001794 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1795 * for some free space in the response buffer, so we might need to call
1796 * it when something changes in the response buffer, but still we pass
1797 * it the request buffer. Note that the SI state might very well still
1798 * be zero due to us returning a flow of redirects!
1799 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001800 res->analysers &= ~AN_REQ_ALL;
1801 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001802 }
1803
Willy Tarreau8f128b42014-11-28 15:07:47 +01001804 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001805 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001806 unsigned int ana_list;
1807 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001808
Willy Tarreau90deb182010-01-07 00:20:41 +01001809 /* it's up to the analysers to stop disable reading or
1810 * closing. Note: if an analyser disables any of these
1811 * bits, it is responsible for enabling them again when
1812 * it disables itself, so that other analysers are called
1813 * in similar conditions.
1814 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001815 channel_auto_read(res);
1816 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001817
1818 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001820 * to MSB. The analysers must remove themselves from
1821 * the list when not needed. Any analyser may return 0
1822 * to break out of the loop, either because of missing
1823 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001824 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001825 * analyser, and we may loop again if other analysers
1826 * are added in the middle.
1827 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001828
Willy Tarreau8f128b42014-11-28 15:07:47 +01001829 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001830 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001831 /* Warning! ensure that analysers are always placed in ascending order! */
1832
Emeric Brun97679e72010-09-23 17:56:44 +02001833 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001834 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001835 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001836 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001837 }
1838
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001839 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001840 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001841 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001843 }
1844
Emeric Brun1d33b292010-01-04 15:47:17 +01001845 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001847 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001848 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001849 }
1850
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001851 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001853 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001854 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001855 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001856
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001857 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001859 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001860 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001862 break;
1863 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001864 }
1865
Willy Tarreau8f128b42014-11-28 15:07:47 +01001866 rp_cons_last = si_f->state;
1867 rp_prod_last = si_b->state;
1868 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001869
Willy Tarreau8f128b42014-11-28 15:07:47 +01001870 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001871 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001872 }
1873
Willy Tarreau576507f2010-01-07 00:09:04 +01001874 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001875 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001876 goto resync_request;
1877
Willy Tarreau8f128b42014-11-28 15:07:47 +01001878 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001879 goto resync_request;
1880
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001881 /* FIXME: here we should call protocol handlers which rely on
1882 * both buffers.
1883 */
1884
1885
1886 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001887 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001888 * we're just in a data phase here since it means we have not
1889 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001890 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001891 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001892 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001893 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001894 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001895 req->analysers = 0;
1896 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001897 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001898 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001899 if (srv)
1900 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001901 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001902 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001903 else if (req->flags & CF_READ_TIMEOUT) {
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_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001909 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001910 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001911 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001912 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001913 if (srv)
1914 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001915 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001916 }
1917 else {
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_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001923 }
Willy Tarreau84455332009-03-15 22:34:05 +01001924 sess_set_term_flags(s);
1925 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001926 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001927 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001928 res->analysers = 0;
1929 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001930 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001931 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001932 if (srv)
1933 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001934 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001935 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001936 else if (res->flags & CF_READ_TIMEOUT) {
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_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001942 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001943 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001944 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001945 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001946 if (srv)
1947 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001948 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001949 }
1950 else {
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_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001956 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001957 sess_set_term_flags(s);
1958 }
Willy Tarreau84455332009-03-15 22:34:05 +01001959 }
1960
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001961 /*
1962 * Here we take care of forwarding unhandled data. This also includes
1963 * connection establishments and shutdown requests.
1964 */
1965
1966
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001967 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001968 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001969 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001970 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001971 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001972 if (unlikely(!req->analysers &&
1973 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1974 (si_f->state >= SI_ST_EST) &&
1975 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001976 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001977 * attached to it. If any data are left in, we'll permit them to
1978 * move.
1979 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001980 channel_auto_read(req);
1981 channel_auto_connect(req);
1982 channel_auto_close(req);
1983 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001984
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001985 /* We'll let data flow between the producer (if still connected)
1986 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001987 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001988 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1989 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02001990
1991 /* Just in order to support fetching HTTP contents after start
1992 * of forwarding when the HTTP forwarding analyser is not used,
1993 * we simply reset msg->sov so that HTTP rewinding points to the
1994 * headers.
1995 */
1996 if (s->txn)
1997 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001998 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001999
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002000 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002001 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2002 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002003 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002004 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2005 (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 +01002006 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002007 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2008 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002009 (req->flags & CF_STREAMER_FAST)))) {
2010 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002011 }
2012
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002013 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002014 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002015
2016 /*
2017 * Now forward all shutdown requests between both sides of the buffer
2018 */
2019
Willy Tarreau520d95e2009-09-19 21:04:57 +02002020 /* first, let's check if the request buffer needs to shutdown(write), which may
2021 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002022 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2023 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002024 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002025 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002026 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002027 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002028 if (tick_isset(sess->fe->timeout.clientfin)) {
2029 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002030 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002031 }
2032 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002033
2034 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002035 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2036 channel_is_empty(req))) {
2037 if (req->flags & CF_READ_ERROR)
2038 si_b->flags |= SI_FL_NOLINGER;
2039 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002040 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002041 res->rto = s->be->timeout.serverfin;
2042 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002043 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002044 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002045
2046 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002047 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2048 !req->analysers))
2049 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002050
2051 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002052 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2053 if (si_f->flags & SI_FL_NOHALF)
2054 si_f->flags |= SI_FL_NOLINGER;
2055 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002056 if (tick_isset(sess->fe->timeout.clientfin)) {
2057 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002058 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002059 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002060 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002061
Willy Tarreau520d95e2009-09-19 21:04:57 +02002062 /* it's possible that an upper layer has requested a connection setup or abort.
2063 * There are 2 situations where we decide to establish a new connection :
2064 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002065 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002066 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002067 if (si_b->state == SI_ST_INI) {
2068 if (!(req->flags & CF_SHUTW)) {
2069 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002070 /* If we have an appctx, there is no connect method, so we
2071 * immediately switch to the connected state, otherwise we
2072 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002073 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002074 si_b->state = SI_ST_REQ; /* new connection requested */
2075 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002076 }
Willy Tarreau73201222009-08-16 18:27:24 +02002077 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002078 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002079 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2080 channel_shutw_now(req); /* fix buffer flags upon abort */
2081 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002082 }
Willy Tarreau92795622009-03-06 12:51:23 +01002083 }
2084
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002085
2086 /* we may have a pending connection request, or a connection waiting
2087 * for completion.
2088 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002089 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002090
2091 /* prune the request variables and swap to the response variables. */
2092 if (s->vars_reqres.scope != SCOPE_RES) {
2093 vars_prune(&s->vars_reqres, s);
2094 vars_init(&s->vars_reqres, SCOPE_RES);
2095 }
2096
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002097 do {
2098 /* nb: step 1 might switch from QUE to ASS, but we first want
2099 * to give a chance to step 2 to perform a redirect if needed.
2100 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002101 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002102 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002104 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002105
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002106 /* applets directly go to the ESTABLISHED state. Similarly,
2107 * servers experience the same fate when their connection
2108 * is reused.
2109 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002110 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002111 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002112
Willy Tarreaub44c8732013-12-31 23:16:50 +01002113 /* Now we can add the server name to a header (if requested) */
2114 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002115 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002116 (s->be->server_id_hdr_name != NULL) &&
2117 (s->be->mode == PR_MODE_HTTP) &&
2118 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002119 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002120 }
2121
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002122 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002123 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002124 http_perform_server_redirect(s, si_b);
2125 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002126 }
2127
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002128 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002129 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002130 goto resync_stream_interface;
2131
Willy Tarreau815a9b22010-07-27 17:15:12 +02002132 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002133 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002134 goto resync_request;
2135
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002136 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002137
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002138 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002139 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002140 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002141 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002142 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002143 if (unlikely(!res->analysers &&
2144 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2145 (si_b->state >= SI_ST_EST) &&
2146 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002147 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002148 * attached to it. If any data are left in, we'll permit them to
2149 * move.
2150 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002151 channel_auto_read(res);
2152 channel_auto_close(res);
2153 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002154
2155 /* We'll let data flow between the producer (if still connected)
2156 * to the consumer.
2157 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002158 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2159 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002160
Willy Tarreau42529c32015-07-09 18:38:57 +02002161 /* Just in order to support fetching HTTP contents after start
2162 * of forwarding when the HTTP forwarding analyser is not used,
2163 * we simply reset msg->sov so that HTTP rewinding points to the
2164 * headers.
2165 */
2166 if (s->txn)
2167 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2168
Willy Tarreauce887fd2012-05-12 12:50:00 +02002169 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002170 * tunnel timeout set, use it now. Note that we must respect
2171 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002172 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002173 if (!req->analysers && s->be->timeout.tunnel) {
2174 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002175 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002176
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002177 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2178 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002179 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2180 res->rto = s->be->timeout.serverfin;
2181 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2182 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002183 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2184 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002185
Willy Tarreau8f128b42014-11-28 15:07:47 +01002186 req->rex = tick_add(now_ms, req->rto);
2187 req->wex = tick_add(now_ms, req->wto);
2188 res->rex = tick_add(now_ms, res->rto);
2189 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002190 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002191 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002192
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002193 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002194 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2195 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002196 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002197 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2198 (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 +01002199 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002200 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2201 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002202 (res->flags & CF_STREAMER_FAST)))) {
2203 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002204 }
2205
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002206 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002207 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002208
2209 /*
2210 * Now forward all shutdown requests between both sides of the buffer
2211 */
2212
2213 /*
2214 * FIXME: this is probably where we should produce error responses.
2215 */
2216
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002217 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002218 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002219 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002220 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002221 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002222 req->wto = s->be->timeout.serverfin;
2223 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002224 }
2225 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002226
2227 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002228 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2229 channel_is_empty(res))) {
2230 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002231 if (tick_isset(sess->fe->timeout.clientfin)) {
2232 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002233 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002234 }
2235 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002236
2237 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002238 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2239 !res->analysers)
2240 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002241
2242 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002243 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2244 if (si_b->flags & SI_FL_NOHALF)
2245 si_b->flags |= SI_FL_NOLINGER;
2246 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002247 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002248 req->wto = s->be->timeout.serverfin;
2249 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002250 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002251 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002252
Willy Tarreau8f128b42014-11-28 15:07:47 +01002253 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002254 goto resync_stream_interface;
2255
Willy Tarreau8f128b42014-11-28 15:07:47 +01002256 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002257 goto resync_request;
2258
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002260 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002261
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002262 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002263 si_f->flags &= ~SI_FL_DONT_WAKE;
2264 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002265
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002266 /* This is needed only when debugging is enabled, to indicate
2267 * client-side or server-side close. Please note that in the unlikely
2268 * event where both sides would close at once, the sequence is reported
2269 * on the server side first.
2270 */
2271 if (unlikely((global.mode & MODE_DEBUG) &&
2272 (!(global.mode & MODE_QUIET) ||
2273 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002274 if (si_b->state == SI_ST_CLO &&
2275 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002276 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002277 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002278 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2279 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002280 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002281 }
2282
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (si_f->state == SI_ST_CLO &&
2284 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002285 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002286 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002287 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2288 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002289 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002290 }
2291 }
2292
Willy Tarreau8f128b42014-11-28 15:07:47 +01002293 if (likely((si_f->state != SI_ST_CLO) ||
2294 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002295
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002296 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002297 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002298
Willy Tarreau563cc372015-04-19 18:13:56 +02002299 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002300 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002301
Willy Tarreau563cc372015-04-19 18:13:56 +02002302 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002303 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002304
Willy Tarreau8f128b42014-11-28 15:07:47 +01002305 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2306 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2307 si_f->prev_state = si_f->state;
2308 si_b->prev_state = si_b->state;
2309 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2310 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002311
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002312 /* Trick: if a request is being waiting for the server to respond,
2313 * and if we know the server can timeout, we don't want the timeout
2314 * to expire on the client side first, but we're still interested
2315 * in passing data from the client to the server (eg: POST). Thus,
2316 * we can cancel the client's request timeout if the server's
2317 * request timeout is set and the server has not yet sent a response.
2318 */
2319
Willy Tarreau8f128b42014-11-28 15:07:47 +01002320 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2321 (tick_isset(req->wex) || tick_isset(res->rex))) {
2322 req->flags |= CF_READ_NOEXP;
2323 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002324 }
2325
Willy Tarreau798f4322012-11-08 14:49:17 +01002326 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002327 t->expire = tick_first(tick_first(req->rex, req->wex),
2328 tick_first(res->rex, res->wex));
2329 if (req->analysers)
2330 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002331
Willy Tarreau8f128b42014-11-28 15:07:47 +01002332 if (si_f->exp)
2333 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002334
Willy Tarreau8f128b42014-11-28 15:07:47 +01002335 if (si_b->exp)
2336 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002337
2338#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002339 fprintf(stderr,
2340 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2341 " 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 +01002342 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2343 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002344#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002345
2346#ifdef DEBUG_DEV
2347 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002348 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002349 ABORT_NOW();
2350#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002351 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002352 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002353 }
2354
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002355 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002356 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002357 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002358 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002359 if (sess->listener) {
2360 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002361 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002362 sess->listener->nbconn--;
2363 if (sess->listener->state == LI_FULL)
2364 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002365
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002366 /* Dequeues all of the listeners waiting for a resource */
2367 if (!LIST_ISEMPTY(&global_listener_queue))
2368 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002369
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002370 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2371 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2372 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002373 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002374
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002375 if (unlikely((global.mode & MODE_DEBUG) &&
2376 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002377 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002378 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002379 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2380 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002381 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002382 }
2383
2384 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002385 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002386
Willy Tarreaueee5b512015-04-03 23:46:31 +02002387 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002388 int n;
2389
Willy Tarreaueee5b512015-04-03 23:46:31 +02002390 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002391 if (n < 1 || n > 5)
2392 n = 0;
2393
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002394 if (sess->fe->mode == PR_MODE_HTTP) {
2395 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002396 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002397 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002398 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002399 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002400 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002401 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002402 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002403 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002404 s->be->be_counters.p.http.comp_rsp++;
2405 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002406 }
2407
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002408 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002409 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002410 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002411 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002412 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002413 }
2414
Willy Tarreau87b09662015-04-03 00:22:06 +02002415 /* update time stats for this stream */
2416 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002417
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002418 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002419 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002420 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002421 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002422 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002423}
2424
Willy Tarreau87b09662015-04-03 00:22:06 +02002425/* Update the stream's backend and server time stats */
2426void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002427{
2428 int t_request;
2429 int t_queue;
2430 int t_connect;
2431 int t_data;
2432 int t_close;
2433 struct server *srv;
2434
2435 t_request = 0;
2436 t_queue = s->logs.t_queue;
2437 t_connect = s->logs.t_connect;
2438 t_close = s->logs.t_close;
2439 t_data = s->logs.t_data;
2440
2441 if (s->be->mode != PR_MODE_HTTP)
2442 t_data = t_connect;
2443
2444 if (t_connect < 0 || t_data < 0)
2445 return;
2446
2447 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2448 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2449
2450 t_data -= t_connect;
2451 t_connect -= t_queue;
2452 t_queue -= t_request;
2453
2454 srv = objt_server(s->target);
2455 if (srv) {
2456 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2457 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2458 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2459 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2460 }
2461 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2462 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2463 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2464 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2465}
2466
Willy Tarreau7c669d72008-06-20 15:04:11 +02002467/*
2468 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002469 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002470 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002471 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002472 * server.
2473 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002474void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002475{
2476 if (sess->srv_conn == newsrv)
2477 return;
2478
2479 if (sess->srv_conn) {
2480 sess->srv_conn->served--;
2481 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2482 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002483 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002484 }
2485
2486 if (newsrv) {
2487 newsrv->served++;
2488 if (newsrv->proxy->lbprm.server_take_conn)
2489 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002490 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002491 }
2492}
2493
Willy Tarreau84455332009-03-15 22:34:05 +01002494/* Handle server-side errors for default protocols. It is called whenever a a
2495 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002496 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002497 * them. It's installed as ->srv_error for the server-side stream_interface.
2498 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002499void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002500{
2501 int err_type = si->err_type;
2502 int err = 0, fin = 0;
2503
2504 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002505 err = SF_ERR_CLICL;
2506 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002507 }
2508 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002509 err = SF_ERR_CLICL;
2510 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002511 }
2512 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002513 err = SF_ERR_SRVTO;
2514 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002515 }
2516 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002517 err = SF_ERR_SRVCL;
2518 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002519 }
2520 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002521 err = SF_ERR_SRVTO;
2522 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002523 }
2524 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002525 err = SF_ERR_SRVCL;
2526 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002527 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002528 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002529 err = SF_ERR_RESOURCE;
2530 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002531 }
Willy Tarreau84455332009-03-15 22:34:05 +01002532 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002533 err = SF_ERR_INTERNAL;
2534 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002535 }
2536
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002538 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002539 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002540 s->flags |= fin;
2541}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002542
Willy Tarreaue7dff022015-04-03 01:14:29 +02002543/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002544void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002545{
Willy Tarreau87b09662015-04-03 00:22:06 +02002546 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002547 return;
2548
Willy Tarreau87b09662015-04-03 00:22:06 +02002549 channel_shutw_now(&stream->req);
2550 channel_shutr_now(&stream->res);
2551 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002552 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002553 stream->flags |= why;
2554 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002555}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002556
Willy Tarreau8b22a712010-06-18 17:46:06 +02002557/************************************************************************/
2558/* All supported ACL keywords must be declared here. */
2559/************************************************************************/
2560
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002561/* Returns a pointer to a stkctr depending on the fetch keyword name.
2562 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002563 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002564 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002565 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002566 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002567 * NULL may be returned if the designated stkctr is not tracked. For
2568 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2569 * passed. When present, the currently tracked key is then looked up
2570 * in the specified table instead of the current table. The purpose is
2571 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002572 * multiple tables). <strm> is allowed to be NULL, in which case only
2573 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002574 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002575struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002576smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002577{
2578 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002579 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002580 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002581 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002582 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002583
Willy Tarreau0f791d42013-07-23 19:56:43 +02002584 if (num == '_' - '0') {
2585 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002586 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002587 if (num >= MAX_SESS_STKCTR)
2588 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002589 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002590 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002591 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002592 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002593
2594 if (!conn)
2595 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002596
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002597 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002598 if (!key)
2599 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002600
Willy Tarreaua65536c2013-07-22 22:40:11 +02002601 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002602 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002603 return &stkctr;
2604 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002605
2606 /* Here, <num> contains the counter number from 0 to 9 for
2607 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002608 * args[arg] is the first optional argument. We first lookup the
2609 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002610 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002611
2612 stkptr = &strm->stkctr[num];
2613 if (!strm || !stkctr_entry(stkptr)) {
2614 stkptr = &sess->stkctr[num];
2615 if (!stkctr_entry(stkptr))
2616 return NULL;
2617 }
2618
2619 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002620 if (!stksess)
2621 return NULL;
2622
Willy Tarreau0f791d42013-07-23 19:56:43 +02002623 if (unlikely(args[arg].type == ARGT_TAB)) {
2624 /* an alternate table was specified, let's look up the same key there */
2625 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002626 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002627 return &stkctr;
2628 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002629 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002630}
2631
Willy Tarreau87b09662015-04-03 00:22:06 +02002632/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002633 * currently being tracked or not.
2634 * Supports being called as "sc[0-9]_tracked" only.
2635 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002636static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002637smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002638{
2639 smp->flags = SMP_F_VOL_TEST;
2640 smp->type = SMP_T_BOOL;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002641 smp->data.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002642 return 1;
2643}
2644
Willy Tarreau87b09662015-04-03 00:22:06 +02002645/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002646 * frontend counters or from the src.
2647 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2648 * zero is returned if the key is new.
2649 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002650static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002651smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002652{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002653 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002654
2655 if (!stkctr)
2656 return 0;
2657
Willy Tarreau37406352012-04-23 16:16:37 +02002658 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002659 smp->type = SMP_T_SINT;
2660 smp->data.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002661
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002662 if (stkctr_entry(stkctr) != NULL) {
2663 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002664 if (!ptr)
2665 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002666 smp->data.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002667 }
2668 return 1;
2669}
2670
Willy Tarreau87b09662015-04-03 00:22:06 +02002671/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002672 * tracked frontend counters or from the src.
2673 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2674 * Value zero is returned if the key is new.
2675 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002676static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002677smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002678{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002679 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002680
2681 if (!stkctr)
2682 return 0;
2683
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002684 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002685 smp->type = SMP_T_SINT;
2686 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002687 if (stkctr_entry(stkctr) != NULL) {
2688 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002689 if (!ptr)
2690 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002691 smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002692 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002693 }
2694 return 1;
2695}
2696
Willy Tarreau87b09662015-04-03 00:22:06 +02002697/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002698 * frontend counters and return it into temp integer.
2699 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002700 */
2701static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002702smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002703{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002704 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002705
2706 if (!stkctr)
2707 return 0;
2708
Willy Tarreau37406352012-04-23 16:16:37 +02002709 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002710 smp->type = SMP_T_SINT;
2711 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002712 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002713 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002714
2715 /* First, update gpc0_rate if it's tracked. Second, update its
2716 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2717 */
Emeric Brun57056f02015-06-15 18:29:57 +02002718 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2719 if (ptr1) {
2720 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002721 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002722 smp->data.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002723 }
2724
Emeric Brun57056f02015-06-15 18:29:57 +02002725 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2726 if (ptr2)
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002727 smp->data.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002728
Emeric Brun57056f02015-06-15 18:29:57 +02002729 /* If data was modified, we need to touch to re-schedule sync */
2730 if (ptr1 || ptr2)
2731 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002732 }
2733 return 1;
2734}
2735
Willy Tarreau87b09662015-04-03 00:22:06 +02002736/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002737 * frontend counters and return its previous value into temp integer.
2738 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002739 */
2740static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002741smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002742{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002743 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002744
2745 if (!stkctr)
2746 return 0;
2747
Willy Tarreau37406352012-04-23 16:16:37 +02002748 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002749 smp->type = SMP_T_SINT;
2750 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002751 if (stkctr_entry(stkctr) != NULL) {
2752 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002753 if (!ptr)
2754 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002755 smp->data.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002756 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002757 /* If data was modified, we need to touch to re-schedule sync */
2758 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002759 }
2760 return 1;
2761}
2762
Willy Tarreau87b09662015-04-03 00:22:06 +02002763/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002764 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2765 * "src_conn_cnt" only.
2766 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002767static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002768smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002769{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002770 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002771
2772 if (!stkctr)
2773 return 0;
2774
Willy Tarreau37406352012-04-23 16:16:37 +02002775 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002776 smp->type = SMP_T_SINT;
2777 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002778 if (stkctr_entry(stkctr) != NULL) {
2779 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002780 if (!ptr)
2781 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002782 smp->data.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002783 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002784 return 1;
2785}
2786
Willy Tarreau87b09662015-04-03 00:22:06 +02002787/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002788 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2789 * only.
2790 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002791static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002792smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002793{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002794 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002795
2796 if (!stkctr)
2797 return 0;
2798
Willy Tarreau37406352012-04-23 16:16:37 +02002799 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002800 smp->type = SMP_T_SINT;
2801 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002802 if (stkctr_entry(stkctr) != NULL) {
2803 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002804 if (!ptr)
2805 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002806 smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002807 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002808 }
2809 return 1;
2810}
2811
Willy Tarreau87b09662015-04-03 00:22:06 +02002812/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002813 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002814 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002815 */
2816static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002817smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002818{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002819 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002820 struct stksess *ts;
2821 struct stktable_key *key;
2822 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002823 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002824
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002825 if (!conn)
2826 return 0;
2827
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002828 key = addr_to_stktable_key(&conn->addr.from, smp->px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002829 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002830 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002831
Willy Tarreau24e32d82012-04-23 23:55:44 +02002832 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002833
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002834 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2835 /* entry does not exist and could not be created */
2836 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002837
2838 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2839 if (!ptr)
2840 return 0; /* parameter not stored in this table */
2841
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002842 smp->type = SMP_T_SINT;
2843 smp->data.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002844 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002845 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002846 return 1;
2847}
2848
Willy Tarreau87b09662015-04-03 00:22:06 +02002849/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002850 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2851 * "src_conn_cur" only.
2852 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002853static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002854smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002855{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002856 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002857
2858 if (!stkctr)
2859 return 0;
2860
Willy Tarreau37406352012-04-23 16:16:37 +02002861 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002862 smp->type = SMP_T_SINT;
2863 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002864 if (stkctr_entry(stkctr) != NULL) {
2865 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002866 if (!ptr)
2867 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002868 smp->data.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002869 }
2870 return 1;
2871}
2872
Willy Tarreau87b09662015-04-03 00:22:06 +02002873/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002874 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2875 * "src_sess_cnt" only.
2876 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002877static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002878smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002879{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002880 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002881
2882 if (!stkctr)
2883 return 0;
2884
Willy Tarreau37406352012-04-23 16:16:37 +02002885 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002886 smp->type = SMP_T_SINT;
2887 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002888 if (stkctr_entry(stkctr) != NULL) {
2889 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002890 if (!ptr)
2891 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002892 smp->data.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002893 }
2894 return 1;
2895}
2896
Willy Tarreau87b09662015-04-03 00:22:06 +02002897/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002898 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2899 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002900static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002901smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002902{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002903 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002904
2905 if (!stkctr)
2906 return 0;
2907
Willy Tarreau37406352012-04-23 16:16:37 +02002908 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002909 smp->type = SMP_T_SINT;
2910 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002911 if (stkctr_entry(stkctr) != NULL) {
2912 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002913 if (!ptr)
2914 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002915 smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002916 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002917 }
2918 return 1;
2919}
2920
Willy Tarreau87b09662015-04-03 00:22:06 +02002921/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002922 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2923 * "src_http_req_cnt" only.
2924 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002925static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002926smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002927{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002928 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002929
2930 if (!stkctr)
2931 return 0;
2932
Willy Tarreau37406352012-04-23 16:16:37 +02002933 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002934 smp->type = SMP_T_SINT;
2935 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002936 if (stkctr_entry(stkctr) != NULL) {
2937 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002938 if (!ptr)
2939 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002940 smp->data.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002941 }
2942 return 1;
2943}
2944
Willy Tarreau87b09662015-04-03 00:22:06 +02002945/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002946 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2947 * "src_http_req_rate" only.
2948 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002949static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002950smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002951{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002952 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002953
2954 if (!stkctr)
2955 return 0;
2956
Willy Tarreau37406352012-04-23 16:16:37 +02002957 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002958 smp->type = SMP_T_SINT;
2959 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002960 if (stkctr_entry(stkctr) != NULL) {
2961 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002962 if (!ptr)
2963 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002964 smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002965 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002966 }
2967 return 1;
2968}
2969
Willy Tarreau87b09662015-04-03 00:22:06 +02002970/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002971 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2972 * "src_http_err_cnt" only.
2973 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002975smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002976{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002977 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002978
2979 if (!stkctr)
2980 return 0;
2981
Willy Tarreau37406352012-04-23 16:16:37 +02002982 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002983 smp->type = SMP_T_SINT;
2984 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002985 if (stkctr_entry(stkctr) != NULL) {
2986 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002987 if (!ptr)
2988 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02002989 smp->data.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002990 }
2991 return 1;
2992}
2993
Willy Tarreau87b09662015-04-03 00:22:06 +02002994/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02002995 * counters. Supports being called as "sc[0-9]_http_err_rate" or
2996 * "src_http_err_rate" only.
2997 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002998static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002999smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003000{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003001 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003002
3003 if (!stkctr)
3004 return 0;
3005
Willy Tarreau37406352012-04-23 16:16:37 +02003006 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003007 smp->type = SMP_T_SINT;
3008 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003009 if (stkctr_entry(stkctr) != NULL) {
3010 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003011 if (!ptr)
3012 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003013 smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003014 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003015 }
3016 return 1;
3017}
3018
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003019/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003020 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003021 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3022 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003023static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003024smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003025{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003026 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003027
3028 if (!stkctr)
3029 return 0;
3030
Willy Tarreau37406352012-04-23 16:16:37 +02003031 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003032 smp->type = SMP_T_SINT;
3033 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003034 if (stkctr_entry(stkctr) != NULL) {
3035 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003036 if (!ptr)
3037 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003038 smp->data.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003039 }
3040 return 1;
3041}
3042
Willy Tarreau613fe992013-07-23 17:39:19 +02003043/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003044 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003045 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003046 */
3047static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003048smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003049{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003050 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003051
3052 if (!stkctr)
3053 return 0;
3054
Willy Tarreau37406352012-04-23 16:16:37 +02003055 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003056 smp->type = SMP_T_SINT;
3057 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003058 if (stkctr_entry(stkctr) != NULL) {
3059 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003060 if (!ptr)
3061 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003062 smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003063 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003064 }
3065 return 1;
3066}
3067
Willy Tarreau53aea102013-07-23 17:39:02 +02003068/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003069 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003070 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3071 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003072static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003073smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003074{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003075 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003076
3077 if (!stkctr)
3078 return 0;
3079
Willy Tarreau37406352012-04-23 16:16:37 +02003080 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003081 smp->type = SMP_T_SINT;
3082 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003083 if (stkctr_entry(stkctr) != NULL) {
3084 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003085 if (!ptr)
3086 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003087 smp->data.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003088 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003089 return 1;
3090}
3091
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003092/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003093 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003094 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003095 */
3096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003097smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003098{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003099 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003100
3101 if (!stkctr)
3102 return 0;
3103
Willy Tarreau37406352012-04-23 16:16:37 +02003104 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003105 smp->type = SMP_T_SINT;
3106 smp->data.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003107 if (stkctr_entry(stkctr) != NULL) {
3108 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003109 if (!ptr)
3110 return 0; /* parameter not stored */
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003111 smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003112 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003113 }
3114 return 1;
3115}
3116
Willy Tarreau87b09662015-04-03 00:22:06 +02003117/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003118 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3119 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003120static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003121smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003122{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003123 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003124
Willy Tarreau563eef42013-07-23 18:32:02 +02003125 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003126 return 0;
3127
Willy Tarreau563eef42013-07-23 18:32:02 +02003128 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003129 smp->type = SMP_T_SINT;
3130 smp->data.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003131 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003132}
3133
Willy Tarreau34db1082012-04-19 17:16:54 +02003134/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003135 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003136 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003137static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003138smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003139{
Willy Tarreau37406352012-04-23 16:16:37 +02003140 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003141 smp->type = SMP_T_SINT;
3142 smp->data.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003143 return 1;
3144}
3145
Willy Tarreau34db1082012-04-19 17:16:54 +02003146/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003147 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003148 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003149static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003150smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003151{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003152 struct proxy *px;
3153
Willy Tarreau24e32d82012-04-23 23:55:44 +02003154 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003155 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003156 smp->type = SMP_T_SINT;
3157 smp->data.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003158 return 1;
3159}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003160
Willy Tarreau61612d42012-04-19 18:42:05 +02003161/* Note: must not be declared <const> as its list will be overwritten.
3162 * Please take care of keeping this list alphabetically sorted.
3163 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003164static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003165 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003166}};
3167
Willy Tarreau281c7992013-01-08 01:23:27 +01003168/* Note: must not be declared <const> as its list will be overwritten.
3169 * Please take care of keeping this list alphabetically sorted.
3170 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003171static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003172 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3173 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3174 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3175 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3176 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3177 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3178 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3179 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3180 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3181 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3182 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3183 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3184 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3185 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3186 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3187 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3188 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3189 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3190 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003191 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3192 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3193 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3194 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3195 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3196 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3197 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3198 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3199 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3200 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3201 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3202 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3203 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3204 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3205 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3206 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3207 { "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 +02003208 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003209 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3210 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3211 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3212 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3213 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3214 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3215 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3216 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3217 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3218 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3219 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3220 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3221 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3222 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3223 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3224 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3225 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3226 { "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 +02003227 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003228 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3229 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3230 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3231 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3232 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3233 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3234 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3235 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3236 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3237 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3238 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3239 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3240 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3241 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3242 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3243 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3244 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3245 { "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 +02003246 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003247 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3248 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3249 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3250 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3251 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3252 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3253 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3254 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3255 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3256 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3257 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3258 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3259 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3260 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3261 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3262 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3263 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3264 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3265 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3266 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3267 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003268 { /* END */ },
3269}};
3270
Willy Tarreau8b22a712010-06-18 17:46:06 +02003271__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003272static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003273{
Willy Tarreau281c7992013-01-08 01:23:27 +01003274 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003275 acl_register_keywords(&acl_kws);
3276}
3277
Willy Tarreaubaaee002006-06-26 02:48:02 +02003278/*
3279 * Local variables:
3280 * c-indent-level: 8
3281 * c-basic-offset: 8
3282 * End:
3283 */