blob: 77a67b808160ab10cc9d4b36fedcf270a8b1f614 [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>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreau87b09662015-04-03 00:22:06 +020054struct pool_head *pool2_stream;
55struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau87b09662015-04-03 00:22:06 +020057/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010058struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
59
Willy Tarreau9903f0e2015-04-04 18:50:31 +020060/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020061 * handshake, in order to complete initialization of a valid stream. It must be
62 * called with a session (which may be embryonic). It returns the pointer to
63 * the newly created stream, or NULL in case of fatal error. The client-facing
64 * end point is assigned to <origin>, which must be valid. The task's context
65 * is set to the new stream, and its function is set to process_stream().
66 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020067 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020068struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020069{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020070 struct stream *s;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020071 struct connection *conn = objt_conn(origin);
72 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020073
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020074 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020075 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020076
77 /* minimum stream initialization required for an embryonic stream is
78 * fairly low. We need very little to execute L4 ACLs, then we need a
79 * task to make the client-side connection live on its own.
80 * - flags
81 * - stick-entry tracking
82 */
83 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020084 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020085 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020086 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
87 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
88 tv_zero(&s->logs.tv_request);
89 s->logs.t_queue = -1;
90 s->logs.t_connect = -1;
91 s->logs.t_data = -1;
92 s->logs.t_close = 0;
93 s->logs.bytes_in = s->logs.bytes_out = 0;
94 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
95 s->logs.srv_queue_size = 0; /* we will get this number soon */
96
97 /* default logging function */
98 s->do_log = strm_log;
99
100 /* default error reporting function, may be changed by analysers */
101 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200102
103 /* Initialise the current rule list pointer to NULL. We are sure that
104 * any rulelist match the NULL pointer.
105 */
106 s->current_rule_list = NULL;
107
108 memset(s->stkctr, 0, sizeof(s->stkctr));
109
110 s->sess = sess;
111 s->si[0].flags = SI_FL_NONE;
112 s->si[1].flags = SI_FL_ISBACK;
113
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200114 s->uniq_id = global.req_count++;
115
Willy Tarreau87b09662015-04-03 00:22:06 +0200116 /* OK, we're keeping the stream, so let's properly initialize the stream */
117 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200118 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100119 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200120
Willy Tarreaue7dff022015-04-03 01:14:29 +0200121 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200122 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200123
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200124 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200125 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200126 t->context = s;
127 t->expire = TICK_ETERNITY;
128
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200130 * This changes later when switching rules are executed or
131 * when the default backend is assigned.
132 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200133 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200134 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100135 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200136 s->req_cap = NULL;
137 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200138
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200139 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100140 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200141 si_set_state(&s->si[0], SI_ST_EST);
142
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200143 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200144 if (conn)
145 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200146 else if (appctx)
147 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200148
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200149 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200150 s->si[0].flags |= SI_FL_INDEP_STR;
151
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200152 /* pre-initialize the other side's stream interface to an INIT state. The
153 * callbacks will be initialized before attempting to connect.
154 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100155 si_reset(&s->si[1]);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200156 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200157
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200158 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200159 s->si[1].flags |= SI_FL_INDEP_STR;
160
Willy Tarreau87b09662015-04-03 00:22:06 +0200161 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200162 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200163 s->pend_pos = NULL;
164
165 /* init store persistence */
166 s->store_count = 0;
167
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100168 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100169 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200170 s->req.analysers = 0;
171 channel_auto_connect(&s->req); /* don't wait to establish connection */
172 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200173
174 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100175 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100176 s->req.rex = TICK_ETERNITY;
177 s->req.wex = TICK_ETERNITY;
178 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200179
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100180 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100181 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100182 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200183
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200184 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100185 s->req.flags |= CF_NEVER_WAIT;
186 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200187 }
188
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200189 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100190 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100191 s->res.rex = TICK_ETERNITY;
192 s->res.wex = TICK_ETERNITY;
193 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200194
Willy Tarreaueee5b512015-04-03 23:46:31 +0200195 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100196
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100197 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100198
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200199 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200200 if (conn)
201 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200202 else if (appctx)
203 s->si[0].flags |= SI_FL_WAIT_DATA;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200204
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200205 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200206 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200207
208 /* it is important not to call the wakeup function directly but to
209 * pass through task_wakeup(), because this one knows how to apply
210 * priorities to tasks.
211 */
212 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200213 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200214
215 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200216 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100217 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200218 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200219 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200220}
221
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200223 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200225static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200227 struct session *sess = strm_sess(s);
228 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100229 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200230 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200231 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100232
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 if (s->pend_pos)
234 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100235
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100236 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200237 if (s->flags & SF_CURR_SESS) {
238 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100239 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100240 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100241 if (may_dequeue_tasks(objt_server(s->target), s->be))
242 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100243 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100244
Willy Tarreau7c669d72008-06-20 15:04:11 +0200245 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200246 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200247 * it should normally be only the same as the one above,
248 * so this should not happen in fact.
249 */
250 sess_change_server(s, NULL);
251 }
252
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100253 if (s->req.pipe)
254 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100255
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100256 if (s->res.pipe)
257 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100258
Willy Tarreaubf883e02014-11-25 21:10:35 +0100259 /* We may still be present in the buffer wait queue */
260 if (!LIST_ISEMPTY(&s->buffer_wait)) {
261 LIST_DEL(&s->buffer_wait);
262 LIST_INIT(&s->buffer_wait);
263 }
264
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100265 b_drop(&s->req.buf);
266 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100267 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200268 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200269
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100270 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200271 if (s->txn)
272 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100273
Willy Tarreau1e954912012-10-12 17:50:05 +0200274 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200275 if (cli_conn)
276 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200277
Willy Tarreaua4cda672010-06-06 18:28:49 +0200278 for (i = 0; i < s->store_count; i++) {
279 if (!s->store[i].ts)
280 continue;
281 stksess_free(s->store[i].table, s->store[i].ts);
282 s->store[i].ts = NULL;
283 }
284
Willy Tarreaueee5b512015-04-03 23:46:31 +0200285 if (s->txn) {
286 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
287 pool_free2(pool2_http_txn, s->txn);
288 s->txn = NULL;
289 }
290
Willy Tarreau92fb9832007-10-16 17:34:28 +0200291 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200292 pool_free2(fe->rsp_cap_pool, s->res_cap);
293 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200294 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100295
Willy Tarreau87b09662015-04-03 00:22:06 +0200296 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200297
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100298 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100299 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200300 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100301 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100302 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100303 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200304 if (s->list.n != &streams)
305 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100306 bref->ref = s->list.n;
307 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100308 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200309 si_release_endpoint(&s->si[1]);
310 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200311
312 /* FIXME: for now we have a 1:1 relation between stream and session so
313 * the stream must free the session.
314 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200315 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200316 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200317
318 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200319 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200320 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200321 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200322 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200323 pool_flush2(pool2_requri);
324 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200325 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200326 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100327 pool_flush2(pool2_connection);
328 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200329 pool_flush2(fe->req_cap_pool);
330 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200331 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200332}
333
Willy Tarreau78955f42014-12-28 13:09:02 +0100334/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
335 * that it's not the last available buffer or it's the response buffer. Unless
336 * the buffer is the response buffer, an extra control is made so that we always
337 * keep <tune.buffers.reserved> buffers available after this allocation. To be
338 * called at the beginning of recv() callbacks to ensure that the required
339 * buffers are properly allocated. Returns 0 in case of failure, non-zero
340 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100341 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200342int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100343{
Willy Tarreau87b09662015-04-03 00:22:06 +0200344 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100345 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100346 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100347
Willy Tarreau78955f42014-12-28 13:09:02 +0100348 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100349 margin = global.tune.reserved_bufs;
350
Willy Tarreau78955f42014-12-28 13:09:02 +0100351 s = chn_sess(chn);
352
353 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100354 if (b)
355 return 1;
356
Willy Tarreaubf883e02014-11-25 21:10:35 +0100357 if (LIST_ISEMPTY(&s->buffer_wait))
358 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100359 return 0;
360}
361
Willy Tarreau87b09662015-04-03 00:22:06 +0200362/* Allocates a work buffer for stream <s>. It is meant to be called inside
363 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200364 * to work fine, which is the response buffer so that an error message may be
365 * built and returned. Response buffers may be allocated from the reserve, this
366 * is critical to ensure that a response may always flow and will never block a
367 * server from releasing a connection. Returns 0 in case of failure, non-zero
368 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100369 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200370int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100371{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100372 if (!LIST_ISEMPTY(&s->buffer_wait)) {
373 LIST_DEL(&s->buffer_wait);
374 LIST_INIT(&s->buffer_wait);
375 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100376
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200377 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100378 return 1;
379
Willy Tarreaubf883e02014-11-25 21:10:35 +0100380 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100381 return 0;
382}
383
384/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100385 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200386 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100387 * a single buffer, so it makes sense to try to wake two up when two buffers
388 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100389 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200390void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100391{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100392 if (s->req.buf->size && buffer_empty(s->req.buf))
393 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100394
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100395 if (s->res.buf->size && buffer_empty(s->res.buf))
396 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100397
Willy Tarreaubf883e02014-11-25 21:10:35 +0100398 /* if we're certain to have at least 1 buffer available, and there is
399 * someone waiting, we can wake up a waiter and offer them.
400 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100401 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200402 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100403}
404
Willy Tarreau87b09662015-04-03 00:22:06 +0200405/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100406 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200407 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100408 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200409void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100410{
Willy Tarreau87b09662015-04-03 00:22:06 +0200411 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100412
413 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100414 if (rqlimit <= run_queue)
415 break;
416
Willy Tarreaubf883e02014-11-25 21:10:35 +0100417 if (sess->task->state & TASK_RUNNING)
418 continue;
419
420 LIST_DEL(&sess->buffer_wait);
421 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100422 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100424}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200425
426/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200427int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200428{
Willy Tarreau87b09662015-04-03 00:22:06 +0200429 LIST_INIT(&streams);
430 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
431 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200432}
433
Willy Tarreau87b09662015-04-03 00:22:06 +0200434void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100435{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200436 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100437 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100438 void *ptr;
439 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100440
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100441 bytes = s->req.total - s->logs.bytes_in;
442 s->logs.bytes_in = s->req.total;
443 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200444 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100445
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100446 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100447
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100448 if (objt_server(s->target))
449 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200450
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200451 if (sess->listener && sess->listener->counters)
452 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200453
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100454 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200455 struct stkctr *stkctr = &s->stkctr[i];
456
457 if (!stkctr_entry(stkctr)) {
458 stkctr = &sess->stkctr[i];
459 if (!stkctr_entry(stkctr))
460 continue;
461 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200462
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200463 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100464 if (ptr)
465 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200466
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200467 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100468 if (ptr)
469 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200470 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100471 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100472 }
473
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100474 bytes = s->res.total - s->logs.bytes_out;
475 s->logs.bytes_out = s->res.total;
476 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200477 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100478
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100479 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100480
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100481 if (objt_server(s->target))
482 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200483
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200484 if (sess->listener && sess->listener->counters)
485 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200486
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100487 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200488 struct stkctr *stkctr = &s->stkctr[i];
489
490 if (!stkctr_entry(stkctr)) {
491 stkctr = &sess->stkctr[i];
492 if (!stkctr_entry(stkctr))
493 continue;
494 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200495
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200496 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100497 if (ptr)
498 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200499
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200500 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100501 if (ptr)
502 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200503 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100504 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100505 }
506}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200507
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100508/* This function is called with (si->state == SI_ST_CON) meaning that a
509 * connection was attempted and that the file descriptor is already allocated.
510 * We must check for establishment, error and abort. Possible output states
511 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
512 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200513 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100514 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200515static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100516{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100517 struct stream_interface *si = &s->si[1];
518 struct channel *req = &s->req;
519 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200520 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100521
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100522 /* If we got an error, or if nothing happened and the connection timed
523 * out, we must give up. The CER state handler will take care of retry
524 * attempts and error reports.
525 */
526 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100527 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100528 /* Some data were sent past the connection establishment,
529 * so we need to pretend we're established to log correctly
530 * and let later states handle the failure.
531 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100532 si->state = SI_ST_EST;
533 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100534 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100535 return 1;
536 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100537 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100539
Willy Tarreauf79c8172013-10-21 16:30:56 +0200540 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541
542 if (si->err_type)
543 return 0;
544
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100545 if (si->flags & SI_FL_ERR)
546 si->err_type = SI_ET_CONN_ERR;
547 else
548 si->err_type = SI_ET_CONN_TO;
549 return 0;
550 }
551
552 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100553 if (!(req->flags & CF_WRITE_PARTIAL) &&
554 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200555 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
556 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100557 s->be->options & PR_O_ABRT_CLOSE)))) {
558 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200559 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100561 if (s->srv_error)
562 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100563 return 1;
564 }
565
566 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200567 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100568 return 1;
569
570 /* OK, this means that a connection succeeded. The caller will be
571 * responsible for handling the transition from CON to EST.
572 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100573 si->state = SI_ST_EST;
574 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 return 1;
576}
577
578/* This function is called with (si->state == SI_ST_CER) meaning that a
579 * previous connection attempt has failed and that the file descriptor
580 * has already been released. Possible causes include asynchronous error
581 * notification and time out. Possible output states are SI_ST_CLO when
582 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
583 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
584 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
585 * marked as in error state. It returns 0.
586 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200587static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100588{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100589 struct stream_interface *si = &s->si[1];
590
Willy Tarreau87b09662015-04-03 00:22:06 +0200591 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100592 if (objt_server(s->target)) {
593 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100594
Willy Tarreaue7dff022015-04-03 01:14:29 +0200595 if (s->flags & SF_CURR_SESS) {
596 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100597 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100599 }
600
601 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200602 si->conn_retries--;
603 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 if (!si->err_type) {
605 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 }
607
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100608 if (objt_server(s->target))
609 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100610 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100611 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100612 if (may_dequeue_tasks(objt_server(s->target), s->be))
613 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614
615 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200616 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100617 s->req.flags |= CF_WRITE_ERROR;
618 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619
620 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100621 if (s->srv_error)
622 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100623 return 0;
624 }
625
626 /* If the "redispatch" option is set on the backend, we are allowed to
627 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200628 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100629 * bit to ignore any persistence cookie. We won't count a retry nor a
630 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200631 * If the connection is not persistent, the balancing algorithm is not
632 * determinist (round robin) and there is more than one active server,
633 * we accept to perform an immediate redispatch without waiting since
634 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100635 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200636 if (objt_server(s->target) &&
637 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200638 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200639 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200640 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100641 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100642 if (may_dequeue_tasks(objt_server(s->target), s->be))
643 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644
Willy Tarreaue7dff022015-04-03 01:14:29 +0200645 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646 si->state = SI_ST_REQ;
647 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100648 if (objt_server(s->target))
649 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100650 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651 si->state = SI_ST_ASS;
652 }
653
654 if (si->flags & SI_FL_ERR) {
655 /* The error was an asynchronous connection error, and we will
656 * likely have to retry connecting to the same server, most
657 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200658 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100659 */
660
Willy Tarreaub0290662014-06-13 17:04:44 +0200661 int delay = 1000;
662
663 if (s->be->timeout.connect && s->be->timeout.connect < delay)
664 delay = s->be->timeout.connect;
665
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666 if (!si->err_type)
667 si->err_type = SI_ET_CONN_ERR;
668
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200669 /* only wait when we're retrying on the same server */
670 if (si->state == SI_ST_ASS ||
671 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
672 (s->be->srv_act <= 1)) {
673 si->state = SI_ST_TAR;
674 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
675 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100676 return 0;
677 }
678 return 0;
679}
680
681/*
682 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200683 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200684 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100685 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200686static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100687{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100688 struct stream_interface *si = &s->si[1];
689 struct channel *req = &s->req;
690 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100691
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100692 /* First, centralize the timers information */
693 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
694 si->exp = TICK_ETERNITY;
695
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100696 if (objt_server(s->target))
697 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100698
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100699 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 /* if the user wants to log as soon as possible, without counting
701 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200702 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100704 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100705 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100706 }
707 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100708 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 }
710
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200711 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200712 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200713 if (req->flags & CF_WAKE_CONNECT) {
714 req->flags |= CF_WAKE_ONCE;
715 req->flags &= ~CF_WAKE_CONNECT;
716 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200717 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200718 /* real connections have timeouts */
719 req->wto = s->be->timeout.server;
720 rep->rto = s->be->timeout.server;
721 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100722 req->wex = TICK_ETERNITY;
723}
724
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100725/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
726 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100727 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
728 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
729 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100730 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200731static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100732{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100733 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100734 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100735 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100736
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100737 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 +0100738 now_ms, __FUNCTION__,
739 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100740 req, s->rep,
741 req->rex, s->res.wex,
742 req->flags, s->res.flags,
743 req->buf->i, req->buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100744
745 if (si->state == SI_ST_ASS) {
746 /* Server assigned to connection request, we have to try to connect now */
747 int conn_err;
748
749 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100750 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100751
Willy Tarreaue7dff022015-04-03 01:14:29 +0200752 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100753 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100754 if (srv)
755 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500756 if (srv)
757 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 return;
759 }
760
761 /* We have received a synchronous error. We might have to
762 * abort, retry immediately or redispatch.
763 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200764 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100765 if (!si->err_type) {
766 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100767 }
768
Willy Tarreau827aee92011-03-10 16:55:02 +0100769 if (srv)
770 srv_inc_sess_ctr(srv);
771 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500772 srv_set_sess_last(srv);
773 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100774 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100775 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100776
Willy Tarreau87b09662015-04-03 00:22:06 +0200777 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100778 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100779 if (may_dequeue_tasks(srv, s->be))
780 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100781
782 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200783 si_shutr(si);
784 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100785 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100786
787 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
788
Willy Tarreau87b09662015-04-03 00:22:06 +0200789 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100790 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100791 if (s->srv_error)
792 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100793 return;
794 }
795
796 /* We are facing a retryable error, but we don't want to run a
797 * turn-around now, as the problem is likely a source port
798 * allocation problem, so we want to retry now.
799 */
800 si->state = SI_ST_CER;
801 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100802 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100803 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
804 return;
805 }
806 else if (si->state == SI_ST_QUE) {
807 /* connection request was queued, check for any update */
808 if (!s->pend_pos) {
809 /* The connection is not in the queue anymore. Either
810 * we have a server connection slot available and we
811 * go directly to the assigned state, or we need to
812 * load-balance first and go to the INI state.
813 */
814 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200815 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100816 si->state = SI_ST_REQ;
817 else {
818 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
819 si->state = SI_ST_ASS;
820 }
821 return;
822 }
823
824 /* Connection request still in queue... */
825 if (si->flags & SI_FL_EXP) {
826 /* ... and timeout expired */
827 si->exp = TICK_ETERNITY;
828 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100829 if (srv)
830 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100831 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200832 si_shutr(si);
833 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100834 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100835 if (!si->err_type)
836 si->err_type = SI_ET_QUEUE_TO;
837 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100838 if (s->srv_error)
839 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100840 return;
841 }
842
843 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100844 if ((req->flags & (CF_READ_ERROR)) ||
845 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
846 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100847 /* give up */
848 si->exp = TICK_ETERNITY;
849 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200850 si_shutr(si);
851 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100852 si->err_type |= SI_ET_QUEUE_ABRT;
853 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100854 if (s->srv_error)
855 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856 return;
857 }
858
859 /* Nothing changed */
860 return;
861 }
862 else if (si->state == SI_ST_TAR) {
863 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100864 if ((req->flags & (CF_READ_ERROR)) ||
865 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
866 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100867 /* give up */
868 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200869 si_shutr(si);
870 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100871 si->err_type |= SI_ET_CONN_ABRT;
872 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100873 if (s->srv_error)
874 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100875 return;
876 }
877
878 if (!(si->flags & SI_FL_EXP))
879 return; /* still in turn-around */
880
881 si->exp = TICK_ETERNITY;
882
Willy Tarreau87b09662015-04-03 00:22:06 +0200883 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100884 * marked "assigned".
885 * FIXME: Should we force a redispatch attempt when the server is down ?
886 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200887 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100888 si->state = SI_ST_ASS;
889 else
890 si->state = SI_ST_REQ;
891 return;
892 }
893}
894
Willy Tarreau87b09662015-04-03 00:22:06 +0200895/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900896 * also counts a failed request if the server state has not reached the request
897 * stage.
898 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200899static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900900{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200901 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900902 if (s->si[1].state < SI_ST_REQ) {
903
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200904 strm_fe(s)->fe_counters.failed_req++;
905 if (strm_li(s)->counters)
906 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900907
Willy Tarreaue7dff022015-04-03 01:14:29 +0200908 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900909 }
910 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200911 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900912 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200913 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900914 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200915 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900916 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200917 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900918 }
919}
920
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100922 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
923 * a real connection to a server, indicating that a server has been assigned,
924 * or SI_ST_EST for a successful connection to an applet. It may also return
925 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100926 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200927static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100928{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100929 struct stream_interface *si = &s->si[1];
930
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100931 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 +0100932 now_ms, __FUNCTION__,
933 s,
934 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100935 s->req.rex, s->res.wex,
936 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100937 s->req.buf->i, s->req.buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->req.cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938
939 if (si->state != SI_ST_REQ)
940 return;
941
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100942 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
943 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100944 struct appctx *appctx = objt_appctx(si->end);
945
946 if (!appctx || appctx->applet != __objt_applet(s->target))
947 appctx = stream_int_register_handler(si, objt_applet(s->target));
948
949 if (!appctx) {
950 /* No more memory, let's immediately abort. Force the
951 * error code to ignore the ERR_LOCAL which is not a
952 * real error.
953 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200954 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100955
956 si_shutr(si);
957 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100958 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100959 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100960 si->state = SI_ST_CLO;
961 if (s->srv_error)
962 s->srv_error(s, si);
963 return;
964 }
965
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100966 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100967 si->state = SI_ST_EST;
968 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +0200969 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +0100970 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100971 return;
972 }
973
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100974 /* Try to assign a server */
975 if (srv_redispatch_connect(s) != 0) {
976 /* We did not get a server. Either we queued the
977 * connection request, or we encountered an error.
978 */
979 if (si->state == SI_ST_QUE)
980 return;
981
982 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200983 si_shutr(si);
984 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100985 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100986 if (!si->err_type)
987 si->err_type = SI_ET_CONN_OTHER;
988 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100989 if (s->srv_error)
990 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100991 return;
992 }
993
994 /* The server is assigned */
995 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
996 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +0200997 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100998}
999
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001000/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001001 * if appropriate. The default_backend rule is also considered, then the
1002 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001003 * It returns 1 if the processing can continue on next analysers, or zero if it
1004 * either needs more data or wants to immediately abort the request.
1005 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001006static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001007{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001008 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001009 struct session *sess = s->sess;
1010 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001011
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001012 req->analysers &= ~an_bit;
1013 req->analyse_exp = TICK_ETERNITY;
1014
Willy Tarreau87b09662015-04-03 00:22:06 +02001015 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 +02001016 now_ms, __FUNCTION__,
1017 s,
1018 req,
1019 req->rex, req->wex,
1020 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001021 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001022 req->analysers);
1023
1024 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001025 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001026 struct switching_rule *rule;
1027
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001028 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001029 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001030
Willy Tarreauf51658d2014-04-23 01:21:56 +02001031 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001032 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001033 ret = acl_pass(ret);
1034 if (rule->cond->pol == ACL_COND_UNLESS)
1035 ret = !ret;
1036 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001037
1038 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001039 /* If the backend name is dynamic, try to resolve the name.
1040 * If we can't resolve the name, or if any error occurs, break
1041 * the loop and fallback to the default backend.
1042 */
1043 struct proxy *backend;
1044
1045 if (rule->dynamic) {
1046 struct chunk *tmp = get_trash_chunk();
1047 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1048 break;
1049 backend = findproxy(tmp->str, PR_CAP_BE);
1050 if (!backend)
1051 break;
1052 }
1053 else
1054 backend = rule->be.backend;
1055
Willy Tarreau87b09662015-04-03 00:22:06 +02001056 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001057 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001058 break;
1059 }
1060 }
1061
1062 /* To ensure correct connection accounting on the backend, we
1063 * have to assign one if it was not set (eg: a listen). This
1064 * measure also takes care of correctly setting the default
1065 * backend if any.
1066 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001067 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001068 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001069 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001070 }
1071
Willy Tarreaufb356202010-08-03 14:02:05 +02001072 /* 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 +02001073 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001074 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1075 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001076 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001077
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001078 /* 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 +02001079 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001080 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001081 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001082 int ret = 1;
1083
1084 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001085 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 +01001086 ret = acl_pass(ret);
1087 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1088 ret = !ret;
1089 }
1090
1091 if (ret) {
1092 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001093 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001094 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001095 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001096 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001097 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001098 break;
1099 }
1100 }
1101
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001102 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001103
1104 sw_failed:
1105 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001106 channel_abort(&s->req);
1107 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001108
Willy Tarreaue7dff022015-04-03 01:14:29 +02001109 if (!(s->flags & SF_ERR_MASK))
1110 s->flags |= SF_ERR_RESOURCE;
1111 if (!(s->flags & SF_FINST_MASK))
1112 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001113
Willy Tarreaueee5b512015-04-03 23:46:31 +02001114 if (s->txn)
1115 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001116 s->req.analysers = 0;
1117 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001118 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001119}
1120
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001121/* This stream analyser works on a request. It applies all use-server rules on
1122 * it then returns 1. The data must already be present in the buffer otherwise
1123 * they won't match. It always returns 1.
1124 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001125static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001126{
1127 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001128 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001129 struct server_rule *rule;
1130
Willy Tarreau87b09662015-04-03 00:22:06 +02001131 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 +02001132 now_ms, __FUNCTION__,
1133 s,
1134 req,
1135 req->rex, req->wex,
1136 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001137 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001138 req->analysers);
1139
Willy Tarreaue7dff022015-04-03 01:14:29 +02001140 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001141 list_for_each_entry(rule, &px->server_rules, list) {
1142 int ret;
1143
Willy Tarreau192252e2015-04-04 01:47:55 +02001144 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001145 ret = acl_pass(ret);
1146 if (rule->cond->pol == ACL_COND_UNLESS)
1147 ret = !ret;
1148
1149 if (ret) {
1150 struct server *srv = rule->srv.ptr;
1151
Willy Tarreau892337c2014-05-13 23:41:20 +02001152 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001153 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001154 (s->flags & SF_FORCE_PRST)) {
1155 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001156 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001157 break;
1158 }
1159 /* if the server is not UP, let's go on with next rules
1160 * just in case another one is suited.
1161 */
1162 }
1163 }
1164 }
1165
1166 req->analysers &= ~an_bit;
1167 req->analyse_exp = TICK_ETERNITY;
1168 return 1;
1169}
1170
Emeric Brun1d33b292010-01-04 15:47:17 +01001171/* This stream analyser works on a request. It applies all sticking rules on
1172 * it then returns 1. The data must already be present in the buffer otherwise
1173 * they won't match. It always returns 1.
1174 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001175static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001176{
1177 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001178 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001179 struct sticking_rule *rule;
1180
Willy Tarreau87b09662015-04-03 00:22:06 +02001181 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 +01001182 now_ms, __FUNCTION__,
1183 s,
1184 req,
1185 req->rex, req->wex,
1186 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001187 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001188 req->analysers);
1189
1190 list_for_each_entry(rule, &px->sticking_rules, list) {
1191 int ret = 1 ;
1192 int i;
1193
Willy Tarreau9667a802013-12-09 12:52:13 +01001194 /* Only the first stick store-request of each table is applied
1195 * and other ones are ignored. The purpose is to allow complex
1196 * configurations which look for multiple entries by decreasing
1197 * order of precision and to stop at the first which matches.
1198 * An example could be a store of the IP address from an HTTP
1199 * header first, then from the source if not found.
1200 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001201 for (i = 0; i < s->store_count; i++) {
1202 if (rule->table.t == s->store[i].table)
1203 break;
1204 }
1205
1206 if (i != s->store_count)
1207 continue;
1208
1209 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001210 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001211 ret = acl_pass(ret);
1212 if (rule->cond->pol == ACL_COND_UNLESS)
1213 ret = !ret;
1214 }
1215
1216 if (ret) {
1217 struct stktable_key *key;
1218
Willy Tarreau192252e2015-04-04 01:47:55 +02001219 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 +01001220 if (!key)
1221 continue;
1222
1223 if (rule->flags & STK_IS_MATCH) {
1224 struct stksess *ts;
1225
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001226 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001227 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001228 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001229 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001230
1231 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001232 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1233 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001234 if (node) {
1235 struct server *srv;
1236
1237 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001238 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001239 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001240 (s->flags & SF_FORCE_PRST)) {
1241 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001242 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001243 }
1244 }
1245 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001246 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001247 }
1248 }
1249 if (rule->flags & STK_IS_STORE) {
1250 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1251 struct stksess *ts;
1252
1253 ts = stksess_new(rule->table.t, key);
1254 if (ts) {
1255 s->store[s->store_count].table = rule->table.t;
1256 s->store[s->store_count++].ts = ts;
1257 }
1258 }
1259 }
1260 }
1261 }
1262
1263 req->analysers &= ~an_bit;
1264 req->analyse_exp = TICK_ETERNITY;
1265 return 1;
1266}
1267
1268/* This stream analyser works on a response. It applies all store rules on it
1269 * then returns 1. The data must already be present in the buffer otherwise
1270 * they won't match. It always returns 1.
1271 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001272static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001273{
1274 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001275 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001276 struct sticking_rule *rule;
1277 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001278 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001279
Willy Tarreau87b09662015-04-03 00:22:06 +02001280 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 +01001281 now_ms, __FUNCTION__,
1282 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001283 rep,
1284 rep->rex, rep->wex,
1285 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001286 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001287 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001288
1289 list_for_each_entry(rule, &px->storersp_rules, list) {
1290 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001291
Willy Tarreau9667a802013-12-09 12:52:13 +01001292 /* Only the first stick store-response of each table is applied
1293 * and other ones are ignored. The purpose is to allow complex
1294 * configurations which look for multiple entries by decreasing
1295 * order of precision and to stop at the first which matches.
1296 * An example could be a store of a set-cookie value, with a
1297 * fallback to a parameter found in a 302 redirect.
1298 *
1299 * The store-response rules are not allowed to override the
1300 * store-request rules for the same table, but they may coexist.
1301 * Thus we can have up to one store-request entry and one store-
1302 * response entry for the same table at any time.
1303 */
1304 for (i = nbreq; i < s->store_count; i++) {
1305 if (rule->table.t == s->store[i].table)
1306 break;
1307 }
1308
1309 /* skip existing entries for this table */
1310 if (i < s->store_count)
1311 continue;
1312
Emeric Brun1d33b292010-01-04 15:47:17 +01001313 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001314 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001315 ret = acl_pass(ret);
1316 if (rule->cond->pol == ACL_COND_UNLESS)
1317 ret = !ret;
1318 }
1319
1320 if (ret) {
1321 struct stktable_key *key;
1322
Willy Tarreau192252e2015-04-04 01:47:55 +02001323 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 +01001324 if (!key)
1325 continue;
1326
Willy Tarreau37e340c2013-12-06 23:05:21 +01001327 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001328 struct stksess *ts;
1329
1330 ts = stksess_new(rule->table.t, key);
1331 if (ts) {
1332 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001333 s->store[s->store_count++].ts = ts;
1334 }
1335 }
1336 }
1337 }
1338
1339 /* process store request and store response */
1340 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001341 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001342 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001343
Willy Tarreauc93cd162014-05-13 15:54:22 +02001344 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001345 stksess_free(s->store[i].table, s->store[i].ts);
1346 s->store[i].ts = NULL;
1347 continue;
1348 }
1349
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001350 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1351 if (ts) {
1352 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001353 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001354 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001355 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001356 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001357 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001358
1359 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001360 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001361 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001362 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001363 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001364
1365 rep->analysers &= ~an_bit;
1366 rep->analyse_exp = TICK_ETERNITY;
1367 return 1;
1368}
1369
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001370/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001371 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001372 */
1373#define UPDATE_ANALYSERS(real, list, back, flag) { \
1374 list = (((list) & ~(flag)) | ~(back)) & (real); \
1375 back = real; \
1376 if (!(list)) \
1377 break; \
1378 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1379 continue; \
1380}
1381
Willy Tarreau87b09662015-04-03 00:22:06 +02001382/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001383 * then puts it back to the wait queue in a clean state, or cleans up its
1384 * resources if it must be deleted. Returns in <next> the date the task wants
1385 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1386 * nothing too many times, the request and response buffers flags are monitored
1387 * and each function is called only if at least another function has changed at
1388 * least one flag it is interested in.
1389 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001390struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001391{
Willy Tarreau827aee92011-03-10 16:55:02 +01001392 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001393 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001394 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001395 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001396 unsigned int rq_prod_last, rq_cons_last;
1397 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001398 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001399 struct channel *req, *res;
1400 struct stream_interface *si_f, *si_b;
1401
1402 req = &s->req;
1403 res = &s->res;
1404
1405 si_f = &s->si[0];
1406 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001407
1408 //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 +01001409 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001410
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001411 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001412 if (s->txn)
1413 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001414
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001415 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001416 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1417 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001418
1419 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001420 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1421 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001422
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001423 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001424 si_f->flags |= SI_FL_DONT_WAKE;
1425 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001426
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001427 /* 1a: Check for low level timeouts if needed. We just set a flag on
1428 * stream interfaces when their timeouts have expired.
1429 */
1430 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001431 stream_int_check_timeouts(si_f);
1432 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001433
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001434 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001435 * for future reads or writes. Note: this will also concern upper layers
1436 * but we do not touch any other flag. We must be careful and correctly
1437 * detect state changes when calling them.
1438 */
1439
Willy Tarreau8f128b42014-11-28 15:07:47 +01001440 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001441
Willy Tarreau8f128b42014-11-28 15:07:47 +01001442 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1443 si_b->flags |= SI_FL_NOLINGER;
1444 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001445 }
1446
Willy Tarreau8f128b42014-11-28 15:07:47 +01001447 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1448 if (si_f->flags & SI_FL_NOHALF)
1449 si_f->flags |= SI_FL_NOLINGER;
1450 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001451 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001452
Willy Tarreau8f128b42014-11-28 15:07:47 +01001453 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001454
Willy Tarreau8f128b42014-11-28 15:07:47 +01001455 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1456 si_f->flags |= SI_FL_NOLINGER;
1457 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001458 }
1459
Willy Tarreau8f128b42014-11-28 15:07:47 +01001460 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1461 if (si_b->flags & SI_FL_NOHALF)
1462 si_b->flags |= SI_FL_NOLINGER;
1463 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001464 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001465
1466 /* Once in a while we're woken up because the task expires. But
1467 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001468 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001469 * timeout needs to be refreshed.
1470 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001471 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001472 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1473 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001474 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001475 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1476 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001477 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001478
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001479 /* below we may emit error messages so we have to ensure that we have
1480 * our buffers properly allocated.
1481 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001482 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001483 /* No buffer available, we've been subscribed to the list of
1484 * buffer waiters, let's wait for our turn.
1485 */
1486 goto update_exp_and_leave;
1487 }
1488
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001489 /* 1b: check for low-level errors reported at the stream interface.
1490 * First we check if it's a retryable error (in which case we don't
1491 * want to tell the buffer). Otherwise we report the error one level
1492 * upper by setting flags into the buffers. Note that the side towards
1493 * the client cannot have connect (hence retryable) errors. Also, the
1494 * connection setup code must be able to deal with any type of abort.
1495 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001496 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001497 if (unlikely(si_f->flags & SI_FL_ERR)) {
1498 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1499 si_shutr(si_f);
1500 si_shutw(si_f);
1501 stream_int_report_error(si_f);
1502 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001503 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001504 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001505 if (srv)
1506 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001507 if (!(s->flags & SF_ERR_MASK))
1508 s->flags |= SF_ERR_CLICL;
1509 if (!(s->flags & SF_FINST_MASK))
1510 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001511 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001512 }
1513 }
1514
Willy Tarreau8f128b42014-11-28 15:07:47 +01001515 if (unlikely(si_b->flags & SI_FL_ERR)) {
1516 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1517 si_shutr(si_b);
1518 si_shutw(si_b);
1519 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001520 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001521 if (srv)
1522 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001523 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001524 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001525 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001526 if (srv)
1527 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001528 if (!(s->flags & SF_ERR_MASK))
1529 s->flags |= SF_ERR_SRVCL;
1530 if (!(s->flags & SF_FINST_MASK))
1531 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001532 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001533 }
1534 /* note: maybe we should process connection errors here ? */
1535 }
1536
Willy Tarreau8f128b42014-11-28 15:07:47 +01001537 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001538 /* we were trying to establish a connection on the server side,
1539 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1540 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001541 if (unlikely(!sess_update_st_con_tcp(s)))
1542 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001543 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001544 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001545
1546 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1547 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1548 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1549 */
1550 }
1551
Willy Tarreau8f128b42014-11-28 15:07:47 +01001552 rq_prod_last = si_f->state;
1553 rq_cons_last = si_b->state;
1554 rp_cons_last = si_f->state;
1555 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001556
1557 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001558 /* Check for connection closure */
1559
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001560 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001561 "[%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 +01001562 now_ms, __FUNCTION__, __LINE__,
1563 t,
1564 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001565 req, res,
1566 req->rex, res->wex,
1567 req->flags, res->flags,
1568 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1569 si_f->err_type, si_b->err_type,
1570 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001571
1572 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001573 if (unlikely(si_f->state == SI_ST_DIS))
1574 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001575
1576 /* When a server-side connection is released, we have to count it and
1577 * check for pending connections on this server.
1578 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001579 if (unlikely(si_b->state == SI_ST_DIS)) {
1580 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001581 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001582 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001583 if (s->flags & SF_CURR_SESS) {
1584 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001585 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586 }
1587 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001588 if (may_dequeue_tasks(srv, s->be))
1589 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001590 }
1591 }
1592
1593 /*
1594 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1595 * at this point.
1596 */
1597
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001598 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001599 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001600 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1601 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1602 si_f->state != rq_prod_last ||
1603 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001604 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001605 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001606
Willy Tarreau8f128b42014-11-28 15:07:47 +01001607 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001608 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001609 unsigned int ana_list;
1610 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001611
Willy Tarreau90deb182010-01-07 00:20:41 +01001612 /* it's up to the analysers to stop new connections,
1613 * disable reading or closing. Note: if an analyser
1614 * disables any of these bits, it is responsible for
1615 * enabling them again when it disables itself, so
1616 * that other analysers are called in similar conditions.
1617 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001618 channel_auto_read(req);
1619 channel_auto_connect(req);
1620 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001621
1622 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001623 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001624 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001625 * the list when not needed. Any analyser may return 0
1626 * to break out of the loop, either because of missing
1627 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001628 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001629 * analyser, and we may loop again if other analysers
1630 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001631 *
1632 * We build a list of analysers to run. We evaluate all
1633 * of these analysers in the order of the lower bit to
1634 * the higher bit. This ordering is very important.
1635 * An analyser will often add/remove other analysers,
1636 * including itself. Any changes to itself have no effect
1637 * on the loop. If it removes any other analysers, we
1638 * want those analysers not to be called anymore during
1639 * this loop. If it adds an analyser that is located
1640 * after itself, we want it to be scheduled for being
1641 * processed during the loop. If it adds an analyser
1642 * which is located before it, we want it to switch to
1643 * it immediately, even if it has already been called
1644 * once but removed since.
1645 *
1646 * In order to achieve this, we compare the analyser
1647 * list after the call with a copy of it before the
1648 * call. The work list is fed with analyser bits that
1649 * appeared during the call. Then we compare previous
1650 * work list with the new one, and check the bits that
1651 * appeared. If the lowest of these bits is lower than
1652 * the current bit, it means we have enabled a previous
1653 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001654 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001655
Willy Tarreau8f128b42014-11-28 15:07:47 +01001656 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001657 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001658 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001659
Willy Tarreaufb356202010-08-03 14:02:05 +02001660 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001661 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001662 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001663 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001664 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001665
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001666 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001667 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001668 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001669 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001670 }
1671
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001672 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001673 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001674 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001675 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001676 }
1677
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001678 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001679 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001680 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001681 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001682 }
1683
Willy Tarreaufb356202010-08-03 14:02:05 +02001684 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001685 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001686 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001687 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001688 }
1689
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001690 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001691 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001692 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001693 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001694 }
1695
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001696 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001697 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001698 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001699 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001700 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001701
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001702 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001703 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001704 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001705 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001706 }
1707
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001708 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001709 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001710 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001711 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001712 }
1713
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001714 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001716 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001718 }
Emeric Brun647caf12009-06-30 17:57:00 +02001719
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001720 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001721 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001722 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001723 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001724 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001725
Emeric Brun1d33b292010-01-04 15:47:17 +01001726 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001727 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001728 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001729 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001730 }
1731
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001732 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001733 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001734 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001735 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001736 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001737 break;
1738 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001739 }
Willy Tarreau84455332009-03-15 22:34:05 +01001740
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 rq_prod_last = si_f->state;
1742 rq_cons_last = si_b->state;
1743 req->flags &= ~CF_WAKE_ONCE;
1744 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001745
Willy Tarreau8f128b42014-11-28 15:07:47 +01001746 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001747 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001748 }
1749
Willy Tarreau576507f2010-01-07 00:09:04 +01001750 /* we'll monitor the request analysers while parsing the response,
1751 * because some response analysers may indirectly enable new request
1752 * analysers (eg: HTTP keep-alive).
1753 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001754 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001755
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001756 resync_response:
1757 /* Analyse response */
1758
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1760 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1761 si_f->state != rp_cons_last ||
1762 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001763 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001764 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001765
Willy Tarreau8f128b42014-11-28 15:07:47 +01001766 if ((res->flags & CF_MASK_ANALYSER) &&
1767 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001768 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1769 * for some free space in the response buffer, so we might need to call
1770 * it when something changes in the response buffer, but still we pass
1771 * it the request buffer. Note that the SI state might very well still
1772 * be zero due to us returning a flow of redirects!
1773 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001774 res->analysers &= ~AN_REQ_ALL;
1775 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001776 }
1777
Willy Tarreau8f128b42014-11-28 15:07:47 +01001778 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001779 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001780 unsigned int ana_list;
1781 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001782
Willy Tarreau90deb182010-01-07 00:20:41 +01001783 /* it's up to the analysers to stop disable reading or
1784 * closing. Note: if an analyser disables any of these
1785 * bits, it is responsible for enabling them again when
1786 * it disables itself, so that other analysers are called
1787 * in similar conditions.
1788 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 channel_auto_read(res);
1790 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001791
1792 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001793 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001794 * to MSB. The analysers must remove themselves from
1795 * the list when not needed. Any analyser may return 0
1796 * to break out of the loop, either because of missing
1797 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001798 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001799 * analyser, and we may loop again if other analysers
1800 * are added in the middle.
1801 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001802
Willy Tarreau8f128b42014-11-28 15:07:47 +01001803 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001804 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001805 /* Warning! ensure that analysers are always placed in ascending order! */
1806
Emeric Brun97679e72010-09-23 17:56:44 +02001807 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001808 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001809 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001810 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001811 }
1812
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001813 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001814 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001815 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001816 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001817 }
1818
Emeric Brun1d33b292010-01-04 15:47:17 +01001819 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001820 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001821 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001822 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001823 }
1824
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001825 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001826 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001827 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001828 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001829 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001830
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001831 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001832 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001833 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001834 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001835 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001836 break;
1837 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001838 }
1839
Willy Tarreau8f128b42014-11-28 15:07:47 +01001840 rp_cons_last = si_f->state;
1841 rp_prod_last = si_b->state;
1842 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001843
Willy Tarreau8f128b42014-11-28 15:07:47 +01001844 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001845 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001846 }
1847
Willy Tarreau576507f2010-01-07 00:09:04 +01001848 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001849 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001850 goto resync_request;
1851
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001853 goto resync_request;
1854
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001855 /* FIXME: here we should call protocol handlers which rely on
1856 * both buffers.
1857 */
1858
1859
1860 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001861 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001862 * we're just in a data phase here since it means we have not
1863 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001864 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001865 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001866 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001868 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001869 req->analysers = 0;
1870 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001871 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001872 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001873 if (srv)
1874 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001875 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001876 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001877 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001878 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001879 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001880 if (srv)
1881 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001882 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001883 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001884 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001885 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001886 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001887 if (srv)
1888 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001889 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001890 }
1891 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001892 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001893 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001894 if (srv)
1895 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001896 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001897 }
Willy Tarreau84455332009-03-15 22:34:05 +01001898 sess_set_term_flags(s);
1899 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001900 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001901 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001902 res->analysers = 0;
1903 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001904 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001905 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001906 if (srv)
1907 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001908 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001909 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001910 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001911 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001912 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001913 if (srv)
1914 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001915 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001916 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001917 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001918 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001919 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001920 if (srv)
1921 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001922 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001923 }
1924 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001925 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001926 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001927 if (srv)
1928 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001929 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001930 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001931 sess_set_term_flags(s);
1932 }
Willy Tarreau84455332009-03-15 22:34:05 +01001933 }
1934
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001935 /*
1936 * Here we take care of forwarding unhandled data. This also includes
1937 * connection establishments and shutdown requests.
1938 */
1939
1940
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001941 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001942 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001943 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001944 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001945 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001946 if (unlikely(!req->analysers &&
1947 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1948 (si_f->state >= SI_ST_EST) &&
1949 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001950 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001951 * attached to it. If any data are left in, we'll permit them to
1952 * move.
1953 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001954 channel_auto_read(req);
1955 channel_auto_connect(req);
1956 channel_auto_close(req);
1957 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001958
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001959 /* We'll let data flow between the producer (if still connected)
1960 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001961 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001962 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1963 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001964 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001965
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001966 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001967 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
1968 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001969 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001970 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
1971 (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 +01001972 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001973 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1974 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001975 (req->flags & CF_STREAMER_FAST)))) {
1976 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001977 }
1978
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001979 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001980 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001981
1982 /*
1983 * Now forward all shutdown requests between both sides of the buffer
1984 */
1985
Willy Tarreau520d95e2009-09-19 21:04:57 +02001986 /* first, let's check if the request buffer needs to shutdown(write), which may
1987 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02001988 * once the server has begun to respond. If a half-closed timeout is set, we adjust
1989 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02001990 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001991 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02001992 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001993 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001994 if (tick_isset(sess->fe->timeout.clientfin)) {
1995 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001996 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02001997 }
1998 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001999
2000 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002001 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2002 channel_is_empty(req))) {
2003 if (req->flags & CF_READ_ERROR)
2004 si_b->flags |= SI_FL_NOLINGER;
2005 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002006 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002007 res->rto = s->be->timeout.serverfin;
2008 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002009 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002010 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002011
2012 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002013 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2014 !req->analysers))
2015 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002016
2017 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002018 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2019 if (si_f->flags & SI_FL_NOHALF)
2020 si_f->flags |= SI_FL_NOLINGER;
2021 si_shutr(si_f);
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 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002026 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002027
Willy Tarreau520d95e2009-09-19 21:04:57 +02002028 /* it's possible that an upper layer has requested a connection setup or abort.
2029 * There are 2 situations where we decide to establish a new connection :
2030 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002031 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002032 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002033 if (si_b->state == SI_ST_INI) {
2034 if (!(req->flags & CF_SHUTW)) {
2035 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002036 /* If we have an appctx, there is no connect method, so we
2037 * immediately switch to the connected state, otherwise we
2038 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002039 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002040 si_b->state = SI_ST_REQ; /* new connection requested */
2041 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002042 }
Willy Tarreau73201222009-08-16 18:27:24 +02002043 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002044 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002045 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2046 channel_shutw_now(req); /* fix buffer flags upon abort */
2047 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002048 }
Willy Tarreau92795622009-03-06 12:51:23 +01002049 }
2050
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002051
2052 /* we may have a pending connection request, or a connection waiting
2053 * for completion.
2054 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002055 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002056 do {
2057 /* nb: step 1 might switch from QUE to ASS, but we first want
2058 * to give a chance to step 2 to perform a redirect if needed.
2059 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002060 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002061 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002062 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002063 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002064
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002065 /* applets directly go to the ESTABLISHED state. Similarly,
2066 * servers experience the same fate when their connection
2067 * is reused.
2068 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002069 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002070 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002071
Willy Tarreaub44c8732013-12-31 23:16:50 +01002072 /* Now we can add the server name to a header (if requested) */
2073 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002074 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002075 (s->be->server_id_hdr_name != NULL) &&
2076 (s->be->mode == PR_MODE_HTTP) &&
2077 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002078 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002079 }
2080
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002081 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002082 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002083 http_perform_server_redirect(s, si_b);
2084 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002085 }
2086
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002087 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002088 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002089 goto resync_stream_interface;
2090
Willy Tarreau815a9b22010-07-27 17:15:12 +02002091 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002092 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002093 goto resync_request;
2094
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002095 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002096
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002097 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002098 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002099 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002100 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002101 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002102 if (unlikely(!res->analysers &&
2103 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2104 (si_b->state >= SI_ST_EST) &&
2105 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002106 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002107 * attached to it. If any data are left in, we'll permit them to
2108 * move.
2109 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002110 channel_auto_read(res);
2111 channel_auto_close(res);
2112 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002113
2114 /* We'll let data flow between the producer (if still connected)
2115 * to the consumer.
2116 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002117 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2118 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002119
2120 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002121 * tunnel timeout set, use it now. Note that we must respect
2122 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002123 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002124 if (!req->analysers && s->be->timeout.tunnel) {
2125 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002126 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002127
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002128 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2129 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002130 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2131 res->rto = s->be->timeout.serverfin;
2132 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2133 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002134 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2135 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002136
Willy Tarreau8f128b42014-11-28 15:07:47 +01002137 req->rex = tick_add(now_ms, req->rto);
2138 req->wex = tick_add(now_ms, req->wto);
2139 res->rex = tick_add(now_ms, res->rto);
2140 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002141 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002142 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002143
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002144 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002145 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2146 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002147 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002148 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2149 (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 +01002150 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002151 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2152 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002153 (res->flags & CF_STREAMER_FAST)))) {
2154 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002155 }
2156
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002157 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002158 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002159
2160 /*
2161 * Now forward all shutdown requests between both sides of the buffer
2162 */
2163
2164 /*
2165 * FIXME: this is probably where we should produce error responses.
2166 */
2167
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002168 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002169 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002170 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002171 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002172 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002173 req->wto = s->be->timeout.serverfin;
2174 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002175 }
2176 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002177
2178 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002179 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2180 channel_is_empty(res))) {
2181 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002182 if (tick_isset(sess->fe->timeout.clientfin)) {
2183 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002184 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002185 }
2186 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002187
2188 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002189 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2190 !res->analysers)
2191 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002192
2193 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002194 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2195 if (si_b->flags & SI_FL_NOHALF)
2196 si_b->flags |= SI_FL_NOLINGER;
2197 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002198 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002199 req->wto = s->be->timeout.serverfin;
2200 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002201 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002202 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002203
Willy Tarreau8f128b42014-11-28 15:07:47 +01002204 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002205 goto resync_stream_interface;
2206
Willy Tarreau8f128b42014-11-28 15:07:47 +01002207 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002208 goto resync_request;
2209
Willy Tarreau8f128b42014-11-28 15:07:47 +01002210 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002211 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002212
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002213 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002214 si_f->flags &= ~SI_FL_DONT_WAKE;
2215 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002216
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002217 /* This is needed only when debugging is enabled, to indicate
2218 * client-side or server-side close. Please note that in the unlikely
2219 * event where both sides would close at once, the sequence is reported
2220 * on the server side first.
2221 */
2222 if (unlikely((global.mode & MODE_DEBUG) &&
2223 (!(global.mode & MODE_QUIET) ||
2224 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002225 if (si_b->state == SI_ST_CLO &&
2226 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002227 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002228 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002229 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2230 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002231 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002232 }
2233
Willy Tarreau8f128b42014-11-28 15:07:47 +01002234 if (si_f->state == SI_ST_CLO &&
2235 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002236 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002237 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002238 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2239 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002240 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002241 }
2242 }
2243
Willy Tarreau8f128b42014-11-28 15:07:47 +01002244 if (likely((si_f->state != SI_ST_CLO) ||
2245 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002246
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002247 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002248 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002249
Willy Tarreau563cc372015-04-19 18:13:56 +02002250 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002251 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002252
Willy Tarreau563cc372015-04-19 18:13:56 +02002253 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002254 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002255
Willy Tarreau8f128b42014-11-28 15:07:47 +01002256 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2257 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2258 si_f->prev_state = si_f->state;
2259 si_b->prev_state = si_b->state;
2260 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2261 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002262
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002263 /* Trick: if a request is being waiting for the server to respond,
2264 * and if we know the server can timeout, we don't want the timeout
2265 * to expire on the client side first, but we're still interested
2266 * in passing data from the client to the server (eg: POST). Thus,
2267 * we can cancel the client's request timeout if the server's
2268 * request timeout is set and the server has not yet sent a response.
2269 */
2270
Willy Tarreau8f128b42014-11-28 15:07:47 +01002271 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2272 (tick_isset(req->wex) || tick_isset(res->rex))) {
2273 req->flags |= CF_READ_NOEXP;
2274 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002275 }
2276
Willy Tarreau798f4322012-11-08 14:49:17 +01002277 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002278 t->expire = tick_first(tick_first(req->rex, req->wex),
2279 tick_first(res->rex, res->wex));
2280 if (req->analysers)
2281 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002282
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (si_f->exp)
2284 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002285
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 if (si_b->exp)
2287 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002288
2289#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002290 fprintf(stderr,
2291 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2292 " 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 +01002293 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2294 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002295#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002296
2297#ifdef DEBUG_DEV
2298 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002299 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002300 ABORT_NOW();
2301#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002302 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002303 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002304 }
2305
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002306 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002307 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002308 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002309 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002310 if (sess->listener) {
2311 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002312 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002313 sess->listener->nbconn--;
2314 if (sess->listener->state == LI_FULL)
2315 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002316
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002317 /* Dequeues all of the listeners waiting for a resource */
2318 if (!LIST_ISEMPTY(&global_listener_queue))
2319 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002320
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002321 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2322 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2323 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002324 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002325
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002326 if (unlikely((global.mode & MODE_DEBUG) &&
2327 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002328 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002329 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002330 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2331 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002332 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002333 }
2334
2335 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002336 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002337
Willy Tarreaueee5b512015-04-03 23:46:31 +02002338 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002339 int n;
2340
Willy Tarreaueee5b512015-04-03 23:46:31 +02002341 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002342 if (n < 1 || n > 5)
2343 n = 0;
2344
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002345 if (sess->fe->mode == PR_MODE_HTTP) {
2346 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002347 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002348 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002349 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002350 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002351 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002352 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002353 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002354 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002355 s->be->be_counters.p.http.comp_rsp++;
2356 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002357 }
2358
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002359 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002360 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002361 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002362 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002363 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002364 }
2365
Willy Tarreau87b09662015-04-03 00:22:06 +02002366 /* update time stats for this stream */
2367 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002368
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002369 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002370 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002371 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002372 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002373 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002374}
2375
Willy Tarreau87b09662015-04-03 00:22:06 +02002376/* Update the stream's backend and server time stats */
2377void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002378{
2379 int t_request;
2380 int t_queue;
2381 int t_connect;
2382 int t_data;
2383 int t_close;
2384 struct server *srv;
2385
2386 t_request = 0;
2387 t_queue = s->logs.t_queue;
2388 t_connect = s->logs.t_connect;
2389 t_close = s->logs.t_close;
2390 t_data = s->logs.t_data;
2391
2392 if (s->be->mode != PR_MODE_HTTP)
2393 t_data = t_connect;
2394
2395 if (t_connect < 0 || t_data < 0)
2396 return;
2397
2398 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2399 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2400
2401 t_data -= t_connect;
2402 t_connect -= t_queue;
2403 t_queue -= t_request;
2404
2405 srv = objt_server(s->target);
2406 if (srv) {
2407 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2408 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2409 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2410 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2411 }
2412 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2413 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2414 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2415 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2416}
2417
Willy Tarreau7c669d72008-06-20 15:04:11 +02002418/*
2419 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002420 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002421 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002422 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002423 * server.
2424 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002425void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002426{
2427 if (sess->srv_conn == newsrv)
2428 return;
2429
2430 if (sess->srv_conn) {
2431 sess->srv_conn->served--;
2432 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2433 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002434 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002435 }
2436
2437 if (newsrv) {
2438 newsrv->served++;
2439 if (newsrv->proxy->lbprm.server_take_conn)
2440 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002441 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002442 }
2443}
2444
Willy Tarreau84455332009-03-15 22:34:05 +01002445/* Handle server-side errors for default protocols. It is called whenever a a
2446 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002447 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002448 * them. It's installed as ->srv_error for the server-side stream_interface.
2449 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002450void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002451{
2452 int err_type = si->err_type;
2453 int err = 0, fin = 0;
2454
2455 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002456 err = SF_ERR_CLICL;
2457 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002458 }
2459 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002460 err = SF_ERR_CLICL;
2461 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002462 }
2463 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002464 err = SF_ERR_SRVTO;
2465 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002466 }
2467 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002468 err = SF_ERR_SRVCL;
2469 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002470 }
2471 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002472 err = SF_ERR_SRVTO;
2473 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002474 }
2475 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002476 err = SF_ERR_SRVCL;
2477 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002478 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002479 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002480 err = SF_ERR_RESOURCE;
2481 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002482 }
Willy Tarreau84455332009-03-15 22:34:05 +01002483 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002484 err = SF_ERR_INTERNAL;
2485 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002486 }
2487
Willy Tarreaue7dff022015-04-03 01:14:29 +02002488 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002489 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002490 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002491 s->flags |= fin;
2492}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002493
Willy Tarreaue7dff022015-04-03 01:14:29 +02002494/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002495void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002496{
Willy Tarreau87b09662015-04-03 00:22:06 +02002497 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002498 return;
2499
Willy Tarreau87b09662015-04-03 00:22:06 +02002500 channel_shutw_now(&stream->req);
2501 channel_shutr_now(&stream->res);
2502 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002503 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002504 stream->flags |= why;
2505 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002506}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002507
Willy Tarreau8b22a712010-06-18 17:46:06 +02002508/************************************************************************/
2509/* All supported ACL keywords must be declared here. */
2510/************************************************************************/
2511
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002512/* Returns a pointer to a stkctr depending on the fetch keyword name.
2513 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002514 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002515 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002516 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002517 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002518 * NULL may be returned if the designated stkctr is not tracked. For
2519 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2520 * passed. When present, the currently tracked key is then looked up
2521 * in the specified table instead of the current table. The purpose is
2522 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002523 * multiple tables). <strm> is allowed to be NULL, in which case only
2524 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002525 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002526struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002527smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002528{
2529 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002530 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002531 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002532 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002533 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002534
Willy Tarreau0f791d42013-07-23 19:56:43 +02002535 if (num == '_' - '0') {
2536 /* sc_* variant, args[0] = ctr# (mandatory) */
2537 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002538 if (num >= MAX_SESS_STKCTR)
2539 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002540 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002541 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002542 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002543 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002544
2545 if (!conn)
2546 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002547
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002548 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002549 if (!key)
2550 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002551
Willy Tarreaua65536c2013-07-22 22:40:11 +02002552 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002553 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002554 return &stkctr;
2555 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002556
2557 /* Here, <num> contains the counter number from 0 to 9 for
2558 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002559 * args[arg] is the first optional argument. We first lookup the
2560 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002561 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002562
2563 stkptr = &strm->stkctr[num];
2564 if (!strm || !stkctr_entry(stkptr)) {
2565 stkptr = &sess->stkctr[num];
2566 if (!stkctr_entry(stkptr))
2567 return NULL;
2568 }
2569
2570 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002571 if (!stksess)
2572 return NULL;
2573
Willy Tarreau0f791d42013-07-23 19:56:43 +02002574 if (unlikely(args[arg].type == ARGT_TAB)) {
2575 /* an alternate table was specified, let's look up the same key there */
2576 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002577 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002578 return &stkctr;
2579 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002580 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002581}
2582
Willy Tarreau87b09662015-04-03 00:22:06 +02002583/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002584 * currently being tracked or not.
2585 * Supports being called as "sc[0-9]_tracked" only.
2586 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002587static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002588smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002589 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002590{
2591 smp->flags = SMP_F_VOL_TEST;
2592 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002593 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002594 return 1;
2595}
2596
Willy Tarreau87b09662015-04-03 00:22:06 +02002597/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002598 * frontend counters or from the src.
2599 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2600 * zero is returned if the key is new.
2601 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002602static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002603smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002604 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002605{
Willy Tarreau192252e2015-04-04 01:47:55 +02002606 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002607
2608 if (!stkctr)
2609 return 0;
2610
Willy Tarreau37406352012-04-23 16:16:37 +02002611 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002612 smp->type = SMP_T_UINT;
2613 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002614
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002615 if (stkctr_entry(stkctr) != NULL) {
2616 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002617 if (!ptr)
2618 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002619 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002620 }
2621 return 1;
2622}
2623
Willy Tarreau87b09662015-04-03 00:22:06 +02002624/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002625 * tracked frontend counters or from the src.
2626 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2627 * Value zero is returned if the key is new.
2628 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002629static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002630smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002631 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002632{
Willy Tarreau192252e2015-04-04 01:47:55 +02002633 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002634
2635 if (!stkctr)
2636 return 0;
2637
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002638 smp->flags = SMP_F_VOL_TEST;
2639 smp->type = SMP_T_UINT;
2640 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002641 if (stkctr_entry(stkctr) != NULL) {
2642 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002643 if (!ptr)
2644 return 0; /* parameter not stored */
2645 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002646 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002647 }
2648 return 1;
2649}
2650
Willy Tarreau87b09662015-04-03 00:22:06 +02002651/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002652 * frontend counters and return it into temp integer.
2653 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002654 */
2655static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002656smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002657 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002658{
Willy Tarreau192252e2015-04-04 01:47:55 +02002659 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002660
2661 if (!stkctr)
2662 return 0;
2663
Willy Tarreau37406352012-04-23 16:16:37 +02002664 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002665 smp->type = SMP_T_UINT;
2666 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002667 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002668 void *ptr;
2669
2670 /* First, update gpc0_rate if it's tracked. Second, update its
2671 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2672 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002673 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002674 if (ptr) {
2675 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002676 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002677 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2678 }
2679
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002680 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002681 if (ptr)
2682 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2683
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002684 }
2685 return 1;
2686}
2687
Willy Tarreau87b09662015-04-03 00:22:06 +02002688/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002689 * frontend counters and return its previous value into temp integer.
2690 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002691 */
2692static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002693smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002694 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002695{
Willy Tarreau192252e2015-04-04 01:47:55 +02002696 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002697
2698 if (!stkctr)
2699 return 0;
2700
Willy Tarreau37406352012-04-23 16:16:37 +02002701 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002702 smp->type = SMP_T_UINT;
2703 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002704 if (stkctr_entry(stkctr) != NULL) {
2705 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002706 if (!ptr)
2707 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002708 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002709 stktable_data_cast(ptr, gpc0) = 0;
2710 }
2711 return 1;
2712}
2713
Willy Tarreau87b09662015-04-03 00:22:06 +02002714/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002715 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2716 * "src_conn_cnt" only.
2717 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002718static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002719smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002720 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002721{
Willy Tarreau192252e2015-04-04 01:47:55 +02002722 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002723
2724 if (!stkctr)
2725 return 0;
2726
Willy Tarreau37406352012-04-23 16:16:37 +02002727 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002728 smp->type = SMP_T_UINT;
2729 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002730 if (stkctr_entry(stkctr) != NULL) {
2731 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002732 if (!ptr)
2733 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002734 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002735 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002736 return 1;
2737}
2738
Willy Tarreau87b09662015-04-03 00:22:06 +02002739/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002740 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2741 * only.
2742 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002743static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002744smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002745 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002746{
Willy Tarreau192252e2015-04-04 01:47:55 +02002747 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002748
2749 if (!stkctr)
2750 return 0;
2751
Willy Tarreau37406352012-04-23 16:16:37 +02002752 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002753 smp->type = SMP_T_UINT;
2754 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002755 if (stkctr_entry(stkctr) != NULL) {
2756 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002757 if (!ptr)
2758 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002759 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002760 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002761 }
2762 return 1;
2763}
2764
Willy Tarreau87b09662015-04-03 00:22:06 +02002765/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002766 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002767 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002768 */
2769static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002770smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002771 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002772{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002773 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002774 struct stksess *ts;
2775 struct stktable_key *key;
2776 void *ptr;
2777
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002778 if (!conn)
2779 return 0;
2780
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002781 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002782 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002783 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002784
Willy Tarreau24e32d82012-04-23 23:55:44 +02002785 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002786
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002787 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2788 /* entry does not exist and could not be created */
2789 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002790
2791 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2792 if (!ptr)
2793 return 0; /* parameter not stored in this table */
2794
Willy Tarreauf853c462012-04-23 18:53:56 +02002795 smp->type = SMP_T_UINT;
2796 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002797 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002798 return 1;
2799}
2800
Willy Tarreau87b09662015-04-03 00:22:06 +02002801/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002802 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2803 * "src_conn_cur" only.
2804 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002805static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002806smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002807 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002808{
Willy Tarreau192252e2015-04-04 01:47:55 +02002809 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002810
2811 if (!stkctr)
2812 return 0;
2813
Willy Tarreau37406352012-04-23 16:16:37 +02002814 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002815 smp->type = SMP_T_UINT;
2816 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002817 if (stkctr_entry(stkctr) != NULL) {
2818 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002819 if (!ptr)
2820 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002821 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002822 }
2823 return 1;
2824}
2825
Willy Tarreau87b09662015-04-03 00:22:06 +02002826/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002827 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2828 * "src_sess_cnt" only.
2829 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002830static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002831smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002832 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002833{
Willy Tarreau192252e2015-04-04 01:47:55 +02002834 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002835
2836 if (!stkctr)
2837 return 0;
2838
Willy Tarreau37406352012-04-23 16:16:37 +02002839 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002840 smp->type = SMP_T_UINT;
2841 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002842 if (stkctr_entry(stkctr) != NULL) {
2843 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002844 if (!ptr)
2845 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002846 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002847 }
2848 return 1;
2849}
2850
Willy Tarreau87b09662015-04-03 00:22:06 +02002851/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002852 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2853 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002854static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002855smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002856 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002857{
Willy Tarreau192252e2015-04-04 01:47:55 +02002858 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002859
2860 if (!stkctr)
2861 return 0;
2862
Willy Tarreau37406352012-04-23 16:16:37 +02002863 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002864 smp->type = SMP_T_UINT;
2865 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002866 if (stkctr_entry(stkctr) != NULL) {
2867 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002868 if (!ptr)
2869 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002870 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002871 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002872 }
2873 return 1;
2874}
2875
Willy Tarreau87b09662015-04-03 00:22:06 +02002876/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002877 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2878 * "src_http_req_cnt" only.
2879 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002880static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002881smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002882 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002883{
Willy Tarreau192252e2015-04-04 01:47:55 +02002884 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002885
2886 if (!stkctr)
2887 return 0;
2888
Willy Tarreau37406352012-04-23 16:16:37 +02002889 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002890 smp->type = SMP_T_UINT;
2891 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002892 if (stkctr_entry(stkctr) != NULL) {
2893 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002894 if (!ptr)
2895 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002896 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002897 }
2898 return 1;
2899}
2900
Willy Tarreau87b09662015-04-03 00:22:06 +02002901/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002902 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2903 * "src_http_req_rate" only.
2904 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002905static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002906smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002907 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002908{
Willy Tarreau192252e2015-04-04 01:47:55 +02002909 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002910
2911 if (!stkctr)
2912 return 0;
2913
Willy Tarreau37406352012-04-23 16:16:37 +02002914 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002915 smp->type = SMP_T_UINT;
2916 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002917 if (stkctr_entry(stkctr) != NULL) {
2918 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002919 if (!ptr)
2920 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002921 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002922 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002923 }
2924 return 1;
2925}
2926
Willy Tarreau87b09662015-04-03 00:22:06 +02002927/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002928 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2929 * "src_http_err_cnt" only.
2930 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002931static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002932smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002933 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002934{
Willy Tarreau192252e2015-04-04 01:47:55 +02002935 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002936
2937 if (!stkctr)
2938 return 0;
2939
Willy Tarreau37406352012-04-23 16:16:37 +02002940 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002941 smp->type = SMP_T_UINT;
2942 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002943 if (stkctr_entry(stkctr) != NULL) {
2944 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002945 if (!ptr)
2946 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002947 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002948 }
2949 return 1;
2950}
2951
Willy Tarreau87b09662015-04-03 00:22:06 +02002952/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02002953 * counters. Supports being called as "sc[0-9]_http_err_rate" or
2954 * "src_http_err_rate" only.
2955 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002956static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002957smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002958 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002959{
Willy Tarreau192252e2015-04-04 01:47:55 +02002960 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02002961
2962 if (!stkctr)
2963 return 0;
2964
Willy Tarreau37406352012-04-23 16:16:37 +02002965 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002966 smp->type = SMP_T_UINT;
2967 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002968 if (stkctr_entry(stkctr) != NULL) {
2969 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002970 if (!ptr)
2971 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002972 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02002973 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002974 }
2975 return 1;
2976}
2977
Willy Tarreau5077d4b2013-07-23 17:17:10 +02002978/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002979 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02002980 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
2981 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002982static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002983smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002984 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002985{
Willy Tarreau192252e2015-04-04 01:47:55 +02002986 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02002987
2988 if (!stkctr)
2989 return 0;
2990
Willy Tarreau37406352012-04-23 16:16:37 +02002991 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002992 smp->type = SMP_T_UINT;
2993 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002994 if (stkctr_entry(stkctr) != NULL) {
2995 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002996 if (!ptr)
2997 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002998 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02002999 }
3000 return 1;
3001}
3002
Willy Tarreau613fe992013-07-23 17:39:19 +02003003/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003004 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003005 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003006 */
3007static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003008smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003009 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003010{
Willy Tarreau192252e2015-04-04 01:47:55 +02003011 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003012
3013 if (!stkctr)
3014 return 0;
3015
Willy Tarreau37406352012-04-23 16:16:37 +02003016 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003017 smp->type = SMP_T_UINT;
3018 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003019 if (stkctr_entry(stkctr) != NULL) {
3020 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003021 if (!ptr)
3022 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003023 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003024 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003025 }
3026 return 1;
3027}
3028
Willy Tarreau53aea102013-07-23 17:39:02 +02003029/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003030 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003031 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3032 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003033static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003034smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003035 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003036{
Willy Tarreau192252e2015-04-04 01:47:55 +02003037 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003038
3039 if (!stkctr)
3040 return 0;
3041
Willy Tarreau37406352012-04-23 16:16:37 +02003042 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003043 smp->type = SMP_T_UINT;
3044 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003045 if (stkctr_entry(stkctr) != NULL) {
3046 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003047 if (!ptr)
3048 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003049 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003050 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003051 return 1;
3052}
3053
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003054/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003055 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003056 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003057 */
3058static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003059smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003060 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003061{
Willy Tarreau192252e2015-04-04 01:47:55 +02003062 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003063
3064 if (!stkctr)
3065 return 0;
3066
Willy Tarreau37406352012-04-23 16:16:37 +02003067 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003068 smp->type = SMP_T_UINT;
3069 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003070 if (stkctr_entry(stkctr) != NULL) {
3071 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003072 if (!ptr)
3073 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003074 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003075 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003076 }
3077 return 1;
3078}
3079
Willy Tarreau87b09662015-04-03 00:22:06 +02003080/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003081 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3082 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003083static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003084smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003085 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003086{
Willy Tarreau192252e2015-04-04 01:47:55 +02003087 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003088
Willy Tarreau563eef42013-07-23 18:32:02 +02003089 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003090 return 0;
3091
Willy Tarreau563eef42013-07-23 18:32:02 +02003092 smp->flags = SMP_F_VOL_TEST;
3093 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003094 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003095 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003096}
3097
Willy Tarreau34db1082012-04-19 17:16:54 +02003098/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003099 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003100 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003101static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003102smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003103 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003104{
Willy Tarreau37406352012-04-23 16:16:37 +02003105 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003106 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003107 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003108 return 1;
3109}
3110
Willy Tarreau34db1082012-04-19 17:16:54 +02003111/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003112 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003113 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003114static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003115smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003116 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003117{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003118 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003119 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003120 smp->type = SMP_T_UINT;
3121 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003122 return 1;
3123}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003124
Willy Tarreau61612d42012-04-19 18:42:05 +02003125/* Note: must not be declared <const> as its list will be overwritten.
3126 * Please take care of keeping this list alphabetically sorted.
3127 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003128static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003129 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003130}};
3131
Willy Tarreau281c7992013-01-08 01:23:27 +01003132/* Note: must not be declared <const> as its list will be overwritten.
3133 * Please take care of keeping this list alphabetically sorted.
3134 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003135static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003136 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3137 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3138 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3139 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3140 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3141 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3142 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3143 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3144 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3145 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3146 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3147 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3148 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3149 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3150 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3151 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3152 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3153 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3154 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3155 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3156 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3157 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3158 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3159 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3160 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3161 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3162 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3163 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3164 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3165 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3166 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3167 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3168 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3169 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3170 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3171 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3172 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3173 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3174 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3175 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3176 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3177 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3178 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3179 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3180 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3181 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3182 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3183 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3184 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3185 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3186 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3187 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3188 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3189 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3190 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3191 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3192 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3193 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3194 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3195 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3196 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3197 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3198 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3199 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3200 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3201 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3202 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3205 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3207 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3208 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3211 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3213 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3214 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3215 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3216 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3217 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3218 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3219 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3220 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3221 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3222 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3223 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3224 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3225 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3226 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3227 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3228 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3229 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3230 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003232 { /* END */ },
3233}};
3234
Willy Tarreau8b22a712010-06-18 17:46:06 +02003235__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003236static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003237{
Willy Tarreau281c7992013-01-08 01:23:27 +01003238 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003239 acl_register_keywords(&acl_kws);
3240}
3241
Willy Tarreaubaaee002006-06-26 02:48:02 +02003242/*
3243 * Local variables:
3244 * c-indent-level: 8
3245 * c-basic-offset: 8
3246 * End:
3247 */