blob: a98ecb06d3effdb5521b014eb6fdf69a6a60338f [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>
Christopher Fauletd7c91962015-04-30 11:48:27 +020025#include <types/filters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010026#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020028#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020029#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020030#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010031#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020032#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010033#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020034#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020035#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020036#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020037#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020038#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020039#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010040#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010041#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020042#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020043#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020044#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020045#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020046#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010047#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010048#include <proto/proto_http.h>
49#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020050#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020051#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010052#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020053#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010054#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010055#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010056#include <proto/task.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020057#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020058
Willy Tarreau87b09662015-04-03 00:22:06 +020059struct pool_head *pool2_stream;
60struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
Willy Tarreau87b09662015-04-03 00:22:06 +020062/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010063struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
64
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020065/* List of all use-service keywords. */
66static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
67
Willy Tarreau9903f0e2015-04-04 18:50:31 +020068/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020069 * handshake, in order to complete initialization of a valid stream. It must be
70 * called with a session (which may be embryonic). It returns the pointer to
71 * the newly created stream, or NULL in case of fatal error. The client-facing
72 * end point is assigned to <origin>, which must be valid. The task's context
73 * is set to the new stream, and its function is set to process_stream().
74 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020075 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020076struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020077{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020078 struct stream *s;
Christopher Fauletd7c91962015-04-30 11:48:27 +020079 struct filter *filter, *back;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020080 struct connection *conn = objt_conn(origin);
81 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020082
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020083 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020084 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020085
86 /* minimum stream initialization required for an embryonic stream is
87 * fairly low. We need very little to execute L4 ACLs, then we need a
88 * task to make the client-side connection live on its own.
89 * - flags
90 * - stick-entry tracking
91 */
92 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020093 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020094 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020095 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
96 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
97 tv_zero(&s->logs.tv_request);
98 s->logs.t_queue = -1;
99 s->logs.t_connect = -1;
100 s->logs.t_data = -1;
101 s->logs.t_close = 0;
102 s->logs.bytes_in = s->logs.bytes_out = 0;
103 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
104 s->logs.srv_queue_size = 0; /* we will get this number soon */
105
106 /* default logging function */
107 s->do_log = strm_log;
108
109 /* default error reporting function, may be changed by analysers */
110 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200111
112 /* Initialise the current rule list pointer to NULL. We are sure that
113 * any rulelist match the NULL pointer.
114 */
115 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200116 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200117
Willy Tarreaua68f7622015-09-21 17:48:24 +0200118 /* Copy SC counters for the stream. We don't touch refcounts because
119 * any reference we have is inherited from the session. Since the stream
120 * doesn't exist without the session, the session's existence guarantees
121 * we don't lose the entry. During the store operation, the stream won't
122 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200123 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200124 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200125
126 s->sess = sess;
127 s->si[0].flags = SI_FL_NONE;
128 s->si[1].flags = SI_FL_ISBACK;
129
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200130 s->uniq_id = global.req_count++;
131
Willy Tarreau87b09662015-04-03 00:22:06 +0200132 /* OK, we're keeping the stream, so let's properly initialize the stream */
133 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200134 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100135 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200136
Willy Tarreaue7dff022015-04-03 01:14:29 +0200137 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200138 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200139
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200140 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200141 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200142 t->context = s;
143 t->expire = TICK_ETERNITY;
144
Willy Tarreau87b09662015-04-03 00:22:06 +0200145 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200146 * This changes later when switching rules are executed or
147 * when the default backend is assigned.
148 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200149 s->be = sess->fe;
William Lallemand82fe75c2012-10-23 10:25:10 +0200150 s->comp_algo = NULL;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100151 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200152 s->req_cap = NULL;
153 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200154
Willy Tarreauebcd4842015-06-19 11:59:02 +0200155 /* Initialise all the variables contexts even if not used.
156 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200157 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200158 vars_init(&s->vars_txn, SCOPE_TXN);
159 vars_init(&s->vars_reqres, SCOPE_REQ);
160
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200161 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100162 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200163 si_set_state(&s->si[0], SI_ST_EST);
164
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200165 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200166 if (conn)
167 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200168 else if (appctx)
169 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200170
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200171 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200172 s->si[0].flags |= SI_FL_INDEP_STR;
173
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200174 /* pre-initialize the other side's stream interface to an INIT state. The
175 * callbacks will be initialized before attempting to connect.
176 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100177 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200178
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200179 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200180 s->si[1].flags |= SI_FL_INDEP_STR;
181
Willy Tarreau87b09662015-04-03 00:22:06 +0200182 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200183 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200184 s->pend_pos = NULL;
185
186 /* init store persistence */
187 s->store_count = 0;
188
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100189 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100190 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200191 s->req.analysers = 0;
192 channel_auto_connect(&s->req); /* don't wait to establish connection */
193 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200194
195 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100196 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100197 s->req.rex = TICK_ETERNITY;
198 s->req.wex = TICK_ETERNITY;
199 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200200
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100201 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100202 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100203 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200204
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200205 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100206 s->req.flags |= CF_NEVER_WAIT;
207 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200208 }
209
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200210 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100211 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100212 s->res.rex = TICK_ETERNITY;
213 s->res.wex = TICK_ETERNITY;
214 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200215
Willy Tarreaueee5b512015-04-03 23:46:31 +0200216 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100217
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100218 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100219
Christopher Fauletd7c91962015-04-30 11:48:27 +0200220 LIST_INIT(&s->strm_flt.filters);
221 memset(s->strm_flt.current, 0, sizeof(s->strm_flt.current));
222 list_for_each_entry(filter, &sess->fe->filters, list) {
223 struct filter *f = pool_alloc2(pool2_filter);
224 if (!f)
225 goto out_fail_accept;
226 memset(f, 0, sizeof(*f));
227 f->id = filter->id;
228 f->ops = filter->ops;
229 f->conf = filter->conf;
230 LIST_ADDQ(&s->strm_flt.filters, &f->list);
231 }
232 if (flt_stream_start(s) < 0)
233 goto out_fail_accept;
234
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200235 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200236 if (conn)
237 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200238 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200239 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200240
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200241 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200242 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200243
244 /* it is important not to call the wakeup function directly but to
245 * pass through task_wakeup(), because this one knows how to apply
246 * priorities to tasks.
247 */
248 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200249 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200250
251 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200252 out_fail_accept:
Christopher Fauletd7c91962015-04-30 11:48:27 +0200253 list_for_each_entry_safe(filter, back, &s->strm_flt.filters, list) {
254 LIST_DEL(&filter->list);
255 pool_free2(pool2_filter, filter);
256 }
Willy Tarreau3b246412014-11-25 17:10:33 +0100257 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200258 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200259 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200260}
261
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200263 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200265static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200266{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200267 struct session *sess = strm_sess(s);
268 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100269 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200270 struct connection *cli_conn = objt_conn(sess->origin);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200271 struct filter *filter, *fback;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200272 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100273
Willy Tarreaubaaee002006-06-26 02:48:02 +0200274 if (s->pend_pos)
275 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100276
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100277 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200278 if (s->flags & SF_CURR_SESS) {
279 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100280 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100281 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100282 if (may_dequeue_tasks(objt_server(s->target), s->be))
283 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100284 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100285
Willy Tarreau7c669d72008-06-20 15:04:11 +0200286 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200287 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200288 * it should normally be only the same as the one above,
289 * so this should not happen in fact.
290 */
291 sess_change_server(s, NULL);
292 }
293
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100294 if (s->req.pipe)
295 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100296
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100297 if (s->res.pipe)
298 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100299
Willy Tarreaubf883e02014-11-25 21:10:35 +0100300 /* We may still be present in the buffer wait queue */
301 if (!LIST_ISEMPTY(&s->buffer_wait)) {
302 LIST_DEL(&s->buffer_wait);
303 LIST_INIT(&s->buffer_wait);
304 }
305
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100306 b_drop(&s->req.buf);
307 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100308 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200309 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200310
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100311 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200312 if (s->txn)
313 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100314
Willy Tarreau1e954912012-10-12 17:50:05 +0200315 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200316 if (cli_conn)
317 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200318
Willy Tarreaua4cda672010-06-06 18:28:49 +0200319 for (i = 0; i < s->store_count; i++) {
320 if (!s->store[i].ts)
321 continue;
322 stksess_free(s->store[i].table, s->store[i].ts);
323 s->store[i].ts = NULL;
324 }
325
Willy Tarreaueee5b512015-04-03 23:46:31 +0200326 if (s->txn) {
327 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
328 pool_free2(pool2_http_txn, s->txn);
329 s->txn = NULL;
330 }
331
Christopher Fauletd7c91962015-04-30 11:48:27 +0200332 flt_stream_stop(s);
333 list_for_each_entry_safe(filter, fback, &s->strm_flt.filters, list) {
334 LIST_DEL(&filter->list);
335 pool_free2(pool2_filter, filter);
336 }
337
Willy Tarreau92fb9832007-10-16 17:34:28 +0200338 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200339 pool_free2(fe->rsp_cap_pool, s->res_cap);
340 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100342
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200343 /* Cleanup all variable contexts. */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200344 vars_prune(&s->vars_txn, s);
345 vars_prune(&s->vars_reqres, s);
346
Willy Tarreau87b09662015-04-03 00:22:06 +0200347 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200348
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100349 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100350 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200351 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100352 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100353 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100354 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200355 if (s->list.n != &streams)
356 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100357 bref->ref = s->list.n;
358 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100359 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200360 si_release_endpoint(&s->si[1]);
361 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200362
363 /* FIXME: for now we have a 1:1 relation between stream and session so
364 * the stream must free the session.
365 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200366 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200367 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200368
369 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200370 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200371 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200372 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200373 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200374 pool_flush2(pool2_requri);
375 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200376 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200377 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100378 pool_flush2(pool2_connection);
379 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200380 pool_flush2(fe->req_cap_pool);
381 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200382 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200383}
384
Willy Tarreau78955f42014-12-28 13:09:02 +0100385/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
386 * that it's not the last available buffer or it's the response buffer. Unless
387 * the buffer is the response buffer, an extra control is made so that we always
388 * keep <tune.buffers.reserved> buffers available after this allocation. To be
389 * called at the beginning of recv() callbacks to ensure that the required
390 * buffers are properly allocated. Returns 0 in case of failure, non-zero
391 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100392 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200393int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100394{
Willy Tarreau87b09662015-04-03 00:22:06 +0200395 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100396 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100397 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100398
Willy Tarreau78955f42014-12-28 13:09:02 +0100399 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100400 margin = global.tune.reserved_bufs;
401
Thierry FOURNIER27929fb2015-09-25 08:36:11 +0200402 s = chn_strm(chn);
Willy Tarreau78955f42014-12-28 13:09:02 +0100403
404 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100405 if (b)
406 return 1;
407
Willy Tarreaubf883e02014-11-25 21:10:35 +0100408 if (LIST_ISEMPTY(&s->buffer_wait))
409 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100410 return 0;
411}
412
Willy Tarreau87b09662015-04-03 00:22:06 +0200413/* Allocates a work buffer for stream <s>. It is meant to be called inside
414 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200415 * to work fine, which is the response buffer so that an error message may be
416 * built and returned. Response buffers may be allocated from the reserve, this
417 * is critical to ensure that a response may always flow and will never block a
418 * server from releasing a connection. Returns 0 in case of failure, non-zero
419 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100420 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200421int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100422{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423 if (!LIST_ISEMPTY(&s->buffer_wait)) {
424 LIST_DEL(&s->buffer_wait);
425 LIST_INIT(&s->buffer_wait);
426 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100427
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200428 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100429 return 1;
430
Willy Tarreaubf883e02014-11-25 21:10:35 +0100431 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100432 return 0;
433}
434
435/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100436 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200437 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100438 * a single buffer, so it makes sense to try to wake two up when two buffers
439 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100440 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200441void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100442{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100443 if (s->req.buf->size && buffer_empty(s->req.buf))
444 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100445
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100446 if (s->res.buf->size && buffer_empty(s->res.buf))
447 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100448
Willy Tarreaubf883e02014-11-25 21:10:35 +0100449 /* if we're certain to have at least 1 buffer available, and there is
450 * someone waiting, we can wake up a waiter and offer them.
451 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100452 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200453 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100454}
455
Willy Tarreau87b09662015-04-03 00:22:06 +0200456/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100457 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200458 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100459 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200460void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100461{
Willy Tarreau87b09662015-04-03 00:22:06 +0200462 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100463
464 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100465 if (rqlimit <= run_queue)
466 break;
467
Willy Tarreaubf883e02014-11-25 21:10:35 +0100468 if (sess->task->state & TASK_RUNNING)
469 continue;
470
471 LIST_DEL(&sess->buffer_wait);
472 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100473 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100474 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100475}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200476
477/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200478int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200479{
Willy Tarreau87b09662015-04-03 00:22:06 +0200480 LIST_INIT(&streams);
481 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
482 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200483}
484
Willy Tarreau87b09662015-04-03 00:22:06 +0200485void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100486{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200487 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100488 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200489 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100490 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100491
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100492 bytes = s->req.total - s->logs.bytes_in;
493 s->logs.bytes_in = s->req.total;
494 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200495 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100496
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100497 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100498
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100499 if (objt_server(s->target))
500 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200501
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200502 if (sess->listener && sess->listener->counters)
503 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200504
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100505 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200506 struct stkctr *stkctr = &s->stkctr[i];
507
508 if (!stkctr_entry(stkctr)) {
509 stkctr = &sess->stkctr[i];
510 if (!stkctr_entry(stkctr))
511 continue;
512 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200513
Emeric Brun57056f02015-06-15 18:29:57 +0200514 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
515 if (ptr1)
516 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200517
Emeric Brun57056f02015-06-15 18:29:57 +0200518 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
519 if (ptr2)
520 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200521 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200522
523 /* If data was modified, we need to touch to re-schedule sync */
524 if (ptr1 || ptr2)
525 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100526 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100527 }
528
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100529 bytes = s->res.total - s->logs.bytes_out;
530 s->logs.bytes_out = s->res.total;
531 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200532 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100533
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100534 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100535
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100536 if (objt_server(s->target))
537 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200538
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200539 if (sess->listener && sess->listener->counters)
540 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200541
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100542 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200543 struct stkctr *stkctr = &s->stkctr[i];
544
545 if (!stkctr_entry(stkctr)) {
546 stkctr = &sess->stkctr[i];
547 if (!stkctr_entry(stkctr))
548 continue;
549 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200550
Emeric Brun57056f02015-06-15 18:29:57 +0200551 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
552 if (ptr1)
553 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200554
Emeric Brun57056f02015-06-15 18:29:57 +0200555 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
556 if (ptr2)
557 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200558 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200559
560 /* If data was modified, we need to touch to re-schedule sync */
561 if (ptr1 || ptr2)
562 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100563 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100564 }
565}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200566
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100567/* This function is called with (si->state == SI_ST_CON) meaning that a
568 * connection was attempted and that the file descriptor is already allocated.
569 * We must check for establishment, error and abort. Possible output states
570 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
571 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200572 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100573 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200574static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100575{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100576 struct stream_interface *si = &s->si[1];
577 struct channel *req = &s->req;
578 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200579 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100580
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100581 /* If we got an error, or if nothing happened and the connection timed
582 * out, we must give up. The CER state handler will take care of retry
583 * attempts and error reports.
584 */
585 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100586 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100587 /* Some data were sent past the connection establishment,
588 * so we need to pretend we're established to log correctly
589 * and let later states handle the failure.
590 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100591 si->state = SI_ST_EST;
592 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100593 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100594 return 1;
595 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100596 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100597 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100598
Willy Tarreauf79c8172013-10-21 16:30:56 +0200599 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100600
601 if (si->err_type)
602 return 0;
603
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 if (si->flags & SI_FL_ERR)
605 si->err_type = SI_ET_CONN_ERR;
606 else
607 si->err_type = SI_ET_CONN_TO;
608 return 0;
609 }
610
611 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100612 if (!(req->flags & CF_WRITE_PARTIAL) &&
613 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200614 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
615 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100616 s->be->options & PR_O_ABRT_CLOSE)))) {
617 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200618 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100620 if (s->srv_error)
621 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622 return 1;
623 }
624
625 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200626 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100627 return 1;
628
629 /* OK, this means that a connection succeeded. The caller will be
630 * responsible for handling the transition from CON to EST.
631 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100632 si->state = SI_ST_EST;
633 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100634 return 1;
635}
636
637/* This function is called with (si->state == SI_ST_CER) meaning that a
638 * previous connection attempt has failed and that the file descriptor
639 * has already been released. Possible causes include asynchronous error
640 * notification and time out. Possible output states are SI_ST_CLO when
641 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
642 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
643 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
644 * marked as in error state. It returns 0.
645 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200646static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100648 struct stream_interface *si = &s->si[1];
649
Willy Tarreau87b09662015-04-03 00:22:06 +0200650 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100651 if (objt_server(s->target)) {
652 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100653
Willy Tarreaue7dff022015-04-03 01:14:29 +0200654 if (s->flags & SF_CURR_SESS) {
655 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100656 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100658 }
659
660 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200661 si->conn_retries--;
662 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100663 if (!si->err_type) {
664 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665 }
666
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100667 if (objt_server(s->target))
668 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100669 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100670 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100671 if (may_dequeue_tasks(objt_server(s->target), s->be))
672 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673
674 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200675 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100676 s->req.flags |= CF_WRITE_ERROR;
677 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100678
679 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100680 if (s->srv_error)
681 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100682 return 0;
683 }
684
685 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700686 * retry on another server. By default this redispatch occurs on the
687 * last retry, but if configured we allow redispatches to occur on
688 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200689 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100690 * bit to ignore any persistence cookie. We won't count a retry nor a
691 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200692 * If the connection is not persistent, the balancing algorithm is not
693 * determinist (round robin) and there is more than one active server,
694 * we accept to perform an immediate redispatch without waiting since
695 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200697 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700698 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100699 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
700 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700701 ((s->be->conn_retries - si->conn_retries) %
702 s->be->redispatch_after == 0)) ||
703 ((s->be->redispatch_after < 0) &&
704 ((s->be->conn_retries - si->conn_retries) %
705 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200706 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700707 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100708 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100709 if (may_dequeue_tasks(objt_server(s->target), s->be))
710 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711
Willy Tarreaue7dff022015-04-03 01:14:29 +0200712 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100713 si->state = SI_ST_REQ;
714 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100715 if (objt_server(s->target))
716 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100717 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100718 si->state = SI_ST_ASS;
719 }
720
721 if (si->flags & SI_FL_ERR) {
722 /* The error was an asynchronous connection error, and we will
723 * likely have to retry connecting to the same server, most
724 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200725 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 */
727
Willy Tarreaub0290662014-06-13 17:04:44 +0200728 int delay = 1000;
729
730 if (s->be->timeout.connect && s->be->timeout.connect < delay)
731 delay = s->be->timeout.connect;
732
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733 if (!si->err_type)
734 si->err_type = SI_ET_CONN_ERR;
735
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200736 /* only wait when we're retrying on the same server */
737 if (si->state == SI_ST_ASS ||
738 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
739 (s->be->srv_act <= 1)) {
740 si->state = SI_ST_TAR;
741 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
742 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743 return 0;
744 }
745 return 0;
746}
747
748/*
749 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200750 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200751 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200753static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100754{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100755 struct stream_interface *si = &s->si[1];
756 struct channel *req = &s->req;
757 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100759 /* First, centralize the timers information */
760 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
761 si->exp = TICK_ETERNITY;
762
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100763 if (objt_server(s->target))
764 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100765
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100766 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100767 /* if the user wants to log as soon as possible, without counting
768 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200769 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100770 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100771 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100772 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100773 }
774 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100775 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100776 }
777
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200778 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200779 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200780 if (req->flags & CF_WAKE_CONNECT) {
781 req->flags |= CF_WAKE_ONCE;
782 req->flags &= ~CF_WAKE_CONNECT;
783 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200784 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200785 /* real connections have timeouts */
786 req->wto = s->be->timeout.server;
787 rep->rto = s->be->timeout.server;
788 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100789 req->wex = TICK_ETERNITY;
790}
791
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100792/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
793 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100794 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
795 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
796 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100797 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200798static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100799{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100800 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100801 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100802 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100803
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100804 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 +0100805 now_ms, __FUNCTION__,
806 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400807 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100808 req->rex, s->res.wex,
809 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400810 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 +0100811
812 if (si->state == SI_ST_ASS) {
813 /* Server assigned to connection request, we have to try to connect now */
814 int conn_err;
815
816 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100817 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100818
Willy Tarreaue7dff022015-04-03 01:14:29 +0200819 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100820 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100821 if (srv)
822 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500823 if (srv)
824 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100825 return;
826 }
827
828 /* We have received a synchronous error. We might have to
829 * abort, retry immediately or redispatch.
830 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200831 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832 if (!si->err_type) {
833 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100834 }
835
Willy Tarreau827aee92011-03-10 16:55:02 +0100836 if (srv)
837 srv_inc_sess_ctr(srv);
838 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500839 srv_set_sess_last(srv);
840 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100841 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100842 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100843
Willy Tarreau87b09662015-04-03 00:22:06 +0200844 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100845 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100846 if (may_dequeue_tasks(srv, s->be))
847 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100848
849 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200850 si_shutr(si);
851 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100852 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100853
854 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
855
Willy Tarreau87b09662015-04-03 00:22:06 +0200856 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100857 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100858 if (s->srv_error)
859 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100860 return;
861 }
862
863 /* We are facing a retryable error, but we don't want to run a
864 * turn-around now, as the problem is likely a source port
865 * allocation problem, so we want to retry now.
866 */
867 si->state = SI_ST_CER;
868 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100869 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100870 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
871 return;
872 }
873 else if (si->state == SI_ST_QUE) {
874 /* connection request was queued, check for any update */
875 if (!s->pend_pos) {
876 /* The connection is not in the queue anymore. Either
877 * we have a server connection slot available and we
878 * go directly to the assigned state, or we need to
879 * load-balance first and go to the INI state.
880 */
881 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200882 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100883 si->state = SI_ST_REQ;
884 else {
885 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
886 si->state = SI_ST_ASS;
887 }
888 return;
889 }
890
891 /* Connection request still in queue... */
892 if (si->flags & SI_FL_EXP) {
893 /* ... and timeout expired */
894 si->exp = TICK_ETERNITY;
895 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100896 if (srv)
897 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100898 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200899 si_shutr(si);
900 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100901 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100902 if (!si->err_type)
903 si->err_type = SI_ET_QUEUE_TO;
904 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100905 if (s->srv_error)
906 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100907 return;
908 }
909
910 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100911 if ((req->flags & (CF_READ_ERROR)) ||
912 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
913 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100914 /* give up */
915 si->exp = TICK_ETERNITY;
916 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200917 si_shutr(si);
918 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919 si->err_type |= SI_ET_QUEUE_ABRT;
920 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100921 if (s->srv_error)
922 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100923 return;
924 }
925
926 /* Nothing changed */
927 return;
928 }
929 else if (si->state == SI_ST_TAR) {
930 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100931 if ((req->flags & (CF_READ_ERROR)) ||
932 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
933 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100934 /* give up */
935 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200936 si_shutr(si);
937 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938 si->err_type |= SI_ET_CONN_ABRT;
939 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100940 if (s->srv_error)
941 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100942 return;
943 }
944
945 if (!(si->flags & SI_FL_EXP))
946 return; /* still in turn-around */
947
948 si->exp = TICK_ETERNITY;
949
Willy Tarreau87b09662015-04-03 00:22:06 +0200950 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100951 * marked "assigned".
952 * FIXME: Should we force a redispatch attempt when the server is down ?
953 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200954 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100955 si->state = SI_ST_ASS;
956 else
957 si->state = SI_ST_REQ;
958 return;
959 }
960}
961
Willy Tarreau87b09662015-04-03 00:22:06 +0200962/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900963 * also counts a failed request if the server state has not reached the request
964 * stage.
965 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200966static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900967{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200968 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900969 if (s->si[1].state < SI_ST_REQ) {
970
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200971 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200972 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200973 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900974
Willy Tarreaue7dff022015-04-03 01:14:29 +0200975 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900976 }
977 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200978 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900979 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900981 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200982 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900983 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200984 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900985 }
986}
987
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100988/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100989 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
990 * a real connection to a server, indicating that a server has been assigned,
991 * or SI_ST_EST for a successful connection to an applet. It may also return
992 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100993 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200994static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100995{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100996 struct stream_interface *si = &s->si[1];
997
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100998 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 +0100999 now_ms, __FUNCTION__,
1000 s,
CJ Ess6eac32e2015-05-02 16:35:24 -04001001 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001002 s->req.rex, s->res.wex,
1003 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -04001004 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 +01001005
1006 if (si->state != SI_ST_REQ)
1007 return;
1008
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001009 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1010 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001011 struct appctx *appctx = objt_appctx(si->end);
1012
1013 if (!appctx || appctx->applet != __objt_applet(s->target))
1014 appctx = stream_int_register_handler(si, objt_applet(s->target));
1015
1016 if (!appctx) {
1017 /* No more memory, let's immediately abort. Force the
1018 * error code to ignore the ERR_LOCAL which is not a
1019 * real error.
1020 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001021 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001022
1023 si_shutr(si);
1024 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001025 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001026 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001027 si->state = SI_ST_CLO;
1028 if (s->srv_error)
1029 s->srv_error(s, si);
1030 return;
1031 }
1032
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001033 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001034 si->state = SI_ST_EST;
1035 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001036 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001037 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001038 return;
1039 }
1040
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001041 /* Try to assign a server */
1042 if (srv_redispatch_connect(s) != 0) {
1043 /* We did not get a server. Either we queued the
1044 * connection request, or we encountered an error.
1045 */
1046 if (si->state == SI_ST_QUE)
1047 return;
1048
1049 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001050 si_shutr(si);
1051 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001052 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001053 if (!si->err_type)
1054 si->err_type = SI_ET_CONN_OTHER;
1055 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001056 if (s->srv_error)
1057 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001058 return;
1059 }
1060
1061 /* The server is assigned */
1062 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1063 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001064 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001065}
1066
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001067/* This function parses the use-service action ruleset. It executes
1068 * the associated ACL and set an applet as a stream or txn final node.
1069 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1070 * consistent state. It returns ACT_RET_STOP in succes case because
1071 * use-service must be a terminal action. Returns ACT_RET_YIELD
1072 * if the initialisation function require more data.
1073 */
1074enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1075 struct session *sess, struct stream *s, int flags)
1076
1077{
1078 struct appctx *appctx;
1079
1080 /* Initialises the applet if it is required. */
1081 if (flags & ACT_FLAG_FIRST) {
1082 /* Register applet. this function schedules the applet. */
1083 s->target = &rule->applet.obj_type;
1084 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1085 return ACT_RET_ERR;
1086
1087 /* Initialise the context. */
1088 appctx = si_appctx(&s->si[1]);
1089 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1090 appctx->rule = rule;
1091 }
1092 else
1093 appctx = si_appctx(&s->si[1]);
1094
1095 /* Stops the applet sheduling, in case of the init function miss
1096 * some data.
1097 */
1098 appctx_pause(appctx);
1099 si_applet_stop_get(&s->si[1]);
1100
1101 /* Call initialisation. */
1102 if (rule->applet.init)
1103 switch (rule->applet.init(appctx, px, s)) {
1104 case 0: return ACT_RET_ERR;
1105 case 1: break;
1106 default: return ACT_RET_YIELD;
1107 }
1108
1109 /* Now we can schedule the applet. */
1110 si_applet_cant_get(&s->si[1]);
1111 appctx_wakeup(appctx);
1112
1113 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1114 sess->fe->fe_counters.intercepted_req++;
1115
1116 /* The flag SF_ASSIGNED prevent from server assignment. */
1117 s->flags |= SF_ASSIGNED;
1118
1119 return ACT_RET_STOP;
1120}
1121
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001122/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001123 * if appropriate. The default_backend rule is also considered, then the
1124 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001125 * It returns 1 if the processing can continue on next analysers, or zero if it
1126 * either needs more data or wants to immediately abort the request.
1127 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001128static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001129{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001130 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001131 struct session *sess = s->sess;
1132 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001133
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001134 req->analysers &= ~an_bit;
1135 req->analyse_exp = TICK_ETERNITY;
1136
Willy Tarreau87b09662015-04-03 00:22:06 +02001137 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 +02001138 now_ms, __FUNCTION__,
1139 s,
1140 req,
1141 req->rex, req->wex,
1142 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001143 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001144 req->analysers);
1145
1146 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001147 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001148 struct switching_rule *rule;
1149
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001150 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001151 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001152
Willy Tarreauf51658d2014-04-23 01:21:56 +02001153 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001154 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001155 ret = acl_pass(ret);
1156 if (rule->cond->pol == ACL_COND_UNLESS)
1157 ret = !ret;
1158 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001159
1160 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001161 /* If the backend name is dynamic, try to resolve the name.
1162 * If we can't resolve the name, or if any error occurs, break
1163 * the loop and fallback to the default backend.
1164 */
1165 struct proxy *backend;
1166
1167 if (rule->dynamic) {
1168 struct chunk *tmp = get_trash_chunk();
1169 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1170 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001171 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001172 if (!backend)
1173 break;
1174 }
1175 else
1176 backend = rule->be.backend;
1177
Willy Tarreau87b09662015-04-03 00:22:06 +02001178 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001179 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001180 break;
1181 }
1182 }
1183
1184 /* To ensure correct connection accounting on the backend, we
1185 * have to assign one if it was not set (eg: a listen). This
1186 * measure also takes care of correctly setting the default
1187 * backend if any.
1188 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001190 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001191 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001192 }
1193
Willy Tarreaufb356202010-08-03 14:02:05 +02001194 /* 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 +02001195 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001196 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1197 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001198 s->req.analysers &= ~AN_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001199 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001200
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001201 /* 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 +02001202 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001203 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001204 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001205 int ret = 1;
1206
1207 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001208 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 +01001209 ret = acl_pass(ret);
1210 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1211 ret = !ret;
1212 }
1213
1214 if (ret) {
1215 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001216 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001217 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001218 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001219 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001220 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001221 break;
1222 }
1223 }
1224
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001225 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001226
1227 sw_failed:
1228 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001229 channel_abort(&s->req);
1230 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001231
Willy Tarreaue7dff022015-04-03 01:14:29 +02001232 if (!(s->flags & SF_ERR_MASK))
1233 s->flags |= SF_ERR_RESOURCE;
1234 if (!(s->flags & SF_FINST_MASK))
1235 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001236
Willy Tarreaueee5b512015-04-03 23:46:31 +02001237 if (s->txn)
1238 s->txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001239 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001240 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001241 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001242}
1243
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001244/* This stream analyser works on a request. It applies all use-server rules on
1245 * it then returns 1. The data must already be present in the buffer otherwise
1246 * they won't match. It always returns 1.
1247 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001248static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001249{
1250 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001251 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001252 struct server_rule *rule;
1253
Willy Tarreau87b09662015-04-03 00:22:06 +02001254 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 +02001255 now_ms, __FUNCTION__,
1256 s,
1257 req,
1258 req->rex, req->wex,
1259 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001260 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001261 req->analysers);
1262
Willy Tarreaue7dff022015-04-03 01:14:29 +02001263 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001264 list_for_each_entry(rule, &px->server_rules, list) {
1265 int ret;
1266
Willy Tarreau192252e2015-04-04 01:47:55 +02001267 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001268 ret = acl_pass(ret);
1269 if (rule->cond->pol == ACL_COND_UNLESS)
1270 ret = !ret;
1271
1272 if (ret) {
1273 struct server *srv = rule->srv.ptr;
1274
Willy Tarreau892337c2014-05-13 23:41:20 +02001275 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001276 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001277 (s->flags & SF_FORCE_PRST)) {
1278 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001279 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001280 break;
1281 }
1282 /* if the server is not UP, let's go on with next rules
1283 * just in case another one is suited.
1284 */
1285 }
1286 }
1287 }
1288
1289 req->analysers &= ~an_bit;
1290 req->analyse_exp = TICK_ETERNITY;
1291 return 1;
1292}
1293
Emeric Brun1d33b292010-01-04 15:47:17 +01001294/* This stream analyser works on a request. It applies all sticking rules on
1295 * it then returns 1. The data must already be present in the buffer otherwise
1296 * they won't match. It always returns 1.
1297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001298static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001299{
1300 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001301 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001302 struct sticking_rule *rule;
1303
Willy Tarreau87b09662015-04-03 00:22:06 +02001304 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 +01001305 now_ms, __FUNCTION__,
1306 s,
1307 req,
1308 req->rex, req->wex,
1309 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001310 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001311 req->analysers);
1312
1313 list_for_each_entry(rule, &px->sticking_rules, list) {
1314 int ret = 1 ;
1315 int i;
1316
Willy Tarreau9667a802013-12-09 12:52:13 +01001317 /* Only the first stick store-request of each table is applied
1318 * and other ones are ignored. The purpose is to allow complex
1319 * configurations which look for multiple entries by decreasing
1320 * order of precision and to stop at the first which matches.
1321 * An example could be a store of the IP address from an HTTP
1322 * header first, then from the source if not found.
1323 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001324 for (i = 0; i < s->store_count; i++) {
1325 if (rule->table.t == s->store[i].table)
1326 break;
1327 }
1328
1329 if (i != s->store_count)
1330 continue;
1331
1332 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001333 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001334 ret = acl_pass(ret);
1335 if (rule->cond->pol == ACL_COND_UNLESS)
1336 ret = !ret;
1337 }
1338
1339 if (ret) {
1340 struct stktable_key *key;
1341
Willy Tarreau192252e2015-04-04 01:47:55 +02001342 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 +01001343 if (!key)
1344 continue;
1345
1346 if (rule->flags & STK_IS_MATCH) {
1347 struct stksess *ts;
1348
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001349 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001350 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001351 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001352 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001353
1354 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001355 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1356 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001357 if (node) {
1358 struct server *srv;
1359
1360 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001361 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001362 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001363 (s->flags & SF_FORCE_PRST)) {
1364 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001365 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001366 }
1367 }
1368 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001369 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001370 }
1371 }
1372 if (rule->flags & STK_IS_STORE) {
1373 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1374 struct stksess *ts;
1375
1376 ts = stksess_new(rule->table.t, key);
1377 if (ts) {
1378 s->store[s->store_count].table = rule->table.t;
1379 s->store[s->store_count++].ts = ts;
1380 }
1381 }
1382 }
1383 }
1384 }
1385
1386 req->analysers &= ~an_bit;
1387 req->analyse_exp = TICK_ETERNITY;
1388 return 1;
1389}
1390
1391/* This stream analyser works on a response. It applies all store rules on it
1392 * then returns 1. The data must already be present in the buffer otherwise
1393 * they won't match. It always returns 1.
1394 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001395static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001396{
1397 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001398 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001399 struct sticking_rule *rule;
1400 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001401 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001402
Willy Tarreau87b09662015-04-03 00:22:06 +02001403 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 +01001404 now_ms, __FUNCTION__,
1405 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001406 rep,
1407 rep->rex, rep->wex,
1408 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001409 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001410 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001411
1412 list_for_each_entry(rule, &px->storersp_rules, list) {
1413 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001414
Willy Tarreau9667a802013-12-09 12:52:13 +01001415 /* Only the first stick store-response of each table is applied
1416 * and other ones are ignored. The purpose is to allow complex
1417 * configurations which look for multiple entries by decreasing
1418 * order of precision and to stop at the first which matches.
1419 * An example could be a store of a set-cookie value, with a
1420 * fallback to a parameter found in a 302 redirect.
1421 *
1422 * The store-response rules are not allowed to override the
1423 * store-request rules for the same table, but they may coexist.
1424 * Thus we can have up to one store-request entry and one store-
1425 * response entry for the same table at any time.
1426 */
1427 for (i = nbreq; i < s->store_count; i++) {
1428 if (rule->table.t == s->store[i].table)
1429 break;
1430 }
1431
1432 /* skip existing entries for this table */
1433 if (i < s->store_count)
1434 continue;
1435
Emeric Brun1d33b292010-01-04 15:47:17 +01001436 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001437 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001438 ret = acl_pass(ret);
1439 if (rule->cond->pol == ACL_COND_UNLESS)
1440 ret = !ret;
1441 }
1442
1443 if (ret) {
1444 struct stktable_key *key;
1445
Willy Tarreau192252e2015-04-04 01:47:55 +02001446 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 +01001447 if (!key)
1448 continue;
1449
Willy Tarreau37e340c2013-12-06 23:05:21 +01001450 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001451 struct stksess *ts;
1452
1453 ts = stksess_new(rule->table.t, key);
1454 if (ts) {
1455 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001456 s->store[s->store_count++].ts = ts;
1457 }
1458 }
1459 }
1460 }
1461
1462 /* process store request and store response */
1463 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001464 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001465 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001466
Willy Tarreauc93cd162014-05-13 15:54:22 +02001467 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001468 stksess_free(s->store[i].table, s->store[i].ts);
1469 s->store[i].ts = NULL;
1470 continue;
1471 }
1472
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001473 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1474 if (ts) {
1475 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001476 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001477 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001478 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001479 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001480 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001481
1482 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001483 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001484 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001485 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001486 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001487
1488 rep->analysers &= ~an_bit;
1489 rep->analyse_exp = TICK_ETERNITY;
1490 return 1;
1491}
1492
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001494 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 */
1496#define UPDATE_ANALYSERS(real, list, back, flag) { \
1497 list = (((list) & ~(flag)) | ~(back)) & (real); \
1498 back = real; \
1499 if (!(list)) \
1500 break; \
1501 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1502 continue; \
1503}
1504
Willy Tarreau87b09662015-04-03 00:22:06 +02001505/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001506 * then puts it back to the wait queue in a clean state, or cleans up its
1507 * resources if it must be deleted. Returns in <next> the date the task wants
1508 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1509 * nothing too many times, the request and response buffers flags are monitored
1510 * and each function is called only if at least another function has changed at
1511 * least one flag it is interested in.
1512 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001513struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001514{
Willy Tarreau827aee92011-03-10 16:55:02 +01001515 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001516 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001517 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001518 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001519 unsigned int rq_prod_last, rq_cons_last;
1520 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001521 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001522 struct channel *req, *res;
1523 struct stream_interface *si_f, *si_b;
1524
1525 req = &s->req;
1526 res = &s->res;
1527
1528 si_f = &s->si[0];
1529 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001530
1531 //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 +01001532 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001533
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001534 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001535 if (s->txn)
1536 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001537
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001538 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001539 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1540 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001541
1542 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001543 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1544 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001545
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001546 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001547 si_f->flags |= SI_FL_DONT_WAKE;
1548 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001549
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001550 /* 1a: Check for low level timeouts if needed. We just set a flag on
1551 * stream interfaces when their timeouts have expired.
1552 */
1553 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001554 stream_int_check_timeouts(si_f);
1555 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001556
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001557 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001558 * for future reads or writes. Note: this will also concern upper layers
1559 * but we do not touch any other flag. We must be careful and correctly
1560 * detect state changes when calling them.
1561 */
1562
Willy Tarreau8f128b42014-11-28 15:07:47 +01001563 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001564
Willy Tarreau8f128b42014-11-28 15:07:47 +01001565 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1566 si_b->flags |= SI_FL_NOLINGER;
1567 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001568 }
1569
Willy Tarreau8f128b42014-11-28 15:07:47 +01001570 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1571 if (si_f->flags & SI_FL_NOHALF)
1572 si_f->flags |= SI_FL_NOLINGER;
1573 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001574 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001575
Willy Tarreau8f128b42014-11-28 15:07:47 +01001576 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001577
Willy Tarreau8f128b42014-11-28 15:07:47 +01001578 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1579 si_f->flags |= SI_FL_NOLINGER;
1580 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001581 }
1582
Willy Tarreau8f128b42014-11-28 15:07:47 +01001583 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1584 if (si_b->flags & SI_FL_NOHALF)
1585 si_b->flags |= SI_FL_NOLINGER;
1586 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001587 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001588
1589 /* Once in a while we're woken up because the task expires. But
1590 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001591 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001592 * timeout needs to be refreshed.
1593 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001594 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001595 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1596 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001597 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001598 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1599 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001600 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001601
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001602 /* below we may emit error messages so we have to ensure that we have
1603 * our buffers properly allocated.
1604 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001605 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001606 /* No buffer available, we've been subscribed to the list of
1607 * buffer waiters, let's wait for our turn.
1608 */
1609 goto update_exp_and_leave;
1610 }
1611
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001612 /* 1b: check for low-level errors reported at the stream interface.
1613 * First we check if it's a retryable error (in which case we don't
1614 * want to tell the buffer). Otherwise we report the error one level
1615 * upper by setting flags into the buffers. Note that the side towards
1616 * the client cannot have connect (hence retryable) errors. Also, the
1617 * connection setup code must be able to deal with any type of abort.
1618 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001619 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001620 if (unlikely(si_f->flags & SI_FL_ERR)) {
1621 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1622 si_shutr(si_f);
1623 si_shutw(si_f);
1624 stream_int_report_error(si_f);
1625 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001626 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001627 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001628 if (srv)
1629 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001630 if (!(s->flags & SF_ERR_MASK))
1631 s->flags |= SF_ERR_CLICL;
1632 if (!(s->flags & SF_FINST_MASK))
1633 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001634 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001635 }
1636 }
1637
Willy Tarreau8f128b42014-11-28 15:07:47 +01001638 if (unlikely(si_b->flags & SI_FL_ERR)) {
1639 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1640 si_shutr(si_b);
1641 si_shutw(si_b);
1642 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001643 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001644 if (srv)
1645 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001646 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001647 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001648 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001649 if (srv)
1650 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001651 if (!(s->flags & SF_ERR_MASK))
1652 s->flags |= SF_ERR_SRVCL;
1653 if (!(s->flags & SF_FINST_MASK))
1654 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001655 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001656 }
1657 /* note: maybe we should process connection errors here ? */
1658 }
1659
Willy Tarreau8f128b42014-11-28 15:07:47 +01001660 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001661 /* we were trying to establish a connection on the server side,
1662 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1663 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001664 if (unlikely(!sess_update_st_con_tcp(s)))
1665 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001666 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001667 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001668
1669 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1670 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1671 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1672 */
1673 }
1674
Willy Tarreau8f128b42014-11-28 15:07:47 +01001675 rq_prod_last = si_f->state;
1676 rq_cons_last = si_b->state;
1677 rp_cons_last = si_f->state;
1678 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001679
1680 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001681 /* Check for connection closure */
1682
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001683 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001684 "[%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 +01001685 now_ms, __FUNCTION__, __LINE__,
1686 t,
1687 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001688 req, res,
1689 req->rex, res->wex,
1690 req->flags, res->flags,
1691 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1692 si_f->err_type, si_b->err_type,
1693 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001694
1695 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001696 if (unlikely(si_f->state == SI_ST_DIS))
1697 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001698
1699 /* When a server-side connection is released, we have to count it and
1700 * check for pending connections on this server.
1701 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001702 if (unlikely(si_b->state == SI_ST_DIS)) {
1703 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001704 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001705 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001706 if (s->flags & SF_CURR_SESS) {
1707 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001708 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001709 }
1710 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001711 if (may_dequeue_tasks(srv, s->be))
1712 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001713 }
1714 }
1715
1716 /*
1717 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1718 * at this point.
1719 */
1720
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001721 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001722 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001723 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1724 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1725 si_f->state != rq_prod_last ||
1726 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001727 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001728 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001729
Willy Tarreau8f128b42014-11-28 15:07:47 +01001730 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001731 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001732 unsigned int ana_list;
1733 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001734
Willy Tarreau90deb182010-01-07 00:20:41 +01001735 /* it's up to the analysers to stop new connections,
1736 * disable reading or closing. Note: if an analyser
1737 * disables any of these bits, it is responsible for
1738 * enabling them again when it disables itself, so
1739 * that other analysers are called in similar conditions.
1740 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001741 channel_auto_read(req);
1742 channel_auto_connect(req);
1743 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001744
1745 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001746 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001747 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001748 * the list when not needed. Any analyser may return 0
1749 * to break out of the loop, either because of missing
1750 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001751 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001752 * analyser, and we may loop again if other analysers
1753 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001754 *
1755 * We build a list of analysers to run. We evaluate all
1756 * of these analysers in the order of the lower bit to
1757 * the higher bit. This ordering is very important.
1758 * An analyser will often add/remove other analysers,
1759 * including itself. Any changes to itself have no effect
1760 * on the loop. If it removes any other analysers, we
1761 * want those analysers not to be called anymore during
1762 * this loop. If it adds an analyser that is located
1763 * after itself, we want it to be scheduled for being
1764 * processed during the loop. If it adds an analyser
1765 * which is located before it, we want it to switch to
1766 * it immediately, even if it has already been called
1767 * once but removed since.
1768 *
1769 * In order to achieve this, we compare the analyser
1770 * list after the call with a copy of it before the
1771 * call. The work list is fed with analyser bits that
1772 * appeared during the call. Then we compare previous
1773 * work list with the new one, and check the bits that
1774 * appeared. If the lowest of these bits is lower than
1775 * the current bit, it means we have enabled a previous
1776 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001777 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001778
Willy Tarreau8f128b42014-11-28 15:07:47 +01001779 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001780 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001781 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauletd7c91962015-04-30 11:48:27 +02001782 if (ana_list & AN_FLT_START_FE) {
1783 if (!flt_start_analyze(s, req, AN_FLT_START_FE))
1784 break;
1785 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_START_FE);
1786 }
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001787
Willy Tarreaufb356202010-08-03 14:02:05 +02001788 if (ana_list & AN_REQ_INSPECT_FE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001789 if (!flt_analyze(s, req, AN_REQ_INSPECT_FE))
1790 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001791 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001792 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001793 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001794 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001795
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001796 if (ana_list & AN_REQ_WAIT_HTTP) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001797 if (!flt_analyze(s, req, AN_REQ_WAIT_HTTP))
1798 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001799 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001800 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001801 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001802 }
1803
Willy Tarreau748179e2015-05-01 21:52:31 +02001804 if (ana_list & AN_REQ_HTTP_BODY) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001805 if (!flt_analyze(s, req, AN_REQ_HTTP_BODY))
1806 break;
Willy Tarreau748179e2015-05-01 21:52:31 +02001807 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1808 break;
1809 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1810 }
1811
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001812 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001813 if (!flt_analyze(s, req, AN_REQ_HTTP_PROCESS_FE))
1814 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001815 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001816 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001817 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001818 }
1819
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001820 if (ana_list & AN_REQ_SWITCHING_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001821 if (!flt_analyze(s, req, AN_REQ_SWITCHING_RULES))
1822 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001823 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001824 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001825 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001826 }
1827
Willy Tarreaufb356202010-08-03 14:02:05 +02001828 if (ana_list & AN_REQ_INSPECT_BE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001829 if (!flt_analyze(s, req, AN_REQ_INSPECT_BE))
1830 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001831 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001832 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001833 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001834 }
1835
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001836 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001837 if (!flt_analyze(s, req, AN_REQ_HTTP_PROCESS_BE))
1838 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001839 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001840 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001841 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001842 }
1843
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001844 if (ana_list & AN_REQ_HTTP_TARPIT) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001845 if (!flt_analyze(s, req, AN_REQ_HTTP_TARPIT))
1846 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001848 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001849 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001850 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001851
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001852 if (ana_list & AN_REQ_SRV_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001853 if (!flt_analyze(s, req, AN_REQ_SRV_RULES))
1854 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001855 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001856 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001857 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001858 }
1859
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001860 if (ana_list & AN_REQ_HTTP_INNER) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001861 if (!flt_analyze(s, req, AN_REQ_HTTP_INNER))
1862 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001863 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001864 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001865 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001866 }
1867
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001868 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001869 if (!flt_analyze(s, req, AN_REQ_PRST_RDP_COOKIE))
1870 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001872 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001873 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001874 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001875
Emeric Brun1d33b292010-01-04 15:47:17 +01001876 if (ana_list & AN_REQ_STICKING_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001877 if (!flt_analyze(s, req, AN_REQ_STICKING_RULES))
1878 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001879 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001880 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001881 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001882 }
1883
Christopher Fauletd7c91962015-04-30 11:48:27 +02001884 if (ana_list & AN_FLT_START_BE) {
1885 if (!flt_start_analyze(s, req, AN_FLT_START_BE))
1886 break;
1887 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_START_BE);
1888 }
1889
1890 if (ana_list & AN_FLT_XFER_DATA) {
1891 if (!flt_xfer_data(s, req, AN_FLT_XFER_DATA))
1892 break;
1893 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_XFER_DATA);
1894 }
1895
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001896 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001897 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001898 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001899 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001900 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001901
1902 if (ana_list & AN_FLT_END) {
1903 if (!flt_end_analyze(s, req, AN_FLT_END))
1904 break;
1905 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_END);
1906 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001907 break;
1908 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001909 }
Willy Tarreau84455332009-03-15 22:34:05 +01001910
Willy Tarreau8f128b42014-11-28 15:07:47 +01001911 rq_prod_last = si_f->state;
1912 rq_cons_last = si_b->state;
1913 req->flags &= ~CF_WAKE_ONCE;
1914 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001915
Willy Tarreau8f128b42014-11-28 15:07:47 +01001916 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001917 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001918 }
1919
Willy Tarreau576507f2010-01-07 00:09:04 +01001920 /* we'll monitor the request analysers while parsing the response,
1921 * because some response analysers may indirectly enable new request
1922 * analysers (eg: HTTP keep-alive).
1923 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001924 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001925
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001926 resync_response:
1927 /* Analyse response */
1928
Willy Tarreau8f128b42014-11-28 15:07:47 +01001929 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1930 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1931 si_f->state != rp_cons_last ||
1932 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001933 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001934 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001935
Willy Tarreau8f128b42014-11-28 15:07:47 +01001936 if ((res->flags & CF_MASK_ANALYSER) &&
1937 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001938 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1939 * for some free space in the response buffer, so we might need to call
1940 * it when something changes in the response buffer, but still we pass
1941 * it the request buffer. Note that the SI state might very well still
1942 * be zero due to us returning a flow of redirects!
1943 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001944 res->analysers &= ~AN_REQ_ALL;
1945 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001946 }
1947
Willy Tarreau8f128b42014-11-28 15:07:47 +01001948 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001949 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001950 unsigned int ana_list;
1951 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001952
Willy Tarreau90deb182010-01-07 00:20:41 +01001953 /* it's up to the analysers to stop disable reading or
1954 * closing. Note: if an analyser disables any of these
1955 * bits, it is responsible for enabling them again when
1956 * it disables itself, so that other analysers are called
1957 * in similar conditions.
1958 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001959 channel_auto_read(res);
1960 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001961
1962 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001963 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001964 * to MSB. The analysers must remove themselves from
1965 * the list when not needed. Any analyser may return 0
1966 * to break out of the loop, either because of missing
1967 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001968 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001969 * analyser, and we may loop again if other analysers
1970 * are added in the middle.
1971 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001972
Willy Tarreau8f128b42014-11-28 15:07:47 +01001973 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001974 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001975 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauletd7c91962015-04-30 11:48:27 +02001976 if (ana_list & AN_FLT_START_FE) {
1977 if (!flt_start_analyze(s, res, AN_FLT_START_FE))
1978 break;
1979 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_START_FE);
1980 }
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001981
Emeric Brun97679e72010-09-23 17:56:44 +02001982 if (ana_list & AN_RES_INSPECT) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001983 if (!flt_analyze(s, res, AN_RES_INSPECT))
1984 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001985 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001986 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001987 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001988 }
1989
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001990 if (ana_list & AN_RES_WAIT_HTTP) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001991 if (!flt_analyze(s, res, AN_RES_WAIT_HTTP))
1992 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001993 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001994 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001995 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001996 }
1997
Emeric Brun1d33b292010-01-04 15:47:17 +01001998 if (ana_list & AN_RES_STORE_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001999 if (!flt_analyze(s, res, AN_RES_STORE_RULES))
2000 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002001 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01002002 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002003 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01002004 }
2005
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002006 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02002007 if (!flt_analyze(s, res, AN_RES_HTTP_PROCESS_BE))
2008 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002009 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002010 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002011 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002012 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01002013
Christopher Fauletd7c91962015-04-30 11:48:27 +02002014 if (ana_list & AN_FLT_START_BE) {
2015 if (!flt_start_analyze(s, res, AN_FLT_START_BE))
2016 break;
2017 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_START_BE);
2018 }
2019
2020 if (ana_list & AN_FLT_XFER_DATA) {
2021 if (!flt_xfer_data(s, res, AN_FLT_XFER_DATA))
2022 break;
2023 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_XFER_DATA);
2024 }
2025
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002026 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002027 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01002028 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002029 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002030 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02002031
2032 if (ana_list & AN_FLT_END) {
2033 if (!flt_end_analyze(s, res, AN_FLT_END))
2034 break;
2035 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_END);
2036 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002037 break;
2038 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002039 }
2040
Willy Tarreau8f128b42014-11-28 15:07:47 +01002041 rp_cons_last = si_f->state;
2042 rp_prod_last = si_b->state;
2043 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002044
Willy Tarreau8f128b42014-11-28 15:07:47 +01002045 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002046 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002047 }
2048
Willy Tarreau576507f2010-01-07 00:09:04 +01002049 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002050 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002051 goto resync_request;
2052
Willy Tarreau8f128b42014-11-28 15:07:47 +01002053 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002054 goto resync_request;
2055
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002056 /* FIXME: here we should call protocol handlers which rely on
2057 * both buffers.
2058 */
2059
2060
2061 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002062 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002063 * we're just in a data phase here since it means we have not
2064 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002065 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002066 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002067 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002068 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002069 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002070 req->analysers = 0;
2071 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002072 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002073 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002074 if (srv)
2075 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002076 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002077 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002078 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002079 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002080 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002081 if (srv)
2082 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002083 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002084 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002085 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002086 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002087 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002088 if (srv)
2089 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002090 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002091 }
2092 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002093 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002094 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002095 if (srv)
2096 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002097 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002098 }
Willy Tarreau84455332009-03-15 22:34:05 +01002099 sess_set_term_flags(s);
2100 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002101 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002102 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 res->analysers = 0;
2104 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002105 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002106 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002107 if (srv)
2108 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002109 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002110 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002111 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002112 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002113 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002114 if (srv)
2115 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002116 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002117 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002118 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002119 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002120 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002121 if (srv)
2122 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002123 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002124 }
2125 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002126 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002127 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002128 if (srv)
2129 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002130 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002131 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002132 sess_set_term_flags(s);
2133 }
Willy Tarreau84455332009-03-15 22:34:05 +01002134 }
2135
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002136 /*
2137 * Here we take care of forwarding unhandled data. This also includes
2138 * connection establishments and shutdown requests.
2139 */
2140
2141
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002142 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002143 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002144 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002145 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002146 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002147 if (unlikely(!req->analysers &&
2148 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2149 (si_f->state >= SI_ST_EST) &&
2150 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002151 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002152 * attached to it. If any data are left in, we'll permit them to
2153 * move.
2154 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002155 channel_auto_read(req);
2156 channel_auto_connect(req);
2157 channel_auto_close(req);
2158 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002159
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002160 /* We'll let data flow between the producer (if still connected)
2161 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002162 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002163 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2164 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02002165
2166 /* Just in order to support fetching HTTP contents after start
2167 * of forwarding when the HTTP forwarding analyser is not used,
2168 * we simply reset msg->sov so that HTTP rewinding points to the
2169 * headers.
2170 */
2171 if (s->txn)
2172 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002173 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002174
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002175 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002176 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2177 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002178 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002179 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2180 (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 +01002181 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002182 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2183 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002184 (req->flags & CF_STREAMER_FAST)))) {
2185 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002186 }
2187
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002188 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002189 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002190
2191 /*
2192 * Now forward all shutdown requests between both sides of the buffer
2193 */
2194
Willy Tarreau520d95e2009-09-19 21:04:57 +02002195 /* first, let's check if the request buffer needs to shutdown(write), which may
2196 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002197 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2198 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002199 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002200 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002201 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002202 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002203 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002204
2205 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002206 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2207 channel_is_empty(req))) {
2208 if (req->flags & CF_READ_ERROR)
2209 si_b->flags |= SI_FL_NOLINGER;
2210 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002211 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002212 res->rto = s->be->timeout.serverfin;
2213 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002214 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002215 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002216
2217 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002218 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2219 !req->analysers))
2220 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002221
2222 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002223 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2224 if (si_f->flags & SI_FL_NOHALF)
2225 si_f->flags |= SI_FL_NOLINGER;
2226 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002227 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002228
Willy Tarreau520d95e2009-09-19 21:04:57 +02002229 /* it's possible that an upper layer has requested a connection setup or abort.
2230 * There are 2 situations where we decide to establish a new connection :
2231 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002232 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002233 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002234 if (si_b->state == SI_ST_INI) {
2235 if (!(req->flags & CF_SHUTW)) {
2236 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002237 /* If we have an appctx, there is no connect method, so we
2238 * immediately switch to the connected state, otherwise we
2239 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002240 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002241 si_b->state = SI_ST_REQ; /* new connection requested */
2242 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002243 }
Willy Tarreau73201222009-08-16 18:27:24 +02002244 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002245 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002246 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2247 channel_shutw_now(req); /* fix buffer flags upon abort */
2248 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002249 }
Willy Tarreau92795622009-03-06 12:51:23 +01002250 }
2251
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002252
2253 /* we may have a pending connection request, or a connection waiting
2254 * for completion.
2255 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002256 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002257
2258 /* prune the request variables and swap to the response variables. */
2259 if (s->vars_reqres.scope != SCOPE_RES) {
2260 vars_prune(&s->vars_reqres, s);
2261 vars_init(&s->vars_reqres, SCOPE_RES);
2262 }
2263
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002264 do {
2265 /* nb: step 1 might switch from QUE to ASS, but we first want
2266 * to give a chance to step 2 to perform a redirect if needed.
2267 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002268 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002269 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002270 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002271 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002272
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002273 /* applets directly go to the ESTABLISHED state. Similarly,
2274 * servers experience the same fate when their connection
2275 * is reused.
2276 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002277 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002278 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002279
Willy Tarreaub44c8732013-12-31 23:16:50 +01002280 /* Now we can add the server name to a header (if requested) */
2281 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002282 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002283 (s->be->server_id_hdr_name != NULL) &&
2284 (s->be->mode == PR_MODE_HTTP) &&
2285 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002286 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002287 }
2288
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002289 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002290 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002291 http_perform_server_redirect(s, si_b);
2292 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002293 }
2294
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002295 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002296 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297 goto resync_stream_interface;
2298
Willy Tarreau815a9b22010-07-27 17:15:12 +02002299 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002300 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002301 goto resync_request;
2302
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002303 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002304
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002305 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002306 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002307 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002308 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002309 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002310 if (unlikely(!res->analysers &&
2311 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2312 (si_b->state >= SI_ST_EST) &&
2313 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002314 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002315 * attached to it. If any data are left in, we'll permit them to
2316 * move.
2317 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002318 channel_auto_read(res);
2319 channel_auto_close(res);
2320 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002321
2322 /* We'll let data flow between the producer (if still connected)
2323 * to the consumer.
2324 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2326 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002327
Willy Tarreau42529c32015-07-09 18:38:57 +02002328 /* Just in order to support fetching HTTP contents after start
2329 * of forwarding when the HTTP forwarding analyser is not used,
2330 * we simply reset msg->sov so that HTTP rewinding points to the
2331 * headers.
2332 */
2333 if (s->txn)
2334 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2335
Willy Tarreauce887fd2012-05-12 12:50:00 +02002336 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002337 * tunnel timeout set, use it now. Note that we must respect
2338 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002339 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 if (!req->analysers && s->be->timeout.tunnel) {
2341 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002342 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002343
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002344 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2345 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002346 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2347 res->rto = s->be->timeout.serverfin;
2348 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2349 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002350 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2351 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002352
Willy Tarreau8f128b42014-11-28 15:07:47 +01002353 req->rex = tick_add(now_ms, req->rto);
2354 req->wex = tick_add(now_ms, req->wto);
2355 res->rex = tick_add(now_ms, res->rto);
2356 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002357 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002358 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002359
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002360 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002361 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2362 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002363 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002364 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2365 (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 +01002366 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002367 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2368 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002369 (res->flags & CF_STREAMER_FAST)))) {
2370 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002371 }
2372
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002373 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002374 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002375
2376 /*
2377 * Now forward all shutdown requests between both sides of the buffer
2378 */
2379
2380 /*
2381 * FIXME: this is probably where we should produce error responses.
2382 */
2383
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002384 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002385 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002386 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002387 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002388 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002389
2390 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002391 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2392 channel_is_empty(res))) {
2393 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002394 if (tick_isset(sess->fe->timeout.clientfin)) {
2395 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002396 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002397 }
2398 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399
2400 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002401 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2402 !res->analysers)
2403 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002404
2405 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002406 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2407 if (si_b->flags & SI_FL_NOHALF)
2408 si_b->flags |= SI_FL_NOLINGER;
2409 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002410 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002411
Willy Tarreau8f128b42014-11-28 15:07:47 +01002412 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002413 goto resync_stream_interface;
2414
Willy Tarreau8f128b42014-11-28 15:07:47 +01002415 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002416 goto resync_request;
2417
Willy Tarreau8f128b42014-11-28 15:07:47 +01002418 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002419 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002420
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002421 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002422 si_f->flags &= ~SI_FL_DONT_WAKE;
2423 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002424
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002425 /* This is needed only when debugging is enabled, to indicate
2426 * client-side or server-side close. Please note that in the unlikely
2427 * event where both sides would close at once, the sequence is reported
2428 * on the server side first.
2429 */
2430 if (unlikely((global.mode & MODE_DEBUG) &&
2431 (!(global.mode & MODE_QUIET) ||
2432 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002433 if (si_b->state == SI_ST_CLO &&
2434 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002435 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002436 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002437 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2438 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002439 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002440 }
2441
Willy Tarreau8f128b42014-11-28 15:07:47 +01002442 if (si_f->state == SI_ST_CLO &&
2443 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002444 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002445 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002446 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2447 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002448 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002449 }
2450 }
2451
Willy Tarreau8f128b42014-11-28 15:07:47 +01002452 if (likely((si_f->state != SI_ST_CLO) ||
2453 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002454
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002455 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002456 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002457
Willy Tarreau563cc372015-04-19 18:13:56 +02002458 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002459 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002460
Willy Tarreau563cc372015-04-19 18:13:56 +02002461 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002462 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002463
Willy Tarreau8f128b42014-11-28 15:07:47 +01002464 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2465 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2466 si_f->prev_state = si_f->state;
2467 si_b->prev_state = si_b->state;
2468 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2469 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002470
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002471 /* Trick: if a request is being waiting for the server to respond,
2472 * and if we know the server can timeout, we don't want the timeout
2473 * to expire on the client side first, but we're still interested
2474 * in passing data from the client to the server (eg: POST). Thus,
2475 * we can cancel the client's request timeout if the server's
2476 * request timeout is set and the server has not yet sent a response.
2477 */
2478
Willy Tarreau8f128b42014-11-28 15:07:47 +01002479 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2480 (tick_isset(req->wex) || tick_isset(res->rex))) {
2481 req->flags |= CF_READ_NOEXP;
2482 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002483 }
2484
Willy Tarreau798f4322012-11-08 14:49:17 +01002485 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002486 t->expire = tick_first(tick_first(req->rex, req->wex),
2487 tick_first(res->rex, res->wex));
2488 if (req->analysers)
2489 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002490
Willy Tarreau8f128b42014-11-28 15:07:47 +01002491 if (si_f->exp)
2492 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002493
Willy Tarreau8f128b42014-11-28 15:07:47 +01002494 if (si_b->exp)
2495 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002496
2497#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002498 fprintf(stderr,
2499 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2500 " 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 +01002501 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2502 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002503#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002504
2505#ifdef DEBUG_DEV
2506 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002507 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002508 ABORT_NOW();
2509#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002510 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002511 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002512 }
2513
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002514 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002515 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002516 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002517 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002518 if (sess->listener) {
2519 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002520 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002521 sess->listener->nbconn--;
2522 if (sess->listener->state == LI_FULL)
2523 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002524
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002525 /* Dequeues all of the listeners waiting for a resource */
2526 if (!LIST_ISEMPTY(&global_listener_queue))
2527 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002528
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002529 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2530 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2531 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002532 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002533
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002534 if (unlikely((global.mode & MODE_DEBUG) &&
2535 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002536 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002537 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002538 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2539 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002540 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002541 }
2542
2543 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002544 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002545
Willy Tarreaueee5b512015-04-03 23:46:31 +02002546 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002547 int n;
2548
Willy Tarreaueee5b512015-04-03 23:46:31 +02002549 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002550 if (n < 1 || n > 5)
2551 n = 0;
2552
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002553 if (sess->fe->mode == PR_MODE_HTTP) {
2554 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002555 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002556 sess->fe->fe_counters.p.http.comp_rsp++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002557 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002558 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002559 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002560 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002561 s->be->be_counters.p.http.cum_req++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002562 if (s->comp_algo && (s->flags & SF_COMP_READY))
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002563 s->be->be_counters.p.http.comp_rsp++;
2564 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002565 }
2566
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002567 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002568 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002569 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002570 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002571 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002572 }
2573
Willy Tarreau87b09662015-04-03 00:22:06 +02002574 /* update time stats for this stream */
2575 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002576
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002577 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002578 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002579 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002580 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002581 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002582}
2583
Willy Tarreau87b09662015-04-03 00:22:06 +02002584/* Update the stream's backend and server time stats */
2585void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002586{
2587 int t_request;
2588 int t_queue;
2589 int t_connect;
2590 int t_data;
2591 int t_close;
2592 struct server *srv;
2593
2594 t_request = 0;
2595 t_queue = s->logs.t_queue;
2596 t_connect = s->logs.t_connect;
2597 t_close = s->logs.t_close;
2598 t_data = s->logs.t_data;
2599
2600 if (s->be->mode != PR_MODE_HTTP)
2601 t_data = t_connect;
2602
2603 if (t_connect < 0 || t_data < 0)
2604 return;
2605
2606 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2607 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2608
2609 t_data -= t_connect;
2610 t_connect -= t_queue;
2611 t_queue -= t_request;
2612
2613 srv = objt_server(s->target);
2614 if (srv) {
2615 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2616 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2617 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2618 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2619 }
2620 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2621 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2622 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2623 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2624}
2625
Willy Tarreau7c669d72008-06-20 15:04:11 +02002626/*
2627 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002628 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002629 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002630 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002631 * server.
2632 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002633void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002634{
2635 if (sess->srv_conn == newsrv)
2636 return;
2637
2638 if (sess->srv_conn) {
2639 sess->srv_conn->served--;
2640 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2641 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002642 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002643 }
2644
2645 if (newsrv) {
2646 newsrv->served++;
2647 if (newsrv->proxy->lbprm.server_take_conn)
2648 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002649 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002650 }
2651}
2652
Willy Tarreau84455332009-03-15 22:34:05 +01002653/* Handle server-side errors for default protocols. It is called whenever a a
2654 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002655 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002656 * them. It's installed as ->srv_error for the server-side stream_interface.
2657 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002658void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002659{
2660 int err_type = si->err_type;
2661 int err = 0, fin = 0;
2662
2663 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002664 err = SF_ERR_CLICL;
2665 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002666 }
2667 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002668 err = SF_ERR_CLICL;
2669 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002670 }
2671 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002672 err = SF_ERR_SRVTO;
2673 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002674 }
2675 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002676 err = SF_ERR_SRVCL;
2677 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002678 }
2679 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002680 err = SF_ERR_SRVTO;
2681 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002682 }
2683 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002684 err = SF_ERR_SRVCL;
2685 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002686 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002687 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002688 err = SF_ERR_RESOURCE;
2689 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002690 }
Willy Tarreau84455332009-03-15 22:34:05 +01002691 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002692 err = SF_ERR_INTERNAL;
2693 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002694 }
2695
Willy Tarreaue7dff022015-04-03 01:14:29 +02002696 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002697 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002698 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002699 s->flags |= fin;
2700}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002701
Willy Tarreaue7dff022015-04-03 01:14:29 +02002702/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002703void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002704{
Willy Tarreau87b09662015-04-03 00:22:06 +02002705 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002706 return;
2707
Willy Tarreau87b09662015-04-03 00:22:06 +02002708 channel_shutw_now(&stream->req);
2709 channel_shutr_now(&stream->res);
2710 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002711 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002712 stream->flags |= why;
2713 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002714}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002715
Willy Tarreau8b22a712010-06-18 17:46:06 +02002716/************************************************************************/
2717/* All supported ACL keywords must be declared here. */
2718/************************************************************************/
2719
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002720/* Returns a pointer to a stkctr depending on the fetch keyword name.
2721 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002722 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002723 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002724 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002725 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002726 * NULL may be returned if the designated stkctr is not tracked. For
2727 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2728 * passed. When present, the currently tracked key is then looked up
2729 * in the specified table instead of the current table. The purpose is
2730 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002731 * multiple tables). <strm> is allowed to be NULL, in which case only
2732 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002733 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002734struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002735smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002736{
2737 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002738 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002739 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002740 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002741 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002742
Willy Tarreau0f791d42013-07-23 19:56:43 +02002743 if (num == '_' - '0') {
2744 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002745 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002746 if (num >= MAX_SESS_STKCTR)
2747 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002748 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002749 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002750 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002751 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002752 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002753
2754 if (!conn)
2755 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002756
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002757 /* Fetch source adress in a sample. */
2758 smp.px = NULL;
2759 smp.sess = sess;
2760 smp.strm = strm;
2761 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2762 return NULL;
2763
2764 /* Converts into key. */
2765 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002766 if (!key)
2767 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002768
Willy Tarreaua65536c2013-07-22 22:40:11 +02002769 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002770 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002771 return &stkctr;
2772 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002773
2774 /* Here, <num> contains the counter number from 0 to 9 for
2775 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002776 * args[arg] is the first optional argument. We first lookup the
2777 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002778 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002779
2780 stkptr = &strm->stkctr[num];
2781 if (!strm || !stkctr_entry(stkptr)) {
2782 stkptr = &sess->stkctr[num];
2783 if (!stkctr_entry(stkptr))
2784 return NULL;
2785 }
2786
2787 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002788 if (!stksess)
2789 return NULL;
2790
Willy Tarreau0f791d42013-07-23 19:56:43 +02002791 if (unlikely(args[arg].type == ARGT_TAB)) {
2792 /* an alternate table was specified, let's look up the same key there */
2793 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002794 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002795 return &stkctr;
2796 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002797 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002798}
2799
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002800/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2801 * the entry if it doesn't exist yet. This is needed for a few fetch
2802 * functions which need to create an entry, such as src_inc_gpc* and
2803 * src_clr_gpc*.
2804 */
2805struct stkctr *
2806smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2807{
2808 static struct stkctr stkctr;
2809 struct stktable_key *key;
2810 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002811 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002812
2813 if (strncmp(kw, "src_", 4) != 0)
2814 return NULL;
2815
2816 if (!conn)
2817 return NULL;
2818
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002819 /* Fetch source adress in a sample. */
2820 smp.px = NULL;
2821 smp.sess = sess;
2822 smp.strm = strm;
2823 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2824 return NULL;
2825
2826 /* Converts into key. */
2827 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002828 if (!key)
2829 return NULL;
2830
2831 stkctr.table = &args->data.prx->table;
2832 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2833 return &stkctr;
2834}
2835
Willy Tarreau87b09662015-04-03 00:22:06 +02002836/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002837 * currently being tracked or not.
2838 * Supports being called as "sc[0-9]_tracked" only.
2839 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002840static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002841smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002842{
2843 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002844 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002845 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002846 return 1;
2847}
2848
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002849/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2850 * frontend counters or from the src.
2851 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2852 * zero is returned if the key is new.
2853 */
2854static int
2855smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2856{
2857 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2858
2859 if (!stkctr)
2860 return 0;
2861
2862 smp->flags = SMP_F_VOL_TEST;
2863 smp->data.type = SMP_T_SINT;
2864 smp->data.u.sint = 0;
2865
2866 if (stkctr_entry(stkctr) != NULL) {
2867 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2868 if (!ptr)
2869 return 0; /* parameter not stored */
2870 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2871 }
2872 return 1;
2873}
2874
Willy Tarreau87b09662015-04-03 00:22:06 +02002875/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002876 * frontend counters or from the src.
2877 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2878 * zero is returned if the key is new.
2879 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002880static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002881smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002882{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002883 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002884
2885 if (!stkctr)
2886 return 0;
2887
Willy Tarreau37406352012-04-23 16:16:37 +02002888 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002889 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002890 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002891
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002892 if (stkctr_entry(stkctr) != NULL) {
2893 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002894 if (!ptr)
2895 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002896 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002897 }
2898 return 1;
2899}
2900
Willy Tarreau87b09662015-04-03 00:22:06 +02002901/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002902 * tracked frontend counters or from the src.
2903 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2904 * Value zero is returned if the key is new.
2905 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002906static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002907smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002908{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002909 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002910
2911 if (!stkctr)
2912 return 0;
2913
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002914 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002915 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002916 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002917 if (stkctr_entry(stkctr) != NULL) {
2918 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002919 if (!ptr)
2920 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002921 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002922 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002923 }
2924 return 1;
2925}
2926
Willy Tarreau87b09662015-04-03 00:22:06 +02002927/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002928 * frontend counters and return it into temp integer.
2929 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002930 */
2931static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002932smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002933{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002934 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002935
2936 if (!stkctr)
2937 return 0;
2938
Willy Tarreau37406352012-04-23 16:16:37 +02002939 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002940 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002941 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002942
2943 if (stkctr_entry(stkctr) == NULL)
2944 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2945
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002946 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002947 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002948
2949 /* First, update gpc0_rate if it's tracked. Second, update its
2950 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2951 */
Emeric Brun57056f02015-06-15 18:29:57 +02002952 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2953 if (ptr1) {
2954 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002955 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002956 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002957 }
2958
Emeric Brun57056f02015-06-15 18:29:57 +02002959 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2960 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002961 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002962
Emeric Brun57056f02015-06-15 18:29:57 +02002963 /* If data was modified, we need to touch to re-schedule sync */
2964 if (ptr1 || ptr2)
2965 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002966 }
2967 return 1;
2968}
2969
Willy Tarreau87b09662015-04-03 00:22:06 +02002970/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002971 * frontend counters and return its previous value into temp integer.
2972 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002973 */
2974static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002975smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002976{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002977 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002978
2979 if (!stkctr)
2980 return 0;
2981
Willy Tarreau37406352012-04-23 16:16:37 +02002982 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002983 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002984 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002985
2986 if (stkctr_entry(stkctr) == NULL)
2987 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2988
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002989 if (stkctr_entry(stkctr) != NULL) {
2990 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002991 if (!ptr)
2992 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002993 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002994 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002995 /* If data was modified, we need to touch to re-schedule sync */
2996 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002997 }
2998 return 1;
2999}
3000
Willy Tarreau87b09662015-04-03 00:22:06 +02003001/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003002 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
3003 * "src_conn_cnt" only.
3004 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003005static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003006smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003007{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003008 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02003009
3010 if (!stkctr)
3011 return 0;
3012
Willy Tarreau37406352012-04-23 16:16:37 +02003013 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003014 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003015 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003016 if (stkctr_entry(stkctr) != NULL) {
3017 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003018 if (!ptr)
3019 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003020 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003021 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003022 return 1;
3023}
3024
Willy Tarreau87b09662015-04-03 00:22:06 +02003025/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02003026 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3027 * only.
3028 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003029static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003030smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003031{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003032 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02003033
3034 if (!stkctr)
3035 return 0;
3036
Willy Tarreau37406352012-04-23 16:16:37 +02003037 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003038 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003039 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003040 if (stkctr_entry(stkctr) != NULL) {
3041 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003042 if (!ptr)
3043 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003044 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02003045 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003046 }
3047 return 1;
3048}
3049
Willy Tarreau87b09662015-04-03 00:22:06 +02003050/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02003051 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003052 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02003053 */
3054static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003055smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003056{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003057 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003058 struct stksess *ts;
3059 struct stktable_key *key;
3060 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003061 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003062
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003063 if (!conn)
3064 return 0;
3065
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02003066 /* Fetch source adress in a sample. */
3067 if (!smp_fetch_src(NULL, smp, NULL, NULL))
3068 return 0;
3069
3070 /* Converts into key. */
3071 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003072 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003073 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003074
Willy Tarreau24e32d82012-04-23 23:55:44 +02003075 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003076
Willy Tarreau1f7e9252010-06-20 12:27:21 +02003077 if ((ts = stktable_update_key(&px->table, key)) == NULL)
3078 /* entry does not exist and could not be created */
3079 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003080
3081 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
3082 if (!ptr)
3083 return 0; /* parameter not stored in this table */
3084
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003085 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003086 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02003087 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02003088 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003089 return 1;
3090}
3091
Willy Tarreau87b09662015-04-03 00:22:06 +02003092/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02003093 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3094 * "src_conn_cur" only.
3095 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003096static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003097smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003098{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003099 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003100
3101 if (!stkctr)
3102 return 0;
3103
Willy Tarreau37406352012-04-23 16:16:37 +02003104 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003105 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003106 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003107 if (stkctr_entry(stkctr) != NULL) {
3108 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003109 if (!ptr)
3110 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003111 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003112 }
3113 return 1;
3114}
3115
Willy Tarreau87b09662015-04-03 00:22:06 +02003116/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003117 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3118 * "src_sess_cnt" only.
3119 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003120static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003121smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003122{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003123 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003124
3125 if (!stkctr)
3126 return 0;
3127
Willy Tarreau37406352012-04-23 16:16:37 +02003128 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003129 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003130 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003131 if (stkctr_entry(stkctr) != NULL) {
3132 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003133 if (!ptr)
3134 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003135 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003136 }
3137 return 1;
3138}
3139
Willy Tarreau87b09662015-04-03 00:22:06 +02003140/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003141 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3142 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003143static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003144smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003145{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003146 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003147
3148 if (!stkctr)
3149 return 0;
3150
Willy Tarreau37406352012-04-23 16:16:37 +02003151 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003152 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003153 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003154 if (stkctr_entry(stkctr) != NULL) {
3155 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003156 if (!ptr)
3157 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003158 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003159 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003160 }
3161 return 1;
3162}
3163
Willy Tarreau87b09662015-04-03 00:22:06 +02003164/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003165 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3166 * "src_http_req_cnt" only.
3167 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003168static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003169smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003170{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003171 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003172
3173 if (!stkctr)
3174 return 0;
3175
Willy Tarreau37406352012-04-23 16:16:37 +02003176 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003177 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003178 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003179 if (stkctr_entry(stkctr) != NULL) {
3180 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003181 if (!ptr)
3182 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003183 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003184 }
3185 return 1;
3186}
3187
Willy Tarreau87b09662015-04-03 00:22:06 +02003188/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003189 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3190 * "src_http_req_rate" only.
3191 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003192static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003193smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003194{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003195 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003196
3197 if (!stkctr)
3198 return 0;
3199
Willy Tarreau37406352012-04-23 16:16:37 +02003200 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003201 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003202 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003203 if (stkctr_entry(stkctr) != NULL) {
3204 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003205 if (!ptr)
3206 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003207 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003208 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003209 }
3210 return 1;
3211}
3212
Willy Tarreau87b09662015-04-03 00:22:06 +02003213/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003214 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3215 * "src_http_err_cnt" only.
3216 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003217static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003218smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003219{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003220 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003221
3222 if (!stkctr)
3223 return 0;
3224
Willy Tarreau37406352012-04-23 16:16:37 +02003225 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003226 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003227 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003228 if (stkctr_entry(stkctr) != NULL) {
3229 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003230 if (!ptr)
3231 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003232 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003233 }
3234 return 1;
3235}
3236
Willy Tarreau87b09662015-04-03 00:22:06 +02003237/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003238 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3239 * "src_http_err_rate" only.
3240 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003241static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003242smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003243{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003244 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003245
3246 if (!stkctr)
3247 return 0;
3248
Willy Tarreau37406352012-04-23 16:16:37 +02003249 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003250 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003251 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003252 if (stkctr_entry(stkctr) != NULL) {
3253 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003254 if (!ptr)
3255 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003256 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003257 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003258 }
3259 return 1;
3260}
3261
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003262/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003263 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003264 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3265 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003266static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003267smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003268{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003269 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003270
3271 if (!stkctr)
3272 return 0;
3273
Willy Tarreau37406352012-04-23 16:16:37 +02003274 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003275 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003276 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003277 if (stkctr_entry(stkctr) != NULL) {
3278 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003279 if (!ptr)
3280 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003281 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003282 }
3283 return 1;
3284}
3285
Willy Tarreau613fe992013-07-23 17:39:19 +02003286/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003287 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003288 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003289 */
3290static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003291smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003292{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003293 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003294
3295 if (!stkctr)
3296 return 0;
3297
Willy Tarreau37406352012-04-23 16:16:37 +02003298 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003299 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003300 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003301 if (stkctr_entry(stkctr) != NULL) {
3302 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003303 if (!ptr)
3304 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003305 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003306 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003307 }
3308 return 1;
3309}
3310
Willy Tarreau53aea102013-07-23 17:39:02 +02003311/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003312 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003313 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3314 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003315static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003316smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003317{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003318 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003319
3320 if (!stkctr)
3321 return 0;
3322
Willy Tarreau37406352012-04-23 16:16:37 +02003323 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003324 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003325 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003326 if (stkctr_entry(stkctr) != NULL) {
3327 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003328 if (!ptr)
3329 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003330 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003331 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003332 return 1;
3333}
3334
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003335/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003336 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003337 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003338 */
3339static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003340smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003341{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003342 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003343
3344 if (!stkctr)
3345 return 0;
3346
Willy Tarreau37406352012-04-23 16:16:37 +02003347 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003348 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003349 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003350 if (stkctr_entry(stkctr) != NULL) {
3351 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003352 if (!ptr)
3353 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003354 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003355 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003356 }
3357 return 1;
3358}
3359
Willy Tarreau87b09662015-04-03 00:22:06 +02003360/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003361 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3362 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003363static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003364smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003365{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003366 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003367
Willy Tarreau563eef42013-07-23 18:32:02 +02003368 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003369 return 0;
3370
Willy Tarreau563eef42013-07-23 18:32:02 +02003371 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003372 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003373 smp->data.u.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003374 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003375}
3376
Willy Tarreau34db1082012-04-19 17:16:54 +02003377/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003378 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003379 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003380static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003381smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003382{
Willy Tarreau37406352012-04-23 16:16:37 +02003383 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003384 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003385 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003386 return 1;
3387}
3388
Willy Tarreau34db1082012-04-19 17:16:54 +02003389/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003390 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003391 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003392static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003393smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003394{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003395 struct proxy *px;
3396
Willy Tarreau24e32d82012-04-23 23:55:44 +02003397 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003398 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003399 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003400 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003401 return 1;
3402}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003403
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003404/* 0=OK, <0=Alert, >0=Warning */
3405static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3406 struct proxy *px, struct act_rule *rule,
3407 char **err)
3408{
3409 struct action_kw *kw;
3410
3411 /* Check if the service name exists. */
3412 if (*(args[*cur_arg]) == 0) {
3413 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003414 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003415 }
3416
3417 /* lookup for keyword corresponding to a service. */
3418 kw = action_lookup(&service_keywords, args[*cur_arg]);
3419 if (!kw) {
3420 memprintf(err, "'%s' unknown service name.", args[1]);
3421 return ACT_RET_PRS_ERR;
3422 }
3423 (*cur_arg)++;
3424
3425 /* executes specific rule parser. */
3426 rule->kw = kw;
3427 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3428 return ACT_RET_PRS_ERR;
3429
3430 /* Register processing function. */
3431 rule->action_ptr = process_use_service;
3432 rule->action = ACT_CUSTOM;
3433
3434 return ACT_RET_PRS_OK;
3435}
3436
3437void service_keywords_register(struct action_kw_list *kw_list)
3438{
3439 LIST_ADDQ(&service_keywords, &kw_list->list);
3440}
3441
3442/* main configuration keyword registration. */
3443static struct action_kw_list stream_tcp_keywords = { ILH, {
3444 { "use-service", stream_parse_use_service },
3445 { /* END */ }
3446}};
3447
3448static struct action_kw_list stream_http_keywords = { ILH, {
3449 { "use-service", stream_parse_use_service },
3450 { /* END */ }
3451}};
3452
Willy Tarreau61612d42012-04-19 18:42:05 +02003453/* Note: must not be declared <const> as its list will be overwritten.
3454 * Please take care of keeping this list alphabetically sorted.
3455 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003456static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003457 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003458}};
3459
Willy Tarreau281c7992013-01-08 01:23:27 +01003460/* Note: must not be declared <const> as its list will be overwritten.
3461 * Please take care of keeping this list alphabetically sorted.
3462 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003463static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003464 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3465 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3466 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3467 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3468 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3469 { "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 +02003470 { "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 +02003471 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3472 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3473 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3474 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3475 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3476 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3477 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3478 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3479 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3480 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3481 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3482 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3483 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003484 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3485 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3486 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3487 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3488 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3489 { "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 +02003490 { "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 +02003491 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3492 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3493 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3494 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3495 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3496 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3497 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3498 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3499 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3500 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3501 { "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 +02003502 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003503 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3504 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3505 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3506 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3507 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3508 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3509 { "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 +02003510 { "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 +02003511 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3512 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3513 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3514 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3515 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3516 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3517 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3518 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3519 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3520 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3521 { "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 +02003522 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003523 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3524 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3525 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3526 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3527 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3528 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3529 { "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 +02003530 { "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 +02003531 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3532 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3533 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3534 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3535 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3536 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3537 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3538 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3539 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3540 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3541 { "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 +02003542 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003543 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3544 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3545 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3546 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3547 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3548 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3549 { "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 +02003550 { "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 +02003551 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3552 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3553 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3554 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3555 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3556 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3557 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3558 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3559 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3560 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3561 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3562 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3563 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3564 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003565 { /* END */ },
3566}};
3567
Willy Tarreau8b22a712010-06-18 17:46:06 +02003568__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003569static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003570{
Willy Tarreau281c7992013-01-08 01:23:27 +01003571 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003572 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003573 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3574 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003575}
3576
Willy Tarreaubaaee002006-06-26 02:48:02 +02003577/*
3578 * Local variables:
3579 * c-indent-level: 8
3580 * c-basic-offset: 8
3581 * End:
3582 */