blob: 0a38f2e994976adf15e6f450fb525c555a078ed6 [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;
108
109 memset(s->stkctr, 0, sizeof(s->stkctr));
110
111 s->sess = sess;
112 s->si[0].flags = SI_FL_NONE;
113 s->si[1].flags = SI_FL_ISBACK;
114
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200115 s->uniq_id = global.req_count++;
116
Willy Tarreau87b09662015-04-03 00:22:06 +0200117 /* OK, we're keeping the stream, so let's properly initialize the stream */
118 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200119 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100120 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200121
Willy Tarreaue7dff022015-04-03 01:14:29 +0200122 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200123 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200124
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200125 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200126 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200127 t->context = s;
128 t->expire = TICK_ETERNITY;
129
Willy Tarreau87b09662015-04-03 00:22:06 +0200130 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200131 * This changes later when switching rules are executed or
132 * when the default backend is assigned.
133 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200134 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200135 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100136 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200137 s->req_cap = NULL;
138 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200139
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200140 /* Initialise alle the variable context even if will not use.
141 * This permits to prune these context without errors.
142 */
143 vars_init(&s->vars_sess, SCOPE_SESS);
144 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 Tarreau2a6e8802013-10-24 15:50:53 +0200164 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200165
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200166 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200167 s->si[1].flags |= SI_FL_INDEP_STR;
168
Willy Tarreau87b09662015-04-03 00:22:06 +0200169 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200170 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200171 s->pend_pos = NULL;
172
173 /* init store persistence */
174 s->store_count = 0;
175
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100176 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100177 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200178 s->req.analysers = 0;
179 channel_auto_connect(&s->req); /* don't wait to establish connection */
180 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200181
182 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100183 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100184 s->req.rex = TICK_ETERNITY;
185 s->req.wex = TICK_ETERNITY;
186 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200187
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100188 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100189 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100190 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200191
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200192 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100193 s->req.flags |= CF_NEVER_WAIT;
194 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200195 }
196
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200197 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100198 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100199 s->res.rex = TICK_ETERNITY;
200 s->res.wex = TICK_ETERNITY;
201 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200202
Willy Tarreaueee5b512015-04-03 23:46:31 +0200203 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100204
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100205 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100206
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200208 if (conn)
209 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200210 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200211 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200212
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200213 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200214 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215
216 /* it is important not to call the wakeup function directly but to
217 * pass through task_wakeup(), because this one knows how to apply
218 * priorities to tasks.
219 */
220 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200221 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200222
223 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200224 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100225 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200226 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200227 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200228}
229
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200231 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200233static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200234{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200235 struct session *sess = strm_sess(s);
236 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100237 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200238 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200239 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100240
Willy Tarreaubaaee002006-06-26 02:48:02 +0200241 if (s->pend_pos)
242 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100243
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100244 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200245 if (s->flags & SF_CURR_SESS) {
246 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100247 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100248 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100249 if (may_dequeue_tasks(objt_server(s->target), s->be))
250 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100251 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100252
Willy Tarreau7c669d72008-06-20 15:04:11 +0200253 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200254 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200255 * it should normally be only the same as the one above,
256 * so this should not happen in fact.
257 */
258 sess_change_server(s, NULL);
259 }
260
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100261 if (s->req.pipe)
262 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100263
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100264 if (s->res.pipe)
265 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100266
Willy Tarreaubf883e02014-11-25 21:10:35 +0100267 /* We may still be present in the buffer wait queue */
268 if (!LIST_ISEMPTY(&s->buffer_wait)) {
269 LIST_DEL(&s->buffer_wait);
270 LIST_INIT(&s->buffer_wait);
271 }
272
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100273 b_drop(&s->req.buf);
274 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100275 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200276 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200277
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100278 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200279 if (s->txn)
280 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100281
Willy Tarreau1e954912012-10-12 17:50:05 +0200282 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200283 if (cli_conn)
284 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200285
Willy Tarreaua4cda672010-06-06 18:28:49 +0200286 for (i = 0; i < s->store_count; i++) {
287 if (!s->store[i].ts)
288 continue;
289 stksess_free(s->store[i].table, s->store[i].ts);
290 s->store[i].ts = NULL;
291 }
292
Willy Tarreaueee5b512015-04-03 23:46:31 +0200293 if (s->txn) {
294 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
295 pool_free2(pool2_http_txn, s->txn);
296 s->txn = NULL;
297 }
298
Willy Tarreau92fb9832007-10-16 17:34:28 +0200299 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200300 pool_free2(fe->rsp_cap_pool, s->res_cap);
301 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200302 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100303
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200304 /* Cleanup all variable contexts. */
305 vars_prune(&s->vars_sess, s);
306 vars_prune(&s->vars_txn, s);
307 vars_prune(&s->vars_reqres, s);
308
Willy Tarreau87b09662015-04-03 00:22:06 +0200309 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200310
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100311 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100312 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200313 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100314 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100315 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100316 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200317 if (s->list.n != &streams)
318 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100319 bref->ref = s->list.n;
320 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100321 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200322 si_release_endpoint(&s->si[1]);
323 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200324
325 /* FIXME: for now we have a 1:1 relation between stream and session so
326 * the stream must free the session.
327 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200328 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200329 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200330
331 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200332 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200333 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200334 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200335 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200336 pool_flush2(pool2_requri);
337 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200338 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200339 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100340 pool_flush2(pool2_connection);
341 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200342 pool_flush2(fe->req_cap_pool);
343 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200344 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200345}
346
Willy Tarreau78955f42014-12-28 13:09:02 +0100347/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
348 * that it's not the last available buffer or it's the response buffer. Unless
349 * the buffer is the response buffer, an extra control is made so that we always
350 * keep <tune.buffers.reserved> buffers available after this allocation. To be
351 * called at the beginning of recv() callbacks to ensure that the required
352 * buffers are properly allocated. Returns 0 in case of failure, non-zero
353 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100354 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200355int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100356{
Willy Tarreau87b09662015-04-03 00:22:06 +0200357 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100358 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100359 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100360
Willy Tarreau78955f42014-12-28 13:09:02 +0100361 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100362 margin = global.tune.reserved_bufs;
363
Willy Tarreau78955f42014-12-28 13:09:02 +0100364 s = chn_sess(chn);
365
366 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100367 if (b)
368 return 1;
369
Willy Tarreaubf883e02014-11-25 21:10:35 +0100370 if (LIST_ISEMPTY(&s->buffer_wait))
371 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100372 return 0;
373}
374
Willy Tarreau87b09662015-04-03 00:22:06 +0200375/* Allocates a work buffer for stream <s>. It is meant to be called inside
376 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200377 * to work fine, which is the response buffer so that an error message may be
378 * built and returned. Response buffers may be allocated from the reserve, this
379 * is critical to ensure that a response may always flow and will never block a
380 * server from releasing a connection. Returns 0 in case of failure, non-zero
381 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100382 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200383int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100384{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100385 if (!LIST_ISEMPTY(&s->buffer_wait)) {
386 LIST_DEL(&s->buffer_wait);
387 LIST_INIT(&s->buffer_wait);
388 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100389
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200390 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100391 return 1;
392
Willy Tarreaubf883e02014-11-25 21:10:35 +0100393 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100394 return 0;
395}
396
397/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100398 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200399 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100400 * a single buffer, so it makes sense to try to wake two up when two buffers
401 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100402 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200403void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100404{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100405 if (s->req.buf->size && buffer_empty(s->req.buf))
406 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100407
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100408 if (s->res.buf->size && buffer_empty(s->res.buf))
409 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100410
Willy Tarreaubf883e02014-11-25 21:10:35 +0100411 /* if we're certain to have at least 1 buffer available, and there is
412 * someone waiting, we can wake up a waiter and offer them.
413 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100414 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200415 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100416}
417
Willy Tarreau87b09662015-04-03 00:22:06 +0200418/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100419 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200420 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100421 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200422void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423{
Willy Tarreau87b09662015-04-03 00:22:06 +0200424 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100425
426 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100427 if (rqlimit <= run_queue)
428 break;
429
Willy Tarreaubf883e02014-11-25 21:10:35 +0100430 if (sess->task->state & TASK_RUNNING)
431 continue;
432
433 LIST_DEL(&sess->buffer_wait);
434 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100435 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100436 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100437}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200438
439/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200440int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200441{
Willy Tarreau87b09662015-04-03 00:22:06 +0200442 LIST_INIT(&streams);
443 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
444 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445}
446
Willy Tarreau87b09662015-04-03 00:22:06 +0200447void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100448{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200449 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100450 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200451 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100452 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100453
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100454 bytes = s->req.total - s->logs.bytes_in;
455 s->logs.bytes_in = s->req.total;
456 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200457 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100458
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100459 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100460
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100461 if (objt_server(s->target))
462 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200463
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200464 if (sess->listener && sess->listener->counters)
465 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200466
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100467 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200468 struct stkctr *stkctr = &s->stkctr[i];
469
470 if (!stkctr_entry(stkctr)) {
471 stkctr = &sess->stkctr[i];
472 if (!stkctr_entry(stkctr))
473 continue;
474 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200475
Emeric Brun57056f02015-06-15 18:29:57 +0200476 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
477 if (ptr1)
478 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200479
Emeric Brun57056f02015-06-15 18:29:57 +0200480 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
481 if (ptr2)
482 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200483 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200484
485 /* If data was modified, we need to touch to re-schedule sync */
486 if (ptr1 || ptr2)
487 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100488 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100489 }
490
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100491 bytes = s->res.total - s->logs.bytes_out;
492 s->logs.bytes_out = s->res.total;
493 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200494 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100495
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100496 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100497
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100498 if (objt_server(s->target))
499 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200500
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200501 if (sess->listener && sess->listener->counters)
502 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200503
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100504 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200505 struct stkctr *stkctr = &s->stkctr[i];
506
507 if (!stkctr_entry(stkctr)) {
508 stkctr = &sess->stkctr[i];
509 if (!stkctr_entry(stkctr))
510 continue;
511 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200512
Emeric Brun57056f02015-06-15 18:29:57 +0200513 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
514 if (ptr1)
515 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200516
Emeric Brun57056f02015-06-15 18:29:57 +0200517 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
518 if (ptr2)
519 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200520 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200521
522 /* If data was modified, we need to touch to re-schedule sync */
523 if (ptr1 || ptr2)
524 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100525 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100526 }
527}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200528
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100529/* This function is called with (si->state == SI_ST_CON) meaning that a
530 * connection was attempted and that the file descriptor is already allocated.
531 * We must check for establishment, error and abort. Possible output states
532 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
533 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200534 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100535 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200536static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100537{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100538 struct stream_interface *si = &s->si[1];
539 struct channel *req = &s->req;
540 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200541 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100543 /* If we got an error, or if nothing happened and the connection timed
544 * out, we must give up. The CER state handler will take care of retry
545 * attempts and error reports.
546 */
547 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100548 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100549 /* Some data were sent past the connection establishment,
550 * so we need to pretend we're established to log correctly
551 * and let later states handle the failure.
552 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100553 si->state = SI_ST_EST;
554 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100555 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100556 return 1;
557 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100558 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100560
Willy Tarreauf79c8172013-10-21 16:30:56 +0200561 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100562
563 if (si->err_type)
564 return 0;
565
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100566 if (si->flags & SI_FL_ERR)
567 si->err_type = SI_ET_CONN_ERR;
568 else
569 si->err_type = SI_ET_CONN_TO;
570 return 0;
571 }
572
573 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100574 if (!(req->flags & CF_WRITE_PARTIAL) &&
575 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200576 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
577 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100578 s->be->options & PR_O_ABRT_CLOSE)))) {
579 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200580 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100582 if (s->srv_error)
583 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100584 return 1;
585 }
586
587 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200588 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100589 return 1;
590
591 /* OK, this means that a connection succeeded. The caller will be
592 * responsible for handling the transition from CON to EST.
593 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100594 si->state = SI_ST_EST;
595 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100596 return 1;
597}
598
599/* This function is called with (si->state == SI_ST_CER) meaning that a
600 * previous connection attempt has failed and that the file descriptor
601 * has already been released. Possible causes include asynchronous error
602 * notification and time out. Possible output states are SI_ST_CLO when
603 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
604 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
605 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
606 * marked as in error state. It returns 0.
607 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200608static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100609{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100610 struct stream_interface *si = &s->si[1];
611
Willy Tarreau87b09662015-04-03 00:22:06 +0200612 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100613 if (objt_server(s->target)) {
614 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100615
Willy Tarreaue7dff022015-04-03 01:14:29 +0200616 if (s->flags & SF_CURR_SESS) {
617 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100618 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100620 }
621
622 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200623 si->conn_retries--;
624 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100625 if (!si->err_type) {
626 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100627 }
628
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100629 if (objt_server(s->target))
630 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100631 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100632 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100633 if (may_dequeue_tasks(objt_server(s->target), s->be))
634 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100635
636 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200637 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100638 s->req.flags |= CF_WRITE_ERROR;
639 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100640
641 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100642 if (s->srv_error)
643 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 return 0;
645 }
646
647 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700648 * retry on another server. By default this redispatch occurs on the
649 * last retry, but if configured we allow redispatches to occur on
650 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200651 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100652 * bit to ignore any persistence cookie. We won't count a retry nor a
653 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200654 * If the connection is not persistent, the balancing algorithm is not
655 * determinist (round robin) and there is more than one active server,
656 * we accept to perform an immediate redispatch without waiting since
657 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100658 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200659 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700660 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
661 ((((s->be->redispatch_after > 0) &&
662 ((s->be->conn_retries - si->conn_retries) %
663 s->be->redispatch_after == 0)) ||
664 ((s->be->redispatch_after < 0) &&
665 ((s->be->conn_retries - si->conn_retries) %
666 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200667 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700668 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100669 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100670 if (may_dequeue_tasks(objt_server(s->target), s->be))
671 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100672
Willy Tarreaue7dff022015-04-03 01:14:29 +0200673 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674 si->state = SI_ST_REQ;
675 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100676 if (objt_server(s->target))
677 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100678 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679 si->state = SI_ST_ASS;
680 }
681
682 if (si->flags & SI_FL_ERR) {
683 /* The error was an asynchronous connection error, and we will
684 * likely have to retry connecting to the same server, most
685 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200686 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687 */
688
Willy Tarreaub0290662014-06-13 17:04:44 +0200689 int delay = 1000;
690
691 if (s->be->timeout.connect && s->be->timeout.connect < delay)
692 delay = s->be->timeout.connect;
693
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694 if (!si->err_type)
695 si->err_type = SI_ET_CONN_ERR;
696
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200697 /* only wait when we're retrying on the same server */
698 if (si->state == SI_ST_ASS ||
699 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
700 (s->be->srv_act <= 1)) {
701 si->state = SI_ST_TAR;
702 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
703 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100704 return 0;
705 }
706 return 0;
707}
708
709/*
710 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200711 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200712 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100713 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200714static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100716 struct stream_interface *si = &s->si[1];
717 struct channel *req = &s->req;
718 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100719
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100720 /* First, centralize the timers information */
721 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
722 si->exp = TICK_ETERNITY;
723
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100724 if (objt_server(s->target))
725 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100726
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100727 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728 /* if the user wants to log as soon as possible, without counting
729 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200730 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100731 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100732 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100734 }
735 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100736 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 }
738
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200739 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200740 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200741 if (req->flags & CF_WAKE_CONNECT) {
742 req->flags |= CF_WAKE_ONCE;
743 req->flags &= ~CF_WAKE_CONNECT;
744 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200745 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200746 /* real connections have timeouts */
747 req->wto = s->be->timeout.server;
748 rep->rto = s->be->timeout.server;
749 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 req->wex = TICK_ETERNITY;
751}
752
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100753/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
754 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100755 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
756 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
757 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200759static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100761 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100762 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100763 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100764
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100765 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 +0100766 now_ms, __FUNCTION__,
767 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400768 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100769 req->rex, s->res.wex,
770 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400771 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 +0100772
773 if (si->state == SI_ST_ASS) {
774 /* Server assigned to connection request, we have to try to connect now */
775 int conn_err;
776
777 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100778 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100779
Willy Tarreaue7dff022015-04-03 01:14:29 +0200780 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100781 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100782 if (srv)
783 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500784 if (srv)
785 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100786 return;
787 }
788
789 /* We have received a synchronous error. We might have to
790 * abort, retry immediately or redispatch.
791 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200792 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100793 if (!si->err_type) {
794 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100795 }
796
Willy Tarreau827aee92011-03-10 16:55:02 +0100797 if (srv)
798 srv_inc_sess_ctr(srv);
799 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500800 srv_set_sess_last(srv);
801 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100802 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100803 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100804
Willy Tarreau87b09662015-04-03 00:22:06 +0200805 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100806 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100807 if (may_dequeue_tasks(srv, s->be))
808 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100809
810 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200811 si_shutr(si);
812 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100813 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814
815 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
816
Willy Tarreau87b09662015-04-03 00:22:06 +0200817 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100819 if (s->srv_error)
820 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100821 return;
822 }
823
824 /* We are facing a retryable error, but we don't want to run a
825 * turn-around now, as the problem is likely a source port
826 * allocation problem, so we want to retry now.
827 */
828 si->state = SI_ST_CER;
829 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100830 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
832 return;
833 }
834 else if (si->state == SI_ST_QUE) {
835 /* connection request was queued, check for any update */
836 if (!s->pend_pos) {
837 /* The connection is not in the queue anymore. Either
838 * we have a server connection slot available and we
839 * go directly to the assigned state, or we need to
840 * load-balance first and go to the INI state.
841 */
842 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200843 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844 si->state = SI_ST_REQ;
845 else {
846 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
847 si->state = SI_ST_ASS;
848 }
849 return;
850 }
851
852 /* Connection request still in queue... */
853 if (si->flags & SI_FL_EXP) {
854 /* ... and timeout expired */
855 si->exp = TICK_ETERNITY;
856 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100857 if (srv)
858 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100859 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200860 si_shutr(si);
861 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100862 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100863 if (!si->err_type)
864 si->err_type = SI_ET_QUEUE_TO;
865 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100866 if (s->srv_error)
867 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100868 return;
869 }
870
871 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100872 if ((req->flags & (CF_READ_ERROR)) ||
873 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
874 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100875 /* give up */
876 si->exp = TICK_ETERNITY;
877 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200878 si_shutr(si);
879 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100880 si->err_type |= SI_ET_QUEUE_ABRT;
881 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100882 if (s->srv_error)
883 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100884 return;
885 }
886
887 /* Nothing changed */
888 return;
889 }
890 else if (si->state == SI_ST_TAR) {
891 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100892 if ((req->flags & (CF_READ_ERROR)) ||
893 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
894 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100895 /* give up */
896 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200897 si_shutr(si);
898 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899 si->err_type |= SI_ET_CONN_ABRT;
900 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100901 if (s->srv_error)
902 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903 return;
904 }
905
906 if (!(si->flags & SI_FL_EXP))
907 return; /* still in turn-around */
908
909 si->exp = TICK_ETERNITY;
910
Willy Tarreau87b09662015-04-03 00:22:06 +0200911 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 * marked "assigned".
913 * FIXME: Should we force a redispatch attempt when the server is down ?
914 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200915 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100916 si->state = SI_ST_ASS;
917 else
918 si->state = SI_ST_REQ;
919 return;
920 }
921}
922
Willy Tarreau87b09662015-04-03 00:22:06 +0200923/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900924 * also counts a failed request if the server state has not reached the request
925 * stage.
926 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200927static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900928{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200929 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900930 if (s->si[1].state < SI_ST_REQ) {
931
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200932 strm_fe(s)->fe_counters.failed_req++;
933 if (strm_li(s)->counters)
934 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900935
Willy Tarreaue7dff022015-04-03 01:14:29 +0200936 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900937 }
938 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200939 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900940 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200941 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900942 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900944 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900946 }
947}
948
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100949/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100950 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
951 * a real connection to a server, indicating that a server has been assigned,
952 * or SI_ST_EST for a successful connection to an applet. It may also return
953 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100954 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200955static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100956{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100957 struct stream_interface *si = &s->si[1];
958
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100959 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 +0100960 now_ms, __FUNCTION__,
961 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400962 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100963 s->req.rex, s->res.wex,
964 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400965 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 +0100966
967 if (si->state != SI_ST_REQ)
968 return;
969
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100970 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
971 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100972 struct appctx *appctx = objt_appctx(si->end);
973
974 if (!appctx || appctx->applet != __objt_applet(s->target))
975 appctx = stream_int_register_handler(si, objt_applet(s->target));
976
977 if (!appctx) {
978 /* No more memory, let's immediately abort. Force the
979 * error code to ignore the ERR_LOCAL which is not a
980 * real error.
981 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200982 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100983
984 si_shutr(si);
985 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100986 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100987 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100988 si->state = SI_ST_CLO;
989 if (s->srv_error)
990 s->srv_error(s, si);
991 return;
992 }
993
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100994 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100995 si->state = SI_ST_EST;
996 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +0200997 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +0100998 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100999 return;
1000 }
1001
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001002 /* Try to assign a server */
1003 if (srv_redispatch_connect(s) != 0) {
1004 /* We did not get a server. Either we queued the
1005 * connection request, or we encountered an error.
1006 */
1007 if (si->state == SI_ST_QUE)
1008 return;
1009
1010 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001011 si_shutr(si);
1012 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001013 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001014 if (!si->err_type)
1015 si->err_type = SI_ET_CONN_OTHER;
1016 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001017 if (s->srv_error)
1018 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001019 return;
1020 }
1021
1022 /* The server is assigned */
1023 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1024 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001025 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001026}
1027
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001028/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001029 * if appropriate. The default_backend rule is also considered, then the
1030 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001031 * It returns 1 if the processing can continue on next analysers, or zero if it
1032 * either needs more data or wants to immediately abort the request.
1033 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001034static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001035{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001036 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001037 struct session *sess = s->sess;
1038 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001039
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001040 req->analysers &= ~an_bit;
1041 req->analyse_exp = TICK_ETERNITY;
1042
Willy Tarreau87b09662015-04-03 00:22:06 +02001043 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 +02001044 now_ms, __FUNCTION__,
1045 s,
1046 req,
1047 req->rex, req->wex,
1048 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001049 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001050 req->analysers);
1051
1052 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001053 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001054 struct switching_rule *rule;
1055
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001056 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001057 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001058
Willy Tarreauf51658d2014-04-23 01:21:56 +02001059 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001060 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001061 ret = acl_pass(ret);
1062 if (rule->cond->pol == ACL_COND_UNLESS)
1063 ret = !ret;
1064 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001065
1066 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001067 /* If the backend name is dynamic, try to resolve the name.
1068 * If we can't resolve the name, or if any error occurs, break
1069 * the loop and fallback to the default backend.
1070 */
1071 struct proxy *backend;
1072
1073 if (rule->dynamic) {
1074 struct chunk *tmp = get_trash_chunk();
1075 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1076 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001077 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001078 if (!backend)
1079 break;
1080 }
1081 else
1082 backend = rule->be.backend;
1083
Willy Tarreau87b09662015-04-03 00:22:06 +02001084 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001085 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001086 break;
1087 }
1088 }
1089
1090 /* To ensure correct connection accounting on the backend, we
1091 * have to assign one if it was not set (eg: a listen). This
1092 * measure also takes care of correctly setting the default
1093 * backend if any.
1094 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001095 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001096 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001097 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001098 }
1099
Willy Tarreaufb356202010-08-03 14:02:05 +02001100 /* 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 +02001101 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001102 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1103 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001104 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001105
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001106 /* 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 +02001107 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001108 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001109 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001110 int ret = 1;
1111
1112 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001113 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 +01001114 ret = acl_pass(ret);
1115 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1116 ret = !ret;
1117 }
1118
1119 if (ret) {
1120 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001121 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001122 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001123 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001124 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001125 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001126 break;
1127 }
1128 }
1129
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001130 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001131
1132 sw_failed:
1133 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001134 channel_abort(&s->req);
1135 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001136
Willy Tarreaue7dff022015-04-03 01:14:29 +02001137 if (!(s->flags & SF_ERR_MASK))
1138 s->flags |= SF_ERR_RESOURCE;
1139 if (!(s->flags & SF_FINST_MASK))
1140 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001141
Willy Tarreaueee5b512015-04-03 23:46:31 +02001142 if (s->txn)
1143 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001144 s->req.analysers = 0;
1145 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001146 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001147}
1148
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001149/* This stream analyser works on a request. It applies all use-server rules on
1150 * it then returns 1. The data must already be present in the buffer otherwise
1151 * they won't match. It always returns 1.
1152 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001153static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001154{
1155 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001156 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001157 struct server_rule *rule;
1158
Willy Tarreau87b09662015-04-03 00:22:06 +02001159 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 +02001160 now_ms, __FUNCTION__,
1161 s,
1162 req,
1163 req->rex, req->wex,
1164 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001165 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001166 req->analysers);
1167
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001169 list_for_each_entry(rule, &px->server_rules, list) {
1170 int ret;
1171
Willy Tarreau192252e2015-04-04 01:47:55 +02001172 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001173 ret = acl_pass(ret);
1174 if (rule->cond->pol == ACL_COND_UNLESS)
1175 ret = !ret;
1176
1177 if (ret) {
1178 struct server *srv = rule->srv.ptr;
1179
Willy Tarreau892337c2014-05-13 23:41:20 +02001180 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001181 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001182 (s->flags & SF_FORCE_PRST)) {
1183 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001184 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001185 break;
1186 }
1187 /* if the server is not UP, let's go on with next rules
1188 * just in case another one is suited.
1189 */
1190 }
1191 }
1192 }
1193
1194 req->analysers &= ~an_bit;
1195 req->analyse_exp = TICK_ETERNITY;
1196 return 1;
1197}
1198
Emeric Brun1d33b292010-01-04 15:47:17 +01001199/* This stream analyser works on a request. It applies all sticking rules on
1200 * it then returns 1. The data must already be present in the buffer otherwise
1201 * they won't match. It always returns 1.
1202 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001203static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001204{
1205 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001206 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001207 struct sticking_rule *rule;
1208
Willy Tarreau87b09662015-04-03 00:22:06 +02001209 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 +01001210 now_ms, __FUNCTION__,
1211 s,
1212 req,
1213 req->rex, req->wex,
1214 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001215 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001216 req->analysers);
1217
1218 list_for_each_entry(rule, &px->sticking_rules, list) {
1219 int ret = 1 ;
1220 int i;
1221
Willy Tarreau9667a802013-12-09 12:52:13 +01001222 /* Only the first stick store-request of each table is applied
1223 * and other ones are ignored. The purpose is to allow complex
1224 * configurations which look for multiple entries by decreasing
1225 * order of precision and to stop at the first which matches.
1226 * An example could be a store of the IP address from an HTTP
1227 * header first, then from the source if not found.
1228 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001229 for (i = 0; i < s->store_count; i++) {
1230 if (rule->table.t == s->store[i].table)
1231 break;
1232 }
1233
1234 if (i != s->store_count)
1235 continue;
1236
1237 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001238 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001239 ret = acl_pass(ret);
1240 if (rule->cond->pol == ACL_COND_UNLESS)
1241 ret = !ret;
1242 }
1243
1244 if (ret) {
1245 struct stktable_key *key;
1246
Willy Tarreau192252e2015-04-04 01:47:55 +02001247 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 +01001248 if (!key)
1249 continue;
1250
1251 if (rule->flags & STK_IS_MATCH) {
1252 struct stksess *ts;
1253
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001254 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001255 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001256 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001257 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001258
1259 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001260 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1261 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001262 if (node) {
1263 struct server *srv;
1264
1265 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001266 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001267 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001268 (s->flags & SF_FORCE_PRST)) {
1269 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001270 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001271 }
1272 }
1273 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001274 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001275 }
1276 }
1277 if (rule->flags & STK_IS_STORE) {
1278 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1279 struct stksess *ts;
1280
1281 ts = stksess_new(rule->table.t, key);
1282 if (ts) {
1283 s->store[s->store_count].table = rule->table.t;
1284 s->store[s->store_count++].ts = ts;
1285 }
1286 }
1287 }
1288 }
1289 }
1290
1291 req->analysers &= ~an_bit;
1292 req->analyse_exp = TICK_ETERNITY;
1293 return 1;
1294}
1295
1296/* This stream analyser works on a response. It applies all store rules on it
1297 * then returns 1. The data must already be present in the buffer otherwise
1298 * they won't match. It always returns 1.
1299 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001300static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001301{
1302 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001303 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001304 struct sticking_rule *rule;
1305 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001306 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001307
Willy Tarreau87b09662015-04-03 00:22:06 +02001308 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 +01001309 now_ms, __FUNCTION__,
1310 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001311 rep,
1312 rep->rex, rep->wex,
1313 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001314 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001315 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001316
1317 list_for_each_entry(rule, &px->storersp_rules, list) {
1318 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001319
Willy Tarreau9667a802013-12-09 12:52:13 +01001320 /* Only the first stick store-response of each table is applied
1321 * and other ones are ignored. The purpose is to allow complex
1322 * configurations which look for multiple entries by decreasing
1323 * order of precision and to stop at the first which matches.
1324 * An example could be a store of a set-cookie value, with a
1325 * fallback to a parameter found in a 302 redirect.
1326 *
1327 * The store-response rules are not allowed to override the
1328 * store-request rules for the same table, but they may coexist.
1329 * Thus we can have up to one store-request entry and one store-
1330 * response entry for the same table at any time.
1331 */
1332 for (i = nbreq; i < s->store_count; i++) {
1333 if (rule->table.t == s->store[i].table)
1334 break;
1335 }
1336
1337 /* skip existing entries for this table */
1338 if (i < s->store_count)
1339 continue;
1340
Emeric Brun1d33b292010-01-04 15:47:17 +01001341 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001342 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001343 ret = acl_pass(ret);
1344 if (rule->cond->pol == ACL_COND_UNLESS)
1345 ret = !ret;
1346 }
1347
1348 if (ret) {
1349 struct stktable_key *key;
1350
Willy Tarreau192252e2015-04-04 01:47:55 +02001351 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 +01001352 if (!key)
1353 continue;
1354
Willy Tarreau37e340c2013-12-06 23:05:21 +01001355 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001356 struct stksess *ts;
1357
1358 ts = stksess_new(rule->table.t, key);
1359 if (ts) {
1360 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001361 s->store[s->store_count++].ts = ts;
1362 }
1363 }
1364 }
1365 }
1366
1367 /* process store request and store response */
1368 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001369 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001370 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001371
Willy Tarreauc93cd162014-05-13 15:54:22 +02001372 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001373 stksess_free(s->store[i].table, s->store[i].ts);
1374 s->store[i].ts = NULL;
1375 continue;
1376 }
1377
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001378 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1379 if (ts) {
1380 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001381 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001382 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001383 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001384 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001385 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001386
1387 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001388 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001389 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001390 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001391 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001392
1393 rep->analysers &= ~an_bit;
1394 rep->analyse_exp = TICK_ETERNITY;
1395 return 1;
1396}
1397
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001398/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001399 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001400 */
1401#define UPDATE_ANALYSERS(real, list, back, flag) { \
1402 list = (((list) & ~(flag)) | ~(back)) & (real); \
1403 back = real; \
1404 if (!(list)) \
1405 break; \
1406 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1407 continue; \
1408}
1409
Willy Tarreau87b09662015-04-03 00:22:06 +02001410/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001411 * then puts it back to the wait queue in a clean state, or cleans up its
1412 * resources if it must be deleted. Returns in <next> the date the task wants
1413 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1414 * nothing too many times, the request and response buffers flags are monitored
1415 * and each function is called only if at least another function has changed at
1416 * least one flag it is interested in.
1417 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001418struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001419{
Willy Tarreau827aee92011-03-10 16:55:02 +01001420 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001421 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001422 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001423 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001424 unsigned int rq_prod_last, rq_cons_last;
1425 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001426 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001427 struct channel *req, *res;
1428 struct stream_interface *si_f, *si_b;
1429
1430 req = &s->req;
1431 res = &s->res;
1432
1433 si_f = &s->si[0];
1434 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001435
1436 //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 +01001437 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001438
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001439 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001440 if (s->txn)
1441 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001442
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001443 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001444 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1445 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001446
1447 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001448 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1449 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001450
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001451 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001452 si_f->flags |= SI_FL_DONT_WAKE;
1453 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001454
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001455 /* 1a: Check for low level timeouts if needed. We just set a flag on
1456 * stream interfaces when their timeouts have expired.
1457 */
1458 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001459 stream_int_check_timeouts(si_f);
1460 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001461
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001462 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001463 * for future reads or writes. Note: this will also concern upper layers
1464 * but we do not touch any other flag. We must be careful and correctly
1465 * detect state changes when calling them.
1466 */
1467
Willy Tarreau8f128b42014-11-28 15:07:47 +01001468 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001469
Willy Tarreau8f128b42014-11-28 15:07:47 +01001470 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1471 si_b->flags |= SI_FL_NOLINGER;
1472 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001473 }
1474
Willy Tarreau8f128b42014-11-28 15:07:47 +01001475 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1476 if (si_f->flags & SI_FL_NOHALF)
1477 si_f->flags |= SI_FL_NOLINGER;
1478 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001479 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001480
Willy Tarreau8f128b42014-11-28 15:07:47 +01001481 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001482
Willy Tarreau8f128b42014-11-28 15:07:47 +01001483 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1484 si_f->flags |= SI_FL_NOLINGER;
1485 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001486 }
1487
Willy Tarreau8f128b42014-11-28 15:07:47 +01001488 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1489 if (si_b->flags & SI_FL_NOHALF)
1490 si_b->flags |= SI_FL_NOLINGER;
1491 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001492 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001493
1494 /* Once in a while we're woken up because the task expires. But
1495 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001496 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001497 * timeout needs to be refreshed.
1498 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001499 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001500 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1501 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001502 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001503 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1504 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001505 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001506
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001507 /* below we may emit error messages so we have to ensure that we have
1508 * our buffers properly allocated.
1509 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001510 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001511 /* No buffer available, we've been subscribed to the list of
1512 * buffer waiters, let's wait for our turn.
1513 */
1514 goto update_exp_and_leave;
1515 }
1516
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001517 /* 1b: check for low-level errors reported at the stream interface.
1518 * First we check if it's a retryable error (in which case we don't
1519 * want to tell the buffer). Otherwise we report the error one level
1520 * upper by setting flags into the buffers. Note that the side towards
1521 * the client cannot have connect (hence retryable) errors. Also, the
1522 * connection setup code must be able to deal with any type of abort.
1523 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001524 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001525 if (unlikely(si_f->flags & SI_FL_ERR)) {
1526 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1527 si_shutr(si_f);
1528 si_shutw(si_f);
1529 stream_int_report_error(si_f);
1530 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001531 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001532 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001533 if (srv)
1534 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001535 if (!(s->flags & SF_ERR_MASK))
1536 s->flags |= SF_ERR_CLICL;
1537 if (!(s->flags & SF_FINST_MASK))
1538 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001539 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001540 }
1541 }
1542
Willy Tarreau8f128b42014-11-28 15:07:47 +01001543 if (unlikely(si_b->flags & SI_FL_ERR)) {
1544 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1545 si_shutr(si_b);
1546 si_shutw(si_b);
1547 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001548 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001549 if (srv)
1550 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001551 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001552 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001553 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001554 if (srv)
1555 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001556 if (!(s->flags & SF_ERR_MASK))
1557 s->flags |= SF_ERR_SRVCL;
1558 if (!(s->flags & SF_FINST_MASK))
1559 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001560 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001561 }
1562 /* note: maybe we should process connection errors here ? */
1563 }
1564
Willy Tarreau8f128b42014-11-28 15:07:47 +01001565 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001566 /* we were trying to establish a connection on the server side,
1567 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1568 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001569 if (unlikely(!sess_update_st_con_tcp(s)))
1570 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001571 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001572 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001573
1574 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1575 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1576 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1577 */
1578 }
1579
Willy Tarreau8f128b42014-11-28 15:07:47 +01001580 rq_prod_last = si_f->state;
1581 rq_cons_last = si_b->state;
1582 rp_cons_last = si_f->state;
1583 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001584
1585 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586 /* Check for connection closure */
1587
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001588 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001589 "[%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 +01001590 now_ms, __FUNCTION__, __LINE__,
1591 t,
1592 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001593 req, res,
1594 req->rex, res->wex,
1595 req->flags, res->flags,
1596 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1597 si_f->err_type, si_b->err_type,
1598 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001599
1600 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001601 if (unlikely(si_f->state == SI_ST_DIS))
1602 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001603
1604 /* When a server-side connection is released, we have to count it and
1605 * check for pending connections on this server.
1606 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001607 if (unlikely(si_b->state == SI_ST_DIS)) {
1608 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001609 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001610 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001611 if (s->flags & SF_CURR_SESS) {
1612 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001613 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001614 }
1615 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001616 if (may_dequeue_tasks(srv, s->be))
1617 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001618 }
1619 }
1620
1621 /*
1622 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1623 * at this point.
1624 */
1625
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001626 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001627 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001628 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1629 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1630 si_f->state != rq_prod_last ||
1631 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001632 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001633 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001634
Willy Tarreau8f128b42014-11-28 15:07:47 +01001635 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001636 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001637 unsigned int ana_list;
1638 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001639
Willy Tarreau90deb182010-01-07 00:20:41 +01001640 /* it's up to the analysers to stop new connections,
1641 * disable reading or closing. Note: if an analyser
1642 * disables any of these bits, it is responsible for
1643 * enabling them again when it disables itself, so
1644 * that other analysers are called in similar conditions.
1645 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001646 channel_auto_read(req);
1647 channel_auto_connect(req);
1648 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001649
1650 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001651 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001652 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001653 * the list when not needed. Any analyser may return 0
1654 * to break out of the loop, either because of missing
1655 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001656 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001657 * analyser, and we may loop again if other analysers
1658 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001659 *
1660 * We build a list of analysers to run. We evaluate all
1661 * of these analysers in the order of the lower bit to
1662 * the higher bit. This ordering is very important.
1663 * An analyser will often add/remove other analysers,
1664 * including itself. Any changes to itself have no effect
1665 * on the loop. If it removes any other analysers, we
1666 * want those analysers not to be called anymore during
1667 * this loop. If it adds an analyser that is located
1668 * after itself, we want it to be scheduled for being
1669 * processed during the loop. If it adds an analyser
1670 * which is located before it, we want it to switch to
1671 * it immediately, even if it has already been called
1672 * once but removed since.
1673 *
1674 * In order to achieve this, we compare the analyser
1675 * list after the call with a copy of it before the
1676 * call. The work list is fed with analyser bits that
1677 * appeared during the call. Then we compare previous
1678 * work list with the new one, and check the bits that
1679 * appeared. If the lowest of these bits is lower than
1680 * the current bit, it means we have enabled a previous
1681 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001682 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001683
Willy Tarreau8f128b42014-11-28 15:07:47 +01001684 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001685 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001686 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001687
Willy Tarreaufb356202010-08-03 14:02:05 +02001688 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001689 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001690 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001691 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001692 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001693
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001694 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001695 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001696 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001697 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001698 }
1699
Willy Tarreau748179e2015-05-01 21:52:31 +02001700 if (ana_list & AN_REQ_HTTP_BODY) {
1701 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1702 break;
1703 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1704 }
1705
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001706 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001707 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001708 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001709 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001710 }
1711
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001712 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001713 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001714 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001716 }
1717
Willy Tarreaufb356202010-08-03 14:02:05 +02001718 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001719 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001720 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001721 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001722 }
1723
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001724 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001725 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001726 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001727 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001728 }
1729
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001730 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001731 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001732 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001733 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001734 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001735
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001736 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001737 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001738 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001739 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001740 }
1741
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001742 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001743 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001744 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001745 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001746 }
1747
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001748 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001749 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001750 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001751 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001752 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001753
Emeric Brun1d33b292010-01-04 15:47:17 +01001754 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001755 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001756 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001757 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001758 }
1759
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001760 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001762 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001763 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001764 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001765 break;
1766 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001767 }
Willy Tarreau84455332009-03-15 22:34:05 +01001768
Willy Tarreau8f128b42014-11-28 15:07:47 +01001769 rq_prod_last = si_f->state;
1770 rq_cons_last = si_b->state;
1771 req->flags &= ~CF_WAKE_ONCE;
1772 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001773
Willy Tarreau8f128b42014-11-28 15:07:47 +01001774 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001775 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001776 }
1777
Willy Tarreau576507f2010-01-07 00:09:04 +01001778 /* we'll monitor the request analysers while parsing the response,
1779 * because some response analysers may indirectly enable new request
1780 * analysers (eg: HTTP keep-alive).
1781 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001782 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001783
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001784 resync_response:
1785 /* Analyse response */
1786
Willy Tarreau8f128b42014-11-28 15:07:47 +01001787 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1788 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1789 si_f->state != rp_cons_last ||
1790 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001791 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001792 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001793
Willy Tarreau8f128b42014-11-28 15:07:47 +01001794 if ((res->flags & CF_MASK_ANALYSER) &&
1795 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001796 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1797 * for some free space in the response buffer, so we might need to call
1798 * it when something changes in the response buffer, but still we pass
1799 * it the request buffer. Note that the SI state might very well still
1800 * be zero due to us returning a flow of redirects!
1801 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001802 res->analysers &= ~AN_REQ_ALL;
1803 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001804 }
1805
Willy Tarreau8f128b42014-11-28 15:07:47 +01001806 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001807 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001808 unsigned int ana_list;
1809 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001810
Willy Tarreau90deb182010-01-07 00:20:41 +01001811 /* it's up to the analysers to stop disable reading or
1812 * closing. Note: if an analyser disables any of these
1813 * bits, it is responsible for enabling them again when
1814 * it disables itself, so that other analysers are called
1815 * in similar conditions.
1816 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001817 channel_auto_read(res);
1818 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001819
1820 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001821 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001822 * to MSB. The analysers must remove themselves from
1823 * the list when not needed. Any analyser may return 0
1824 * to break out of the loop, either because of missing
1825 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001826 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001827 * analyser, and we may loop again if other analysers
1828 * are added in the middle.
1829 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001830
Willy Tarreau8f128b42014-11-28 15:07:47 +01001831 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001832 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001833 /* Warning! ensure that analysers are always placed in ascending order! */
1834
Emeric Brun97679e72010-09-23 17:56:44 +02001835 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001836 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001837 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001838 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001839 }
1840
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001841 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001843 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001844 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001845 }
1846
Emeric Brun1d33b292010-01-04 15:47:17 +01001847 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001848 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001849 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001850 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001851 }
1852
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001853 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001854 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001855 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001856 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001857 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001858
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001859 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001860 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001861 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001862 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001863 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001864 break;
1865 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001866 }
1867
Willy Tarreau8f128b42014-11-28 15:07:47 +01001868 rp_cons_last = si_f->state;
1869 rp_prod_last = si_b->state;
1870 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001871
Willy Tarreau8f128b42014-11-28 15:07:47 +01001872 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001873 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001874 }
1875
Willy Tarreau576507f2010-01-07 00:09:04 +01001876 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001877 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001878 goto resync_request;
1879
Willy Tarreau8f128b42014-11-28 15:07:47 +01001880 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001881 goto resync_request;
1882
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001883 /* FIXME: here we should call protocol handlers which rely on
1884 * both buffers.
1885 */
1886
1887
1888 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001889 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001890 * we're just in a data phase here since it means we have not
1891 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001892 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001893 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001894 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001895 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001896 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 req->analysers = 0;
1898 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001899 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001900 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001901 if (srv)
1902 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001903 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001904 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001905 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001906 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001907 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001908 if (srv)
1909 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001910 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001911 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001912 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001913 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001914 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001915 if (srv)
1916 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001917 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001918 }
1919 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001920 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001921 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001922 if (srv)
1923 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001924 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001925 }
Willy Tarreau84455332009-03-15 22:34:05 +01001926 sess_set_term_flags(s);
1927 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001928 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001929 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001930 res->analysers = 0;
1931 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001932 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001933 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001934 if (srv)
1935 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001936 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001937 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001938 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001939 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001940 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001941 if (srv)
1942 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001943 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001944 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001945 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001946 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001947 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001948 if (srv)
1949 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001950 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001951 }
1952 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001953 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001954 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001955 if (srv)
1956 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001957 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001958 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001959 sess_set_term_flags(s);
1960 }
Willy Tarreau84455332009-03-15 22:34:05 +01001961 }
1962
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001963 /*
1964 * Here we take care of forwarding unhandled data. This also includes
1965 * connection establishments and shutdown requests.
1966 */
1967
1968
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001969 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001970 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001971 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001972 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001973 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001974 if (unlikely(!req->analysers &&
1975 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1976 (si_f->state >= SI_ST_EST) &&
1977 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001978 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001979 * attached to it. If any data are left in, we'll permit them to
1980 * move.
1981 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001982 channel_auto_read(req);
1983 channel_auto_connect(req);
1984 channel_auto_close(req);
1985 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001986
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001987 /* We'll let data flow between the producer (if still connected)
1988 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001989 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001990 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1991 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001992 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001993
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001994 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001995 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
1996 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001997 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001998 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
1999 (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 +01002000 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002001 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2002 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002003 (req->flags & CF_STREAMER_FAST)))) {
2004 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002005 }
2006
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002007 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002008 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002009
2010 /*
2011 * Now forward all shutdown requests between both sides of the buffer
2012 */
2013
Willy Tarreau520d95e2009-09-19 21:04:57 +02002014 /* first, let's check if the request buffer needs to shutdown(write), which may
2015 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002016 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2017 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002018 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002019 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002020 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002021 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002022 if (tick_isset(sess->fe->timeout.clientfin)) {
2023 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002024 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002025 }
2026 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002027
2028 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002029 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2030 channel_is_empty(req))) {
2031 if (req->flags & CF_READ_ERROR)
2032 si_b->flags |= SI_FL_NOLINGER;
2033 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002034 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002035 res->rto = s->be->timeout.serverfin;
2036 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002037 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002038 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002039
2040 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002041 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2042 !req->analysers))
2043 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002044
2045 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002046 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2047 if (si_f->flags & SI_FL_NOHALF)
2048 si_f->flags |= SI_FL_NOLINGER;
2049 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002050 if (tick_isset(sess->fe->timeout.clientfin)) {
2051 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002052 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002053 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002054 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002055
Willy Tarreau520d95e2009-09-19 21:04:57 +02002056 /* it's possible that an upper layer has requested a connection setup or abort.
2057 * There are 2 situations where we decide to establish a new connection :
2058 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002059 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002060 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002061 if (si_b->state == SI_ST_INI) {
2062 if (!(req->flags & CF_SHUTW)) {
2063 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002064 /* If we have an appctx, there is no connect method, so we
2065 * immediately switch to the connected state, otherwise we
2066 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002067 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002068 si_b->state = SI_ST_REQ; /* new connection requested */
2069 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002070 }
Willy Tarreau73201222009-08-16 18:27:24 +02002071 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002072 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002073 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2074 channel_shutw_now(req); /* fix buffer flags upon abort */
2075 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002076 }
Willy Tarreau92795622009-03-06 12:51:23 +01002077 }
2078
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002079
2080 /* we may have a pending connection request, or a connection waiting
2081 * for completion.
2082 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002083 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002084
2085 /* prune the request variables and swap to the response variables. */
2086 if (s->vars_reqres.scope != SCOPE_RES) {
2087 vars_prune(&s->vars_reqres, s);
2088 vars_init(&s->vars_reqres, SCOPE_RES);
2089 }
2090
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002091 do {
2092 /* nb: step 1 might switch from QUE to ASS, but we first want
2093 * to give a chance to step 2 to perform a redirect if needed.
2094 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002095 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002096 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002097 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002098 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002099
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002100 /* applets directly go to the ESTABLISHED state. Similarly,
2101 * servers experience the same fate when their connection
2102 * is reused.
2103 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002104 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002105 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002106
Willy Tarreaub44c8732013-12-31 23:16:50 +01002107 /* Now we can add the server name to a header (if requested) */
2108 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002109 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002110 (s->be->server_id_hdr_name != NULL) &&
2111 (s->be->mode == PR_MODE_HTTP) &&
2112 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002113 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002114 }
2115
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002116 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002117 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002118 http_perform_server_redirect(s, si_b);
2119 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002120 }
2121
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002122 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002123 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002124 goto resync_stream_interface;
2125
Willy Tarreau815a9b22010-07-27 17:15:12 +02002126 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002127 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002128 goto resync_request;
2129
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002130 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002131
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002132 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002133 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002134 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002135 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002136 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002137 if (unlikely(!res->analysers &&
2138 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2139 (si_b->state >= SI_ST_EST) &&
2140 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002141 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002142 * attached to it. If any data are left in, we'll permit them to
2143 * move.
2144 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002145 channel_auto_read(res);
2146 channel_auto_close(res);
2147 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002148
2149 /* We'll let data flow between the producer (if still connected)
2150 * to the consumer.
2151 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002152 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2153 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002154
2155 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002156 * tunnel timeout set, use it now. Note that we must respect
2157 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002158 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002159 if (!req->analysers && s->be->timeout.tunnel) {
2160 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002161 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002162
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002163 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2164 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002165 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2166 res->rto = s->be->timeout.serverfin;
2167 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2168 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002169 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2170 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002171
Willy Tarreau8f128b42014-11-28 15:07:47 +01002172 req->rex = tick_add(now_ms, req->rto);
2173 req->wex = tick_add(now_ms, req->wto);
2174 res->rex = tick_add(now_ms, res->rto);
2175 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002176 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002177 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002178
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002179 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002180 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2181 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002182 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002183 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2184 (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 +01002185 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002186 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2187 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002188 (res->flags & CF_STREAMER_FAST)))) {
2189 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002190 }
2191
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002192 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002193 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002194
2195 /*
2196 * Now forward all shutdown requests between both sides of the buffer
2197 */
2198
2199 /*
2200 * FIXME: this is probably where we should produce error responses.
2201 */
2202
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002203 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002204 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002205 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002206 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002207 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002208 req->wto = s->be->timeout.serverfin;
2209 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002210 }
2211 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002212
2213 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002214 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2215 channel_is_empty(res))) {
2216 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002217 if (tick_isset(sess->fe->timeout.clientfin)) {
2218 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002219 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002220 }
2221 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002222
2223 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002224 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2225 !res->analysers)
2226 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002227
2228 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002229 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2230 if (si_b->flags & SI_FL_NOHALF)
2231 si_b->flags |= SI_FL_NOLINGER;
2232 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002233 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002234 req->wto = s->be->timeout.serverfin;
2235 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002236 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002237 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002238
Willy Tarreau8f128b42014-11-28 15:07:47 +01002239 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002240 goto resync_stream_interface;
2241
Willy Tarreau8f128b42014-11-28 15:07:47 +01002242 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002243 goto resync_request;
2244
Willy Tarreau8f128b42014-11-28 15:07:47 +01002245 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002246 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002247
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002248 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 si_f->flags &= ~SI_FL_DONT_WAKE;
2250 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002251
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002252 /* This is needed only when debugging is enabled, to indicate
2253 * client-side or server-side close. Please note that in the unlikely
2254 * event where both sides would close at once, the sequence is reported
2255 * on the server side first.
2256 */
2257 if (unlikely((global.mode & MODE_DEBUG) &&
2258 (!(global.mode & MODE_QUIET) ||
2259 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002260 if (si_b->state == SI_ST_CLO &&
2261 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002262 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002263 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002264 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2265 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002266 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002267 }
2268
Willy Tarreau8f128b42014-11-28 15:07:47 +01002269 if (si_f->state == SI_ST_CLO &&
2270 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002271 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002272 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002273 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2274 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002275 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276 }
2277 }
2278
Willy Tarreau8f128b42014-11-28 15:07:47 +01002279 if (likely((si_f->state != SI_ST_CLO) ||
2280 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002281
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002282 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002283 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002284
Willy Tarreau563cc372015-04-19 18:13:56 +02002285 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002287
Willy Tarreau563cc372015-04-19 18:13:56 +02002288 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002289 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002290
Willy Tarreau8f128b42014-11-28 15:07:47 +01002291 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2292 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2293 si_f->prev_state = si_f->state;
2294 si_b->prev_state = si_b->state;
2295 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2296 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002298 /* Trick: if a request is being waiting for the server to respond,
2299 * and if we know the server can timeout, we don't want the timeout
2300 * to expire on the client side first, but we're still interested
2301 * in passing data from the client to the server (eg: POST). Thus,
2302 * we can cancel the client's request timeout if the server's
2303 * request timeout is set and the server has not yet sent a response.
2304 */
2305
Willy Tarreau8f128b42014-11-28 15:07:47 +01002306 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2307 (tick_isset(req->wex) || tick_isset(res->rex))) {
2308 req->flags |= CF_READ_NOEXP;
2309 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002310 }
2311
Willy Tarreau798f4322012-11-08 14:49:17 +01002312 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002313 t->expire = tick_first(tick_first(req->rex, req->wex),
2314 tick_first(res->rex, res->wex));
2315 if (req->analysers)
2316 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002317
Willy Tarreau8f128b42014-11-28 15:07:47 +01002318 if (si_f->exp)
2319 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002320
Willy Tarreau8f128b42014-11-28 15:07:47 +01002321 if (si_b->exp)
2322 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002323
2324#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002325 fprintf(stderr,
2326 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2327 " 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 +01002328 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2329 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002330#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002331
2332#ifdef DEBUG_DEV
2333 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002334 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002335 ABORT_NOW();
2336#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002337 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002338 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002339 }
2340
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002341 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002342 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002343 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002344 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002345 if (sess->listener) {
2346 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002347 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002348 sess->listener->nbconn--;
2349 if (sess->listener->state == LI_FULL)
2350 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002351
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002352 /* Dequeues all of the listeners waiting for a resource */
2353 if (!LIST_ISEMPTY(&global_listener_queue))
2354 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002355
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002356 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2357 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2358 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002359 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002360
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361 if (unlikely((global.mode & MODE_DEBUG) &&
2362 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002363 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002364 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002365 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2366 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002367 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002368 }
2369
2370 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002371 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002372
Willy Tarreaueee5b512015-04-03 23:46:31 +02002373 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002374 int n;
2375
Willy Tarreaueee5b512015-04-03 23:46:31 +02002376 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002377 if (n < 1 || n > 5)
2378 n = 0;
2379
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002380 if (sess->fe->mode == PR_MODE_HTTP) {
2381 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002382 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002383 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002384 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002385 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002386 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002387 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002388 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002389 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002390 s->be->be_counters.p.http.comp_rsp++;
2391 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002392 }
2393
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002394 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002395 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002396 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002397 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002398 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399 }
2400
Willy Tarreau87b09662015-04-03 00:22:06 +02002401 /* update time stats for this stream */
2402 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002403
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002404 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002405 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002406 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002407 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002408 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002409}
2410
Willy Tarreau87b09662015-04-03 00:22:06 +02002411/* Update the stream's backend and server time stats */
2412void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002413{
2414 int t_request;
2415 int t_queue;
2416 int t_connect;
2417 int t_data;
2418 int t_close;
2419 struct server *srv;
2420
2421 t_request = 0;
2422 t_queue = s->logs.t_queue;
2423 t_connect = s->logs.t_connect;
2424 t_close = s->logs.t_close;
2425 t_data = s->logs.t_data;
2426
2427 if (s->be->mode != PR_MODE_HTTP)
2428 t_data = t_connect;
2429
2430 if (t_connect < 0 || t_data < 0)
2431 return;
2432
2433 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2434 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2435
2436 t_data -= t_connect;
2437 t_connect -= t_queue;
2438 t_queue -= t_request;
2439
2440 srv = objt_server(s->target);
2441 if (srv) {
2442 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2443 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2444 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2445 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2446 }
2447 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2448 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2449 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2450 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2451}
2452
Willy Tarreau7c669d72008-06-20 15:04:11 +02002453/*
2454 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002455 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002456 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002457 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002458 * server.
2459 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002460void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002461{
2462 if (sess->srv_conn == newsrv)
2463 return;
2464
2465 if (sess->srv_conn) {
2466 sess->srv_conn->served--;
2467 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2468 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002469 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002470 }
2471
2472 if (newsrv) {
2473 newsrv->served++;
2474 if (newsrv->proxy->lbprm.server_take_conn)
2475 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002476 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002477 }
2478}
2479
Willy Tarreau84455332009-03-15 22:34:05 +01002480/* Handle server-side errors for default protocols. It is called whenever a a
2481 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002482 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002483 * them. It's installed as ->srv_error for the server-side stream_interface.
2484 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002485void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002486{
2487 int err_type = si->err_type;
2488 int err = 0, fin = 0;
2489
2490 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002491 err = SF_ERR_CLICL;
2492 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002493 }
2494 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002495 err = SF_ERR_CLICL;
2496 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002497 }
2498 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002499 err = SF_ERR_SRVTO;
2500 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002501 }
2502 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002503 err = SF_ERR_SRVCL;
2504 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002505 }
2506 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002507 err = SF_ERR_SRVTO;
2508 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002509 }
2510 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002511 err = SF_ERR_SRVCL;
2512 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002513 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002514 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002515 err = SF_ERR_RESOURCE;
2516 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002517 }
Willy Tarreau84455332009-03-15 22:34:05 +01002518 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002519 err = SF_ERR_INTERNAL;
2520 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002521 }
2522
Willy Tarreaue7dff022015-04-03 01:14:29 +02002523 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002524 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002525 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002526 s->flags |= fin;
2527}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002528
Willy Tarreaue7dff022015-04-03 01:14:29 +02002529/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002530void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002531{
Willy Tarreau87b09662015-04-03 00:22:06 +02002532 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002533 return;
2534
Willy Tarreau87b09662015-04-03 00:22:06 +02002535 channel_shutw_now(&stream->req);
2536 channel_shutr_now(&stream->res);
2537 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002538 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002539 stream->flags |= why;
2540 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002541}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002542
Willy Tarreau8b22a712010-06-18 17:46:06 +02002543/************************************************************************/
2544/* All supported ACL keywords must be declared here. */
2545/************************************************************************/
2546
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002547/* Returns a pointer to a stkctr depending on the fetch keyword name.
2548 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002549 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002550 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002551 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002552 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002553 * NULL may be returned if the designated stkctr is not tracked. For
2554 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2555 * passed. When present, the currently tracked key is then looked up
2556 * in the specified table instead of the current table. The purpose is
2557 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002558 * multiple tables). <strm> is allowed to be NULL, in which case only
2559 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002560 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002561struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002562smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002563{
2564 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002565 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002566 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002567 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002568 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002569
Willy Tarreau0f791d42013-07-23 19:56:43 +02002570 if (num == '_' - '0') {
2571 /* sc_* variant, args[0] = ctr# (mandatory) */
2572 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002573 if (num >= MAX_SESS_STKCTR)
2574 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002575 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002576 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002577 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002578 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002579
2580 if (!conn)
2581 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002582
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002583 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002584 if (!key)
2585 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002586
Willy Tarreaua65536c2013-07-22 22:40:11 +02002587 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002588 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002589 return &stkctr;
2590 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002591
2592 /* Here, <num> contains the counter number from 0 to 9 for
2593 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002594 * args[arg] is the first optional argument. We first lookup the
2595 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002596 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002597
2598 stkptr = &strm->stkctr[num];
2599 if (!strm || !stkctr_entry(stkptr)) {
2600 stkptr = &sess->stkctr[num];
2601 if (!stkctr_entry(stkptr))
2602 return NULL;
2603 }
2604
2605 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002606 if (!stksess)
2607 return NULL;
2608
Willy Tarreau0f791d42013-07-23 19:56:43 +02002609 if (unlikely(args[arg].type == ARGT_TAB)) {
2610 /* an alternate table was specified, let's look up the same key there */
2611 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002612 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002613 return &stkctr;
2614 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002615 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002616}
2617
Willy Tarreau87b09662015-04-03 00:22:06 +02002618/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002619 * currently being tracked or not.
2620 * Supports being called as "sc[0-9]_tracked" only.
2621 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002622static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002623smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002624{
2625 smp->flags = SMP_F_VOL_TEST;
2626 smp->type = SMP_T_BOOL;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002627 smp->data.uint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002628 return 1;
2629}
2630
Willy Tarreau87b09662015-04-03 00:22:06 +02002631/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002632 * frontend counters or from the src.
2633 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2634 * zero is returned if the key is new.
2635 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002636static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002637smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002638{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002639 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002640
2641 if (!stkctr)
2642 return 0;
2643
Willy Tarreau37406352012-04-23 16:16:37 +02002644 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002645 smp->type = SMP_T_UINT;
2646 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002647
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002648 if (stkctr_entry(stkctr) != NULL) {
2649 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002650 if (!ptr)
2651 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002652 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002653 }
2654 return 1;
2655}
2656
Willy Tarreau87b09662015-04-03 00:22:06 +02002657/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002658 * tracked frontend counters or from the src.
2659 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2660 * Value zero is returned if the key is new.
2661 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002662static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002663smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002664{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002665 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002666
2667 if (!stkctr)
2668 return 0;
2669
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002670 smp->flags = SMP_F_VOL_TEST;
2671 smp->type = SMP_T_UINT;
2672 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002673 if (stkctr_entry(stkctr) != NULL) {
2674 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002675 if (!ptr)
2676 return 0; /* parameter not stored */
2677 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002678 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002679 }
2680 return 1;
2681}
2682
Willy Tarreau87b09662015-04-03 00:22:06 +02002683/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002684 * frontend counters and return it into temp integer.
2685 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002686 */
2687static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002688smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002689{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002690 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002691
2692 if (!stkctr)
2693 return 0;
2694
Willy Tarreau37406352012-04-23 16:16:37 +02002695 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002696 smp->type = SMP_T_UINT;
2697 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002698 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002699 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002700
2701 /* First, update gpc0_rate if it's tracked. Second, update its
2702 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2703 */
Emeric Brun57056f02015-06-15 18:29:57 +02002704 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2705 if (ptr1) {
2706 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002707 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Emeric Brun57056f02015-06-15 18:29:57 +02002708 smp->data.uint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002709 }
2710
Emeric Brun57056f02015-06-15 18:29:57 +02002711 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2712 if (ptr2)
2713 smp->data.uint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002714
Emeric Brun57056f02015-06-15 18:29:57 +02002715 /* If data was modified, we need to touch to re-schedule sync */
2716 if (ptr1 || ptr2)
2717 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002718 }
2719 return 1;
2720}
2721
Willy Tarreau87b09662015-04-03 00:22:06 +02002722/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002723 * frontend counters and return its previous value into temp integer.
2724 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002725 */
2726static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002727smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002728{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002729 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002730
2731 if (!stkctr)
2732 return 0;
2733
Willy Tarreau37406352012-04-23 16:16:37 +02002734 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002735 smp->type = SMP_T_UINT;
2736 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002737 if (stkctr_entry(stkctr) != NULL) {
2738 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002739 if (!ptr)
2740 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002741 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002742 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002743 /* If data was modified, we need to touch to re-schedule sync */
2744 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002745 }
2746 return 1;
2747}
2748
Willy Tarreau87b09662015-04-03 00:22:06 +02002749/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002750 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2751 * "src_conn_cnt" only.
2752 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002753static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002754smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002755{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002756 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002757
2758 if (!stkctr)
2759 return 0;
2760
Willy Tarreau37406352012-04-23 16:16:37 +02002761 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002762 smp->type = SMP_T_UINT;
2763 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002764 if (stkctr_entry(stkctr) != NULL) {
2765 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002766 if (!ptr)
2767 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002768 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002769 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002770 return 1;
2771}
2772
Willy Tarreau87b09662015-04-03 00:22:06 +02002773/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002774 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2775 * only.
2776 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002777static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002778smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002779{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002780 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002781
2782 if (!stkctr)
2783 return 0;
2784
Willy Tarreau37406352012-04-23 16:16:37 +02002785 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002786 smp->type = SMP_T_UINT;
2787 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002788 if (stkctr_entry(stkctr) != NULL) {
2789 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002790 if (!ptr)
2791 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002792 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002793 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002794 }
2795 return 1;
2796}
2797
Willy Tarreau87b09662015-04-03 00:22:06 +02002798/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002799 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002800 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002801 */
2802static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002803smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002804{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002805 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002806 struct stksess *ts;
2807 struct stktable_key *key;
2808 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002809 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002810
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002811 if (!conn)
2812 return 0;
2813
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002814 key = addr_to_stktable_key(&conn->addr.from, smp->px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002815 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002816 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002817
Willy Tarreau24e32d82012-04-23 23:55:44 +02002818 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002819
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002820 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2821 /* entry does not exist and could not be created */
2822 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002823
2824 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2825 if (!ptr)
2826 return 0; /* parameter not stored in this table */
2827
Willy Tarreauf853c462012-04-23 18:53:56 +02002828 smp->type = SMP_T_UINT;
2829 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002830 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002831 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002832 return 1;
2833}
2834
Willy Tarreau87b09662015-04-03 00:22:06 +02002835/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002836 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2837 * "src_conn_cur" only.
2838 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002839static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002840smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002841{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002842 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002843
2844 if (!stkctr)
2845 return 0;
2846
Willy Tarreau37406352012-04-23 16:16:37 +02002847 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002848 smp->type = SMP_T_UINT;
2849 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002850 if (stkctr_entry(stkctr) != NULL) {
2851 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002852 if (!ptr)
2853 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002854 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002855 }
2856 return 1;
2857}
2858
Willy Tarreau87b09662015-04-03 00:22:06 +02002859/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002860 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2861 * "src_sess_cnt" only.
2862 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002863static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002864smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002865{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002866 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002867
2868 if (!stkctr)
2869 return 0;
2870
Willy Tarreau37406352012-04-23 16:16:37 +02002871 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002872 smp->type = SMP_T_UINT;
2873 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002874 if (stkctr_entry(stkctr) != NULL) {
2875 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002876 if (!ptr)
2877 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002878 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002879 }
2880 return 1;
2881}
2882
Willy Tarreau87b09662015-04-03 00:22:06 +02002883/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002884 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2885 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002886static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002887smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002888{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002889 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002890
2891 if (!stkctr)
2892 return 0;
2893
Willy Tarreau37406352012-04-23 16:16:37 +02002894 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002895 smp->type = SMP_T_UINT;
2896 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002897 if (stkctr_entry(stkctr) != NULL) {
2898 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002899 if (!ptr)
2900 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002901 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002902 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002903 }
2904 return 1;
2905}
2906
Willy Tarreau87b09662015-04-03 00:22:06 +02002907/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002908 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2909 * "src_http_req_cnt" only.
2910 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002911static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002912smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002913{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002914 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002915
2916 if (!stkctr)
2917 return 0;
2918
Willy Tarreau37406352012-04-23 16:16:37 +02002919 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002920 smp->type = SMP_T_UINT;
2921 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002922 if (stkctr_entry(stkctr) != NULL) {
2923 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002924 if (!ptr)
2925 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002926 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002927 }
2928 return 1;
2929}
2930
Willy Tarreau87b09662015-04-03 00:22:06 +02002931/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002932 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2933 * "src_http_req_rate" only.
2934 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002935static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002936smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002937{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002938 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002939
2940 if (!stkctr)
2941 return 0;
2942
Willy Tarreau37406352012-04-23 16:16:37 +02002943 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002944 smp->type = SMP_T_UINT;
2945 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002946 if (stkctr_entry(stkctr) != NULL) {
2947 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002948 if (!ptr)
2949 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002950 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002951 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002952 }
2953 return 1;
2954}
2955
Willy Tarreau87b09662015-04-03 00:22:06 +02002956/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002957 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2958 * "src_http_err_cnt" only.
2959 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002960static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002961smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002962{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002963 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002964
2965 if (!stkctr)
2966 return 0;
2967
Willy Tarreau37406352012-04-23 16:16:37 +02002968 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002969 smp->type = SMP_T_UINT;
2970 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002971 if (stkctr_entry(stkctr) != NULL) {
2972 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002973 if (!ptr)
2974 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002975 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002976 }
2977 return 1;
2978}
2979
Willy Tarreau87b09662015-04-03 00:22:06 +02002980/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02002981 * counters. Supports being called as "sc[0-9]_http_err_rate" or
2982 * "src_http_err_rate" only.
2983 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002984static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002985smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002986{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002987 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02002988
2989 if (!stkctr)
2990 return 0;
2991
Willy Tarreau37406352012-04-23 16:16:37 +02002992 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002993 smp->type = SMP_T_UINT;
2994 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002995 if (stkctr_entry(stkctr) != NULL) {
2996 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002997 if (!ptr)
2998 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002999 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003000 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003001 }
3002 return 1;
3003}
3004
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003005/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003006 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003007 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3008 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003009static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003010smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003011{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003012 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003013
3014 if (!stkctr)
3015 return 0;
3016
Willy Tarreau37406352012-04-23 16:16:37 +02003017 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003018 smp->type = SMP_T_UINT;
3019 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003020 if (stkctr_entry(stkctr) != NULL) {
3021 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003022 if (!ptr)
3023 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003024 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003025 }
3026 return 1;
3027}
3028
Willy Tarreau613fe992013-07-23 17:39:19 +02003029/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003030 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003031 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003032 */
3033static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003034smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003035{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003036 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003037
3038 if (!stkctr)
3039 return 0;
3040
Willy Tarreau37406352012-04-23 16:16:37 +02003041 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003042 smp->type = SMP_T_UINT;
3043 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003044 if (stkctr_entry(stkctr) != NULL) {
3045 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003046 if (!ptr)
3047 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003048 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003049 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003050 }
3051 return 1;
3052}
3053
Willy Tarreau53aea102013-07-23 17:39:02 +02003054/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003055 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003056 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3057 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003058static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003059smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003060{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003061 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003062
3063 if (!stkctr)
3064 return 0;
3065
Willy Tarreau37406352012-04-23 16:16:37 +02003066 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003067 smp->type = SMP_T_UINT;
3068 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003069 if (stkctr_entry(stkctr) != NULL) {
3070 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003071 if (!ptr)
3072 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003073 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003074 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003075 return 1;
3076}
3077
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003078/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003079 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003080 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003081 */
3082static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003083smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003084{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003085 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003086
3087 if (!stkctr)
3088 return 0;
3089
Willy Tarreau37406352012-04-23 16:16:37 +02003090 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003091 smp->type = SMP_T_UINT;
3092 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003093 if (stkctr_entry(stkctr) != NULL) {
3094 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003095 if (!ptr)
3096 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003097 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003098 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003099 }
3100 return 1;
3101}
3102
Willy Tarreau87b09662015-04-03 00:22:06 +02003103/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003104 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3105 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003106static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003107smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003108{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003109 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003110
Willy Tarreau563eef42013-07-23 18:32:02 +02003111 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003112 return 0;
3113
Willy Tarreau563eef42013-07-23 18:32:02 +02003114 smp->flags = SMP_F_VOL_TEST;
3115 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003116 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003117 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003118}
3119
Willy Tarreau34db1082012-04-19 17:16:54 +02003120/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003121 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003122 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003123static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003124smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003125{
Willy Tarreau37406352012-04-23 16:16:37 +02003126 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003127 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003128 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003129 return 1;
3130}
3131
Willy Tarreau34db1082012-04-19 17:16:54 +02003132/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003133 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003134 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003135static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003136smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003137{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003138 struct proxy *px;
3139
Willy Tarreau24e32d82012-04-23 23:55:44 +02003140 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003141 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003142 smp->type = SMP_T_UINT;
3143 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003144 return 1;
3145}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003146
Willy Tarreau61612d42012-04-19 18:42:05 +02003147/* Note: must not be declared <const> as its list will be overwritten.
3148 * Please take care of keeping this list alphabetically sorted.
3149 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003150static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003151 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003152}};
3153
Willy Tarreau281c7992013-01-08 01:23:27 +01003154/* Note: must not be declared <const> as its list will be overwritten.
3155 * Please take care of keeping this list alphabetically sorted.
3156 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003157static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003158 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3159 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3160 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3161 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3162 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3163 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3164 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3165 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3166 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3167 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3168 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3169 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3170 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3171 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3172 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3173 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3174 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3175 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3176 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3177 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3178 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3179 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3180 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3181 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3182 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3183 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3184 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3185 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3186 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3187 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3188 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3189 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3190 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3191 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3192 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3193 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3194 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3195 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3196 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3197 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3198 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3199 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3200 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3201 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3207 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3210 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3211 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3214 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3216 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3217 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3219 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3220 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3221 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3222 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3224 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3226 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3229 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3230 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3233 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3235 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3236 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3237 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3238 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3239 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3240 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3241 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3242 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3243 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3244 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3245 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3246 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3247 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3248 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3249 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3250 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3251 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3252 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3253 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003254 { /* END */ },
3255}};
3256
Willy Tarreau8b22a712010-06-18 17:46:06 +02003257__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003258static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003259{
Willy Tarreau281c7992013-01-08 01:23:27 +01003260 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003261 acl_register_keywords(&acl_kws);
3262}
3263
Willy Tarreaubaaee002006-06-26 02:48:02 +02003264/*
3265 * Local variables:
3266 * c-indent-level: 8
3267 * c-basic-offset: 8
3268 * End:
3269 */