blob: ed97e488640c079378ef02bc5fb1c6cfb3e2233f [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 Tarreaua24adf02014-11-27 01:11:56 +0100364 * to work fine. For a regular connection, only the response is needed so that
365 * an error message may be built and returned. In case where the initiator is
366 * an applet (eg: peers), then we need to allocate the request buffer for the
367 * applet to be able to send its data (in this case a response is not needed).
368 * Request buffers are never picked from the reserved pool, but response
369 * buffers may be allocated from the reserve. This is critical to ensure that
370 * a response may always flow and will never block a server from releasing a
371 * connection. Returns 0 in case of failure, non-zero otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100372 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200373int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100374{
Willy Tarreaua24adf02014-11-27 01:11:56 +0100375 int margin;
376 struct buffer **buf;
377
378 if (objt_appctx(s->si[0].end)) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100379 buf = &s->req.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100380 margin = global.tune.reserved_bufs;
381 }
382 else {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100383 buf = &s->res.buf;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100384 margin = 0;
385 }
386
Willy Tarreaubf883e02014-11-25 21:10:35 +0100387 if (!LIST_ISEMPTY(&s->buffer_wait)) {
388 LIST_DEL(&s->buffer_wait);
389 LIST_INIT(&s->buffer_wait);
390 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100391
Willy Tarreaua24adf02014-11-27 01:11:56 +0100392 if (b_alloc_margin(buf, margin))
Willy Tarreau656859d2014-11-25 19:46:36 +0100393 return 1;
394
Willy Tarreaubf883e02014-11-25 21:10:35 +0100395 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100396 return 0;
397}
398
399/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100400 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200401 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100402 * a single buffer, so it makes sense to try to wake two up when two buffers
403 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100404 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200405void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100406{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100407 if (s->req.buf->size && buffer_empty(s->req.buf))
408 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100409
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100410 if (s->res.buf->size && buffer_empty(s->res.buf))
411 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100412
Willy Tarreaubf883e02014-11-25 21:10:35 +0100413 /* if we're certain to have at least 1 buffer available, and there is
414 * someone waiting, we can wake up a waiter and offer them.
415 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100416 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200417 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100418}
419
Willy Tarreau87b09662015-04-03 00:22:06 +0200420/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100421 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200422 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200424void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100425{
Willy Tarreau87b09662015-04-03 00:22:06 +0200426 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100427
428 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100429 if (rqlimit <= run_queue)
430 break;
431
Willy Tarreaubf883e02014-11-25 21:10:35 +0100432 if (sess->task->state & TASK_RUNNING)
433 continue;
434
435 LIST_DEL(&sess->buffer_wait);
436 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100437 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100438 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100439}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200440
441/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200442int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200443{
Willy Tarreau87b09662015-04-03 00:22:06 +0200444 LIST_INIT(&streams);
445 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
446 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200447}
448
Willy Tarreau87b09662015-04-03 00:22:06 +0200449void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100450{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200451 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100452 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100453 void *ptr;
454 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100455
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100456 bytes = s->req.total - s->logs.bytes_in;
457 s->logs.bytes_in = s->req.total;
458 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200459 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100460
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100461 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100462
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100463 if (objt_server(s->target))
464 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200465
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200466 if (sess->listener && sess->listener->counters)
467 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200468
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100469 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200470 struct stkctr *stkctr = &s->stkctr[i];
471
472 if (!stkctr_entry(stkctr)) {
473 stkctr = &sess->stkctr[i];
474 if (!stkctr_entry(stkctr))
475 continue;
476 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200477
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200478 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100479 if (ptr)
480 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200481
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200482 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100483 if (ptr)
484 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200485 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100486 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100487 }
488
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100489 bytes = s->res.total - s->logs.bytes_out;
490 s->logs.bytes_out = s->res.total;
491 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200492 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100493
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100494 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100495
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100496 if (objt_server(s->target))
497 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200498
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200499 if (sess->listener && sess->listener->counters)
500 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200501
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100502 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200503 struct stkctr *stkctr = &s->stkctr[i];
504
505 if (!stkctr_entry(stkctr)) {
506 stkctr = &sess->stkctr[i];
507 if (!stkctr_entry(stkctr))
508 continue;
509 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200510
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200511 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100512 if (ptr)
513 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200514
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200515 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100516 if (ptr)
517 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200518 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau30e71012007-11-26 20:15:35 +0100519 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100520 }
521}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100523/* This function is called with (si->state == SI_ST_CON) meaning that a
524 * connection was attempted and that the file descriptor is already allocated.
525 * We must check for establishment, error and abort. Possible output states
526 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
527 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200528 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100529 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200530static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100531{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100532 struct stream_interface *si = &s->si[1];
533 struct channel *req = &s->req;
534 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200535 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100537 /* If we got an error, or if nothing happened and the connection timed
538 * out, we must give up. The CER state handler will take care of retry
539 * attempts and error reports.
540 */
541 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100542 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100543 /* Some data were sent past the connection establishment,
544 * so we need to pretend we're established to log correctly
545 * and let later states handle the failure.
546 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100547 si->state = SI_ST_EST;
548 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100549 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100550 return 1;
551 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100552 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100553 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100554
Willy Tarreauf79c8172013-10-21 16:30:56 +0200555 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100556
557 if (si->err_type)
558 return 0;
559
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560 if (si->flags & SI_FL_ERR)
561 si->err_type = SI_ET_CONN_ERR;
562 else
563 si->err_type = SI_ET_CONN_TO;
564 return 0;
565 }
566
567 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100568 if (!(req->flags & CF_WRITE_PARTIAL) &&
569 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200570 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
571 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100572 s->be->options & PR_O_ABRT_CLOSE)))) {
573 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200574 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100576 if (s->srv_error)
577 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100578 return 1;
579 }
580
581 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200582 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 return 1;
584
585 /* OK, this means that a connection succeeded. The caller will be
586 * responsible for handling the transition from CON to EST.
587 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100588 si->state = SI_ST_EST;
589 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590 return 1;
591}
592
593/* This function is called with (si->state == SI_ST_CER) meaning that a
594 * previous connection attempt has failed and that the file descriptor
595 * has already been released. Possible causes include asynchronous error
596 * notification and time out. Possible output states are SI_ST_CLO when
597 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
598 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
599 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
600 * marked as in error state. It returns 0.
601 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200602static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100604 struct stream_interface *si = &s->si[1];
605
Willy Tarreau87b09662015-04-03 00:22:06 +0200606 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100607 if (objt_server(s->target)) {
608 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100609
Willy Tarreaue7dff022015-04-03 01:14:29 +0200610 if (s->flags & SF_CURR_SESS) {
611 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100612 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100614 }
615
616 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200617 si->conn_retries--;
618 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619 if (!si->err_type) {
620 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621 }
622
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100623 if (objt_server(s->target))
624 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100625 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100626 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100627 if (may_dequeue_tasks(objt_server(s->target), s->be))
628 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100629
630 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200631 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100632 s->req.flags |= CF_WRITE_ERROR;
633 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634
635 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100636 if (s->srv_error)
637 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 return 0;
639 }
640
641 /* If the "redispatch" option is set on the backend, we are allowed to
642 * retry on another server for the last retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200643 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 * bit to ignore any persistence cookie. We won't count a retry nor a
645 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200646 * If the connection is not persistent, the balancing algorithm is not
647 * determinist (round robin) and there is more than one active server,
648 * we accept to perform an immediate redispatch without waiting since
649 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100650 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200651 if (objt_server(s->target) &&
652 (si->conn_retries == 0 ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200653 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Willy Tarreau33a14e52014-06-13 17:49:40 +0200654 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreaue7dff022015-04-03 01:14:29 +0200655 s->be->options & PR_O_REDISP && !(s->flags & SF_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100656 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100657 if (may_dequeue_tasks(objt_server(s->target), s->be))
658 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100659
Willy Tarreaue7dff022015-04-03 01:14:29 +0200660 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661 si->state = SI_ST_REQ;
662 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100663 if (objt_server(s->target))
664 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100665 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666 si->state = SI_ST_ASS;
667 }
668
669 if (si->flags & SI_FL_ERR) {
670 /* The error was an asynchronous connection error, and we will
671 * likely have to retry connecting to the same server, most
672 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200673 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674 */
675
Willy Tarreaub0290662014-06-13 17:04:44 +0200676 int delay = 1000;
677
678 if (s->be->timeout.connect && s->be->timeout.connect < delay)
679 delay = s->be->timeout.connect;
680
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 if (!si->err_type)
682 si->err_type = SI_ET_CONN_ERR;
683
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200684 /* only wait when we're retrying on the same server */
685 if (si->state == SI_ST_ASS ||
686 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
687 (s->be->srv_act <= 1)) {
688 si->state = SI_ST_TAR;
689 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
690 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100691 return 0;
692 }
693 return 0;
694}
695
696/*
697 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200698 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200699 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100700 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200701static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100702{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100703 struct stream_interface *si = &s->si[1];
704 struct channel *req = &s->req;
705 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100706
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100707 /* First, centralize the timers information */
708 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
709 si->exp = TICK_ETERNITY;
710
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100711 if (objt_server(s->target))
712 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100713
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100714 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100715 /* if the user wants to log as soon as possible, without counting
716 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200717 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100718 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100719 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100720 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100721 }
722 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100723 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724 }
725
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200726 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200727 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200728 if (req->flags & CF_WAKE_CONNECT) {
729 req->flags |= CF_WAKE_ONCE;
730 req->flags &= ~CF_WAKE_CONNECT;
731 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200732 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200733 /* real connections have timeouts */
734 req->wto = s->be->timeout.server;
735 rep->rto = s->be->timeout.server;
736 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 req->wex = TICK_ETERNITY;
738}
739
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100740/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
741 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100742 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
743 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
744 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200746static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100747{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100748 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100749 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100750 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100751
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100752 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 +0100753 now_ms, __FUNCTION__,
754 s,
Willy Tarreau103197d2014-11-28 15:26:12 +0100755 req, s->rep,
756 req->rex, s->res.wex,
757 req->flags, s->res.flags,
758 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 +0100759
760 if (si->state == SI_ST_ASS) {
761 /* Server assigned to connection request, we have to try to connect now */
762 int conn_err;
763
764 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100765 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100766
Willy Tarreaue7dff022015-04-03 01:14:29 +0200767 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100768 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100769 if (srv)
770 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500771 if (srv)
772 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100773 return;
774 }
775
776 /* We have received a synchronous error. We might have to
777 * abort, retry immediately or redispatch.
778 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200779 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100780 if (!si->err_type) {
781 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100782 }
783
Willy Tarreau827aee92011-03-10 16:55:02 +0100784 if (srv)
785 srv_inc_sess_ctr(srv);
786 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500787 srv_set_sess_last(srv);
788 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100789 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100790 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100791
Willy Tarreau87b09662015-04-03 00:22:06 +0200792 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100793 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100794 if (may_dequeue_tasks(srv, s->be))
795 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100796
797 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200798 si_shutr(si);
799 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100800 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100801
802 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
803
Willy Tarreau87b09662015-04-03 00:22:06 +0200804 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100805 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100806 if (s->srv_error)
807 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100808 return;
809 }
810
811 /* We are facing a retryable error, but we don't want to run a
812 * turn-around now, as the problem is likely a source port
813 * allocation problem, so we want to retry now.
814 */
815 si->state = SI_ST_CER;
816 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100817 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
819 return;
820 }
821 else if (si->state == SI_ST_QUE) {
822 /* connection request was queued, check for any update */
823 if (!s->pend_pos) {
824 /* The connection is not in the queue anymore. Either
825 * we have a server connection slot available and we
826 * go directly to the assigned state, or we need to
827 * load-balance first and go to the INI state.
828 */
829 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200830 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100831 si->state = SI_ST_REQ;
832 else {
833 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
834 si->state = SI_ST_ASS;
835 }
836 return;
837 }
838
839 /* Connection request still in queue... */
840 if (si->flags & SI_FL_EXP) {
841 /* ... and timeout expired */
842 si->exp = TICK_ETERNITY;
843 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100844 if (srv)
845 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100846 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200847 si_shutr(si);
848 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100849 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100850 if (!si->err_type)
851 si->err_type = SI_ET_QUEUE_TO;
852 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100853 if (s->srv_error)
854 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100855 return;
856 }
857
858 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100859 if ((req->flags & (CF_READ_ERROR)) ||
860 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
861 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100862 /* give up */
863 si->exp = TICK_ETERNITY;
864 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200865 si_shutr(si);
866 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100867 si->err_type |= SI_ET_QUEUE_ABRT;
868 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100869 if (s->srv_error)
870 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100871 return;
872 }
873
874 /* Nothing changed */
875 return;
876 }
877 else if (si->state == SI_ST_TAR) {
878 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100879 if ((req->flags & (CF_READ_ERROR)) ||
880 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
881 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100882 /* give up */
883 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200884 si_shutr(si);
885 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100886 si->err_type |= SI_ET_CONN_ABRT;
887 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100888 if (s->srv_error)
889 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100890 return;
891 }
892
893 if (!(si->flags & SI_FL_EXP))
894 return; /* still in turn-around */
895
896 si->exp = TICK_ETERNITY;
897
Willy Tarreau87b09662015-04-03 00:22:06 +0200898 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899 * marked "assigned".
900 * FIXME: Should we force a redispatch attempt when the server is down ?
901 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200902 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100903 si->state = SI_ST_ASS;
904 else
905 si->state = SI_ST_REQ;
906 return;
907 }
908}
909
Willy Tarreau87b09662015-04-03 00:22:06 +0200910/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900911 * also counts a failed request if the server state has not reached the request
912 * stage.
913 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200914static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900915{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200916 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900917 if (s->si[1].state < SI_ST_REQ) {
918
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200919 strm_fe(s)->fe_counters.failed_req++;
920 if (strm_li(s)->counters)
921 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900922
Willy Tarreaue7dff022015-04-03 01:14:29 +0200923 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900924 }
925 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200926 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900927 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200928 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900929 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200930 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900931 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200932 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900933 }
934}
935
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100936/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100937 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
938 * a real connection to a server, indicating that a server has been assigned,
939 * or SI_ST_EST for a successful connection to an applet. It may also return
940 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100941 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200942static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100943{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100944 struct stream_interface *si = &s->si[1];
945
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100946 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 +0100947 now_ms, __FUNCTION__,
948 s,
949 s->req, s->rep,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100950 s->req.rex, s->res.wex,
951 s->req.flags, s->res.flags,
Willy Tarreau350f4872014-11-28 14:42:25 +0100952 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 +0100953
954 if (si->state != SI_ST_REQ)
955 return;
956
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100957 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
958 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100959 struct appctx *appctx = objt_appctx(si->end);
960
961 if (!appctx || appctx->applet != __objt_applet(s->target))
962 appctx = stream_int_register_handler(si, objt_applet(s->target));
963
964 if (!appctx) {
965 /* No more memory, let's immediately abort. Force the
966 * error code to ignore the ERR_LOCAL which is not a
967 * real error.
968 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200969 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100970
971 si_shutr(si);
972 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100973 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100974 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100975 si->state = SI_ST_CLO;
976 if (s->srv_error)
977 s->srv_error(s, si);
978 return;
979 }
980
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100981 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100982 si->state = SI_ST_EST;
983 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +0200984 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +0100985 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100986 return;
987 }
988
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100989 /* Try to assign a server */
990 if (srv_redispatch_connect(s) != 0) {
991 /* We did not get a server. Either we queued the
992 * connection request, or we encountered an error.
993 */
994 if (si->state == SI_ST_QUE)
995 return;
996
997 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200998 si_shutr(si);
999 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001000 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001001 if (!si->err_type)
1002 si->err_type = SI_ET_CONN_OTHER;
1003 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001004 if (s->srv_error)
1005 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001006 return;
1007 }
1008
1009 /* The server is assigned */
1010 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1011 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001012 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001013}
1014
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001015/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001016 * if appropriate. The default_backend rule is also considered, then the
1017 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001018 * It returns 1 if the processing can continue on next analysers, or zero if it
1019 * either needs more data or wants to immediately abort the request.
1020 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001021static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001022{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001023 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001024 struct session *sess = s->sess;
1025 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001026
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001027 req->analysers &= ~an_bit;
1028 req->analyse_exp = TICK_ETERNITY;
1029
Willy Tarreau87b09662015-04-03 00:22:06 +02001030 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 +02001031 now_ms, __FUNCTION__,
1032 s,
1033 req,
1034 req->rex, req->wex,
1035 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001036 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001037 req->analysers);
1038
1039 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001040 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001041 struct switching_rule *rule;
1042
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001043 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001044 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001045
Willy Tarreauf51658d2014-04-23 01:21:56 +02001046 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001047 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001048 ret = acl_pass(ret);
1049 if (rule->cond->pol == ACL_COND_UNLESS)
1050 ret = !ret;
1051 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001052
1053 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001054 /* If the backend name is dynamic, try to resolve the name.
1055 * If we can't resolve the name, or if any error occurs, break
1056 * the loop and fallback to the default backend.
1057 */
1058 struct proxy *backend;
1059
1060 if (rule->dynamic) {
1061 struct chunk *tmp = get_trash_chunk();
1062 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1063 break;
1064 backend = findproxy(tmp->str, PR_CAP_BE);
1065 if (!backend)
1066 break;
1067 }
1068 else
1069 backend = rule->be.backend;
1070
Willy Tarreau87b09662015-04-03 00:22:06 +02001071 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001072 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001073 break;
1074 }
1075 }
1076
1077 /* To ensure correct connection accounting on the backend, we
1078 * have to assign one if it was not set (eg: a listen). This
1079 * measure also takes care of correctly setting the default
1080 * backend if any.
1081 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001082 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001083 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001084 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001085 }
1086
Willy Tarreaufb356202010-08-03 14:02:05 +02001087 /* 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 +02001088 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001089 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1090 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001091 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001092
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001093 /* 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 +02001094 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001095 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001096 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001097 int ret = 1;
1098
1099 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001100 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 +01001101 ret = acl_pass(ret);
1102 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1103 ret = !ret;
1104 }
1105
1106 if (ret) {
1107 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001108 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001109 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001110 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001111 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001112 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001113 break;
1114 }
1115 }
1116
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001117 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001118
1119 sw_failed:
1120 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001121 channel_abort(&s->req);
1122 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001123
Willy Tarreaue7dff022015-04-03 01:14:29 +02001124 if (!(s->flags & SF_ERR_MASK))
1125 s->flags |= SF_ERR_RESOURCE;
1126 if (!(s->flags & SF_FINST_MASK))
1127 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001128
Willy Tarreaueee5b512015-04-03 23:46:31 +02001129 if (s->txn)
1130 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001131 s->req.analysers = 0;
1132 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001133 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001134}
1135
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001136/* This stream analyser works on a request. It applies all use-server rules on
1137 * it then returns 1. The data must already be present in the buffer otherwise
1138 * they won't match. It always returns 1.
1139 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001140static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001141{
1142 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001143 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001144 struct server_rule *rule;
1145
Willy Tarreau87b09662015-04-03 00:22:06 +02001146 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 +02001147 now_ms, __FUNCTION__,
1148 s,
1149 req,
1150 req->rex, req->wex,
1151 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001152 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001153 req->analysers);
1154
Willy Tarreaue7dff022015-04-03 01:14:29 +02001155 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001156 list_for_each_entry(rule, &px->server_rules, list) {
1157 int ret;
1158
Willy Tarreau192252e2015-04-04 01:47:55 +02001159 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001160 ret = acl_pass(ret);
1161 if (rule->cond->pol == ACL_COND_UNLESS)
1162 ret = !ret;
1163
1164 if (ret) {
1165 struct server *srv = rule->srv.ptr;
1166
Willy Tarreau892337c2014-05-13 23:41:20 +02001167 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001168 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001169 (s->flags & SF_FORCE_PRST)) {
1170 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001171 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001172 break;
1173 }
1174 /* if the server is not UP, let's go on with next rules
1175 * just in case another one is suited.
1176 */
1177 }
1178 }
1179 }
1180
1181 req->analysers &= ~an_bit;
1182 req->analyse_exp = TICK_ETERNITY;
1183 return 1;
1184}
1185
Emeric Brun1d33b292010-01-04 15:47:17 +01001186/* This stream analyser works on a request. It applies all sticking rules on
1187 * it then returns 1. The data must already be present in the buffer otherwise
1188 * they won't match. It always returns 1.
1189 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001190static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001191{
1192 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001193 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001194 struct sticking_rule *rule;
1195
Willy Tarreau87b09662015-04-03 00:22:06 +02001196 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 +01001197 now_ms, __FUNCTION__,
1198 s,
1199 req,
1200 req->rex, req->wex,
1201 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001202 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001203 req->analysers);
1204
1205 list_for_each_entry(rule, &px->sticking_rules, list) {
1206 int ret = 1 ;
1207 int i;
1208
Willy Tarreau9667a802013-12-09 12:52:13 +01001209 /* Only the first stick store-request of each table is applied
1210 * and other ones are ignored. The purpose is to allow complex
1211 * configurations which look for multiple entries by decreasing
1212 * order of precision and to stop at the first which matches.
1213 * An example could be a store of the IP address from an HTTP
1214 * header first, then from the source if not found.
1215 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001216 for (i = 0; i < s->store_count; i++) {
1217 if (rule->table.t == s->store[i].table)
1218 break;
1219 }
1220
1221 if (i != s->store_count)
1222 continue;
1223
1224 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001225 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001226 ret = acl_pass(ret);
1227 if (rule->cond->pol == ACL_COND_UNLESS)
1228 ret = !ret;
1229 }
1230
1231 if (ret) {
1232 struct stktable_key *key;
1233
Willy Tarreau192252e2015-04-04 01:47:55 +02001234 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 +01001235 if (!key)
1236 continue;
1237
1238 if (rule->flags & STK_IS_MATCH) {
1239 struct stksess *ts;
1240
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001241 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001242 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001243 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001244 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001245
1246 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001247 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1248 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001249 if (node) {
1250 struct server *srv;
1251
1252 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001253 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001254 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001255 (s->flags & SF_FORCE_PRST)) {
1256 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001257 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001258 }
1259 }
1260 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001261 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001262 }
1263 }
1264 if (rule->flags & STK_IS_STORE) {
1265 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1266 struct stksess *ts;
1267
1268 ts = stksess_new(rule->table.t, key);
1269 if (ts) {
1270 s->store[s->store_count].table = rule->table.t;
1271 s->store[s->store_count++].ts = ts;
1272 }
1273 }
1274 }
1275 }
1276 }
1277
1278 req->analysers &= ~an_bit;
1279 req->analyse_exp = TICK_ETERNITY;
1280 return 1;
1281}
1282
1283/* This stream analyser works on a response. It applies all store rules on it
1284 * then returns 1. The data must already be present in the buffer otherwise
1285 * they won't match. It always returns 1.
1286 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001287static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001288{
1289 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001290 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001291 struct sticking_rule *rule;
1292 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001293 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001294
Willy Tarreau87b09662015-04-03 00:22:06 +02001295 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 +01001296 now_ms, __FUNCTION__,
1297 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001298 rep,
1299 rep->rex, rep->wex,
1300 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001301 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001302 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001303
1304 list_for_each_entry(rule, &px->storersp_rules, list) {
1305 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001306
Willy Tarreau9667a802013-12-09 12:52:13 +01001307 /* Only the first stick store-response of each table is applied
1308 * and other ones are ignored. The purpose is to allow complex
1309 * configurations which look for multiple entries by decreasing
1310 * order of precision and to stop at the first which matches.
1311 * An example could be a store of a set-cookie value, with a
1312 * fallback to a parameter found in a 302 redirect.
1313 *
1314 * The store-response rules are not allowed to override the
1315 * store-request rules for the same table, but they may coexist.
1316 * Thus we can have up to one store-request entry and one store-
1317 * response entry for the same table at any time.
1318 */
1319 for (i = nbreq; i < s->store_count; i++) {
1320 if (rule->table.t == s->store[i].table)
1321 break;
1322 }
1323
1324 /* skip existing entries for this table */
1325 if (i < s->store_count)
1326 continue;
1327
Emeric Brun1d33b292010-01-04 15:47:17 +01001328 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001329 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001330 ret = acl_pass(ret);
1331 if (rule->cond->pol == ACL_COND_UNLESS)
1332 ret = !ret;
1333 }
1334
1335 if (ret) {
1336 struct stktable_key *key;
1337
Willy Tarreau192252e2015-04-04 01:47:55 +02001338 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 +01001339 if (!key)
1340 continue;
1341
Willy Tarreau37e340c2013-12-06 23:05:21 +01001342 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001343 struct stksess *ts;
1344
1345 ts = stksess_new(rule->table.t, key);
1346 if (ts) {
1347 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001348 s->store[s->store_count++].ts = ts;
1349 }
1350 }
1351 }
1352 }
1353
1354 /* process store request and store response */
1355 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001356 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001357 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001358
Willy Tarreauc93cd162014-05-13 15:54:22 +02001359 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001360 stksess_free(s->store[i].table, s->store[i].ts);
1361 s->store[i].ts = NULL;
1362 continue;
1363 }
1364
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001365 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1366 if (ts) {
1367 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001368 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001369 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001370 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001371 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001372 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001373
1374 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001375 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001376 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001377 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001378 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001379
1380 rep->analysers &= ~an_bit;
1381 rep->analyse_exp = TICK_ETERNITY;
1382 return 1;
1383}
1384
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001385/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001386 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001387 */
1388#define UPDATE_ANALYSERS(real, list, back, flag) { \
1389 list = (((list) & ~(flag)) | ~(back)) & (real); \
1390 back = real; \
1391 if (!(list)) \
1392 break; \
1393 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1394 continue; \
1395}
1396
Willy Tarreau87b09662015-04-03 00:22:06 +02001397/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001398 * then puts it back to the wait queue in a clean state, or cleans up its
1399 * resources if it must be deleted. Returns in <next> the date the task wants
1400 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1401 * nothing too many times, the request and response buffers flags are monitored
1402 * and each function is called only if at least another function has changed at
1403 * least one flag it is interested in.
1404 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001405struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001406{
Willy Tarreau827aee92011-03-10 16:55:02 +01001407 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001408 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001409 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001410 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001411 unsigned int rq_prod_last, rq_cons_last;
1412 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001413 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001414 struct channel *req, *res;
1415 struct stream_interface *si_f, *si_b;
1416
1417 req = &s->req;
1418 res = &s->res;
1419
1420 si_f = &s->si[0];
1421 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001422
1423 //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 +01001424 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001425
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001426 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001427 if (s->txn)
1428 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001429
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001430 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001431 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1432 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001433
1434 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001435 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1436 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001437
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001438 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001439 si_f->flags |= SI_FL_DONT_WAKE;
1440 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001441
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001442 /* 1a: Check for low level timeouts if needed. We just set a flag on
1443 * stream interfaces when their timeouts have expired.
1444 */
1445 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001446 stream_int_check_timeouts(si_f);
1447 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001448
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001449 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001450 * for future reads or writes. Note: this will also concern upper layers
1451 * but we do not touch any other flag. We must be careful and correctly
1452 * detect state changes when calling them.
1453 */
1454
Willy Tarreau8f128b42014-11-28 15:07:47 +01001455 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001456
Willy Tarreau8f128b42014-11-28 15:07:47 +01001457 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1458 si_b->flags |= SI_FL_NOLINGER;
1459 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001460 }
1461
Willy Tarreau8f128b42014-11-28 15:07:47 +01001462 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1463 if (si_f->flags & SI_FL_NOHALF)
1464 si_f->flags |= SI_FL_NOLINGER;
1465 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001466 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001467
Willy Tarreau8f128b42014-11-28 15:07:47 +01001468 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001469
Willy Tarreau8f128b42014-11-28 15:07:47 +01001470 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1471 si_f->flags |= SI_FL_NOLINGER;
1472 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001473 }
1474
Willy Tarreau8f128b42014-11-28 15:07:47 +01001475 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1476 if (si_b->flags & SI_FL_NOHALF)
1477 si_b->flags |= SI_FL_NOLINGER;
1478 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001479 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001480
1481 /* Once in a while we're woken up because the task expires. But
1482 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001483 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001484 * timeout needs to be refreshed.
1485 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001486 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001487 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1488 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001489 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001490 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1491 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001492 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001493
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001494 /* below we may emit error messages so we have to ensure that we have
1495 * our buffers properly allocated.
1496 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001497 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001498 /* No buffer available, we've been subscribed to the list of
1499 * buffer waiters, let's wait for our turn.
1500 */
1501 goto update_exp_and_leave;
1502 }
1503
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001504 /* 1b: check for low-level errors reported at the stream interface.
1505 * First we check if it's a retryable error (in which case we don't
1506 * want to tell the buffer). Otherwise we report the error one level
1507 * upper by setting flags into the buffers. Note that the side towards
1508 * the client cannot have connect (hence retryable) errors. Also, the
1509 * connection setup code must be able to deal with any type of abort.
1510 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001511 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001512 if (unlikely(si_f->flags & SI_FL_ERR)) {
1513 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1514 si_shutr(si_f);
1515 si_shutw(si_f);
1516 stream_int_report_error(si_f);
1517 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001518 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001519 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001520 if (srv)
1521 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001522 if (!(s->flags & SF_ERR_MASK))
1523 s->flags |= SF_ERR_CLICL;
1524 if (!(s->flags & SF_FINST_MASK))
1525 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001526 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001527 }
1528 }
1529
Willy Tarreau8f128b42014-11-28 15:07:47 +01001530 if (unlikely(si_b->flags & SI_FL_ERR)) {
1531 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1532 si_shutr(si_b);
1533 si_shutw(si_b);
1534 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001535 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001536 if (srv)
1537 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001538 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001539 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001540 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001541 if (srv)
1542 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001543 if (!(s->flags & SF_ERR_MASK))
1544 s->flags |= SF_ERR_SRVCL;
1545 if (!(s->flags & SF_FINST_MASK))
1546 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001547 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001548 }
1549 /* note: maybe we should process connection errors here ? */
1550 }
1551
Willy Tarreau8f128b42014-11-28 15:07:47 +01001552 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001553 /* we were trying to establish a connection on the server side,
1554 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1555 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001556 if (unlikely(!sess_update_st_con_tcp(s)))
1557 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001558 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001559 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001560
1561 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1562 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1563 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1564 */
1565 }
1566
Willy Tarreau8f128b42014-11-28 15:07:47 +01001567 rq_prod_last = si_f->state;
1568 rq_cons_last = si_b->state;
1569 rp_cons_last = si_f->state;
1570 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001571
1572 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001573 /* Check for connection closure */
1574
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001575 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001576 "[%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 +01001577 now_ms, __FUNCTION__, __LINE__,
1578 t,
1579 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001580 req, res,
1581 req->rex, res->wex,
1582 req->flags, res->flags,
1583 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1584 si_f->err_type, si_b->err_type,
1585 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586
1587 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001588 if (unlikely(si_f->state == SI_ST_DIS))
1589 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001590
1591 /* When a server-side connection is released, we have to count it and
1592 * check for pending connections on this server.
1593 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001594 if (unlikely(si_b->state == SI_ST_DIS)) {
1595 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001596 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001597 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001598 if (s->flags & SF_CURR_SESS) {
1599 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001600 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001601 }
1602 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001603 if (may_dequeue_tasks(srv, s->be))
1604 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001605 }
1606 }
1607
1608 /*
1609 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1610 * at this point.
1611 */
1612
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001613 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001614 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001615 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1616 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1617 si_f->state != rq_prod_last ||
1618 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001619 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001620 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001621
Willy Tarreau8f128b42014-11-28 15:07:47 +01001622 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001623 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001624 unsigned int ana_list;
1625 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001626
Willy Tarreau90deb182010-01-07 00:20:41 +01001627 /* it's up to the analysers to stop new connections,
1628 * disable reading or closing. Note: if an analyser
1629 * disables any of these bits, it is responsible for
1630 * enabling them again when it disables itself, so
1631 * that other analysers are called in similar conditions.
1632 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001633 channel_auto_read(req);
1634 channel_auto_connect(req);
1635 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001636
1637 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001638 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001639 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001640 * the list when not needed. Any analyser may return 0
1641 * to break out of the loop, either because of missing
1642 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001643 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001644 * analyser, and we may loop again if other analysers
1645 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001646 *
1647 * We build a list of analysers to run. We evaluate all
1648 * of these analysers in the order of the lower bit to
1649 * the higher bit. This ordering is very important.
1650 * An analyser will often add/remove other analysers,
1651 * including itself. Any changes to itself have no effect
1652 * on the loop. If it removes any other analysers, we
1653 * want those analysers not to be called anymore during
1654 * this loop. If it adds an analyser that is located
1655 * after itself, we want it to be scheduled for being
1656 * processed during the loop. If it adds an analyser
1657 * which is located before it, we want it to switch to
1658 * it immediately, even if it has already been called
1659 * once but removed since.
1660 *
1661 * In order to achieve this, we compare the analyser
1662 * list after the call with a copy of it before the
1663 * call. The work list is fed with analyser bits that
1664 * appeared during the call. Then we compare previous
1665 * work list with the new one, and check the bits that
1666 * appeared. If the lowest of these bits is lower than
1667 * the current bit, it means we have enabled a previous
1668 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001669 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001670
Willy Tarreau8f128b42014-11-28 15:07:47 +01001671 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001672 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001673 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001674
Willy Tarreaufb356202010-08-03 14:02:05 +02001675 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001676 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001677 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001678 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001679 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001680
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001681 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001682 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001683 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001684 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001685 }
1686
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001687 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001688 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001689 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001690 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001691 }
1692
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001693 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001694 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001695 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001696 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001697 }
1698
Willy Tarreaufb356202010-08-03 14:02:05 +02001699 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001700 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001701 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001702 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001703 }
1704
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001705 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001706 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001707 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001708 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001709 }
1710
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001711 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001712 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001713 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001714 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001715 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001716
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001717 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001718 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001719 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001720 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001721 }
1722
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001723 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001724 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001725 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001726 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001727 }
1728
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001729 if (ana_list & AN_REQ_HTTP_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001730 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
Willy Tarreaud34af782008-11-30 23:36:37 +01001731 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001732 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001733 }
Emeric Brun647caf12009-06-30 17:57:00 +02001734
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001735 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001736 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001737 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001738 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001739 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001740
Emeric Brun1d33b292010-01-04 15:47:17 +01001741 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001742 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001743 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001744 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001745 }
1746
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001747 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001749 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001750 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001751 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001752 break;
1753 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001754 }
Willy Tarreau84455332009-03-15 22:34:05 +01001755
Willy Tarreau8f128b42014-11-28 15:07:47 +01001756 rq_prod_last = si_f->state;
1757 rq_cons_last = si_b->state;
1758 req->flags &= ~CF_WAKE_ONCE;
1759 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001760
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001762 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001763 }
1764
Willy Tarreau576507f2010-01-07 00:09:04 +01001765 /* we'll monitor the request analysers while parsing the response,
1766 * because some response analysers may indirectly enable new request
1767 * analysers (eg: HTTP keep-alive).
1768 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001769 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001770
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001771 resync_response:
1772 /* Analyse response */
1773
Willy Tarreau8f128b42014-11-28 15:07:47 +01001774 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1775 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1776 si_f->state != rp_cons_last ||
1777 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001778 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001779 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001780
Willy Tarreau8f128b42014-11-28 15:07:47 +01001781 if ((res->flags & CF_MASK_ANALYSER) &&
1782 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001783 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1784 * for some free space in the response buffer, so we might need to call
1785 * it when something changes in the response buffer, but still we pass
1786 * it the request buffer. Note that the SI state might very well still
1787 * be zero due to us returning a flow of redirects!
1788 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 res->analysers &= ~AN_REQ_ALL;
1790 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001791 }
1792
Willy Tarreau8f128b42014-11-28 15:07:47 +01001793 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001794 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001795 unsigned int ana_list;
1796 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001797
Willy Tarreau90deb182010-01-07 00:20:41 +01001798 /* it's up to the analysers to stop disable reading or
1799 * closing. Note: if an analyser disables any of these
1800 * bits, it is responsible for enabling them again when
1801 * it disables itself, so that other analysers are called
1802 * in similar conditions.
1803 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001804 channel_auto_read(res);
1805 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001806
1807 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001808 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001809 * to MSB. The analysers must remove themselves from
1810 * the list when not needed. Any analyser may return 0
1811 * to break out of the loop, either because of missing
1812 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001813 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001814 * analyser, and we may loop again if other analysers
1815 * are added in the middle.
1816 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001817
Willy Tarreau8f128b42014-11-28 15:07:47 +01001818 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001819 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001820 /* Warning! ensure that analysers are always placed in ascending order! */
1821
Emeric Brun97679e72010-09-23 17:56:44 +02001822 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001823 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001824 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001825 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001826 }
1827
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001828 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001829 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001830 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001831 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001832 }
1833
Emeric Brun1d33b292010-01-04 15:47:17 +01001834 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001835 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001836 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001837 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001838 }
1839
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001840 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001841 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001842 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001843 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001844 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001845
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001846 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001848 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001849 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001850 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001851 break;
1852 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001853 }
1854
Willy Tarreau8f128b42014-11-28 15:07:47 +01001855 rp_cons_last = si_f->state;
1856 rp_prod_last = si_b->state;
1857 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001858
Willy Tarreau8f128b42014-11-28 15:07:47 +01001859 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001860 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001861 }
1862
Willy Tarreau576507f2010-01-07 00:09:04 +01001863 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001864 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001865 goto resync_request;
1866
Willy Tarreau8f128b42014-11-28 15:07:47 +01001867 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001868 goto resync_request;
1869
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001870 /* FIXME: here we should call protocol handlers which rely on
1871 * both buffers.
1872 */
1873
1874
1875 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001876 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001877 * we're just in a data phase here since it means we have not
1878 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001879 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001880 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001881 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001882 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001883 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001884 req->analysers = 0;
1885 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001886 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001887 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001888 if (srv)
1889 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001890 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001891 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001892 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001893 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001894 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001895 if (srv)
1896 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001897 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001898 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001899 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001900 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001901 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001902 if (srv)
1903 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001904 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001905 }
1906 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001907 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001908 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001909 if (srv)
1910 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001911 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001912 }
Willy Tarreau84455332009-03-15 22:34:05 +01001913 sess_set_term_flags(s);
1914 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001915 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001916 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001917 res->analysers = 0;
1918 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001919 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001920 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001921 if (srv)
1922 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001923 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001924 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001925 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001926 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001927 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001928 if (srv)
1929 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001930 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001931 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001932 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001933 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001934 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001935 if (srv)
1936 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001937 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001938 }
1939 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001940 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001941 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001942 if (srv)
1943 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001944 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001945 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001946 sess_set_term_flags(s);
1947 }
Willy Tarreau84455332009-03-15 22:34:05 +01001948 }
1949
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001950 /*
1951 * Here we take care of forwarding unhandled data. This also includes
1952 * connection establishments and shutdown requests.
1953 */
1954
1955
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001956 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02001957 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001958 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001959 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001960 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001961 if (unlikely(!req->analysers &&
1962 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
1963 (si_f->state >= SI_ST_EST) &&
1964 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01001965 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001966 * attached to it. If any data are left in, we'll permit them to
1967 * move.
1968 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001969 channel_auto_read(req);
1970 channel_auto_connect(req);
1971 channel_auto_close(req);
1972 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01001973
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01001974 /* We'll let data flow between the producer (if still connected)
1975 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001976 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001977 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
1978 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01001979 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01001980
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001981 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001982 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
1983 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001984 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001985 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
1986 (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 +01001987 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001988 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
1989 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001990 (req->flags & CF_STREAMER_FAST)))) {
1991 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01001992 }
1993
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001994 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001995 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001996
1997 /*
1998 * Now forward all shutdown requests between both sides of the buffer
1999 */
2000
Willy Tarreau520d95e2009-09-19 21:04:57 +02002001 /* first, let's check if the request buffer needs to shutdown(write), which may
2002 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002003 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2004 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002005 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002006 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002007 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002008 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002009 if (tick_isset(sess->fe->timeout.clientfin)) {
2010 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002011 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002012 }
2013 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002014
2015 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002016 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2017 channel_is_empty(req))) {
2018 if (req->flags & CF_READ_ERROR)
2019 si_b->flags |= SI_FL_NOLINGER;
2020 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002021 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002022 res->rto = s->be->timeout.serverfin;
2023 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002024 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002025 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002026
2027 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002028 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2029 !req->analysers))
2030 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002031
2032 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002033 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2034 if (si_f->flags & SI_FL_NOHALF)
2035 si_f->flags |= SI_FL_NOLINGER;
2036 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002037 if (tick_isset(sess->fe->timeout.clientfin)) {
2038 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002039 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002040 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002041 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002042
Willy Tarreau520d95e2009-09-19 21:04:57 +02002043 /* it's possible that an upper layer has requested a connection setup or abort.
2044 * There are 2 situations where we decide to establish a new connection :
2045 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002046 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002047 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002048 if (si_b->state == SI_ST_INI) {
2049 if (!(req->flags & CF_SHUTW)) {
2050 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002051 /* If we have an appctx, there is no connect method, so we
2052 * immediately switch to the connected state, otherwise we
2053 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002054 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002055 si_b->state = SI_ST_REQ; /* new connection requested */
2056 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002057 }
Willy Tarreau73201222009-08-16 18:27:24 +02002058 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002059 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002060 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2061 channel_shutw_now(req); /* fix buffer flags upon abort */
2062 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002063 }
Willy Tarreau92795622009-03-06 12:51:23 +01002064 }
2065
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002066
2067 /* we may have a pending connection request, or a connection waiting
2068 * for completion.
2069 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002070 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002071 do {
2072 /* nb: step 1 might switch from QUE to ASS, but we first want
2073 * to give a chance to step 2 to perform a redirect if needed.
2074 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002075 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002076 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002077 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002078 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002079
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002080 /* applets directly go to the ESTABLISHED state. Similarly,
2081 * servers experience the same fate when their connection
2082 * is reused.
2083 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002084 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002085 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002086
Willy Tarreaub44c8732013-12-31 23:16:50 +01002087 /* Now we can add the server name to a header (if requested) */
2088 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002089 if ((si_b->state >= SI_ST_CON) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002090 (s->be->server_id_hdr_name != NULL) &&
2091 (s->be->mode == PR_MODE_HTTP) &&
2092 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002093 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002094 }
2095
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002096 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002097 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002098 http_perform_server_redirect(s, si_b);
2099 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002100 }
2101
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002102 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002104 goto resync_stream_interface;
2105
Willy Tarreau815a9b22010-07-27 17:15:12 +02002106 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002107 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002108 goto resync_request;
2109
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002110 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002111
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002112 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002113 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002114 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002115 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002116 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002117 if (unlikely(!res->analysers &&
2118 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2119 (si_b->state >= SI_ST_EST) &&
2120 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002121 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002122 * attached to it. If any data are left in, we'll permit them to
2123 * move.
2124 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002125 channel_auto_read(res);
2126 channel_auto_close(res);
2127 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002128
2129 /* We'll let data flow between the producer (if still connected)
2130 * to the consumer.
2131 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002132 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2133 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002134
2135 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002136 * tunnel timeout set, use it now. Note that we must respect
2137 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002138 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002139 if (!req->analysers && s->be->timeout.tunnel) {
2140 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002141 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002142
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002143 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2144 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002145 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2146 res->rto = s->be->timeout.serverfin;
2147 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2148 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002149 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2150 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002151
Willy Tarreau8f128b42014-11-28 15:07:47 +01002152 req->rex = tick_add(now_ms, req->rto);
2153 req->wex = tick_add(now_ms, req->wto);
2154 res->rex = tick_add(now_ms, res->rto);
2155 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002156 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002157 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002158
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002159 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002160 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2161 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002162 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002163 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2164 (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 +01002165 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002166 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2167 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002168 (res->flags & CF_STREAMER_FAST)))) {
2169 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002170 }
2171
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002172 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002173 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002174
2175 /*
2176 * Now forward all shutdown requests between both sides of the buffer
2177 */
2178
2179 /*
2180 * FIXME: this is probably where we should produce error responses.
2181 */
2182
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002183 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002184 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002185 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002186 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002187 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002188 req->wto = s->be->timeout.serverfin;
2189 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002190 }
2191 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002192
2193 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002194 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2195 channel_is_empty(res))) {
2196 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002197 if (tick_isset(sess->fe->timeout.clientfin)) {
2198 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002199 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002200 }
2201 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002202
2203 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002204 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2205 !res->analysers)
2206 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002207
2208 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002209 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2210 if (si_b->flags & SI_FL_NOHALF)
2211 si_b->flags |= SI_FL_NOLINGER;
2212 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002213 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002214 req->wto = s->be->timeout.serverfin;
2215 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002216 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002217 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002218
Willy Tarreau8f128b42014-11-28 15:07:47 +01002219 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002220 goto resync_stream_interface;
2221
Willy Tarreau8f128b42014-11-28 15:07:47 +01002222 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002223 goto resync_request;
2224
Willy Tarreau8f128b42014-11-28 15:07:47 +01002225 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002226 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002227
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002228 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002229 si_f->flags &= ~SI_FL_DONT_WAKE;
2230 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002231
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002232 /* This is needed only when debugging is enabled, to indicate
2233 * client-side or server-side close. Please note that in the unlikely
2234 * event where both sides would close at once, the sequence is reported
2235 * on the server side first.
2236 */
2237 if (unlikely((global.mode & MODE_DEBUG) &&
2238 (!(global.mode & MODE_QUIET) ||
2239 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002240 if (si_b->state == SI_ST_CLO &&
2241 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002242 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002243 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002244 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2245 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002246 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002247 }
2248
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 if (si_f->state == SI_ST_CLO &&
2250 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002251 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002252 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002253 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2254 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002255 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002256 }
2257 }
2258
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 if (likely((si_f->state != SI_ST_CLO) ||
2260 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002261
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002262 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002263 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002264
Willy Tarreau8f128b42014-11-28 15:07:47 +01002265 if (si_f->state == SI_ST_EST && obj_type(si_f->end) != OBJ_TYPE_APPCTX)
2266 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002267
Willy Tarreau8f128b42014-11-28 15:07:47 +01002268 if (si_b->state == SI_ST_EST && obj_type(si_b->end) != OBJ_TYPE_APPCTX)
2269 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002270
Willy Tarreau8f128b42014-11-28 15:07:47 +01002271 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2272 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2273 si_f->prev_state = si_f->state;
2274 si_b->prev_state = si_b->state;
2275 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2276 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002277
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002278 /* Trick: if a request is being waiting for the server to respond,
2279 * and if we know the server can timeout, we don't want the timeout
2280 * to expire on the client side first, but we're still interested
2281 * in passing data from the client to the server (eg: POST). Thus,
2282 * we can cancel the client's request timeout if the server's
2283 * request timeout is set and the server has not yet sent a response.
2284 */
2285
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2287 (tick_isset(req->wex) || tick_isset(res->rex))) {
2288 req->flags |= CF_READ_NOEXP;
2289 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002290 }
2291
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002292 /* When any of the stream interfaces is attached to an applet,
2293 * we have to call it here. Note that this one may wake the
2294 * task up again. If at least one applet was called, the current
2295 * task might have been woken up, in which case we don't want it
2296 * to be requeued to the wait queue but rather to the run queue
2297 * to run ASAP. The bitwise "or" in the condition ensures that
2298 * both functions are always called and that we wake up if at
2299 * least one did something.
Willy Tarreau1accfc02009-09-05 20:57:35 +02002300 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002301 if ((si_applet_call(si_b) | si_applet_call(si_f)) != 0) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002302 if (task_in_rq(t)) {
Willy Tarreau1accfc02009-09-05 20:57:35 +02002303 t->expire = TICK_ETERNITY;
Willy Tarreau87b09662015-04-03 00:22:06 +02002304 stream_release_buffers(s);
Willy Tarreau1accfc02009-09-05 20:57:35 +02002305 return t;
2306 }
2307 }
2308
Willy Tarreau798f4322012-11-08 14:49:17 +01002309 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002310 t->expire = tick_first(tick_first(req->rex, req->wex),
2311 tick_first(res->rex, res->wex));
2312 if (req->analysers)
2313 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002314
Willy Tarreau8f128b42014-11-28 15:07:47 +01002315 if (si_f->exp)
2316 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002317
Willy Tarreau8f128b42014-11-28 15:07:47 +01002318 if (si_b->exp)
2319 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002320
2321#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002322 fprintf(stderr,
2323 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2324 " 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 +01002325 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2326 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002327#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002328
2329#ifdef DEBUG_DEV
2330 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002331 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002332 ABORT_NOW();
2333#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002334 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002335 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002336 }
2337
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002338 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002339 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002340 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002341 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002342 if (sess->listener) {
2343 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002344 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002345 sess->listener->nbconn--;
2346 if (sess->listener->state == LI_FULL)
2347 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002348
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002349 /* Dequeues all of the listeners waiting for a resource */
2350 if (!LIST_ISEMPTY(&global_listener_queue))
2351 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002352
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002353 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2354 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2355 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002356 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002357
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002358 if (unlikely((global.mode & MODE_DEBUG) &&
2359 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002360 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002362 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2363 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002364 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002365 }
2366
2367 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002368 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002369
Willy Tarreaueee5b512015-04-03 23:46:31 +02002370 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002371 int n;
2372
Willy Tarreaueee5b512015-04-03 23:46:31 +02002373 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002374 if (n < 1 || n > 5)
2375 n = 0;
2376
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002377 if (sess->fe->mode == PR_MODE_HTTP) {
2378 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002379 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002380 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002381 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002382 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002383 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002384 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002385 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002386 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002387 s->be->be_counters.p.http.comp_rsp++;
2388 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002389 }
2390
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002391 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002392 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002393 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002394 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002395 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002396 }
2397
Willy Tarreau87b09662015-04-03 00:22:06 +02002398 /* update time stats for this stream */
2399 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002400
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002401 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002402 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002403 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002404 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002405 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002406}
2407
Willy Tarreau87b09662015-04-03 00:22:06 +02002408/* Update the stream's backend and server time stats */
2409void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002410{
2411 int t_request;
2412 int t_queue;
2413 int t_connect;
2414 int t_data;
2415 int t_close;
2416 struct server *srv;
2417
2418 t_request = 0;
2419 t_queue = s->logs.t_queue;
2420 t_connect = s->logs.t_connect;
2421 t_close = s->logs.t_close;
2422 t_data = s->logs.t_data;
2423
2424 if (s->be->mode != PR_MODE_HTTP)
2425 t_data = t_connect;
2426
2427 if (t_connect < 0 || t_data < 0)
2428 return;
2429
2430 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2431 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2432
2433 t_data -= t_connect;
2434 t_connect -= t_queue;
2435 t_queue -= t_request;
2436
2437 srv = objt_server(s->target);
2438 if (srv) {
2439 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2440 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2441 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2442 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2443 }
2444 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2445 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2446 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2447 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2448}
2449
Willy Tarreau7c669d72008-06-20 15:04:11 +02002450/*
2451 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002452 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002453 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002454 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002455 * server.
2456 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002457void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002458{
2459 if (sess->srv_conn == newsrv)
2460 return;
2461
2462 if (sess->srv_conn) {
2463 sess->srv_conn->served--;
2464 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2465 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002466 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002467 }
2468
2469 if (newsrv) {
2470 newsrv->served++;
2471 if (newsrv->proxy->lbprm.server_take_conn)
2472 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002473 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002474 }
2475}
2476
Willy Tarreau84455332009-03-15 22:34:05 +01002477/* Handle server-side errors for default protocols. It is called whenever a a
2478 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002479 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002480 * them. It's installed as ->srv_error for the server-side stream_interface.
2481 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002482void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002483{
2484 int err_type = si->err_type;
2485 int err = 0, fin = 0;
2486
2487 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002488 err = SF_ERR_CLICL;
2489 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002490 }
2491 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002492 err = SF_ERR_CLICL;
2493 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002494 }
2495 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002496 err = SF_ERR_SRVTO;
2497 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002498 }
2499 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002500 err = SF_ERR_SRVCL;
2501 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002502 }
2503 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002504 err = SF_ERR_SRVTO;
2505 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002506 }
2507 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002508 err = SF_ERR_SRVCL;
2509 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002510 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002511 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002512 err = SF_ERR_RESOURCE;
2513 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002514 }
Willy Tarreau84455332009-03-15 22:34:05 +01002515 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002516 err = SF_ERR_INTERNAL;
2517 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002518 }
2519
Willy Tarreaue7dff022015-04-03 01:14:29 +02002520 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002521 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002522 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002523 s->flags |= fin;
2524}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002525
Willy Tarreaue7dff022015-04-03 01:14:29 +02002526/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002527void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002528{
Willy Tarreau87b09662015-04-03 00:22:06 +02002529 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002530 return;
2531
Willy Tarreau87b09662015-04-03 00:22:06 +02002532 channel_shutw_now(&stream->req);
2533 channel_shutr_now(&stream->res);
2534 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002535 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002536 stream->flags |= why;
2537 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002538}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002539
Willy Tarreau8b22a712010-06-18 17:46:06 +02002540/************************************************************************/
2541/* All supported ACL keywords must be declared here. */
2542/************************************************************************/
2543
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002544/* Returns a pointer to a stkctr depending on the fetch keyword name.
2545 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002546 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002547 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002548 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002549 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002550 * NULL may be returned if the designated stkctr is not tracked. For
2551 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2552 * passed. When present, the currently tracked key is then looked up
2553 * in the specified table instead of the current table. The purpose is
2554 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002555 * multiple tables). <strm> is allowed to be NULL, in which case only
2556 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002557 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002558struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002559smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002560{
2561 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002562 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002563 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002564 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002565 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002566
Willy Tarreau0f791d42013-07-23 19:56:43 +02002567 if (num == '_' - '0') {
2568 /* sc_* variant, args[0] = ctr# (mandatory) */
2569 num = args[arg++].data.uint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002570 if (num >= MAX_SESS_STKCTR)
2571 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002572 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002573 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002574 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002575 struct connection *conn = objt_conn(sess->origin);
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002576
2577 if (!conn)
2578 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002579
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002580 key = addr_to_stktable_key(&conn->addr.from, args->data.prx->table.type);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002581 if (!key)
2582 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002583
Willy Tarreaua65536c2013-07-22 22:40:11 +02002584 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002585 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002586 return &stkctr;
2587 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002588
2589 /* Here, <num> contains the counter number from 0 to 9 for
2590 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002591 * args[arg] is the first optional argument. We first lookup the
2592 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002593 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002594
2595 stkptr = &strm->stkctr[num];
2596 if (!strm || !stkctr_entry(stkptr)) {
2597 stkptr = &sess->stkctr[num];
2598 if (!stkctr_entry(stkptr))
2599 return NULL;
2600 }
2601
2602 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002603 if (!stksess)
2604 return NULL;
2605
Willy Tarreau0f791d42013-07-23 19:56:43 +02002606 if (unlikely(args[arg].type == ARGT_TAB)) {
2607 /* an alternate table was specified, let's look up the same key there */
2608 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002609 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002610 return &stkctr;
2611 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002612 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002613}
2614
Willy Tarreau87b09662015-04-03 00:22:06 +02002615/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002616 * currently being tracked or not.
2617 * Supports being called as "sc[0-9]_tracked" only.
2618 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002619static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002620smp_fetch_sc_tracked(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02002621 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002622{
2623 smp->flags = SMP_F_VOL_TEST;
2624 smp->type = SMP_T_BOOL;
Willy Tarreau192252e2015-04-04 01:47:55 +02002625 smp->data.uint = !!smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002626 return 1;
2627}
2628
Willy Tarreau87b09662015-04-03 00:22:06 +02002629/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002630 * frontend counters or from the src.
2631 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2632 * zero is returned if the key is new.
2633 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002634static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002635smp_fetch_sc_get_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002636 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002637{
Willy Tarreau192252e2015-04-04 01:47:55 +02002638 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002639
2640 if (!stkctr)
2641 return 0;
2642
Willy Tarreau37406352012-04-23 16:16:37 +02002643 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002644 smp->type = SMP_T_UINT;
2645 smp->data.uint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002646
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002647 if (stkctr_entry(stkctr) != NULL) {
2648 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002649 if (!ptr)
2650 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002651 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002652 }
2653 return 1;
2654}
2655
Willy Tarreau87b09662015-04-03 00:22:06 +02002656/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002657 * tracked frontend counters or from the src.
2658 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2659 * Value zero is returned if the key is new.
2660 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002661static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002662smp_fetch_sc_gpc0_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002663 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002664{
Willy Tarreau192252e2015-04-04 01:47:55 +02002665 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002666
2667 if (!stkctr)
2668 return 0;
2669
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002670 smp->flags = SMP_F_VOL_TEST;
2671 smp->type = SMP_T_UINT;
2672 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002673 if (stkctr_entry(stkctr) != NULL) {
2674 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002675 if (!ptr)
2676 return 0; /* parameter not stored */
2677 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002678 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002679 }
2680 return 1;
2681}
2682
Willy Tarreau87b09662015-04-03 00:22:06 +02002683/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002684 * frontend counters and return it into temp integer.
2685 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002686 */
2687static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002688smp_fetch_sc_inc_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002689 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002690{
Willy Tarreau192252e2015-04-04 01:47:55 +02002691 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002692
2693 if (!stkctr)
2694 return 0;
2695
Willy Tarreau37406352012-04-23 16:16:37 +02002696 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002697 smp->type = SMP_T_UINT;
2698 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002699 if (stkctr_entry(stkctr) != NULL) {
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002700 void *ptr;
2701
2702 /* First, update gpc0_rate if it's tracked. Second, update its
2703 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2704 */
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002705 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002706 if (ptr) {
2707 update_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002708 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002709 smp->data.uint = (&stktable_data_cast(ptr, gpc0_rate))->curr_ctr;
2710 }
2711
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002712 ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002713 if (ptr)
2714 smp->data.uint = ++stktable_data_cast(ptr, gpc0);
2715
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002716 }
2717 return 1;
2718}
2719
Willy Tarreau87b09662015-04-03 00:22:06 +02002720/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002721 * frontend counters and return its previous value into temp integer.
2722 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002723 */
2724static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002725smp_fetch_sc_clr_gpc0(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002726 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002727{
Willy Tarreau192252e2015-04-04 01:47:55 +02002728 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002729
2730 if (!stkctr)
2731 return 0;
2732
Willy Tarreau37406352012-04-23 16:16:37 +02002733 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002734 smp->type = SMP_T_UINT;
2735 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002736 if (stkctr_entry(stkctr) != NULL) {
2737 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002738 if (!ptr)
2739 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002740 smp->data.uint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002741 stktable_data_cast(ptr, gpc0) = 0;
2742 }
2743 return 1;
2744}
2745
Willy Tarreau87b09662015-04-03 00:22:06 +02002746/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002747 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2748 * "src_conn_cnt" only.
2749 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002750static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002751smp_fetch_sc_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002752 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002753{
Willy Tarreau192252e2015-04-04 01:47:55 +02002754 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002755
2756 if (!stkctr)
2757 return 0;
2758
Willy Tarreau37406352012-04-23 16:16:37 +02002759 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002760 smp->type = SMP_T_UINT;
2761 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002762 if (stkctr_entry(stkctr) != NULL) {
2763 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002764 if (!ptr)
2765 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002766 smp->data.uint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002767 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002768 return 1;
2769}
2770
Willy Tarreau87b09662015-04-03 00:22:06 +02002771/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002772 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2773 * only.
2774 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002775static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002776smp_fetch_sc_conn_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002777 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002778{
Willy Tarreau192252e2015-04-04 01:47:55 +02002779 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002780
2781 if (!stkctr)
2782 return 0;
2783
Willy Tarreau37406352012-04-23 16:16:37 +02002784 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002785 smp->type = SMP_T_UINT;
2786 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002787 if (stkctr_entry(stkctr) != NULL) {
2788 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002789 if (!ptr)
2790 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002791 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002792 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002793 }
2794 return 1;
2795}
2796
Willy Tarreau87b09662015-04-03 00:22:06 +02002797/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002798 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002799 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002800 */
2801static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002802smp_fetch_src_updt_conn_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002803 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002804{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002805 struct connection *conn = objt_conn(sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002806 struct stksess *ts;
2807 struct stktable_key *key;
2808 void *ptr;
2809
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002810 if (!conn)
2811 return 0;
2812
Thierry FOURNIER74c219d2014-04-14 14:35:40 +02002813 key = addr_to_stktable_key(&conn->addr.from, px->table.type);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002814 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002815 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002816
Willy Tarreau24e32d82012-04-23 23:55:44 +02002817 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002818
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002819 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2820 /* entry does not exist and could not be created */
2821 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002822
2823 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2824 if (!ptr)
2825 return 0; /* parameter not stored in this table */
2826
Willy Tarreauf853c462012-04-23 18:53:56 +02002827 smp->type = SMP_T_UINT;
2828 smp->data.uint = ++stktable_data_cast(ptr, conn_cnt);
Willy Tarreau37406352012-04-23 16:16:37 +02002829 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002830 return 1;
2831}
2832
Willy Tarreau87b09662015-04-03 00:22:06 +02002833/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002834 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2835 * "src_conn_cur" only.
2836 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002837static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002838smp_fetch_sc_conn_cur(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002839 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002840{
Willy Tarreau192252e2015-04-04 01:47:55 +02002841 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02002842
2843 if (!stkctr)
2844 return 0;
2845
Willy Tarreau37406352012-04-23 16:16:37 +02002846 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002847 smp->type = SMP_T_UINT;
2848 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002849 if (stkctr_entry(stkctr) != NULL) {
2850 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002851 if (!ptr)
2852 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002853 smp->data.uint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002854 }
2855 return 1;
2856}
2857
Willy Tarreau87b09662015-04-03 00:22:06 +02002858/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02002859 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
2860 * "src_sess_cnt" only.
2861 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002862static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002863smp_fetch_sc_sess_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002864 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002865{
Willy Tarreau192252e2015-04-04 01:47:55 +02002866 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02002867
2868 if (!stkctr)
2869 return 0;
2870
Willy Tarreau37406352012-04-23 16:16:37 +02002871 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002872 smp->type = SMP_T_UINT;
2873 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002874 if (stkctr_entry(stkctr) != NULL) {
2875 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002876 if (!ptr)
2877 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002878 smp->data.uint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02002879 }
2880 return 1;
2881}
2882
Willy Tarreau87b09662015-04-03 00:22:06 +02002883/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002884 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
2885 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002886static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002887smp_fetch_sc_sess_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002888 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002889{
Willy Tarreau192252e2015-04-04 01:47:55 +02002890 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002891
2892 if (!stkctr)
2893 return 0;
2894
Willy Tarreau37406352012-04-23 16:16:37 +02002895 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002896 smp->type = SMP_T_UINT;
2897 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002898 if (stkctr_entry(stkctr) != NULL) {
2899 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002900 if (!ptr)
2901 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002902 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02002903 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002904 }
2905 return 1;
2906}
2907
Willy Tarreau87b09662015-04-03 00:22:06 +02002908/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02002909 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
2910 * "src_http_req_cnt" only.
2911 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002912static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002913smp_fetch_sc_http_req_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002914 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002915{
Willy Tarreau192252e2015-04-04 01:47:55 +02002916 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02002917
2918 if (!stkctr)
2919 return 0;
2920
Willy Tarreau37406352012-04-23 16:16:37 +02002921 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002922 smp->type = SMP_T_UINT;
2923 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002924 if (stkctr_entry(stkctr) != NULL) {
2925 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002926 if (!ptr)
2927 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002928 smp->data.uint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002929 }
2930 return 1;
2931}
2932
Willy Tarreau87b09662015-04-03 00:22:06 +02002933/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02002934 * counters. Supports being called as "sc[0-9]_http_req_rate" or
2935 * "src_http_req_rate" only.
2936 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002937static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002938smp_fetch_sc_http_req_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002939 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002940{
Willy Tarreau192252e2015-04-04 01:47:55 +02002941 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02002942
2943 if (!stkctr)
2944 return 0;
2945
Willy Tarreau37406352012-04-23 16:16:37 +02002946 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002947 smp->type = SMP_T_UINT;
2948 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002949 if (stkctr_entry(stkctr) != NULL) {
2950 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002951 if (!ptr)
2952 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002953 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02002954 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002955 }
2956 return 1;
2957}
2958
Willy Tarreau87b09662015-04-03 00:22:06 +02002959/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02002960 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
2961 * "src_http_err_cnt" only.
2962 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002963static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002964smp_fetch_sc_http_err_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002965 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002966{
Willy Tarreau192252e2015-04-04 01:47:55 +02002967 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02002968
2969 if (!stkctr)
2970 return 0;
2971
Willy Tarreau37406352012-04-23 16:16:37 +02002972 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002973 smp->type = SMP_T_UINT;
2974 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002975 if (stkctr_entry(stkctr) != NULL) {
2976 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002977 if (!ptr)
2978 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02002979 smp->data.uint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02002980 }
2981 return 1;
2982}
2983
Willy Tarreau87b09662015-04-03 00:22:06 +02002984/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02002985 * counters. Supports being called as "sc[0-9]_http_err_rate" or
2986 * "src_http_err_rate" only.
2987 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02002988static int
Willy Tarreau192252e2015-04-04 01:47:55 +02002989smp_fetch_sc_http_err_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01002990 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02002991{
Willy Tarreau192252e2015-04-04 01:47:55 +02002992 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02002993
2994 if (!stkctr)
2995 return 0;
2996
Willy Tarreau37406352012-04-23 16:16:37 +02002997 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02002998 smp->type = SMP_T_UINT;
2999 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003000 if (stkctr_entry(stkctr) != NULL) {
3001 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003002 if (!ptr)
3003 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003004 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003005 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003006 }
3007 return 1;
3008}
3009
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003010/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003011 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003012 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3013 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003014static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003015smp_fetch_sc_kbytes_in(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003016 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003017{
Willy Tarreau192252e2015-04-04 01:47:55 +02003018 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003019
3020 if (!stkctr)
3021 return 0;
3022
Willy Tarreau37406352012-04-23 16:16:37 +02003023 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003024 smp->type = SMP_T_UINT;
3025 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003026 if (stkctr_entry(stkctr) != NULL) {
3027 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003028 if (!ptr)
3029 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003030 smp->data.uint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003031 }
3032 return 1;
3033}
3034
Willy Tarreau613fe992013-07-23 17:39:19 +02003035/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003036 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003037 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003038 */
3039static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003040smp_fetch_sc_bytes_in_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003041 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003042{
Willy Tarreau192252e2015-04-04 01:47:55 +02003043 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003044
3045 if (!stkctr)
3046 return 0;
3047
Willy Tarreau37406352012-04-23 16:16:37 +02003048 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003049 smp->type = SMP_T_UINT;
3050 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003051 if (stkctr_entry(stkctr) != NULL) {
3052 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003053 if (!ptr)
3054 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003055 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003056 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003057 }
3058 return 1;
3059}
3060
Willy Tarreau53aea102013-07-23 17:39:02 +02003061/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003062 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003063 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3064 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003065static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003066smp_fetch_sc_kbytes_out(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Willy Tarreau15e91e12015-04-04 00:52:09 +02003067 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003068{
Willy Tarreau192252e2015-04-04 01:47:55 +02003069 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003070
3071 if (!stkctr)
3072 return 0;
3073
Willy Tarreau37406352012-04-23 16:16:37 +02003074 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003075 smp->type = SMP_T_UINT;
3076 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003077 if (stkctr_entry(stkctr) != NULL) {
3078 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003079 if (!ptr)
3080 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003081 smp->data.uint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003082 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003083 return 1;
3084}
3085
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003086/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003087 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003088 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003089 */
3090static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003091smp_fetch_sc_bytes_out_rate(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003092 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003093{
Willy Tarreau192252e2015-04-04 01:47:55 +02003094 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003095
3096 if (!stkctr)
3097 return 0;
3098
Willy Tarreau37406352012-04-23 16:16:37 +02003099 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003100 smp->type = SMP_T_UINT;
3101 smp->data.uint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003102 if (stkctr_entry(stkctr) != NULL) {
3103 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003104 if (!ptr)
3105 return 0; /* parameter not stored */
Willy Tarreauf853c462012-04-23 18:53:56 +02003106 smp->data.uint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003107 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003108 }
3109 return 1;
3110}
3111
Willy Tarreau87b09662015-04-03 00:22:06 +02003112/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003113 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3114 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003115static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003116smp_fetch_sc_trackers(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003117 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003118{
Willy Tarreau192252e2015-04-04 01:47:55 +02003119 struct stkctr *stkctr = smp_fetch_sc_stkctr(sess, strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003120
Willy Tarreau563eef42013-07-23 18:32:02 +02003121 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003122 return 0;
3123
Willy Tarreau563eef42013-07-23 18:32:02 +02003124 smp->flags = SMP_F_VOL_TEST;
3125 smp->type = SMP_T_UINT;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003126 smp->data.uint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003127 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003128}
3129
Willy Tarreau34db1082012-04-19 17:16:54 +02003130/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003131 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003132 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003133static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003134smp_fetch_table_cnt(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003135 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003136{
Willy Tarreau37406352012-04-23 16:16:37 +02003137 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003138 smp->type = SMP_T_UINT;
Willy Tarreau24e32d82012-04-23 23:55:44 +02003139 smp->data.uint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003140 return 1;
3141}
3142
Willy Tarreau34db1082012-04-19 17:16:54 +02003143/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003144 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003145 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003146static int
Willy Tarreau192252e2015-04-04 01:47:55 +02003147smp_fetch_table_avl(struct proxy *px, struct session *sess, struct stream *strm, unsigned int opt,
Thierry FOURNIERf41a8092014-12-07 18:37:57 +01003148 const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003149{
Willy Tarreau24e32d82012-04-23 23:55:44 +02003150 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003151 smp->flags = SMP_F_VOL_TEST;
Willy Tarreauf853c462012-04-23 18:53:56 +02003152 smp->type = SMP_T_UINT;
3153 smp->data.uint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003154 return 1;
3155}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003156
Willy Tarreau61612d42012-04-19 18:42:05 +02003157/* Note: must not be declared <const> as its list will be overwritten.
3158 * Please take care of keeping this list alphabetically sorted.
3159 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003160static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003161 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003162}};
3163
Willy Tarreau281c7992013-01-08 01:23:27 +01003164/* Note: must not be declared <const> as its list will be overwritten.
3165 * Please take care of keeping this list alphabetically sorted.
3166 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003167static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Willy Tarreau0f791d42013-07-23 19:56:43 +02003168 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3169 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3170 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3171 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3172 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3173 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3174 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3175 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3176 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3177 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3178 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3179 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3180 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3181 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3182 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3183 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3184 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3185 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,UINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3186 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,UINT,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3187 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3188 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3189 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3190 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3191 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3192 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3193 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3194 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3195 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3196 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3197 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3198 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3199 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3200 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3201 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3202 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3203 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3204 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3205 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3206 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3207 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3208 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3209 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3210 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3211 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3212 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3213 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3214 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3215 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3216 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3217 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3218 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3219 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3220 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3221 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3222 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3223 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3224 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3225 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3226 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3227 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3228 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3229 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3230 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3231 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3232 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3233 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3234 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3235 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3236 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3237 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3238 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3239 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3240 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3241 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3242 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3243 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3244 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3245 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3246 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3247 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3248 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3249 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3250 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3251 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3252 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3253 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3254 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3255 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3256 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3257 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3258 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3259 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3260 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3261 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_L4CLI, },
3262 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
3263 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_UINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003264 { /* END */ },
3265}};
3266
Willy Tarreau8b22a712010-06-18 17:46:06 +02003267__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003268static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003269{
Willy Tarreau281c7992013-01-08 01:23:27 +01003270 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003271 acl_register_keywords(&acl_kws);
3272}
3273
Willy Tarreaubaaee002006-06-26 02:48:02 +02003274/*
3275 * Local variables:
3276 * c-indent-level: 8
3277 * c-basic-offset: 8
3278 * End:
3279 */