blob: 13108968396cde3ab69b37074fc61e8abf6fcc73 [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
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020017#include <common/cfgparse.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020018#include <common/config.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020019#include <common/buffer.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020020#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020023#include <types/applet.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010025#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020027#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020028#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020029#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010030#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020031#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010032#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020033#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020034#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020035#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020036#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020037#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010038#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010039#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020040#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020041#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020042#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020043#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020044#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010045#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010046#include <proto/proto_http.h>
47#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020048#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020049#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010050#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020051#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010052#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010053#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010054#include <proto/task.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020055#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020056
Willy Tarreau87b09662015-04-03 00:22:06 +020057struct pool_head *pool2_stream;
58struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau87b09662015-04-03 00:22:06 +020060/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010061struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
62
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020063/* List of all use-service keywords. */
64static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
65
Willy Tarreau9903f0e2015-04-04 18:50:31 +020066/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020067 * handshake, in order to complete initialization of a valid stream. It must be
68 * called with a session (which may be embryonic). It returns the pointer to
69 * the newly created stream, or NULL in case of fatal error. The client-facing
70 * end point is assigned to <origin>, which must be valid. The task's context
71 * is set to the new stream, and its function is set to process_stream().
72 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020073 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020074struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020075{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020076 struct stream *s;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020077 struct connection *conn = objt_conn(origin);
78 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020079
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020080 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020081 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020082
83 /* minimum stream initialization required for an embryonic stream is
84 * fairly low. We need very little to execute L4 ACLs, then we need a
85 * task to make the client-side connection live on its own.
86 * - flags
87 * - stick-entry tracking
88 */
89 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020090 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020091 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020092 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
93 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
94 tv_zero(&s->logs.tv_request);
95 s->logs.t_queue = -1;
96 s->logs.t_connect = -1;
97 s->logs.t_data = -1;
98 s->logs.t_close = 0;
99 s->logs.bytes_in = s->logs.bytes_out = 0;
100 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
101 s->logs.srv_queue_size = 0; /* we will get this number soon */
102
103 /* default logging function */
104 s->do_log = strm_log;
105
106 /* default error reporting function, may be changed by analysers */
107 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200108
109 /* Initialise the current rule list pointer to NULL. We are sure that
110 * any rulelist match the NULL pointer.
111 */
112 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200113 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200114
Willy Tarreaua68f7622015-09-21 17:48:24 +0200115 /* Copy SC counters for the stream. We don't touch refcounts because
116 * any reference we have is inherited from the session. Since the stream
117 * doesn't exist without the session, the session's existence guarantees
118 * we don't lose the entry. During the store operation, the stream won't
119 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200120 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200121 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200122
123 s->sess = sess;
124 s->si[0].flags = SI_FL_NONE;
125 s->si[1].flags = SI_FL_ISBACK;
126
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200127 s->uniq_id = global.req_count++;
128
Willy Tarreau87b09662015-04-03 00:22:06 +0200129 /* OK, we're keeping the stream, so let's properly initialize the stream */
130 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200131 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100132 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200133
Willy Tarreaue7dff022015-04-03 01:14:29 +0200134 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200135 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200136
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200137 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200138 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200139 t->context = s;
140 t->expire = TICK_ETERNITY;
141
Willy Tarreau87b09662015-04-03 00:22:06 +0200142 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200143 * This changes later when switching rules are executed or
144 * when the default backend is assigned.
145 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200146 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200147 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100148 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200149 s->req_cap = NULL;
150 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200151
Willy Tarreauebcd4842015-06-19 11:59:02 +0200152 /* Initialise all the variables contexts even if not used.
153 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200154 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200155 vars_init(&s->vars_txn, SCOPE_TXN);
156 vars_init(&s->vars_reqres, SCOPE_REQ);
157
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200158 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100159 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200160 si_set_state(&s->si[0], SI_ST_EST);
161
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200162 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200163 if (conn)
164 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200165 else if (appctx)
166 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200167
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200168 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200169 s->si[0].flags |= SI_FL_INDEP_STR;
170
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200171 /* pre-initialize the other side's stream interface to an INIT state. The
172 * callbacks will be initialized before attempting to connect.
173 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100174 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200175
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200176 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200177 s->si[1].flags |= SI_FL_INDEP_STR;
178
Willy Tarreau87b09662015-04-03 00:22:06 +0200179 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200180 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200181 s->pend_pos = NULL;
182
183 /* init store persistence */
184 s->store_count = 0;
185
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100186 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100187 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200188 s->req.analysers = 0;
189 channel_auto_connect(&s->req); /* don't wait to establish connection */
190 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200191
192 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100193 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100194 s->req.rex = TICK_ETERNITY;
195 s->req.wex = TICK_ETERNITY;
196 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100198 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100199 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100200 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200201
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200202 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100203 s->req.flags |= CF_NEVER_WAIT;
204 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200205 }
206
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200207 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100208 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100209 s->res.rex = TICK_ETERNITY;
210 s->res.wex = TICK_ETERNITY;
211 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200212
Willy Tarreaueee5b512015-04-03 23:46:31 +0200213 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100214
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100215 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100216
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200217 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200218 if (conn)
219 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200220 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200221 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200222
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200223 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200224 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200225
226 /* it is important not to call the wakeup function directly but to
227 * pass through task_wakeup(), because this one knows how to apply
228 * priorities to tasks.
229 */
230 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200231 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200232
233 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200234 out_fail_accept:
Willy Tarreau3b246412014-11-25 17:10:33 +0100235 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200236 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200237 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200238}
239
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200241 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200242 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200243static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200245 struct session *sess = strm_sess(s);
246 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100247 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200248 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200249 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100250
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251 if (s->pend_pos)
252 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100253
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100254 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200255 if (s->flags & SF_CURR_SESS) {
256 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100257 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100258 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100259 if (may_dequeue_tasks(objt_server(s->target), s->be))
260 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100261 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100262
Willy Tarreau7c669d72008-06-20 15:04:11 +0200263 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200264 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200265 * it should normally be only the same as the one above,
266 * so this should not happen in fact.
267 */
268 sess_change_server(s, NULL);
269 }
270
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100271 if (s->req.pipe)
272 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100273
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100274 if (s->res.pipe)
275 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100276
Willy Tarreaubf883e02014-11-25 21:10:35 +0100277 /* We may still be present in the buffer wait queue */
278 if (!LIST_ISEMPTY(&s->buffer_wait)) {
279 LIST_DEL(&s->buffer_wait);
280 LIST_INIT(&s->buffer_wait);
281 }
282
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100283 b_drop(&s->req.buf);
284 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100285 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200286 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200287
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100288 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200289 if (s->txn)
290 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100291
Willy Tarreau1e954912012-10-12 17:50:05 +0200292 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200293 if (cli_conn)
294 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200295
Willy Tarreaua4cda672010-06-06 18:28:49 +0200296 for (i = 0; i < s->store_count; i++) {
297 if (!s->store[i].ts)
298 continue;
299 stksess_free(s->store[i].table, s->store[i].ts);
300 s->store[i].ts = NULL;
301 }
302
Willy Tarreaueee5b512015-04-03 23:46:31 +0200303 if (s->txn) {
304 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
305 pool_free2(pool2_http_txn, s->txn);
306 s->txn = NULL;
307 }
308
Willy Tarreau92fb9832007-10-16 17:34:28 +0200309 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200310 pool_free2(fe->rsp_cap_pool, s->res_cap);
311 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100313
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200314 /* Cleanup all variable contexts. */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200315 vars_prune(&s->vars_txn, s);
316 vars_prune(&s->vars_reqres, s);
317
Willy Tarreau87b09662015-04-03 00:22:06 +0200318 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200319
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100320 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100321 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200322 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100323 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100324 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100325 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200326 if (s->list.n != &streams)
327 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100328 bref->ref = s->list.n;
329 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100330 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200331 si_release_endpoint(&s->si[1]);
332 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200333
334 /* FIXME: for now we have a 1:1 relation between stream and session so
335 * the stream must free the session.
336 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200337 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200338 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200339
340 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200341 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200342 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200343 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200344 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200345 pool_flush2(pool2_requri);
346 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200347 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200348 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100349 pool_flush2(pool2_connection);
350 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200351 pool_flush2(fe->req_cap_pool);
352 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200353 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200354}
355
Willy Tarreau78955f42014-12-28 13:09:02 +0100356/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
357 * that it's not the last available buffer or it's the response buffer. Unless
358 * the buffer is the response buffer, an extra control is made so that we always
359 * keep <tune.buffers.reserved> buffers available after this allocation. To be
360 * called at the beginning of recv() callbacks to ensure that the required
361 * buffers are properly allocated. Returns 0 in case of failure, non-zero
362 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100363 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200364int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100365{
Willy Tarreau87b09662015-04-03 00:22:06 +0200366 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100367 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100368 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100369
Willy Tarreau78955f42014-12-28 13:09:02 +0100370 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100371 margin = global.tune.reserved_bufs;
372
Thierry FOURNIER27929fb2015-09-25 08:36:11 +0200373 s = chn_strm(chn);
Willy Tarreau78955f42014-12-28 13:09:02 +0100374
375 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100376 if (b)
377 return 1;
378
Willy Tarreaubf883e02014-11-25 21:10:35 +0100379 if (LIST_ISEMPTY(&s->buffer_wait))
380 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100381 return 0;
382}
383
Willy Tarreau87b09662015-04-03 00:22:06 +0200384/* Allocates a work buffer for stream <s>. It is meant to be called inside
385 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200386 * to work fine, which is the response buffer so that an error message may be
387 * built and returned. Response buffers may be allocated from the reserve, this
388 * is critical to ensure that a response may always flow and will never block a
389 * server from releasing a connection. Returns 0 in case of failure, non-zero
390 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100391 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200392int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100393{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100394 if (!LIST_ISEMPTY(&s->buffer_wait)) {
395 LIST_DEL(&s->buffer_wait);
396 LIST_INIT(&s->buffer_wait);
397 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100398
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200399 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100400 return 1;
401
Willy Tarreaubf883e02014-11-25 21:10:35 +0100402 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100403 return 0;
404}
405
406/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100407 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200408 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100409 * a single buffer, so it makes sense to try to wake two up when two buffers
410 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100411 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200412void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100413{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100414 if (s->req.buf->size && buffer_empty(s->req.buf))
415 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100416
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100417 if (s->res.buf->size && buffer_empty(s->res.buf))
418 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100419
Willy Tarreaubf883e02014-11-25 21:10:35 +0100420 /* if we're certain to have at least 1 buffer available, and there is
421 * someone waiting, we can wake up a waiter and offer them.
422 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100423 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200424 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100425}
426
Willy Tarreau87b09662015-04-03 00:22:06 +0200427/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100428 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200429 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100430 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200431void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100432{
Willy Tarreau87b09662015-04-03 00:22:06 +0200433 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100434
435 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100436 if (rqlimit <= run_queue)
437 break;
438
Willy Tarreaubf883e02014-11-25 21:10:35 +0100439 if (sess->task->state & TASK_RUNNING)
440 continue;
441
442 LIST_DEL(&sess->buffer_wait);
443 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100444 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100445 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100446}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200447
448/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200449int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200450{
Willy Tarreau87b09662015-04-03 00:22:06 +0200451 LIST_INIT(&streams);
452 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
453 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200454}
455
Willy Tarreau87b09662015-04-03 00:22:06 +0200456void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100457{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200458 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100459 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200460 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100461 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100462
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100463 bytes = s->req.total - s->logs.bytes_in;
464 s->logs.bytes_in = s->req.total;
465 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200466 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100467
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100468 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100469
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100470 if (objt_server(s->target))
471 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200472
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200473 if (sess->listener && sess->listener->counters)
474 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200475
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100476 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200477 struct stkctr *stkctr = &s->stkctr[i];
478
479 if (!stkctr_entry(stkctr)) {
480 stkctr = &sess->stkctr[i];
481 if (!stkctr_entry(stkctr))
482 continue;
483 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200484
Emeric Brun57056f02015-06-15 18:29:57 +0200485 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
486 if (ptr1)
487 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200488
Emeric Brun57056f02015-06-15 18:29:57 +0200489 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
490 if (ptr2)
491 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200492 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200493
494 /* If data was modified, we need to touch to re-schedule sync */
495 if (ptr1 || ptr2)
496 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100497 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100498 }
499
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100500 bytes = s->res.total - s->logs.bytes_out;
501 s->logs.bytes_out = s->res.total;
502 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200503 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100504
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100505 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100506
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100507 if (objt_server(s->target))
508 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200509
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200510 if (sess->listener && sess->listener->counters)
511 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200512
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200514 struct stkctr *stkctr = &s->stkctr[i];
515
516 if (!stkctr_entry(stkctr)) {
517 stkctr = &sess->stkctr[i];
518 if (!stkctr_entry(stkctr))
519 continue;
520 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200521
Emeric Brun57056f02015-06-15 18:29:57 +0200522 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
523 if (ptr1)
524 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200525
Emeric Brun57056f02015-06-15 18:29:57 +0200526 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
527 if (ptr2)
528 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200529 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200530
531 /* If data was modified, we need to touch to re-schedule sync */
532 if (ptr1 || ptr2)
533 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100534 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100535 }
536}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100538/* This function is called with (si->state == SI_ST_CON) meaning that a
539 * connection was attempted and that the file descriptor is already allocated.
540 * We must check for establishment, error and abort. Possible output states
541 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
542 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200543 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100544 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200545static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100546{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100547 struct stream_interface *si = &s->si[1];
548 struct channel *req = &s->req;
549 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200550 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100551
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 /* If we got an error, or if nothing happened and the connection timed
553 * out, we must give up. The CER state handler will take care of retry
554 * attempts and error reports.
555 */
556 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100557 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100558 /* Some data were sent past the connection establishment,
559 * so we need to pretend we're established to log correctly
560 * and let later states handle the failure.
561 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100562 si->state = SI_ST_EST;
563 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100564 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100565 return 1;
566 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100567 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100568 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100569
Willy Tarreauf79c8172013-10-21 16:30:56 +0200570 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100571
572 if (si->err_type)
573 return 0;
574
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575 if (si->flags & SI_FL_ERR)
576 si->err_type = SI_ET_CONN_ERR;
577 else
578 si->err_type = SI_ET_CONN_TO;
579 return 0;
580 }
581
582 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100583 if (!(req->flags & CF_WRITE_PARTIAL) &&
584 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200585 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
586 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100587 s->be->options & PR_O_ABRT_CLOSE)))) {
588 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200589 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100590 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100591 if (s->srv_error)
592 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100593 return 1;
594 }
595
596 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200597 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598 return 1;
599
600 /* OK, this means that a connection succeeded. The caller will be
601 * responsible for handling the transition from CON to EST.
602 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100603 si->state = SI_ST_EST;
604 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100605 return 1;
606}
607
608/* This function is called with (si->state == SI_ST_CER) meaning that a
609 * previous connection attempt has failed and that the file descriptor
610 * has already been released. Possible causes include asynchronous error
611 * notification and time out. Possible output states are SI_ST_CLO when
612 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
613 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
614 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
615 * marked as in error state. It returns 0.
616 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200617static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100618{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100619 struct stream_interface *si = &s->si[1];
620
Willy Tarreau87b09662015-04-03 00:22:06 +0200621 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100622 if (objt_server(s->target)) {
623 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100624
Willy Tarreaue7dff022015-04-03 01:14:29 +0200625 if (s->flags & SF_CURR_SESS) {
626 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100627 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100628 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100629 }
630
631 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200632 si->conn_retries--;
633 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634 if (!si->err_type) {
635 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 }
637
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100638 if (objt_server(s->target))
639 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100640 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100641 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100642 if (may_dequeue_tasks(objt_server(s->target), s->be))
643 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644
645 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200646 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100647 s->req.flags |= CF_WRITE_ERROR;
648 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100649
650 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100651 if (s->srv_error)
652 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100653 return 0;
654 }
655
656 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700657 * retry on another server. By default this redispatch occurs on the
658 * last retry, but if configured we allow redispatches to occur on
659 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200660 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661 * bit to ignore any persistence cookie. We won't count a retry nor a
662 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200663 * If the connection is not persistent, the balancing algorithm is not
664 * determinist (round robin) and there is more than one active server,
665 * we accept to perform an immediate redispatch without waiting since
666 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200668 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700669 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
670 ((((s->be->redispatch_after > 0) &&
671 ((s->be->conn_retries - si->conn_retries) %
672 s->be->redispatch_after == 0)) ||
673 ((s->be->redispatch_after < 0) &&
674 ((s->be->conn_retries - si->conn_retries) %
675 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200676 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700677 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100678 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100679 if (may_dequeue_tasks(objt_server(s->target), s->be))
680 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681
Willy Tarreaue7dff022015-04-03 01:14:29 +0200682 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100683 si->state = SI_ST_REQ;
684 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100685 if (objt_server(s->target))
686 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100687 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100688 si->state = SI_ST_ASS;
689 }
690
691 if (si->flags & SI_FL_ERR) {
692 /* The error was an asynchronous connection error, and we will
693 * likely have to retry connecting to the same server, most
694 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200695 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 */
697
Willy Tarreaub0290662014-06-13 17:04:44 +0200698 int delay = 1000;
699
700 if (s->be->timeout.connect && s->be->timeout.connect < delay)
701 delay = s->be->timeout.connect;
702
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 if (!si->err_type)
704 si->err_type = SI_ET_CONN_ERR;
705
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200706 /* only wait when we're retrying on the same server */
707 if (si->state == SI_ST_ASS ||
708 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
709 (s->be->srv_act <= 1)) {
710 si->state = SI_ST_TAR;
711 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
712 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100713 return 0;
714 }
715 return 0;
716}
717
718/*
719 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200720 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200721 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100722 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200723static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100724{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100725 struct stream_interface *si = &s->si[1];
726 struct channel *req = &s->req;
727 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100729 /* First, centralize the timers information */
730 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
731 si->exp = TICK_ETERNITY;
732
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100733 if (objt_server(s->target))
734 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100735
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100736 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 /* if the user wants to log as soon as possible, without counting
738 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200739 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100740 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100741 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100742 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743 }
744 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100745 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746 }
747
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200748 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200749 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200750 if (req->flags & CF_WAKE_CONNECT) {
751 req->flags |= CF_WAKE_ONCE;
752 req->flags &= ~CF_WAKE_CONNECT;
753 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200754 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200755 /* real connections have timeouts */
756 req->wto = s->be->timeout.server;
757 rep->rto = s->be->timeout.server;
758 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759 req->wex = TICK_ETERNITY;
760}
761
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100762/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
763 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100764 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
765 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
766 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100767 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200768static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100770 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100771 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100772 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100773
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100774 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 +0100775 now_ms, __FUNCTION__,
776 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400777 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100778 req->rex, s->res.wex,
779 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400780 req->buf->i, req->buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100781
782 if (si->state == SI_ST_ASS) {
783 /* Server assigned to connection request, we have to try to connect now */
784 int conn_err;
785
786 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100787 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100788
Willy Tarreaue7dff022015-04-03 01:14:29 +0200789 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100790 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100791 if (srv)
792 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500793 if (srv)
794 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100795 return;
796 }
797
798 /* We have received a synchronous error. We might have to
799 * abort, retry immediately or redispatch.
800 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200801 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100802 if (!si->err_type) {
803 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100804 }
805
Willy Tarreau827aee92011-03-10 16:55:02 +0100806 if (srv)
807 srv_inc_sess_ctr(srv);
808 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500809 srv_set_sess_last(srv);
810 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100811 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100812 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813
Willy Tarreau87b09662015-04-03 00:22:06 +0200814 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100815 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100816 if (may_dequeue_tasks(srv, s->be))
817 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100818
819 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200820 si_shutr(si);
821 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100822 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823
824 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
825
Willy Tarreau87b09662015-04-03 00:22:06 +0200826 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100827 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100828 if (s->srv_error)
829 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100830 return;
831 }
832
833 /* We are facing a retryable error, but we don't want to run a
834 * turn-around now, as the problem is likely a source port
835 * allocation problem, so we want to retry now.
836 */
837 si->state = SI_ST_CER;
838 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100839 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100840 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
841 return;
842 }
843 else if (si->state == SI_ST_QUE) {
844 /* connection request was queued, check for any update */
845 if (!s->pend_pos) {
846 /* The connection is not in the queue anymore. Either
847 * we have a server connection slot available and we
848 * go directly to the assigned state, or we need to
849 * load-balance first and go to the INI state.
850 */
851 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200852 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100853 si->state = SI_ST_REQ;
854 else {
855 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
856 si->state = SI_ST_ASS;
857 }
858 return;
859 }
860
861 /* Connection request still in queue... */
862 if (si->flags & SI_FL_EXP) {
863 /* ... and timeout expired */
864 si->exp = TICK_ETERNITY;
865 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100866 if (srv)
867 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100868 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200869 si_shutr(si);
870 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100871 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100872 if (!si->err_type)
873 si->err_type = SI_ET_QUEUE_TO;
874 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100875 if (s->srv_error)
876 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 return;
878 }
879
880 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100881 if ((req->flags & (CF_READ_ERROR)) ||
882 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
883 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100884 /* give up */
885 si->exp = TICK_ETERNITY;
886 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200887 si_shutr(si);
888 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100889 si->err_type |= SI_ET_QUEUE_ABRT;
890 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100891 if (s->srv_error)
892 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100893 return;
894 }
895
896 /* Nothing changed */
897 return;
898 }
899 else if (si->state == SI_ST_TAR) {
900 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100901 if ((req->flags & (CF_READ_ERROR)) ||
902 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
903 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 /* give up */
905 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200906 si_shutr(si);
907 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 si->err_type |= SI_ET_CONN_ABRT;
909 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100910 if (s->srv_error)
911 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 return;
913 }
914
915 if (!(si->flags & SI_FL_EXP))
916 return; /* still in turn-around */
917
918 si->exp = TICK_ETERNITY;
919
Willy Tarreau87b09662015-04-03 00:22:06 +0200920 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921 * marked "assigned".
922 * FIXME: Should we force a redispatch attempt when the server is down ?
923 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200924 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100925 si->state = SI_ST_ASS;
926 else
927 si->state = SI_ST_REQ;
928 return;
929 }
930}
931
Willy Tarreau87b09662015-04-03 00:22:06 +0200932/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900933 * also counts a failed request if the server state has not reached the request
934 * stage.
935 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200936static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900937{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200938 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900939 if (s->si[1].state < SI_ST_REQ) {
940
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200941 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200942 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200943 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900944
Willy Tarreaue7dff022015-04-03 01:14:29 +0200945 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900946 }
947 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200948 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900949 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200950 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900951 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200952 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900953 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200954 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900955 }
956}
957
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100959 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
960 * a real connection to a server, indicating that a server has been assigned,
961 * or SI_ST_EST for a successful connection to an applet. It may also return
962 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100963 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200964static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100965{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100966 struct stream_interface *si = &s->si[1];
967
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100968 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 +0100969 now_ms, __FUNCTION__,
970 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400971 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100972 s->req.rex, s->res.wex,
973 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400974 s->req.buf->i, s->req.buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975
976 if (si->state != SI_ST_REQ)
977 return;
978
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100979 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
980 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100981 struct appctx *appctx = objt_appctx(si->end);
982
983 if (!appctx || appctx->applet != __objt_applet(s->target))
984 appctx = stream_int_register_handler(si, objt_applet(s->target));
985
986 if (!appctx) {
987 /* No more memory, let's immediately abort. Force the
988 * error code to ignore the ERR_LOCAL which is not a
989 * real error.
990 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200991 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100992
993 si_shutr(si);
994 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100995 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +0100996 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100997 si->state = SI_ST_CLO;
998 if (s->srv_error)
999 s->srv_error(s, si);
1000 return;
1001 }
1002
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001003 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001004 si->state = SI_ST_EST;
1005 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001006 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001007 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001008 return;
1009 }
1010
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001011 /* Try to assign a server */
1012 if (srv_redispatch_connect(s) != 0) {
1013 /* We did not get a server. Either we queued the
1014 * connection request, or we encountered an error.
1015 */
1016 if (si->state == SI_ST_QUE)
1017 return;
1018
1019 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001020 si_shutr(si);
1021 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001022 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001023 if (!si->err_type)
1024 si->err_type = SI_ET_CONN_OTHER;
1025 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001026 if (s->srv_error)
1027 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001028 return;
1029 }
1030
1031 /* The server is assigned */
1032 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1033 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001034 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001035}
1036
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001037/* This function parses the use-service action ruleset. It executes
1038 * the associated ACL and set an applet as a stream or txn final node.
1039 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1040 * consistent state. It returns ACT_RET_STOP in succes case because
1041 * use-service must be a terminal action. Returns ACT_RET_YIELD
1042 * if the initialisation function require more data.
1043 */
1044enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1045 struct session *sess, struct stream *s, int flags)
1046
1047{
1048 struct appctx *appctx;
1049
1050 /* Initialises the applet if it is required. */
1051 if (flags & ACT_FLAG_FIRST) {
1052 /* Register applet. this function schedules the applet. */
1053 s->target = &rule->applet.obj_type;
1054 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1055 return ACT_RET_ERR;
1056
1057 /* Initialise the context. */
1058 appctx = si_appctx(&s->si[1]);
1059 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1060 appctx->rule = rule;
1061 }
1062 else
1063 appctx = si_appctx(&s->si[1]);
1064
1065 /* Stops the applet sheduling, in case of the init function miss
1066 * some data.
1067 */
1068 appctx_pause(appctx);
1069 si_applet_stop_get(&s->si[1]);
1070
1071 /* Call initialisation. */
1072 if (rule->applet.init)
1073 switch (rule->applet.init(appctx, px, s)) {
1074 case 0: return ACT_RET_ERR;
1075 case 1: break;
1076 default: return ACT_RET_YIELD;
1077 }
1078
1079 /* Now we can schedule the applet. */
1080 si_applet_cant_get(&s->si[1]);
1081 appctx_wakeup(appctx);
1082
1083 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1084 sess->fe->fe_counters.intercepted_req++;
1085
1086 /* The flag SF_ASSIGNED prevent from server assignment. */
1087 s->flags |= SF_ASSIGNED;
1088
1089 return ACT_RET_STOP;
1090}
1091
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001092/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001093 * if appropriate. The default_backend rule is also considered, then the
1094 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001095 * It returns 1 if the processing can continue on next analysers, or zero if it
1096 * either needs more data or wants to immediately abort the request.
1097 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001098static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001099{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001100 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001101 struct session *sess = s->sess;
1102 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001103
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001104 req->analysers &= ~an_bit;
1105 req->analyse_exp = TICK_ETERNITY;
1106
Willy Tarreau87b09662015-04-03 00:22:06 +02001107 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 +02001108 now_ms, __FUNCTION__,
1109 s,
1110 req,
1111 req->rex, req->wex,
1112 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001113 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001114 req->analysers);
1115
1116 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001117 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001118 struct switching_rule *rule;
1119
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001120 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001121 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001122
Willy Tarreauf51658d2014-04-23 01:21:56 +02001123 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001124 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001125 ret = acl_pass(ret);
1126 if (rule->cond->pol == ACL_COND_UNLESS)
1127 ret = !ret;
1128 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001129
1130 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001131 /* If the backend name is dynamic, try to resolve the name.
1132 * If we can't resolve the name, or if any error occurs, break
1133 * the loop and fallback to the default backend.
1134 */
1135 struct proxy *backend;
1136
1137 if (rule->dynamic) {
1138 struct chunk *tmp = get_trash_chunk();
1139 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1140 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001141 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001142 if (!backend)
1143 break;
1144 }
1145 else
1146 backend = rule->be.backend;
1147
Willy Tarreau87b09662015-04-03 00:22:06 +02001148 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001149 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001150 break;
1151 }
1152 }
1153
1154 /* To ensure correct connection accounting on the backend, we
1155 * have to assign one if it was not set (eg: a listen). This
1156 * measure also takes care of correctly setting the default
1157 * backend if any.
1158 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001159 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001160 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001161 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001162 }
1163
Willy Tarreaufb356202010-08-03 14:02:05 +02001164 /* 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 +02001165 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001166 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1167 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001168 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001169
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001170 /* 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 +02001171 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001172 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001173 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001174 int ret = 1;
1175
1176 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001177 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 +01001178 ret = acl_pass(ret);
1179 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1180 ret = !ret;
1181 }
1182
1183 if (ret) {
1184 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001185 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001186 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001187 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001188 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001189 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001190 break;
1191 }
1192 }
1193
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001194 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001195
1196 sw_failed:
1197 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001198 channel_abort(&s->req);
1199 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001200
Willy Tarreaue7dff022015-04-03 01:14:29 +02001201 if (!(s->flags & SF_ERR_MASK))
1202 s->flags |= SF_ERR_RESOURCE;
1203 if (!(s->flags & SF_FINST_MASK))
1204 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001205
Willy Tarreaueee5b512015-04-03 23:46:31 +02001206 if (s->txn)
1207 s->txn->status = 500;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001208 s->req.analysers = 0;
1209 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001210 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001211}
1212
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001213/* This stream analyser works on a request. It applies all use-server rules on
1214 * it then returns 1. The data must already be present in the buffer otherwise
1215 * they won't match. It always returns 1.
1216 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001217static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001218{
1219 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001220 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001221 struct server_rule *rule;
1222
Willy Tarreau87b09662015-04-03 00:22:06 +02001223 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 +02001224 now_ms, __FUNCTION__,
1225 s,
1226 req,
1227 req->rex, req->wex,
1228 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001229 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001230 req->analysers);
1231
Willy Tarreaue7dff022015-04-03 01:14:29 +02001232 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001233 list_for_each_entry(rule, &px->server_rules, list) {
1234 int ret;
1235
Willy Tarreau192252e2015-04-04 01:47:55 +02001236 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001237 ret = acl_pass(ret);
1238 if (rule->cond->pol == ACL_COND_UNLESS)
1239 ret = !ret;
1240
1241 if (ret) {
1242 struct server *srv = rule->srv.ptr;
1243
Willy Tarreau892337c2014-05-13 23:41:20 +02001244 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001245 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001246 (s->flags & SF_FORCE_PRST)) {
1247 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001248 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001249 break;
1250 }
1251 /* if the server is not UP, let's go on with next rules
1252 * just in case another one is suited.
1253 */
1254 }
1255 }
1256 }
1257
1258 req->analysers &= ~an_bit;
1259 req->analyse_exp = TICK_ETERNITY;
1260 return 1;
1261}
1262
Emeric Brun1d33b292010-01-04 15:47:17 +01001263/* This stream analyser works on a request. It applies all sticking rules on
1264 * it then returns 1. The data must already be present in the buffer otherwise
1265 * they won't match. It always returns 1.
1266 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001267static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001268{
1269 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001270 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001271 struct sticking_rule *rule;
1272
Willy Tarreau87b09662015-04-03 00:22:06 +02001273 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 +01001274 now_ms, __FUNCTION__,
1275 s,
1276 req,
1277 req->rex, req->wex,
1278 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001279 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001280 req->analysers);
1281
1282 list_for_each_entry(rule, &px->sticking_rules, list) {
1283 int ret = 1 ;
1284 int i;
1285
Willy Tarreau9667a802013-12-09 12:52:13 +01001286 /* Only the first stick store-request of each table is applied
1287 * and other ones are ignored. The purpose is to allow complex
1288 * configurations which look for multiple entries by decreasing
1289 * order of precision and to stop at the first which matches.
1290 * An example could be a store of the IP address from an HTTP
1291 * header first, then from the source if not found.
1292 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001293 for (i = 0; i < s->store_count; i++) {
1294 if (rule->table.t == s->store[i].table)
1295 break;
1296 }
1297
1298 if (i != s->store_count)
1299 continue;
1300
1301 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001302 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001303 ret = acl_pass(ret);
1304 if (rule->cond->pol == ACL_COND_UNLESS)
1305 ret = !ret;
1306 }
1307
1308 if (ret) {
1309 struct stktable_key *key;
1310
Willy Tarreau192252e2015-04-04 01:47:55 +02001311 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 +01001312 if (!key)
1313 continue;
1314
1315 if (rule->flags & STK_IS_MATCH) {
1316 struct stksess *ts;
1317
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001318 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001319 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001320 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001321 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001322
1323 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001324 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1325 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001326 if (node) {
1327 struct server *srv;
1328
1329 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001330 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001331 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001332 (s->flags & SF_FORCE_PRST)) {
1333 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001334 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001335 }
1336 }
1337 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001338 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001339 }
1340 }
1341 if (rule->flags & STK_IS_STORE) {
1342 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1343 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;
1348 s->store[s->store_count++].ts = ts;
1349 }
1350 }
1351 }
1352 }
1353 }
1354
1355 req->analysers &= ~an_bit;
1356 req->analyse_exp = TICK_ETERNITY;
1357 return 1;
1358}
1359
1360/* This stream analyser works on a response. It applies all store rules on it
1361 * then returns 1. The data must already be present in the buffer otherwise
1362 * they won't match. It always returns 1.
1363 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001364static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001365{
1366 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001367 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001368 struct sticking_rule *rule;
1369 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001370 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001371
Willy Tarreau87b09662015-04-03 00:22:06 +02001372 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 +01001373 now_ms, __FUNCTION__,
1374 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001375 rep,
1376 rep->rex, rep->wex,
1377 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001378 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001379 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001380
1381 list_for_each_entry(rule, &px->storersp_rules, list) {
1382 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001383
Willy Tarreau9667a802013-12-09 12:52:13 +01001384 /* Only the first stick store-response of each table is applied
1385 * and other ones are ignored. The purpose is to allow complex
1386 * configurations which look for multiple entries by decreasing
1387 * order of precision and to stop at the first which matches.
1388 * An example could be a store of a set-cookie value, with a
1389 * fallback to a parameter found in a 302 redirect.
1390 *
1391 * The store-response rules are not allowed to override the
1392 * store-request rules for the same table, but they may coexist.
1393 * Thus we can have up to one store-request entry and one store-
1394 * response entry for the same table at any time.
1395 */
1396 for (i = nbreq; i < s->store_count; i++) {
1397 if (rule->table.t == s->store[i].table)
1398 break;
1399 }
1400
1401 /* skip existing entries for this table */
1402 if (i < s->store_count)
1403 continue;
1404
Emeric Brun1d33b292010-01-04 15:47:17 +01001405 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001406 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001407 ret = acl_pass(ret);
1408 if (rule->cond->pol == ACL_COND_UNLESS)
1409 ret = !ret;
1410 }
1411
1412 if (ret) {
1413 struct stktable_key *key;
1414
Willy Tarreau192252e2015-04-04 01:47:55 +02001415 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 +01001416 if (!key)
1417 continue;
1418
Willy Tarreau37e340c2013-12-06 23:05:21 +01001419 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001420 struct stksess *ts;
1421
1422 ts = stksess_new(rule->table.t, key);
1423 if (ts) {
1424 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001425 s->store[s->store_count++].ts = ts;
1426 }
1427 }
1428 }
1429 }
1430
1431 /* process store request and store response */
1432 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001433 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001434 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001435
Willy Tarreauc93cd162014-05-13 15:54:22 +02001436 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001437 stksess_free(s->store[i].table, s->store[i].ts);
1438 s->store[i].ts = NULL;
1439 continue;
1440 }
1441
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001442 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1443 if (ts) {
1444 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001445 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001446 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001447 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001448 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001449 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001450
1451 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001452 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001453 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001454 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001455 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001456
1457 rep->analysers &= ~an_bit;
1458 rep->analyse_exp = TICK_ETERNITY;
1459 return 1;
1460}
1461
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001462/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001463 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001464 */
1465#define UPDATE_ANALYSERS(real, list, back, flag) { \
1466 list = (((list) & ~(flag)) | ~(back)) & (real); \
1467 back = real; \
1468 if (!(list)) \
1469 break; \
1470 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1471 continue; \
1472}
1473
Willy Tarreau87b09662015-04-03 00:22:06 +02001474/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001475 * then puts it back to the wait queue in a clean state, or cleans up its
1476 * resources if it must be deleted. Returns in <next> the date the task wants
1477 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1478 * nothing too many times, the request and response buffers flags are monitored
1479 * and each function is called only if at least another function has changed at
1480 * least one flag it is interested in.
1481 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001482struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001483{
Willy Tarreau827aee92011-03-10 16:55:02 +01001484 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001485 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001486 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001487 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001488 unsigned int rq_prod_last, rq_cons_last;
1489 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001490 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001491 struct channel *req, *res;
1492 struct stream_interface *si_f, *si_b;
1493
1494 req = &s->req;
1495 res = &s->res;
1496
1497 si_f = &s->si[0];
1498 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001499
1500 //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 +01001501 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001502
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001503 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001504 if (s->txn)
1505 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001506
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001507 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001508 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1509 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001510
1511 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001512 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1513 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001514
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001515 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001516 si_f->flags |= SI_FL_DONT_WAKE;
1517 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001518
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001519 /* 1a: Check for low level timeouts if needed. We just set a flag on
1520 * stream interfaces when their timeouts have expired.
1521 */
1522 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001523 stream_int_check_timeouts(si_f);
1524 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001525
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001526 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001527 * for future reads or writes. Note: this will also concern upper layers
1528 * but we do not touch any other flag. We must be careful and correctly
1529 * detect state changes when calling them.
1530 */
1531
Willy Tarreau8f128b42014-11-28 15:07:47 +01001532 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001533
Willy Tarreau8f128b42014-11-28 15:07:47 +01001534 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1535 si_b->flags |= SI_FL_NOLINGER;
1536 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001537 }
1538
Willy Tarreau8f128b42014-11-28 15:07:47 +01001539 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1540 if (si_f->flags & SI_FL_NOHALF)
1541 si_f->flags |= SI_FL_NOLINGER;
1542 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001543 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001544
Willy Tarreau8f128b42014-11-28 15:07:47 +01001545 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001546
Willy Tarreau8f128b42014-11-28 15:07:47 +01001547 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1548 si_f->flags |= SI_FL_NOLINGER;
1549 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001550 }
1551
Willy Tarreau8f128b42014-11-28 15:07:47 +01001552 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1553 if (si_b->flags & SI_FL_NOHALF)
1554 si_b->flags |= SI_FL_NOLINGER;
1555 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001556 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001557
1558 /* Once in a while we're woken up because the task expires. But
1559 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001560 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001561 * timeout needs to be refreshed.
1562 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001563 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001564 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1565 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001566 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001567 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1568 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001569 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001570
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001571 /* below we may emit error messages so we have to ensure that we have
1572 * our buffers properly allocated.
1573 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001574 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001575 /* No buffer available, we've been subscribed to the list of
1576 * buffer waiters, let's wait for our turn.
1577 */
1578 goto update_exp_and_leave;
1579 }
1580
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001581 /* 1b: check for low-level errors reported at the stream interface.
1582 * First we check if it's a retryable error (in which case we don't
1583 * want to tell the buffer). Otherwise we report the error one level
1584 * upper by setting flags into the buffers. Note that the side towards
1585 * the client cannot have connect (hence retryable) errors. Also, the
1586 * connection setup code must be able to deal with any type of abort.
1587 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001588 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001589 if (unlikely(si_f->flags & SI_FL_ERR)) {
1590 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1591 si_shutr(si_f);
1592 si_shutw(si_f);
1593 stream_int_report_error(si_f);
1594 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001595 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001596 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001597 if (srv)
1598 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001599 if (!(s->flags & SF_ERR_MASK))
1600 s->flags |= SF_ERR_CLICL;
1601 if (!(s->flags & SF_FINST_MASK))
1602 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001603 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001604 }
1605 }
1606
Willy Tarreau8f128b42014-11-28 15:07:47 +01001607 if (unlikely(si_b->flags & SI_FL_ERR)) {
1608 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1609 si_shutr(si_b);
1610 si_shutw(si_b);
1611 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001612 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001613 if (srv)
1614 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001615 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001616 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001617 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001618 if (srv)
1619 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001620 if (!(s->flags & SF_ERR_MASK))
1621 s->flags |= SF_ERR_SRVCL;
1622 if (!(s->flags & SF_FINST_MASK))
1623 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001624 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001625 }
1626 /* note: maybe we should process connection errors here ? */
1627 }
1628
Willy Tarreau8f128b42014-11-28 15:07:47 +01001629 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001630 /* we were trying to establish a connection on the server side,
1631 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1632 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001633 if (unlikely(!sess_update_st_con_tcp(s)))
1634 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001635 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001636 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001637
1638 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1639 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1640 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1641 */
1642 }
1643
Willy Tarreau8f128b42014-11-28 15:07:47 +01001644 rq_prod_last = si_f->state;
1645 rq_cons_last = si_b->state;
1646 rp_cons_last = si_f->state;
1647 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001648
1649 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001650 /* Check for connection closure */
1651
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001652 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001653 "[%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 +01001654 now_ms, __FUNCTION__, __LINE__,
1655 t,
1656 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001657 req, res,
1658 req->rex, res->wex,
1659 req->flags, res->flags,
1660 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1661 si_f->err_type, si_b->err_type,
1662 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001663
1664 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001665 if (unlikely(si_f->state == SI_ST_DIS))
1666 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001667
1668 /* When a server-side connection is released, we have to count it and
1669 * check for pending connections on this server.
1670 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001671 if (unlikely(si_b->state == SI_ST_DIS)) {
1672 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001673 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001674 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001675 if (s->flags & SF_CURR_SESS) {
1676 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001677 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001678 }
1679 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001680 if (may_dequeue_tasks(srv, s->be))
1681 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001682 }
1683 }
1684
1685 /*
1686 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1687 * at this point.
1688 */
1689
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001690 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001691 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001692 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1693 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1694 si_f->state != rq_prod_last ||
1695 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001696 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001697 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001698
Willy Tarreau8f128b42014-11-28 15:07:47 +01001699 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001700 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001701 unsigned int ana_list;
1702 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001703
Willy Tarreau90deb182010-01-07 00:20:41 +01001704 /* it's up to the analysers to stop new connections,
1705 * disable reading or closing. Note: if an analyser
1706 * disables any of these bits, it is responsible for
1707 * enabling them again when it disables itself, so
1708 * that other analysers are called in similar conditions.
1709 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001710 channel_auto_read(req);
1711 channel_auto_connect(req);
1712 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001713
1714 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001716 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001717 * the list when not needed. Any analyser may return 0
1718 * to break out of the loop, either because of missing
1719 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001720 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001721 * analyser, and we may loop again if other analysers
1722 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001723 *
1724 * We build a list of analysers to run. We evaluate all
1725 * of these analysers in the order of the lower bit to
1726 * the higher bit. This ordering is very important.
1727 * An analyser will often add/remove other analysers,
1728 * including itself. Any changes to itself have no effect
1729 * on the loop. If it removes any other analysers, we
1730 * want those analysers not to be called anymore during
1731 * this loop. If it adds an analyser that is located
1732 * after itself, we want it to be scheduled for being
1733 * processed during the loop. If it adds an analyser
1734 * which is located before it, we want it to switch to
1735 * it immediately, even if it has already been called
1736 * once but removed since.
1737 *
1738 * In order to achieve this, we compare the analyser
1739 * list after the call with a copy of it before the
1740 * call. The work list is fed with analyser bits that
1741 * appeared during the call. Then we compare previous
1742 * work list with the new one, and check the bits that
1743 * appeared. If the lowest of these bits is lower than
1744 * the current bit, it means we have enabled a previous
1745 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001746 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001747
Willy Tarreau8f128b42014-11-28 15:07:47 +01001748 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001749 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001750 /* Warning! ensure that analysers are always placed in ascending order! */
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001751
Willy Tarreaufb356202010-08-03 14:02:05 +02001752 if (ana_list & AN_REQ_INSPECT_FE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001753 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001754 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001755 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001756 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001757
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001758 if (ana_list & AN_REQ_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001759 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001760 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001761 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001762 }
1763
Willy Tarreau748179e2015-05-01 21:52:31 +02001764 if (ana_list & AN_REQ_HTTP_BODY) {
1765 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1766 break;
1767 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1768 }
1769
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001770 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001771 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001772 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001773 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001774 }
1775
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001776 if (ana_list & AN_REQ_SWITCHING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001777 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001778 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001779 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001780 }
1781
Willy Tarreaufb356202010-08-03 14:02:05 +02001782 if (ana_list & AN_REQ_INSPECT_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001783 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001784 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001785 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001786 }
1787
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001788 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001790 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001791 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001792 }
1793
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001794 if (ana_list & AN_REQ_HTTP_TARPIT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001795 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001796 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001797 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001798 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001799
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001800 if (ana_list & AN_REQ_SRV_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001801 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001802 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001803 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001804 }
1805
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001806 if (ana_list & AN_REQ_HTTP_INNER) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001807 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001808 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001809 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001810 }
1811
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001812 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001813 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001814 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001815 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001816 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001817
Emeric Brun1d33b292010-01-04 15:47:17 +01001818 if (ana_list & AN_REQ_STICKING_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001820 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001821 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001822 }
1823
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001824 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001825 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001826 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001827 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001828 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001829 break;
1830 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001831 }
Willy Tarreau84455332009-03-15 22:34:05 +01001832
Willy Tarreau8f128b42014-11-28 15:07:47 +01001833 rq_prod_last = si_f->state;
1834 rq_cons_last = si_b->state;
1835 req->flags &= ~CF_WAKE_ONCE;
1836 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001837
Willy Tarreau8f128b42014-11-28 15:07:47 +01001838 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001839 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001840 }
1841
Willy Tarreau576507f2010-01-07 00:09:04 +01001842 /* we'll monitor the request analysers while parsing the response,
1843 * because some response analysers may indirectly enable new request
1844 * analysers (eg: HTTP keep-alive).
1845 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001847
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001848 resync_response:
1849 /* Analyse response */
1850
Willy Tarreau8f128b42014-11-28 15:07:47 +01001851 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1852 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1853 si_f->state != rp_cons_last ||
1854 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001855 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001856 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001857
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 if ((res->flags & CF_MASK_ANALYSER) &&
1859 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001860 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1861 * for some free space in the response buffer, so we might need to call
1862 * it when something changes in the response buffer, but still we pass
1863 * it the request buffer. Note that the SI state might very well still
1864 * be zero due to us returning a flow of redirects!
1865 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001866 res->analysers &= ~AN_REQ_ALL;
1867 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001868 }
1869
Willy Tarreau8f128b42014-11-28 15:07:47 +01001870 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001871 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001872 unsigned int ana_list;
1873 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001874
Willy Tarreau90deb182010-01-07 00:20:41 +01001875 /* it's up to the analysers to stop disable reading or
1876 * closing. Note: if an analyser disables any of these
1877 * bits, it is responsible for enabling them again when
1878 * it disables itself, so that other analysers are called
1879 * in similar conditions.
1880 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001881 channel_auto_read(res);
1882 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001883
1884 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001885 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001886 * to MSB. The analysers must remove themselves from
1887 * the list when not needed. Any analyser may return 0
1888 * to break out of the loop, either because of missing
1889 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001890 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001891 * analyser, and we may loop again if other analysers
1892 * are added in the middle.
1893 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001894
Willy Tarreau8f128b42014-11-28 15:07:47 +01001895 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001896 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001897 /* Warning! ensure that analysers are always placed in ascending order! */
1898
Emeric Brun97679e72010-09-23 17:56:44 +02001899 if (ana_list & AN_RES_INSPECT) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001900 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001901 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001902 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001903 }
1904
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001905 if (ana_list & AN_RES_WAIT_HTTP) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001906 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001907 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001908 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001909 }
1910
Emeric Brun1d33b292010-01-04 15:47:17 +01001911 if (ana_list & AN_RES_STORE_RULES) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001912 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001913 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001914 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001915 }
1916
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001917 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001918 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001919 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001920 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001921 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001922
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001923 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001924 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001925 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001926 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001927 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001928 break;
1929 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001930 }
1931
Willy Tarreau8f128b42014-11-28 15:07:47 +01001932 rp_cons_last = si_f->state;
1933 rp_prod_last = si_b->state;
1934 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001935
Willy Tarreau8f128b42014-11-28 15:07:47 +01001936 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001937 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001938 }
1939
Willy Tarreau576507f2010-01-07 00:09:04 +01001940 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001941 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001942 goto resync_request;
1943
Willy Tarreau8f128b42014-11-28 15:07:47 +01001944 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001945 goto resync_request;
1946
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001947 /* FIXME: here we should call protocol handlers which rely on
1948 * both buffers.
1949 */
1950
1951
1952 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001953 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001954 * we're just in a data phase here since it means we have not
1955 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001956 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001957 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001958 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001959 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001960 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001961 req->analysers = 0;
1962 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001963 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001964 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001965 if (srv)
1966 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001967 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001968 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001969 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001970 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001971 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001972 if (srv)
1973 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001974 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001975 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001976 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001977 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001978 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001979 if (srv)
1980 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001981 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001982 }
1983 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001984 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001985 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001986 if (srv)
1987 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001988 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001989 }
Willy Tarreau84455332009-03-15 22:34:05 +01001990 sess_set_term_flags(s);
1991 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001992 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001993 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001994 res->analysers = 0;
1995 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001996 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001997 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001998 if (srv)
1999 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002000 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002001 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002002 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002003 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002004 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002005 if (srv)
2006 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002007 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002008 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002009 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002010 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002011 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002012 if (srv)
2013 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002014 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002015 }
2016 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002017 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002018 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002019 if (srv)
2020 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002021 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002022 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002023 sess_set_term_flags(s);
2024 }
Willy Tarreau84455332009-03-15 22:34:05 +01002025 }
2026
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002027 /*
2028 * Here we take care of forwarding unhandled data. This also includes
2029 * connection establishments and shutdown requests.
2030 */
2031
2032
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002033 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002034 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002035 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002036 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002037 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002038 if (unlikely(!req->analysers &&
2039 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2040 (si_f->state >= SI_ST_EST) &&
2041 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002042 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002043 * attached to it. If any data are left in, we'll permit them to
2044 * move.
2045 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002046 channel_auto_read(req);
2047 channel_auto_connect(req);
2048 channel_auto_close(req);
2049 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002050
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002051 /* We'll let data flow between the producer (if still connected)
2052 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002053 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002054 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2055 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02002056
2057 /* Just in order to support fetching HTTP contents after start
2058 * of forwarding when the HTTP forwarding analyser is not used,
2059 * we simply reset msg->sov so that HTTP rewinding points to the
2060 * headers.
2061 */
2062 if (s->txn)
2063 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002064 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002065
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002066 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002067 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2068 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002069 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002070 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2071 (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 +01002072 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002073 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2074 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002075 (req->flags & CF_STREAMER_FAST)))) {
2076 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002077 }
2078
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002079 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002080 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002081
2082 /*
2083 * Now forward all shutdown requests between both sides of the buffer
2084 */
2085
Willy Tarreau520d95e2009-09-19 21:04:57 +02002086 /* first, let's check if the request buffer needs to shutdown(write), which may
2087 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002088 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2089 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002090 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002091 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002092 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002093 channel_shutw_now(req);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002094 if (tick_isset(sess->fe->timeout.clientfin)) {
2095 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002096 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002097 }
2098 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002099
2100 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002101 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2102 channel_is_empty(req))) {
2103 if (req->flags & CF_READ_ERROR)
2104 si_b->flags |= SI_FL_NOLINGER;
2105 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002106 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002107 res->rto = s->be->timeout.serverfin;
2108 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002109 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002110 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002111
2112 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002113 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2114 !req->analysers))
2115 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002116
2117 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002118 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2119 if (si_f->flags & SI_FL_NOHALF)
2120 si_f->flags |= SI_FL_NOLINGER;
2121 si_shutr(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002122 if (tick_isset(sess->fe->timeout.clientfin)) {
2123 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002124 res->wex = tick_add(now_ms, res->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002125 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002126 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002127
Willy Tarreau520d95e2009-09-19 21:04:57 +02002128 /* it's possible that an upper layer has requested a connection setup or abort.
2129 * There are 2 situations where we decide to establish a new connection :
2130 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002131 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002132 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002133 if (si_b->state == SI_ST_INI) {
2134 if (!(req->flags & CF_SHUTW)) {
2135 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002136 /* If we have an appctx, there is no connect method, so we
2137 * immediately switch to the connected state, otherwise we
2138 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002139 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002140 si_b->state = SI_ST_REQ; /* new connection requested */
2141 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002142 }
Willy Tarreau73201222009-08-16 18:27:24 +02002143 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002144 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002145 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2146 channel_shutw_now(req); /* fix buffer flags upon abort */
2147 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002148 }
Willy Tarreau92795622009-03-06 12:51:23 +01002149 }
2150
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002151
2152 /* we may have a pending connection request, or a connection waiting
2153 * for completion.
2154 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002155 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002156
2157 /* prune the request variables and swap to the response variables. */
2158 if (s->vars_reqres.scope != SCOPE_RES) {
2159 vars_prune(&s->vars_reqres, s);
2160 vars_init(&s->vars_reqres, SCOPE_RES);
2161 }
2162
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002163 do {
2164 /* nb: step 1 might switch from QUE to ASS, but we first want
2165 * to give a chance to step 2 to perform a redirect if needed.
2166 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002167 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002168 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002169 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002170 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002171
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002172 /* applets directly go to the ESTABLISHED state. Similarly,
2173 * servers experience the same fate when their connection
2174 * is reused.
2175 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002176 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002177 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002178
Willy Tarreaub44c8732013-12-31 23:16:50 +01002179 /* Now we can add the server name to a header (if requested) */
2180 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002181 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002182 (s->be->server_id_hdr_name != NULL) &&
2183 (s->be->mode == PR_MODE_HTTP) &&
2184 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002185 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002186 }
2187
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002188 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002189 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002190 http_perform_server_redirect(s, si_b);
2191 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002192 }
2193
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002194 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002195 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002196 goto resync_stream_interface;
2197
Willy Tarreau815a9b22010-07-27 17:15:12 +02002198 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002199 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002200 goto resync_request;
2201
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002202 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002203
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002204 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002205 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002206 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002207 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002208 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002209 if (unlikely(!res->analysers &&
2210 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2211 (si_b->state >= SI_ST_EST) &&
2212 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002213 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002214 * attached to it. If any data are left in, we'll permit them to
2215 * move.
2216 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002217 channel_auto_read(res);
2218 channel_auto_close(res);
2219 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002220
2221 /* We'll let data flow between the producer (if still connected)
2222 * to the consumer.
2223 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002224 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2225 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002226
Willy Tarreau42529c32015-07-09 18:38:57 +02002227 /* Just in order to support fetching HTTP contents after start
2228 * of forwarding when the HTTP forwarding analyser is not used,
2229 * we simply reset msg->sov so that HTTP rewinding points to the
2230 * headers.
2231 */
2232 if (s->txn)
2233 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2234
Willy Tarreauce887fd2012-05-12 12:50:00 +02002235 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002236 * tunnel timeout set, use it now. Note that we must respect
2237 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002238 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002239 if (!req->analysers && s->be->timeout.tunnel) {
2240 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002241 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002242
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002243 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2244 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002245 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2246 res->rto = s->be->timeout.serverfin;
2247 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2248 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002249 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2250 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002251
Willy Tarreau8f128b42014-11-28 15:07:47 +01002252 req->rex = tick_add(now_ms, req->rto);
2253 req->wex = tick_add(now_ms, req->wto);
2254 res->rex = tick_add(now_ms, res->rto);
2255 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002256 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002257 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002258
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002259 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002260 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2261 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002262 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002263 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2264 (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 +01002265 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002266 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2267 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002268 (res->flags & CF_STREAMER_FAST)))) {
2269 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002270 }
2271
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002272 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002273 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002274
2275 /*
2276 * Now forward all shutdown requests between both sides of the buffer
2277 */
2278
2279 /*
2280 * FIXME: this is probably where we should produce error responses.
2281 */
2282
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002283 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002284 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002285 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002287 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002288 req->wto = s->be->timeout.serverfin;
2289 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002290 }
2291 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002292
2293 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002294 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2295 channel_is_empty(res))) {
2296 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002297 if (tick_isset(sess->fe->timeout.clientfin)) {
2298 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002300 }
2301 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002302
2303 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002304 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2305 !res->analysers)
2306 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002307
2308 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002309 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2310 if (si_b->flags & SI_FL_NOHALF)
2311 si_b->flags |= SI_FL_NOLINGER;
2312 si_shutr(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002313 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002314 req->wto = s->be->timeout.serverfin;
2315 req->wex = tick_add(now_ms, req->wto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002316 }
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002317 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002318
Willy Tarreau8f128b42014-11-28 15:07:47 +01002319 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002320 goto resync_stream_interface;
2321
Willy Tarreau8f128b42014-11-28 15:07:47 +01002322 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002323 goto resync_request;
2324
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002326 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002327
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002328 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002329 si_f->flags &= ~SI_FL_DONT_WAKE;
2330 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002331
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002332 /* This is needed only when debugging is enabled, to indicate
2333 * client-side or server-side close. Please note that in the unlikely
2334 * event where both sides would close at once, the sequence is reported
2335 * on the server side first.
2336 */
2337 if (unlikely((global.mode & MODE_DEBUG) &&
2338 (!(global.mode & MODE_QUIET) ||
2339 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 if (si_b->state == SI_ST_CLO &&
2341 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002342 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002343 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002344 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2345 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002346 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002347 }
2348
Willy Tarreau8f128b42014-11-28 15:07:47 +01002349 if (si_f->state == SI_ST_CLO &&
2350 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002351 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002352 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002353 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2354 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002355 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002356 }
2357 }
2358
Willy Tarreau8f128b42014-11-28 15:07:47 +01002359 if (likely((si_f->state != SI_ST_CLO) ||
2360 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002361
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002362 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002363 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002364
Willy Tarreau563cc372015-04-19 18:13:56 +02002365 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002366 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002367
Willy Tarreau563cc372015-04-19 18:13:56 +02002368 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002369 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002370
Willy Tarreau8f128b42014-11-28 15:07:47 +01002371 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2372 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2373 si_f->prev_state = si_f->state;
2374 si_b->prev_state = si_b->state;
2375 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2376 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002377
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002378 /* Trick: if a request is being waiting for the server to respond,
2379 * and if we know the server can timeout, we don't want the timeout
2380 * to expire on the client side first, but we're still interested
2381 * in passing data from the client to the server (eg: POST). Thus,
2382 * we can cancel the client's request timeout if the server's
2383 * request timeout is set and the server has not yet sent a response.
2384 */
2385
Willy Tarreau8f128b42014-11-28 15:07:47 +01002386 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2387 (tick_isset(req->wex) || tick_isset(res->rex))) {
2388 req->flags |= CF_READ_NOEXP;
2389 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002390 }
2391
Willy Tarreau798f4322012-11-08 14:49:17 +01002392 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002393 t->expire = tick_first(tick_first(req->rex, req->wex),
2394 tick_first(res->rex, res->wex));
2395 if (req->analysers)
2396 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002397
Willy Tarreau8f128b42014-11-28 15:07:47 +01002398 if (si_f->exp)
2399 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002400
Willy Tarreau8f128b42014-11-28 15:07:47 +01002401 if (si_b->exp)
2402 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002403
2404#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002405 fprintf(stderr,
2406 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2407 " 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 +01002408 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2409 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002410#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002411
2412#ifdef DEBUG_DEV
2413 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002414 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002415 ABORT_NOW();
2416#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002417 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002418 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002419 }
2420
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002421 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002422 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002423 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002424 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002425 if (sess->listener) {
2426 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002427 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002428 sess->listener->nbconn--;
2429 if (sess->listener->state == LI_FULL)
2430 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002431
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002432 /* Dequeues all of the listeners waiting for a resource */
2433 if (!LIST_ISEMPTY(&global_listener_queue))
2434 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002435
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002436 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2437 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2438 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002439 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002440
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002441 if (unlikely((global.mode & MODE_DEBUG) &&
2442 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002443 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002444 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002445 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2446 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002447 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002448 }
2449
2450 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002451 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002452
Willy Tarreaueee5b512015-04-03 23:46:31 +02002453 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002454 int n;
2455
Willy Tarreaueee5b512015-04-03 23:46:31 +02002456 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002457 if (n < 1 || n > 5)
2458 n = 0;
2459
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002460 if (sess->fe->mode == PR_MODE_HTTP) {
2461 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002462 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002463 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002464 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002465 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002466 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002467 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002468 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002469 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002470 s->be->be_counters.p.http.comp_rsp++;
2471 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002472 }
2473
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002474 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002475 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002476 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002477 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002478 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002479 }
2480
Willy Tarreau87b09662015-04-03 00:22:06 +02002481 /* update time stats for this stream */
2482 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002483
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002484 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002485 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002486 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002487 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002488 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002489}
2490
Willy Tarreau87b09662015-04-03 00:22:06 +02002491/* Update the stream's backend and server time stats */
2492void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002493{
2494 int t_request;
2495 int t_queue;
2496 int t_connect;
2497 int t_data;
2498 int t_close;
2499 struct server *srv;
2500
2501 t_request = 0;
2502 t_queue = s->logs.t_queue;
2503 t_connect = s->logs.t_connect;
2504 t_close = s->logs.t_close;
2505 t_data = s->logs.t_data;
2506
2507 if (s->be->mode != PR_MODE_HTTP)
2508 t_data = t_connect;
2509
2510 if (t_connect < 0 || t_data < 0)
2511 return;
2512
2513 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2514 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2515
2516 t_data -= t_connect;
2517 t_connect -= t_queue;
2518 t_queue -= t_request;
2519
2520 srv = objt_server(s->target);
2521 if (srv) {
2522 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2523 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2524 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2525 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2526 }
2527 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2528 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2529 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2530 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2531}
2532
Willy Tarreau7c669d72008-06-20 15:04:11 +02002533/*
2534 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002535 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002536 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002537 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002538 * server.
2539 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002540void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002541{
2542 if (sess->srv_conn == newsrv)
2543 return;
2544
2545 if (sess->srv_conn) {
2546 sess->srv_conn->served--;
2547 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2548 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002549 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002550 }
2551
2552 if (newsrv) {
2553 newsrv->served++;
2554 if (newsrv->proxy->lbprm.server_take_conn)
2555 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002556 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002557 }
2558}
2559
Willy Tarreau84455332009-03-15 22:34:05 +01002560/* Handle server-side errors for default protocols. It is called whenever a a
2561 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002562 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002563 * them. It's installed as ->srv_error for the server-side stream_interface.
2564 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002565void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002566{
2567 int err_type = si->err_type;
2568 int err = 0, fin = 0;
2569
2570 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002571 err = SF_ERR_CLICL;
2572 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002573 }
2574 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002575 err = SF_ERR_CLICL;
2576 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002577 }
2578 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002579 err = SF_ERR_SRVTO;
2580 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002581 }
2582 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002583 err = SF_ERR_SRVCL;
2584 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002585 }
2586 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002587 err = SF_ERR_SRVTO;
2588 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002589 }
2590 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002591 err = SF_ERR_SRVCL;
2592 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002593 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002594 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002595 err = SF_ERR_RESOURCE;
2596 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002597 }
Willy Tarreau84455332009-03-15 22:34:05 +01002598 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002599 err = SF_ERR_INTERNAL;
2600 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002601 }
2602
Willy Tarreaue7dff022015-04-03 01:14:29 +02002603 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002604 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002605 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002606 s->flags |= fin;
2607}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002608
Willy Tarreaue7dff022015-04-03 01:14:29 +02002609/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002610void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002611{
Willy Tarreau87b09662015-04-03 00:22:06 +02002612 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002613 return;
2614
Willy Tarreau87b09662015-04-03 00:22:06 +02002615 channel_shutw_now(&stream->req);
2616 channel_shutr_now(&stream->res);
2617 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002618 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002619 stream->flags |= why;
2620 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002621}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002622
Willy Tarreau8b22a712010-06-18 17:46:06 +02002623/************************************************************************/
2624/* All supported ACL keywords must be declared here. */
2625/************************************************************************/
2626
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002627/* Returns a pointer to a stkctr depending on the fetch keyword name.
2628 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002629 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002630 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002631 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002632 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002633 * NULL may be returned if the designated stkctr is not tracked. For
2634 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2635 * passed. When present, the currently tracked key is then looked up
2636 * in the specified table instead of the current table. The purpose is
2637 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002638 * multiple tables). <strm> is allowed to be NULL, in which case only
2639 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002640 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002641struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002642smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002643{
2644 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002645 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002646 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002647 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002648 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002649
Willy Tarreau0f791d42013-07-23 19:56:43 +02002650 if (num == '_' - '0') {
2651 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002652 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002653 if (num >= MAX_SESS_STKCTR)
2654 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002655 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002656 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002657 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002658 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002659 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002660
2661 if (!conn)
2662 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002663
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002664 /* Fetch source adress in a sample. */
2665 smp.px = NULL;
2666 smp.sess = sess;
2667 smp.strm = strm;
2668 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2669 return NULL;
2670
2671 /* Converts into key. */
2672 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002673 if (!key)
2674 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002675
Willy Tarreaua65536c2013-07-22 22:40:11 +02002676 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002677 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002678 return &stkctr;
2679 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002680
2681 /* Here, <num> contains the counter number from 0 to 9 for
2682 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002683 * args[arg] is the first optional argument. We first lookup the
2684 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002685 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002686
2687 stkptr = &strm->stkctr[num];
2688 if (!strm || !stkctr_entry(stkptr)) {
2689 stkptr = &sess->stkctr[num];
2690 if (!stkctr_entry(stkptr))
2691 return NULL;
2692 }
2693
2694 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002695 if (!stksess)
2696 return NULL;
2697
Willy Tarreau0f791d42013-07-23 19:56:43 +02002698 if (unlikely(args[arg].type == ARGT_TAB)) {
2699 /* an alternate table was specified, let's look up the same key there */
2700 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002701 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002702 return &stkctr;
2703 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002704 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002705}
2706
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002707/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2708 * the entry if it doesn't exist yet. This is needed for a few fetch
2709 * functions which need to create an entry, such as src_inc_gpc* and
2710 * src_clr_gpc*.
2711 */
2712struct stkctr *
2713smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2714{
2715 static struct stkctr stkctr;
2716 struct stktable_key *key;
2717 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002718 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002719
2720 if (strncmp(kw, "src_", 4) != 0)
2721 return NULL;
2722
2723 if (!conn)
2724 return NULL;
2725
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002726 /* Fetch source adress in a sample. */
2727 smp.px = NULL;
2728 smp.sess = sess;
2729 smp.strm = strm;
2730 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2731 return NULL;
2732
2733 /* Converts into key. */
2734 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002735 if (!key)
2736 return NULL;
2737
2738 stkctr.table = &args->data.prx->table;
2739 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2740 return &stkctr;
2741}
2742
Willy Tarreau87b09662015-04-03 00:22:06 +02002743/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002744 * currently being tracked or not.
2745 * Supports being called as "sc[0-9]_tracked" only.
2746 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002747static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002748smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002749{
2750 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002751 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002752 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002753 return 1;
2754}
2755
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002756/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2757 * frontend counters or from the src.
2758 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2759 * zero is returned if the key is new.
2760 */
2761static int
2762smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2763{
2764 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2765
2766 if (!stkctr)
2767 return 0;
2768
2769 smp->flags = SMP_F_VOL_TEST;
2770 smp->data.type = SMP_T_SINT;
2771 smp->data.u.sint = 0;
2772
2773 if (stkctr_entry(stkctr) != NULL) {
2774 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2775 if (!ptr)
2776 return 0; /* parameter not stored */
2777 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2778 }
2779 return 1;
2780}
2781
Willy Tarreau87b09662015-04-03 00:22:06 +02002782/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002783 * frontend counters or from the src.
2784 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2785 * zero is returned if the key is new.
2786 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002787static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002788smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002789{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002790 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002791
2792 if (!stkctr)
2793 return 0;
2794
Willy Tarreau37406352012-04-23 16:16:37 +02002795 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002796 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002797 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002798
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002799 if (stkctr_entry(stkctr) != NULL) {
2800 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002801 if (!ptr)
2802 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002803 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002804 }
2805 return 1;
2806}
2807
Willy Tarreau87b09662015-04-03 00:22:06 +02002808/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002809 * tracked frontend counters or from the src.
2810 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2811 * Value zero is returned if the key is new.
2812 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002813static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002814smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002815{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002816 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002817
2818 if (!stkctr)
2819 return 0;
2820
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002821 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002822 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002823 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002824 if (stkctr_entry(stkctr) != NULL) {
2825 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002826 if (!ptr)
2827 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002828 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002829 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002830 }
2831 return 1;
2832}
2833
Willy Tarreau87b09662015-04-03 00:22:06 +02002834/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002835 * frontend counters and return it into temp integer.
2836 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002837 */
2838static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002839smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002840{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002841 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002842
2843 if (!stkctr)
2844 return 0;
2845
Willy Tarreau37406352012-04-23 16:16:37 +02002846 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002847 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002848 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002849
2850 if (stkctr_entry(stkctr) == NULL)
2851 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2852
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002853 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002854 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002855
2856 /* First, update gpc0_rate if it's tracked. Second, update its
2857 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2858 */
Emeric Brun57056f02015-06-15 18:29:57 +02002859 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2860 if (ptr1) {
2861 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002862 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002863 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002864 }
2865
Emeric Brun57056f02015-06-15 18:29:57 +02002866 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2867 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002868 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002869
Emeric Brun57056f02015-06-15 18:29:57 +02002870 /* If data was modified, we need to touch to re-schedule sync */
2871 if (ptr1 || ptr2)
2872 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002873 }
2874 return 1;
2875}
2876
Willy Tarreau87b09662015-04-03 00:22:06 +02002877/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002878 * frontend counters and return its previous value into temp integer.
2879 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002880 */
2881static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002882smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002883{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002884 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002885
2886 if (!stkctr)
2887 return 0;
2888
Willy Tarreau37406352012-04-23 16:16:37 +02002889 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002890 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002891 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002892
2893 if (stkctr_entry(stkctr) == NULL)
2894 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2895
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002896 if (stkctr_entry(stkctr) != NULL) {
2897 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002898 if (!ptr)
2899 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002900 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002901 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002902 /* If data was modified, we need to touch to re-schedule sync */
2903 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002904 }
2905 return 1;
2906}
2907
Willy Tarreau87b09662015-04-03 00:22:06 +02002908/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002909 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2910 * "src_conn_cnt" only.
2911 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002912static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002913smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002914{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002915 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002916
2917 if (!stkctr)
2918 return 0;
2919
Willy Tarreau37406352012-04-23 16:16:37 +02002920 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002921 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002922 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002923 if (stkctr_entry(stkctr) != NULL) {
2924 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002925 if (!ptr)
2926 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002927 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002928 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002929 return 1;
2930}
2931
Willy Tarreau87b09662015-04-03 00:22:06 +02002932/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002933 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2934 * only.
2935 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002936static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002937smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002938{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002939 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002940
2941 if (!stkctr)
2942 return 0;
2943
Willy Tarreau37406352012-04-23 16:16:37 +02002944 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002945 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002946 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002947 if (stkctr_entry(stkctr) != NULL) {
2948 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002949 if (!ptr)
2950 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002951 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002952 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002953 }
2954 return 1;
2955}
2956
Willy Tarreau87b09662015-04-03 00:22:06 +02002957/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002958 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002959 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002960 */
2961static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002962smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002963{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002964 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002965 struct stksess *ts;
2966 struct stktable_key *key;
2967 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002968 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002969
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002970 if (!conn)
2971 return 0;
2972
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002973 /* Fetch source adress in a sample. */
2974 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2975 return 0;
2976
2977 /* Converts into key. */
2978 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002979 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002980 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002981
Willy Tarreau24e32d82012-04-23 23:55:44 +02002982 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002983
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002984 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2985 /* entry does not exist and could not be created */
2986 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002987
2988 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2989 if (!ptr)
2990 return 0; /* parameter not stored in this table */
2991
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002992 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002993 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002994 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002995 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002996 return 1;
2997}
2998
Willy Tarreau87b09662015-04-03 00:22:06 +02002999/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02003000 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3001 * "src_conn_cur" only.
3002 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003003static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003004smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003005{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003006 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003007
3008 if (!stkctr)
3009 return 0;
3010
Willy Tarreau37406352012-04-23 16:16:37 +02003011 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003012 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003013 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003014 if (stkctr_entry(stkctr) != NULL) {
3015 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003016 if (!ptr)
3017 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003018 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003019 }
3020 return 1;
3021}
3022
Willy Tarreau87b09662015-04-03 00:22:06 +02003023/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003024 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3025 * "src_sess_cnt" only.
3026 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003027static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003028smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003029{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003030 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003031
3032 if (!stkctr)
3033 return 0;
3034
Willy Tarreau37406352012-04-23 16:16:37 +02003035 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003036 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003037 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003038 if (stkctr_entry(stkctr) != NULL) {
3039 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003040 if (!ptr)
3041 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003042 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003043 }
3044 return 1;
3045}
3046
Willy Tarreau87b09662015-04-03 00:22:06 +02003047/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003048 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3049 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003050static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003051smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003052{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003053 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003054
3055 if (!stkctr)
3056 return 0;
3057
Willy Tarreau37406352012-04-23 16:16:37 +02003058 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003059 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003060 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003061 if (stkctr_entry(stkctr) != NULL) {
3062 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003063 if (!ptr)
3064 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003065 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003066 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003067 }
3068 return 1;
3069}
3070
Willy Tarreau87b09662015-04-03 00:22:06 +02003071/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003072 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3073 * "src_http_req_cnt" only.
3074 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003075static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003076smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003077{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003078 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003079
3080 if (!stkctr)
3081 return 0;
3082
Willy Tarreau37406352012-04-23 16:16:37 +02003083 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003084 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003085 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003086 if (stkctr_entry(stkctr) != NULL) {
3087 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003088 if (!ptr)
3089 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003090 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003091 }
3092 return 1;
3093}
3094
Willy Tarreau87b09662015-04-03 00:22:06 +02003095/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003096 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3097 * "src_http_req_rate" only.
3098 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003099static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003100smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003101{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003102 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003103
3104 if (!stkctr)
3105 return 0;
3106
Willy Tarreau37406352012-04-23 16:16:37 +02003107 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003108 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003109 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003110 if (stkctr_entry(stkctr) != NULL) {
3111 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003112 if (!ptr)
3113 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003114 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003115 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003116 }
3117 return 1;
3118}
3119
Willy Tarreau87b09662015-04-03 00:22:06 +02003120/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003121 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3122 * "src_http_err_cnt" only.
3123 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003124static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003125smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003126{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003127 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003128
3129 if (!stkctr)
3130 return 0;
3131
Willy Tarreau37406352012-04-23 16:16:37 +02003132 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003133 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003134 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003135 if (stkctr_entry(stkctr) != NULL) {
3136 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003137 if (!ptr)
3138 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003139 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003140 }
3141 return 1;
3142}
3143
Willy Tarreau87b09662015-04-03 00:22:06 +02003144/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003145 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3146 * "src_http_err_rate" only.
3147 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003148static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003149smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003150{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003151 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003152
3153 if (!stkctr)
3154 return 0;
3155
Willy Tarreau37406352012-04-23 16:16:37 +02003156 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003157 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003158 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003159 if (stkctr_entry(stkctr) != NULL) {
3160 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003161 if (!ptr)
3162 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003163 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003164 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003165 }
3166 return 1;
3167}
3168
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003169/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003170 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003171 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3172 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003173static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003174smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003175{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003176 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003177
3178 if (!stkctr)
3179 return 0;
3180
Willy Tarreau37406352012-04-23 16:16:37 +02003181 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003182 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003183 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003184 if (stkctr_entry(stkctr) != NULL) {
3185 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003186 if (!ptr)
3187 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003188 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003189 }
3190 return 1;
3191}
3192
Willy Tarreau613fe992013-07-23 17:39:19 +02003193/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003194 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003195 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003196 */
3197static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003198smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003199{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003200 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003201
3202 if (!stkctr)
3203 return 0;
3204
Willy Tarreau37406352012-04-23 16:16:37 +02003205 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003206 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003207 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003208 if (stkctr_entry(stkctr) != NULL) {
3209 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003210 if (!ptr)
3211 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003212 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003213 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003214 }
3215 return 1;
3216}
3217
Willy Tarreau53aea102013-07-23 17:39:02 +02003218/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003219 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003220 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3221 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003222static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003223smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003224{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003225 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003226
3227 if (!stkctr)
3228 return 0;
3229
Willy Tarreau37406352012-04-23 16:16:37 +02003230 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003231 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003232 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003233 if (stkctr_entry(stkctr) != NULL) {
3234 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003235 if (!ptr)
3236 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003237 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003238 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003239 return 1;
3240}
3241
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003242/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003243 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003244 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003245 */
3246static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003247smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003248{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003249 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003250
3251 if (!stkctr)
3252 return 0;
3253
Willy Tarreau37406352012-04-23 16:16:37 +02003254 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003255 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003256 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003257 if (stkctr_entry(stkctr) != NULL) {
3258 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003259 if (!ptr)
3260 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003261 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003262 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003263 }
3264 return 1;
3265}
3266
Willy Tarreau87b09662015-04-03 00:22:06 +02003267/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003268 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3269 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003270static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003271smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003272{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003273 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003274
Willy Tarreau563eef42013-07-23 18:32:02 +02003275 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003276 return 0;
3277
Willy Tarreau563eef42013-07-23 18:32:02 +02003278 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003279 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003280 smp->data.u.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003281 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003282}
3283
Willy Tarreau34db1082012-04-19 17:16:54 +02003284/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003285 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003286 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003287static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003288smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003289{
Willy Tarreau37406352012-04-23 16:16:37 +02003290 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003291 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003292 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003293 return 1;
3294}
3295
Willy Tarreau34db1082012-04-19 17:16:54 +02003296/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003297 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003298 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003299static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003300smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003301{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003302 struct proxy *px;
3303
Willy Tarreau24e32d82012-04-23 23:55:44 +02003304 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003305 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003306 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003307 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003308 return 1;
3309}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003310
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003311/* 0=OK, <0=Alert, >0=Warning */
3312static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3313 struct proxy *px, struct act_rule *rule,
3314 char **err)
3315{
3316 struct action_kw *kw;
3317
3318 /* Check if the service name exists. */
3319 if (*(args[*cur_arg]) == 0) {
3320 memprintf(err, "'%s' expects a service name.", args[0]);
3321 return -1;
3322 }
3323
3324 /* lookup for keyword corresponding to a service. */
3325 kw = action_lookup(&service_keywords, args[*cur_arg]);
3326 if (!kw) {
3327 memprintf(err, "'%s' unknown service name.", args[1]);
3328 return ACT_RET_PRS_ERR;
3329 }
3330 (*cur_arg)++;
3331
3332 /* executes specific rule parser. */
3333 rule->kw = kw;
3334 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3335 return ACT_RET_PRS_ERR;
3336
3337 /* Register processing function. */
3338 rule->action_ptr = process_use_service;
3339 rule->action = ACT_CUSTOM;
3340
3341 return ACT_RET_PRS_OK;
3342}
3343
3344void service_keywords_register(struct action_kw_list *kw_list)
3345{
3346 LIST_ADDQ(&service_keywords, &kw_list->list);
3347}
3348
3349/* main configuration keyword registration. */
3350static struct action_kw_list stream_tcp_keywords = { ILH, {
3351 { "use-service", stream_parse_use_service },
3352 { /* END */ }
3353}};
3354
3355static struct action_kw_list stream_http_keywords = { ILH, {
3356 { "use-service", stream_parse_use_service },
3357 { /* END */ }
3358}};
3359
Willy Tarreau61612d42012-04-19 18:42:05 +02003360/* Note: must not be declared <const> as its list will be overwritten.
3361 * Please take care of keeping this list alphabetically sorted.
3362 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003363static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003364 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003365}};
3366
Willy Tarreau281c7992013-01-08 01:23:27 +01003367/* Note: must not be declared <const> as its list will be overwritten.
3368 * Please take care of keeping this list alphabetically sorted.
3369 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003370static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003371 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3372 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3373 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3374 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3375 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3376 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003377 { "sc_get_gpt0", smp_fetch_sc_get_gpt0, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003378 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3379 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3380 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3381 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3382 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3383 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3384 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3385 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3386 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3387 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3388 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3389 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3390 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003391 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3392 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3393 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3394 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3395 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3396 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003397 { "sc0_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003398 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3399 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3400 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3401 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3402 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3403 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3404 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3405 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3406 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3407 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3408 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003409 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003410 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3411 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3412 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3413 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3414 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3415 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3416 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003417 { "sc1_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003418 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3419 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3420 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3421 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3422 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3423 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3424 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3425 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3426 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3427 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3428 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003429 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003430 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3431 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3432 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3433 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3434 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3435 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3436 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003437 { "sc2_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003438 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3439 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3440 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3441 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3442 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3443 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3444 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3445 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3446 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3447 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3448 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau0f791d42013-07-23 19:56:43 +02003449 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003450 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3451 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3452 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3453 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3454 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3455 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3456 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02003457 { "src_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(1,TAB), NULL, SMP_T_BOOL, SMP_USE_L4CLI, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003458 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3459 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3460 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3461 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3462 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3463 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3464 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3465 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3466 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3467 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3468 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3469 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3470 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3471 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003472 { /* END */ },
3473}};
3474
Willy Tarreau8b22a712010-06-18 17:46:06 +02003475__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003476static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003477{
Willy Tarreau281c7992013-01-08 01:23:27 +01003478 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003479 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003480 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3481 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003482}
3483
Willy Tarreaubaaee002006-06-26 02:48:02 +02003484/*
3485 * Local variables:
3486 * c-indent-level: 8
3487 * c-basic-offset: 8
3488 * End:
3489 */