blob: b5cfcfdfc1de3ba78009b2aa9ae629e7adcc48b1 [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;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020079 struct connection *conn = objt_conn(origin);
80 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020081
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020082 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020083 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020084
85 /* minimum stream initialization required for an embryonic stream is
86 * fairly low. We need very little to execute L4 ACLs, then we need a
87 * task to make the client-side connection live on its own.
88 * - flags
89 * - stick-entry tracking
90 */
91 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020092 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020093 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020094 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
95 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
96 tv_zero(&s->logs.tv_request);
97 s->logs.t_queue = -1;
98 s->logs.t_connect = -1;
99 s->logs.t_data = -1;
100 s->logs.t_close = 0;
101 s->logs.bytes_in = s->logs.bytes_out = 0;
102 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
103 s->logs.srv_queue_size = 0; /* we will get this number soon */
104
105 /* default logging function */
106 s->do_log = strm_log;
107
108 /* default error reporting function, may be changed by analysers */
109 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200110
111 /* Initialise the current rule list pointer to NULL. We are sure that
112 * any rulelist match the NULL pointer.
113 */
114 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200115 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200116
Willy Tarreaua68f7622015-09-21 17:48:24 +0200117 /* Copy SC counters for the stream. We don't touch refcounts because
118 * any reference we have is inherited from the session. Since the stream
119 * doesn't exist without the session, the session's existence guarantees
120 * we don't lose the entry. During the store operation, the stream won't
121 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200122 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200123 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200124
125 s->sess = sess;
126 s->si[0].flags = SI_FL_NONE;
127 s->si[1].flags = SI_FL_ISBACK;
128
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200129 s->uniq_id = global.req_count++;
130
Willy Tarreau87b09662015-04-03 00:22:06 +0200131 /* OK, we're keeping the stream, so let's properly initialize the stream */
132 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200133 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100134 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200135
Willy Tarreaue7dff022015-04-03 01:14:29 +0200136 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200137 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200138
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200139 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200140 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200141 t->context = s;
142 t->expire = TICK_ETERNITY;
143
Willy Tarreau87b09662015-04-03 00:22:06 +0200144 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200145 * This changes later when switching rules are executed or
146 * when the default backend is assigned.
147 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200148 s->be = sess->fe;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100149 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200150 s->req_cap = NULL;
151 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200152
Willy Tarreauebcd4842015-06-19 11:59:02 +0200153 /* Initialise all the variables contexts even if not used.
154 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200155 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200156 vars_init(&s->vars_txn, SCOPE_TXN);
157 vars_init(&s->vars_reqres, SCOPE_REQ);
158
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200159 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100160 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200161 si_set_state(&s->si[0], SI_ST_EST);
162
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200163 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200164 if (conn)
165 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200166 else if (appctx)
167 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200168
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200169 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200170 s->si[0].flags |= SI_FL_INDEP_STR;
171
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200172 /* pre-initialize the other side's stream interface to an INIT state. The
173 * callbacks will be initialized before attempting to connect.
174 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100175 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200176
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200177 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200178 s->si[1].flags |= SI_FL_INDEP_STR;
179
Willy Tarreau87b09662015-04-03 00:22:06 +0200180 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200181 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200182 s->pend_pos = NULL;
183
184 /* init store persistence */
185 s->store_count = 0;
186
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100187 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100188 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200189 s->req.analysers = 0;
190 channel_auto_connect(&s->req); /* don't wait to establish connection */
191 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200192
193 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100194 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100195 s->req.rex = TICK_ETERNITY;
196 s->req.wex = TICK_ETERNITY;
197 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200198
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100199 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100200 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100201 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200202
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200203 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100204 s->req.flags |= CF_NEVER_WAIT;
205 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200206 }
207
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200208 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100209 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100210 s->res.rex = TICK_ETERNITY;
211 s->res.wex = TICK_ETERNITY;
212 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200213
Willy Tarreaueee5b512015-04-03 23:46:31 +0200214 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100215
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100216 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100217
Christopher Faulet92d36382015-11-05 13:35:03 +0100218 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200219 goto out_fail_accept;
220
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200221 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200222 if (conn)
223 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200224 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200225 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200226
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200227 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200228 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200229
230 /* it is important not to call the wakeup function directly but to
231 * pass through task_wakeup(), because this one knows how to apply
232 * priorities to tasks.
233 */
234 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200235 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200236
237 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200238 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100239 flt_stream_release(s, 0);
Willy Tarreau3b246412014-11-25 17:10:33 +0100240 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200241 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200242 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200243}
244
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200246 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200248static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200250 struct session *sess = strm_sess(s);
251 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100252 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200253 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200254 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100255
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256 if (s->pend_pos)
257 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100258
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100259 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200260 if (s->flags & SF_CURR_SESS) {
261 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100262 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100263 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100264 if (may_dequeue_tasks(objt_server(s->target), s->be))
265 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100266 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100267
Willy Tarreau7c669d72008-06-20 15:04:11 +0200268 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200269 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200270 * it should normally be only the same as the one above,
271 * so this should not happen in fact.
272 */
273 sess_change_server(s, NULL);
274 }
275
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100276 if (s->req.pipe)
277 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100278
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100279 if (s->res.pipe)
280 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100281
Willy Tarreaubf883e02014-11-25 21:10:35 +0100282 /* We may still be present in the buffer wait queue */
283 if (!LIST_ISEMPTY(&s->buffer_wait)) {
284 LIST_DEL(&s->buffer_wait);
285 LIST_INIT(&s->buffer_wait);
286 }
287
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100288 b_drop(&s->req.buf);
289 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100290 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200291 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200292
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100293 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200294 if (s->txn)
295 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100296
Willy Tarreau1e954912012-10-12 17:50:05 +0200297 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200298 if (cli_conn)
299 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200300
Willy Tarreaua4cda672010-06-06 18:28:49 +0200301 for (i = 0; i < s->store_count; i++) {
302 if (!s->store[i].ts)
303 continue;
304 stksess_free(s->store[i].table, s->store[i].ts);
305 s->store[i].ts = NULL;
306 }
307
Willy Tarreaueee5b512015-04-03 23:46:31 +0200308 if (s->txn) {
309 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
310 pool_free2(pool2_http_txn, s->txn);
311 s->txn = NULL;
312 }
313
Christopher Fauletd7c91962015-04-30 11:48:27 +0200314 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100315 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200316
Willy Tarreau92fb9832007-10-16 17:34:28 +0200317 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200318 pool_free2(fe->rsp_cap_pool, s->res_cap);
319 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100321
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200322 /* Cleanup all variable contexts. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100323 vars_prune(&s->vars_txn, s->sess, s);
324 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200325
Willy Tarreau87b09662015-04-03 00:22:06 +0200326 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200327
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100328 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100329 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200330 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100331 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100332 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100333 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200334 if (s->list.n != &streams)
335 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100336 bref->ref = s->list.n;
337 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100338 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200339 si_release_endpoint(&s->si[1]);
340 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200341
342 /* FIXME: for now we have a 1:1 relation between stream and session so
343 * the stream must free the session.
344 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200345 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200346 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200347
348 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200349 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200350 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200351 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200352 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200353 pool_flush2(pool2_requri);
354 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200355 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200356 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100357 pool_flush2(pool2_connection);
358 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200359 pool_flush2(fe->req_cap_pool);
360 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200361 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200362}
363
Willy Tarreau78955f42014-12-28 13:09:02 +0100364/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
365 * that it's not the last available buffer or it's the response buffer. Unless
366 * the buffer is the response buffer, an extra control is made so that we always
367 * keep <tune.buffers.reserved> buffers available after this allocation. To be
368 * called at the beginning of recv() callbacks to ensure that the required
369 * buffers are properly allocated. Returns 0 in case of failure, non-zero
370 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100371 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200372int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100373{
Willy Tarreau87b09662015-04-03 00:22:06 +0200374 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100375 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100376 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100377
Willy Tarreau78955f42014-12-28 13:09:02 +0100378 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100379 margin = global.tune.reserved_bufs;
380
Thierry FOURNIER27929fb2015-09-25 08:36:11 +0200381 s = chn_strm(chn);
Willy Tarreau78955f42014-12-28 13:09:02 +0100382
383 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100384 if (b)
385 return 1;
386
Willy Tarreaubf883e02014-11-25 21:10:35 +0100387 if (LIST_ISEMPTY(&s->buffer_wait))
388 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100389 return 0;
390}
391
Willy Tarreau87b09662015-04-03 00:22:06 +0200392/* Allocates a work buffer for stream <s>. It is meant to be called inside
393 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200394 * to work fine, which is the response buffer so that an error message may be
395 * built and returned. Response buffers may be allocated from the reserve, this
396 * is critical to ensure that a response may always flow and will never block a
397 * server from releasing a connection. Returns 0 in case of failure, non-zero
398 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100399 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200400int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100401{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100402 if (!LIST_ISEMPTY(&s->buffer_wait)) {
403 LIST_DEL(&s->buffer_wait);
404 LIST_INIT(&s->buffer_wait);
405 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100406
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200407 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100408 return 1;
409
Willy Tarreaubf883e02014-11-25 21:10:35 +0100410 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100411 return 0;
412}
413
414/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100415 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200416 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100417 * a single buffer, so it makes sense to try to wake two up when two buffers
418 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100419 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200420void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100421{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100422 if (s->req.buf->size && buffer_empty(s->req.buf))
423 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100424
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100425 if (s->res.buf->size && buffer_empty(s->res.buf))
426 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100427
Willy Tarreaubf883e02014-11-25 21:10:35 +0100428 /* if we're certain to have at least 1 buffer available, and there is
429 * someone waiting, we can wake up a waiter and offer them.
430 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100431 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200432 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100433}
434
Willy Tarreau87b09662015-04-03 00:22:06 +0200435/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100436 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200437 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100438 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200439void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100440{
Willy Tarreau87b09662015-04-03 00:22:06 +0200441 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100442
443 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100444 if (rqlimit <= run_queue)
445 break;
446
Willy Tarreaubf883e02014-11-25 21:10:35 +0100447 if (sess->task->state & TASK_RUNNING)
448 continue;
449
450 LIST_DEL(&sess->buffer_wait);
451 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100452 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100453 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100454}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200455
456/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200457int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200458{
Willy Tarreau87b09662015-04-03 00:22:06 +0200459 LIST_INIT(&streams);
460 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
461 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200462}
463
Willy Tarreau87b09662015-04-03 00:22:06 +0200464void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100465{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200466 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100467 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200468 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100469 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100470
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100471 bytes = s->req.total - s->logs.bytes_in;
472 s->logs.bytes_in = s->req.total;
473 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200474 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100475
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100476 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100477
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100478 if (objt_server(s->target))
479 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200480
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200481 if (sess->listener && sess->listener->counters)
482 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200483
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100484 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200485 struct stkctr *stkctr = &s->stkctr[i];
486
487 if (!stkctr_entry(stkctr)) {
488 stkctr = &sess->stkctr[i];
489 if (!stkctr_entry(stkctr))
490 continue;
491 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200492
Emeric Brun57056f02015-06-15 18:29:57 +0200493 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
494 if (ptr1)
495 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200496
Emeric Brun57056f02015-06-15 18:29:57 +0200497 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
498 if (ptr2)
499 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200500 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200501
502 /* If data was modified, we need to touch to re-schedule sync */
503 if (ptr1 || ptr2)
504 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100505 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100506 }
507
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100508 bytes = s->res.total - s->logs.bytes_out;
509 s->logs.bytes_out = s->res.total;
510 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200511 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100512
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100513 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100514
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100515 if (objt_server(s->target))
516 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200517
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200518 if (sess->listener && sess->listener->counters)
519 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200520
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100521 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200522 struct stkctr *stkctr = &s->stkctr[i];
523
524 if (!stkctr_entry(stkctr)) {
525 stkctr = &sess->stkctr[i];
526 if (!stkctr_entry(stkctr))
527 continue;
528 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200529
Emeric Brun57056f02015-06-15 18:29:57 +0200530 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
531 if (ptr1)
532 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200533
Emeric Brun57056f02015-06-15 18:29:57 +0200534 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
535 if (ptr2)
536 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200537 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200538
539 /* If data was modified, we need to touch to re-schedule sync */
540 if (ptr1 || ptr2)
541 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100542 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100543 }
544}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200545
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100546/* This function is called with (si->state == SI_ST_CON) meaning that a
547 * connection was attempted and that the file descriptor is already allocated.
548 * We must check for establishment, error and abort. Possible output states
549 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
550 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200551 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200553static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100554{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100555 struct stream_interface *si = &s->si[1];
556 struct channel *req = &s->req;
557 struct channel *rep = &s->res;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200558 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100559
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560 /* If we got an error, or if nothing happened and the connection timed
561 * out, we must give up. The CER state handler will take care of retry
562 * attempts and error reports.
563 */
564 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100565 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100566 /* Some data were sent past the connection establishment,
567 * so we need to pretend we're established to log correctly
568 * and let later states handle the failure.
569 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100570 si->state = SI_ST_EST;
571 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100572 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100573 return 1;
574 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100575 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100576 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100577
Willy Tarreauf79c8172013-10-21 16:30:56 +0200578 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100579
580 if (si->err_type)
581 return 0;
582
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 if (si->flags & SI_FL_ERR)
584 si->err_type = SI_ET_CONN_ERR;
585 else
586 si->err_type = SI_ET_CONN_TO;
587 return 0;
588 }
589
590 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100591 if (!(req->flags & CF_WRITE_PARTIAL) &&
592 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200593 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
594 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100595 s->be->options & PR_O_ABRT_CLOSE)))) {
596 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200597 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100598 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100599 if (s->srv_error)
600 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100601 return 1;
602 }
603
604 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200605 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100606 return 1;
607
608 /* OK, this means that a connection succeeded. The caller will be
609 * responsible for handling the transition from CON to EST.
610 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100611 si->state = SI_ST_EST;
612 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100613 return 1;
614}
615
616/* This function is called with (si->state == SI_ST_CER) meaning that a
617 * previous connection attempt has failed and that the file descriptor
618 * has already been released. Possible causes include asynchronous error
619 * notification and time out. Possible output states are SI_ST_CLO when
620 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
621 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
622 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
623 * marked as in error state. It returns 0.
624 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200625static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100626{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100627 struct stream_interface *si = &s->si[1];
628
Willy Tarreau87b09662015-04-03 00:22:06 +0200629 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100630 if (objt_server(s->target)) {
631 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100632
Willy Tarreaue7dff022015-04-03 01:14:29 +0200633 if (s->flags & SF_CURR_SESS) {
634 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100635 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100636 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100637 }
638
639 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200640 si->conn_retries--;
641 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 if (!si->err_type) {
643 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100644 }
645
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100646 if (objt_server(s->target))
647 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100648 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100649 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100650 if (may_dequeue_tasks(objt_server(s->target), s->be))
651 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100652
653 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200654 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100655 s->req.flags |= CF_WRITE_ERROR;
656 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100657
658 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100659 if (s->srv_error)
660 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661 return 0;
662 }
663
664 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700665 * retry on another server. By default this redispatch occurs on the
666 * last retry, but if configured we allow redispatches to occur on
667 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200668 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100669 * bit to ignore any persistence cookie. We won't count a retry nor a
670 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200671 * If the connection is not persistent, the balancing algorithm is not
672 * determinist (round robin) and there is more than one active server,
673 * we accept to perform an immediate redispatch without waiting since
674 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100675 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200676 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700677 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100678 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
679 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700680 ((s->be->conn_retries - si->conn_retries) %
681 s->be->redispatch_after == 0)) ||
682 ((s->be->redispatch_after < 0) &&
683 ((s->be->conn_retries - si->conn_retries) %
684 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200685 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700686 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100687 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100688 if (may_dequeue_tasks(objt_server(s->target), s->be))
689 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100690
Willy Tarreaue7dff022015-04-03 01:14:29 +0200691 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100692 si->state = SI_ST_REQ;
693 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100694 if (objt_server(s->target))
695 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100696 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100697 si->state = SI_ST_ASS;
698 }
699
700 if (si->flags & SI_FL_ERR) {
701 /* The error was an asynchronous connection error, and we will
702 * likely have to retry connecting to the same server, most
703 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200704 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100705 */
706
Willy Tarreaub0290662014-06-13 17:04:44 +0200707 int delay = 1000;
708
709 if (s->be->timeout.connect && s->be->timeout.connect < delay)
710 delay = s->be->timeout.connect;
711
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100712 if (!si->err_type)
713 si->err_type = SI_ET_CONN_ERR;
714
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200715 /* only wait when we're retrying on the same server */
716 if (si->state == SI_ST_ASS ||
717 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
718 (s->be->srv_act <= 1)) {
719 si->state = SI_ST_TAR;
720 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
721 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100722 return 0;
723 }
724 return 0;
725}
726
727/*
728 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200729 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200730 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100731 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200732static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100734 struct stream_interface *si = &s->si[1];
735 struct channel *req = &s->req;
736 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100738 /* First, centralize the timers information */
739 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
740 si->exp = TICK_ETERNITY;
741
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100742 if (objt_server(s->target))
743 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100744
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100745 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100746 /* if the user wants to log as soon as possible, without counting
747 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200748 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100749 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100750 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 }
753 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100754 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 }
756
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200757 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100758
759 /* Be sure to filter response headers if the backend is an HTTP proxy
760 * and if there are filters attached to the stream. */
761 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
762 rep->analysers |= AN_FLT_HTTP_HDRS;
763
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200764 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200765 if (req->flags & CF_WAKE_CONNECT) {
766 req->flags |= CF_WAKE_ONCE;
767 req->flags &= ~CF_WAKE_CONNECT;
768 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200769 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200770 /* real connections have timeouts */
771 req->wto = s->be->timeout.server;
772 rep->rto = s->be->timeout.server;
773 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100774 req->wex = TICK_ETERNITY;
775}
776
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100777/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
778 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100779 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
780 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
781 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100782 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200783static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100784{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100785 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100786 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100787 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100788
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100789 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 +0100790 now_ms, __FUNCTION__,
791 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400792 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100793 req->rex, s->res.wex,
794 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400795 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 +0100796
797 if (si->state == SI_ST_ASS) {
798 /* Server assigned to connection request, we have to try to connect now */
799 int conn_err;
800
801 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100802 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100803
Willy Tarreaue7dff022015-04-03 01:14:29 +0200804 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100805 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100806 if (srv)
807 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500808 if (srv)
809 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100810 return;
811 }
812
813 /* We have received a synchronous error. We might have to
814 * abort, retry immediately or redispatch.
815 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200816 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817 if (!si->err_type) {
818 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100819 }
820
Willy Tarreau827aee92011-03-10 16:55:02 +0100821 if (srv)
822 srv_inc_sess_ctr(srv);
823 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500824 srv_set_sess_last(srv);
825 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100826 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100827 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100828
Willy Tarreau87b09662015-04-03 00:22:06 +0200829 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100830 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100831 if (may_dequeue_tasks(srv, s->be))
832 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100833
834 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200835 si_shutr(si);
836 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100837 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100838
839 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
840
Willy Tarreau87b09662015-04-03 00:22:06 +0200841 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100843 if (s->srv_error)
844 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100845 return;
846 }
847
848 /* We are facing a retryable error, but we don't want to run a
849 * turn-around now, as the problem is likely a source port
850 * allocation problem, so we want to retry now.
851 */
852 si->state = SI_ST_CER;
853 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100854 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100855 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
856 return;
857 }
858 else if (si->state == SI_ST_QUE) {
859 /* connection request was queued, check for any update */
860 if (!s->pend_pos) {
861 /* The connection is not in the queue anymore. Either
862 * we have a server connection slot available and we
863 * go directly to the assigned state, or we need to
864 * load-balance first and go to the INI state.
865 */
866 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200867 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100868 si->state = SI_ST_REQ;
869 else {
870 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
871 si->state = SI_ST_ASS;
872 }
873 return;
874 }
875
876 /* Connection request still in queue... */
877 if (si->flags & SI_FL_EXP) {
878 /* ... and timeout expired */
879 si->exp = TICK_ETERNITY;
880 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100881 if (srv)
882 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100883 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200884 si_shutr(si);
885 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100886 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100887 if (!si->err_type)
888 si->err_type = SI_ET_QUEUE_TO;
889 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100890 if (s->srv_error)
891 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100892 return;
893 }
894
895 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100896 if ((req->flags & (CF_READ_ERROR)) ||
897 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
898 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899 /* give up */
900 si->exp = TICK_ETERNITY;
901 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200902 si_shutr(si);
903 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 si->err_type |= SI_ET_QUEUE_ABRT;
905 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100906 if (s->srv_error)
907 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100908 return;
909 }
910
911 /* Nothing changed */
912 return;
913 }
914 else if (si->state == SI_ST_TAR) {
915 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100916 if ((req->flags & (CF_READ_ERROR)) ||
917 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
918 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919 /* give up */
920 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200921 si_shutr(si);
922 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100923 si->err_type |= SI_ET_CONN_ABRT;
924 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100925 if (s->srv_error)
926 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100927 return;
928 }
929
930 if (!(si->flags & SI_FL_EXP))
931 return; /* still in turn-around */
932
933 si->exp = TICK_ETERNITY;
934
Willy Tarreau87b09662015-04-03 00:22:06 +0200935 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100936 * marked "assigned".
937 * FIXME: Should we force a redispatch attempt when the server is down ?
938 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200939 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100940 si->state = SI_ST_ASS;
941 else
942 si->state = SI_ST_REQ;
943 return;
944 }
945}
946
Willy Tarreau87b09662015-04-03 00:22:06 +0200947/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900948 * also counts a failed request if the server state has not reached the request
949 * stage.
950 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200951static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900952{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200953 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900954 if (s->si[1].state < SI_ST_REQ) {
955
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200956 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200957 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200958 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900959
Willy Tarreaue7dff022015-04-03 01:14:29 +0200960 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900961 }
962 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200963 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900964 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200965 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900966 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200967 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900968 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200969 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900970 }
971}
972
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100973/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100974 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
975 * a real connection to a server, indicating that a server has been assigned,
976 * or SI_ST_EST for a successful connection to an applet. It may also return
977 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100978 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200979static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100980{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100981 struct stream_interface *si = &s->si[1];
982
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100983 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 +0100984 now_ms, __FUNCTION__,
985 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400986 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100987 s->req.rex, s->res.wex,
988 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400989 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 +0100990
991 if (si->state != SI_ST_REQ)
992 return;
993
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100994 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
995 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100996 struct appctx *appctx = objt_appctx(si->end);
997
998 if (!appctx || appctx->applet != __objt_applet(s->target))
999 appctx = stream_int_register_handler(si, objt_applet(s->target));
1000
1001 if (!appctx) {
1002 /* No more memory, let's immediately abort. Force the
1003 * error code to ignore the ERR_LOCAL which is not a
1004 * real error.
1005 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001006 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001007
1008 si_shutr(si);
1009 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001010 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001011 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001012 si->state = SI_ST_CLO;
1013 if (s->srv_error)
1014 s->srv_error(s, si);
1015 return;
1016 }
1017
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001018 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001019 si->state = SI_ST_EST;
1020 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001021 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001022 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001023 return;
1024 }
1025
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001026 /* Try to assign a server */
1027 if (srv_redispatch_connect(s) != 0) {
1028 /* We did not get a server. Either we queued the
1029 * connection request, or we encountered an error.
1030 */
1031 if (si->state == SI_ST_QUE)
1032 return;
1033
1034 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001035 si_shutr(si);
1036 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001037 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001038 if (!si->err_type)
1039 si->err_type = SI_ET_CONN_OTHER;
1040 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001041 if (s->srv_error)
1042 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001043 return;
1044 }
1045
1046 /* The server is assigned */
1047 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1048 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001049 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001050}
1051
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001052/* This function parses the use-service action ruleset. It executes
1053 * the associated ACL and set an applet as a stream or txn final node.
1054 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1055 * consistent state. It returns ACT_RET_STOP in succes case because
1056 * use-service must be a terminal action. Returns ACT_RET_YIELD
1057 * if the initialisation function require more data.
1058 */
1059enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1060 struct session *sess, struct stream *s, int flags)
1061
1062{
1063 struct appctx *appctx;
1064
1065 /* Initialises the applet if it is required. */
1066 if (flags & ACT_FLAG_FIRST) {
1067 /* Register applet. this function schedules the applet. */
1068 s->target = &rule->applet.obj_type;
1069 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1070 return ACT_RET_ERR;
1071
1072 /* Initialise the context. */
1073 appctx = si_appctx(&s->si[1]);
1074 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1075 appctx->rule = rule;
1076 }
1077 else
1078 appctx = si_appctx(&s->si[1]);
1079
1080 /* Stops the applet sheduling, in case of the init function miss
1081 * some data.
1082 */
1083 appctx_pause(appctx);
1084 si_applet_stop_get(&s->si[1]);
1085
1086 /* Call initialisation. */
1087 if (rule->applet.init)
1088 switch (rule->applet.init(appctx, px, s)) {
1089 case 0: return ACT_RET_ERR;
1090 case 1: break;
1091 default: return ACT_RET_YIELD;
1092 }
1093
1094 /* Now we can schedule the applet. */
1095 si_applet_cant_get(&s->si[1]);
1096 appctx_wakeup(appctx);
1097
1098 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1099 sess->fe->fe_counters.intercepted_req++;
1100
1101 /* The flag SF_ASSIGNED prevent from server assignment. */
1102 s->flags |= SF_ASSIGNED;
1103
1104 return ACT_RET_STOP;
1105}
1106
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001107/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001108 * if appropriate. The default_backend rule is also considered, then the
1109 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001110 * It returns 1 if the processing can continue on next analysers, or zero if it
1111 * either needs more data or wants to immediately abort the request.
1112 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001113static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001114{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001115 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001116 struct session *sess = s->sess;
1117 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001118
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001119 req->analysers &= ~an_bit;
1120 req->analyse_exp = TICK_ETERNITY;
1121
Willy Tarreau87b09662015-04-03 00:22:06 +02001122 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 +02001123 now_ms, __FUNCTION__,
1124 s,
1125 req,
1126 req->rex, req->wex,
1127 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001128 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001129 req->analysers);
1130
1131 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001132 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001133 struct switching_rule *rule;
1134
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001135 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001136 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001137
Willy Tarreauf51658d2014-04-23 01:21:56 +02001138 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001139 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001140 ret = acl_pass(ret);
1141 if (rule->cond->pol == ACL_COND_UNLESS)
1142 ret = !ret;
1143 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001144
1145 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001146 /* If the backend name is dynamic, try to resolve the name.
1147 * If we can't resolve the name, or if any error occurs, break
1148 * the loop and fallback to the default backend.
1149 */
1150 struct proxy *backend;
1151
1152 if (rule->dynamic) {
1153 struct chunk *tmp = get_trash_chunk();
1154 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1155 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001156 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001157 if (!backend)
1158 break;
1159 }
1160 else
1161 backend = rule->be.backend;
1162
Willy Tarreau87b09662015-04-03 00:22:06 +02001163 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001164 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001165 break;
1166 }
1167 }
1168
1169 /* To ensure correct connection accounting on the backend, we
1170 * have to assign one if it was not set (eg: a listen). This
1171 * measure also takes care of correctly setting the default
1172 * backend if any.
1173 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001174 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001175 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001176 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001177 }
1178
Willy Tarreaufb356202010-08-03 14:02:05 +02001179 /* 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 +02001180 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001181 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1182 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001183 s->req.analysers &= ~AN_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001184 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001185
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001186 /* 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 +02001187 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001188 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001189 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001190 int ret = 1;
1191
1192 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001193 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 +01001194 ret = acl_pass(ret);
1195 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1196 ret = !ret;
1197 }
1198
1199 if (ret) {
1200 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001201 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001202 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001203 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001204 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001205 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001206 break;
1207 }
1208 }
1209
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001210 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001211
1212 sw_failed:
1213 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001214 channel_abort(&s->req);
1215 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001216
Willy Tarreaue7dff022015-04-03 01:14:29 +02001217 if (!(s->flags & SF_ERR_MASK))
1218 s->flags |= SF_ERR_RESOURCE;
1219 if (!(s->flags & SF_FINST_MASK))
1220 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001221
Willy Tarreaueee5b512015-04-03 23:46:31 +02001222 if (s->txn)
1223 s->txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001224 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001225 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001226 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001227}
1228
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001229/* This stream analyser works on a request. It applies all use-server rules on
1230 * it then returns 1. The data must already be present in the buffer otherwise
1231 * they won't match. It always returns 1.
1232 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001233static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001234{
1235 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001236 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001237 struct server_rule *rule;
1238
Willy Tarreau87b09662015-04-03 00:22:06 +02001239 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 +02001240 now_ms, __FUNCTION__,
1241 s,
1242 req,
1243 req->rex, req->wex,
1244 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001245 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001246 req->analysers);
1247
Willy Tarreaue7dff022015-04-03 01:14:29 +02001248 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001249 list_for_each_entry(rule, &px->server_rules, list) {
1250 int ret;
1251
Willy Tarreau192252e2015-04-04 01:47:55 +02001252 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001253 ret = acl_pass(ret);
1254 if (rule->cond->pol == ACL_COND_UNLESS)
1255 ret = !ret;
1256
1257 if (ret) {
1258 struct server *srv = rule->srv.ptr;
1259
Willy Tarreau892337c2014-05-13 23:41:20 +02001260 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001261 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001262 (s->flags & SF_FORCE_PRST)) {
1263 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001264 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001265 break;
1266 }
1267 /* if the server is not UP, let's go on with next rules
1268 * just in case another one is suited.
1269 */
1270 }
1271 }
1272 }
1273
1274 req->analysers &= ~an_bit;
1275 req->analyse_exp = TICK_ETERNITY;
1276 return 1;
1277}
1278
Emeric Brun1d33b292010-01-04 15:47:17 +01001279/* This stream analyser works on a request. It applies all sticking rules on
1280 * it then returns 1. The data must already be present in the buffer otherwise
1281 * they won't match. It always returns 1.
1282 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001283static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001284{
1285 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001286 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001287 struct sticking_rule *rule;
1288
Willy Tarreau87b09662015-04-03 00:22:06 +02001289 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 +01001290 now_ms, __FUNCTION__,
1291 s,
1292 req,
1293 req->rex, req->wex,
1294 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001295 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001296 req->analysers);
1297
1298 list_for_each_entry(rule, &px->sticking_rules, list) {
1299 int ret = 1 ;
1300 int i;
1301
Willy Tarreau9667a802013-12-09 12:52:13 +01001302 /* Only the first stick store-request of each table is applied
1303 * and other ones are ignored. The purpose is to allow complex
1304 * configurations which look for multiple entries by decreasing
1305 * order of precision and to stop at the first which matches.
1306 * An example could be a store of the IP address from an HTTP
1307 * header first, then from the source if not found.
1308 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001309 for (i = 0; i < s->store_count; i++) {
1310 if (rule->table.t == s->store[i].table)
1311 break;
1312 }
1313
1314 if (i != s->store_count)
1315 continue;
1316
1317 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001318 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001319 ret = acl_pass(ret);
1320 if (rule->cond->pol == ACL_COND_UNLESS)
1321 ret = !ret;
1322 }
1323
1324 if (ret) {
1325 struct stktable_key *key;
1326
Willy Tarreau192252e2015-04-04 01:47:55 +02001327 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 +01001328 if (!key)
1329 continue;
1330
1331 if (rule->flags & STK_IS_MATCH) {
1332 struct stksess *ts;
1333
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001334 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001335 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001336 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001337 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001338
1339 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001340 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1341 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001342 if (node) {
1343 struct server *srv;
1344
1345 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001346 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001347 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001348 (s->flags & SF_FORCE_PRST)) {
1349 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001350 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001351 }
1352 }
1353 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001354 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001355 }
1356 }
1357 if (rule->flags & STK_IS_STORE) {
1358 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1359 struct stksess *ts;
1360
1361 ts = stksess_new(rule->table.t, key);
1362 if (ts) {
1363 s->store[s->store_count].table = rule->table.t;
1364 s->store[s->store_count++].ts = ts;
1365 }
1366 }
1367 }
1368 }
1369 }
1370
1371 req->analysers &= ~an_bit;
1372 req->analyse_exp = TICK_ETERNITY;
1373 return 1;
1374}
1375
1376/* This stream analyser works on a response. It applies all store rules on it
1377 * then returns 1. The data must already be present in the buffer otherwise
1378 * they won't match. It always returns 1.
1379 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001380static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001381{
1382 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001383 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001384 struct sticking_rule *rule;
1385 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001386 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001387
Willy Tarreau87b09662015-04-03 00:22:06 +02001388 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 +01001389 now_ms, __FUNCTION__,
1390 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001391 rep,
1392 rep->rex, rep->wex,
1393 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001394 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001395 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001396
1397 list_for_each_entry(rule, &px->storersp_rules, list) {
1398 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001399
Willy Tarreau9667a802013-12-09 12:52:13 +01001400 /* Only the first stick store-response of each table is applied
1401 * and other ones are ignored. The purpose is to allow complex
1402 * configurations which look for multiple entries by decreasing
1403 * order of precision and to stop at the first which matches.
1404 * An example could be a store of a set-cookie value, with a
1405 * fallback to a parameter found in a 302 redirect.
1406 *
1407 * The store-response rules are not allowed to override the
1408 * store-request rules for the same table, but they may coexist.
1409 * Thus we can have up to one store-request entry and one store-
1410 * response entry for the same table at any time.
1411 */
1412 for (i = nbreq; i < s->store_count; i++) {
1413 if (rule->table.t == s->store[i].table)
1414 break;
1415 }
1416
1417 /* skip existing entries for this table */
1418 if (i < s->store_count)
1419 continue;
1420
Emeric Brun1d33b292010-01-04 15:47:17 +01001421 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001422 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001423 ret = acl_pass(ret);
1424 if (rule->cond->pol == ACL_COND_UNLESS)
1425 ret = !ret;
1426 }
1427
1428 if (ret) {
1429 struct stktable_key *key;
1430
Willy Tarreau192252e2015-04-04 01:47:55 +02001431 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 +01001432 if (!key)
1433 continue;
1434
Willy Tarreau37e340c2013-12-06 23:05:21 +01001435 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001436 struct stksess *ts;
1437
1438 ts = stksess_new(rule->table.t, key);
1439 if (ts) {
1440 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001441 s->store[s->store_count++].ts = ts;
1442 }
1443 }
1444 }
1445 }
1446
1447 /* process store request and store response */
1448 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001449 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001450 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001451
Willy Tarreauc93cd162014-05-13 15:54:22 +02001452 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001453 stksess_free(s->store[i].table, s->store[i].ts);
1454 s->store[i].ts = NULL;
1455 continue;
1456 }
1457
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001458 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1459 if (ts) {
1460 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001461 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001462 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001463 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001464 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001465 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001466
1467 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001468 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001469 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001470 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001471 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001472
1473 rep->analysers &= ~an_bit;
1474 rep->analyse_exp = TICK_ETERNITY;
1475 return 1;
1476}
1477
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001478/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001479 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001480 */
1481#define UPDATE_ANALYSERS(real, list, back, flag) { \
1482 list = (((list) & ~(flag)) | ~(back)) & (real); \
1483 back = real; \
1484 if (!(list)) \
1485 break; \
1486 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1487 continue; \
1488}
1489
Willy Tarreau87b09662015-04-03 00:22:06 +02001490/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001491 * then puts it back to the wait queue in a clean state, or cleans up its
1492 * resources if it must be deleted. Returns in <next> the date the task wants
1493 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1494 * nothing too many times, the request and response buffers flags are monitored
1495 * and each function is called only if at least another function has changed at
1496 * least one flag it is interested in.
1497 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001498struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001499{
Willy Tarreau827aee92011-03-10 16:55:02 +01001500 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001501 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001502 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001503 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001504 unsigned int rq_prod_last, rq_cons_last;
1505 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001506 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001507 struct channel *req, *res;
1508 struct stream_interface *si_f, *si_b;
1509
1510 req = &s->req;
1511 res = &s->res;
1512
1513 si_f = &s->si[0];
1514 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001515
1516 //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 +01001517 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001518
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001519 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001520 if (s->txn)
1521 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001522
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001523 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001524 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1525 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001526
1527 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001528 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1529 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001530
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001531 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001532 si_f->flags |= SI_FL_DONT_WAKE;
1533 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001534
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001535 /* 1a: Check for low level timeouts if needed. We just set a flag on
1536 * stream interfaces when their timeouts have expired.
1537 */
1538 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001539 stream_int_check_timeouts(si_f);
1540 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001541
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001542 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001543 * for future reads or writes. Note: this will also concern upper layers
1544 * but we do not touch any other flag. We must be careful and correctly
1545 * detect state changes when calling them.
1546 */
1547
Willy Tarreau8f128b42014-11-28 15:07:47 +01001548 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001549
Willy Tarreau8f128b42014-11-28 15:07:47 +01001550 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1551 si_b->flags |= SI_FL_NOLINGER;
1552 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001553 }
1554
Willy Tarreau8f128b42014-11-28 15:07:47 +01001555 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1556 if (si_f->flags & SI_FL_NOHALF)
1557 si_f->flags |= SI_FL_NOLINGER;
1558 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001559 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001560
Willy Tarreau8f128b42014-11-28 15:07:47 +01001561 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001562
Willy Tarreau8f128b42014-11-28 15:07:47 +01001563 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1564 si_f->flags |= SI_FL_NOLINGER;
1565 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001566 }
1567
Willy Tarreau8f128b42014-11-28 15:07:47 +01001568 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1569 if (si_b->flags & SI_FL_NOHALF)
1570 si_b->flags |= SI_FL_NOLINGER;
1571 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001572 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001573
1574 /* Once in a while we're woken up because the task expires. But
1575 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001576 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001577 * timeout needs to be refreshed.
1578 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001579 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001580 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1581 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001582 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001583 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1584 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001585 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001586
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001587 /* below we may emit error messages so we have to ensure that we have
1588 * our buffers properly allocated.
1589 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001590 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001591 /* No buffer available, we've been subscribed to the list of
1592 * buffer waiters, let's wait for our turn.
1593 */
1594 goto update_exp_and_leave;
1595 }
1596
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001597 /* 1b: check for low-level errors reported at the stream interface.
1598 * First we check if it's a retryable error (in which case we don't
1599 * want to tell the buffer). Otherwise we report the error one level
1600 * upper by setting flags into the buffers. Note that the side towards
1601 * the client cannot have connect (hence retryable) errors. Also, the
1602 * connection setup code must be able to deal with any type of abort.
1603 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001604 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001605 if (unlikely(si_f->flags & SI_FL_ERR)) {
1606 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1607 si_shutr(si_f);
1608 si_shutw(si_f);
1609 stream_int_report_error(si_f);
1610 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001611 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001612 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001613 if (srv)
1614 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001615 if (!(s->flags & SF_ERR_MASK))
1616 s->flags |= SF_ERR_CLICL;
1617 if (!(s->flags & SF_FINST_MASK))
1618 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001619 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001620 }
1621 }
1622
Willy Tarreau8f128b42014-11-28 15:07:47 +01001623 if (unlikely(si_b->flags & SI_FL_ERR)) {
1624 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1625 si_shutr(si_b);
1626 si_shutw(si_b);
1627 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001628 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001629 if (srv)
1630 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001631 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001632 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001633 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001634 if (srv)
1635 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001636 if (!(s->flags & SF_ERR_MASK))
1637 s->flags |= SF_ERR_SRVCL;
1638 if (!(s->flags & SF_FINST_MASK))
1639 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001640 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001641 }
1642 /* note: maybe we should process connection errors here ? */
1643 }
1644
Willy Tarreau8f128b42014-11-28 15:07:47 +01001645 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001646 /* we were trying to establish a connection on the server side,
1647 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1648 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001649 if (unlikely(!sess_update_st_con_tcp(s)))
1650 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001651 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001652 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001653
1654 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1655 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1656 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1657 */
1658 }
1659
Willy Tarreau8f128b42014-11-28 15:07:47 +01001660 rq_prod_last = si_f->state;
1661 rq_cons_last = si_b->state;
1662 rp_cons_last = si_f->state;
1663 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001664
1665 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001666 /* Check for connection closure */
1667
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001668 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001669 "[%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 +01001670 now_ms, __FUNCTION__, __LINE__,
1671 t,
1672 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001673 req, res,
1674 req->rex, res->wex,
1675 req->flags, res->flags,
1676 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1677 si_f->err_type, si_b->err_type,
1678 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001679
1680 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001681 if (unlikely(si_f->state == SI_ST_DIS))
1682 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001683
1684 /* When a server-side connection is released, we have to count it and
1685 * check for pending connections on this server.
1686 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001687 if (unlikely(si_b->state == SI_ST_DIS)) {
1688 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001689 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001690 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001691 if (s->flags & SF_CURR_SESS) {
1692 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001693 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001694 }
1695 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001696 if (may_dequeue_tasks(srv, s->be))
1697 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001698 }
1699 }
1700
1701 /*
1702 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1703 * at this point.
1704 */
1705
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001706 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001707 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001708 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1709 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1710 si_f->state != rq_prod_last ||
1711 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001712 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001713 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001714
Willy Tarreau8f128b42014-11-28 15:07:47 +01001715 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001716 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001717 unsigned int ana_list;
1718 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001719
Willy Tarreau90deb182010-01-07 00:20:41 +01001720 /* it's up to the analysers to stop new connections,
1721 * disable reading or closing. Note: if an analyser
1722 * disables any of these bits, it is responsible for
1723 * enabling them again when it disables itself, so
1724 * that other analysers are called in similar conditions.
1725 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001726 channel_auto_read(req);
1727 channel_auto_connect(req);
1728 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001729
1730 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001731 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001732 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001733 * the list when not needed. Any analyser may return 0
1734 * to break out of the loop, either because of missing
1735 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001736 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001737 * analyser, and we may loop again if other analysers
1738 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001739 *
1740 * We build a list of analysers to run. We evaluate all
1741 * of these analysers in the order of the lower bit to
1742 * the higher bit. This ordering is very important.
1743 * An analyser will often add/remove other analysers,
1744 * including itself. Any changes to itself have no effect
1745 * on the loop. If it removes any other analysers, we
1746 * want those analysers not to be called anymore during
1747 * this loop. If it adds an analyser that is located
1748 * after itself, we want it to be scheduled for being
1749 * processed during the loop. If it adds an analyser
1750 * which is located before it, we want it to switch to
1751 * it immediately, even if it has already been called
1752 * once but removed since.
1753 *
1754 * In order to achieve this, we compare the analyser
1755 * list after the call with a copy of it before the
1756 * call. The work list is fed with analyser bits that
1757 * appeared during the call. Then we compare previous
1758 * work list with the new one, and check the bits that
1759 * appeared. If the lowest of these bits is lower than
1760 * the current bit, it means we have enabled a previous
1761 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001762 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001763
Willy Tarreau8f128b42014-11-28 15:07:47 +01001764 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001765 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001766 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauletd7c91962015-04-30 11:48:27 +02001767 if (ana_list & AN_FLT_START_FE) {
1768 if (!flt_start_analyze(s, req, AN_FLT_START_FE))
1769 break;
1770 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_START_FE);
1771 }
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001772
Willy Tarreaufb356202010-08-03 14:02:05 +02001773 if (ana_list & AN_REQ_INSPECT_FE) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001774 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_INSPECT_FE);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001775 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001776 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001777 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001778 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001779
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001780 if (ana_list & AN_REQ_WAIT_HTTP) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001781 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_WAIT_HTTP);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001782 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001783 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001784 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001785 }
1786
Willy Tarreau748179e2015-05-01 21:52:31 +02001787 if (ana_list & AN_REQ_HTTP_BODY) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001788 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_HTTP_BODY);
Willy Tarreau748179e2015-05-01 21:52:31 +02001789 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1790 break;
1791 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1792 }
1793
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001794 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001795 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001796 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001797 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001798 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001799 }
1800
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001801 if (ana_list & AN_REQ_SWITCHING_RULES) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001802 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_SWITCHING_RULES);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001803 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001804 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001805 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001806 }
1807
Christopher Faulet3e344292015-11-24 16:24:13 +01001808 if (ana_list & AN_FLT_START_BE) {
1809 if (!flt_start_analyze(s, req, AN_FLT_START_BE))
Christopher Fauletd7c91962015-04-30 11:48:27 +02001810 break;
Christopher Faulet3e344292015-11-24 16:24:13 +01001811 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_START_BE);
1812 }
1813
1814 if (ana_list & AN_REQ_INSPECT_BE) {
1815 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_INSPECT_BE);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001816 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001817 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001818 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001819 }
1820
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001821 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001822 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001823 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
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_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001826 }
1827
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001828 if (ana_list & AN_REQ_HTTP_TARPIT) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001829 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_HTTP_TARPIT);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001830 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001831 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001832 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001833 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001834
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001835 if (ana_list & AN_REQ_SRV_RULES) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001836 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_SRV_RULES);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001837 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001838 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001839 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001840 }
1841
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001842 if (ana_list & AN_REQ_HTTP_INNER) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001843 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_HTTP_INNER);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001844 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001845 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001846 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001847 }
1848
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001849 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001850 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_PRST_RDP_COOKIE);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001851 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001852 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001853 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001854 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001855
Emeric Brun1d33b292010-01-04 15:47:17 +01001856 if (ana_list & AN_REQ_STICKING_RULES) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001857 CALL_FILTER_ANALYZER(flt_analyze, s, req, AN_REQ_STICKING_RULES);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 if (!process_sticking_rules(s, req, AN_REQ_STICKING_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001859 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001860 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_STICKING_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001861 }
1862
Christopher Faulet309c6412015-12-02 09:57:32 +01001863 if (ana_list & AN_FLT_HTTP_HDRS) {
1864 if (!flt_analyze_http_headers(s, req, AN_FLT_HTTP_HDRS))
1865 break;
1866 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1867 }
1868
Christopher Fauletd7c91962015-04-30 11:48:27 +02001869 if (ana_list & AN_FLT_XFER_DATA) {
1870 if (!flt_xfer_data(s, req, AN_FLT_XFER_DATA))
1871 break;
1872 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_XFER_DATA);
1873 }
1874
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001875 if (ana_list & AN_REQ_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001876 if (!http_request_forward_body(s, req, AN_REQ_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01001877 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001878 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01001879 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02001880
1881 if (ana_list & AN_FLT_END) {
1882 if (!flt_end_analyze(s, req, AN_FLT_END))
1883 break;
1884 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_END);
1885 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01001886 break;
1887 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001888 }
Willy Tarreau84455332009-03-15 22:34:05 +01001889
Willy Tarreau8f128b42014-11-28 15:07:47 +01001890 rq_prod_last = si_f->state;
1891 rq_cons_last = si_b->state;
1892 req->flags &= ~CF_WAKE_ONCE;
1893 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001894
Willy Tarreau8f128b42014-11-28 15:07:47 +01001895 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001896 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001897 }
1898
Willy Tarreau576507f2010-01-07 00:09:04 +01001899 /* we'll monitor the request analysers while parsing the response,
1900 * because some response analysers may indirectly enable new request
1901 * analysers (eg: HTTP keep-alive).
1902 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001903 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001904
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001905 resync_response:
1906 /* Analyse response */
1907
Willy Tarreau8f128b42014-11-28 15:07:47 +01001908 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1909 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1910 si_f->state != rp_cons_last ||
1911 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001912 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001913 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001914
Willy Tarreau8f128b42014-11-28 15:07:47 +01001915 if ((res->flags & CF_MASK_ANALYSER) &&
1916 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001917 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1918 * for some free space in the response buffer, so we might need to call
1919 * it when something changes in the response buffer, but still we pass
1920 * it the request buffer. Note that the SI state might very well still
1921 * be zero due to us returning a flow of redirects!
1922 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001923 res->analysers &= ~AN_REQ_ALL;
1924 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001925 }
1926
Willy Tarreau8f128b42014-11-28 15:07:47 +01001927 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001928 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001929 unsigned int ana_list;
1930 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001931
Willy Tarreau90deb182010-01-07 00:20:41 +01001932 /* it's up to the analysers to stop disable reading or
1933 * closing. Note: if an analyser disables any of these
1934 * bits, it is responsible for enabling them again when
1935 * it disables itself, so that other analysers are called
1936 * in similar conditions.
1937 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001938 channel_auto_read(res);
1939 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001940
1941 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001942 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001943 * to MSB. The analysers must remove themselves from
1944 * the list when not needed. Any analyser may return 0
1945 * to break out of the loop, either because of missing
1946 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001947 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001948 * analyser, and we may loop again if other analysers
1949 * are added in the middle.
1950 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001951
Willy Tarreau8f128b42014-11-28 15:07:47 +01001952 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001953 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001954 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauletd7c91962015-04-30 11:48:27 +02001955 if (ana_list & AN_FLT_START_FE) {
1956 if (!flt_start_analyze(s, res, AN_FLT_START_FE))
1957 break;
1958 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_START_FE);
1959 }
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001960
Christopher Faulet3e344292015-11-24 16:24:13 +01001961 if (ana_list & AN_FLT_START_BE) {
1962 if (!flt_start_analyze(s, res, AN_FLT_START_BE))
Christopher Fauletd7c91962015-04-30 11:48:27 +02001963 break;
Christopher Faulet3e344292015-11-24 16:24:13 +01001964 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_START_BE);
1965 }
1966
1967 if (ana_list & AN_RES_INSPECT) {
1968 CALL_FILTER_ANALYZER(flt_analyze, s, res, AN_RES_INSPECT);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001969 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001970 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001971 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001972 }
1973
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001974 if (ana_list & AN_RES_WAIT_HTTP) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001975 CALL_FILTER_ANALYZER(flt_analyze, s, res, AN_RES_WAIT_HTTP);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001976 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001977 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001978 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001979 }
1980
Emeric Brun1d33b292010-01-04 15:47:17 +01001981 if (ana_list & AN_RES_STORE_RULES) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001982 CALL_FILTER_ANALYZER(flt_analyze, s, res, AN_RES_STORE_RULES);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001983 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001984 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001985 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001986 }
1987
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001988 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Christopher Faulet3e344292015-11-24 16:24:13 +01001989 CALL_FILTER_ANALYZER(flt_analyze, s, res, AN_RES_HTTP_PROCESS_BE);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001990 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001991 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001992 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001993 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001994
Christopher Faulet309c6412015-12-02 09:57:32 +01001995 if (ana_list & AN_FLT_HTTP_HDRS) {
1996 if (!flt_analyze_http_headers(s, res, AN_FLT_HTTP_HDRS))
1997 break;
1998 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1999 }
2000
Christopher Fauletd7c91962015-04-30 11:48:27 +02002001 if (ana_list & AN_FLT_XFER_DATA) {
2002 if (!flt_xfer_data(s, res, AN_FLT_XFER_DATA))
2003 break;
2004 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_XFER_DATA);
2005 }
2006
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002007 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002008 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002010 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002011 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02002012
2013 if (ana_list & AN_FLT_END) {
2014 if (!flt_end_analyze(s, res, AN_FLT_END))
2015 break;
2016 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_END);
2017 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002018 break;
2019 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002020 }
2021
Willy Tarreau8f128b42014-11-28 15:07:47 +01002022 rp_cons_last = si_f->state;
2023 rp_prod_last = si_b->state;
2024 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002025
Willy Tarreau8f128b42014-11-28 15:07:47 +01002026 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002027 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002028 }
2029
Willy Tarreau576507f2010-01-07 00:09:04 +01002030 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002031 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002032 goto resync_request;
2033
Willy Tarreau8f128b42014-11-28 15:07:47 +01002034 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002035 goto resync_request;
2036
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002037 /* FIXME: here we should call protocol handlers which rely on
2038 * both buffers.
2039 */
2040
2041
2042 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002043 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002044 * we're just in a data phase here since it means we have not
2045 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002046 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002047 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002048 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002049 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002050 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002051 req->analysers = 0;
2052 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002053 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002054 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002055 if (srv)
2056 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002057 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002058 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002059 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002060 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002061 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002062 if (srv)
2063 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002064 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002065 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002066 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002067 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002068 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002069 if (srv)
2070 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002071 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002072 }
2073 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002074 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002075 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002076 if (srv)
2077 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002078 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002079 }
Willy Tarreau84455332009-03-15 22:34:05 +01002080 sess_set_term_flags(s);
2081 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002082 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002083 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002084 res->analysers = 0;
2085 if (res->flags & CF_READ_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 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002092 else if (res->flags & CF_READ_TIMEOUT) {
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 Tarreau8f128b42014-11-28 15:07:47 +01002099 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002100 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002101 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002102 if (srv)
2103 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002104 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002105 }
2106 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002107 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002108 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002109 if (srv)
2110 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002111 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002112 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002113 sess_set_term_flags(s);
2114 }
Willy Tarreau84455332009-03-15 22:34:05 +01002115 }
2116
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002117 /*
2118 * Here we take care of forwarding unhandled data. This also includes
2119 * connection establishments and shutdown requests.
2120 */
2121
2122
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002123 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002124 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002125 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002126 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002127 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002128 if (unlikely(!req->analysers &&
2129 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2130 (si_f->state >= SI_ST_EST) &&
2131 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002132 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002133 * attached to it. If any data are left in, we'll permit them to
2134 * move.
2135 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002136 channel_auto_read(req);
2137 channel_auto_connect(req);
2138 channel_auto_close(req);
2139 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002140
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002141 /* We'll let data flow between the producer (if still connected)
2142 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002143 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002144 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2145 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02002146
2147 /* Just in order to support fetching HTTP contents after start
2148 * of forwarding when the HTTP forwarding analyser is not used,
2149 * we simply reset msg->sov so that HTTP rewinding points to the
2150 * headers.
2151 */
2152 if (s->txn)
2153 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002154 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002155
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002156 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002157 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2158 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002159 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002160 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2161 (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 +01002162 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002163 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2164 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002165 (req->flags & CF_STREAMER_FAST)))) {
2166 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002167 }
2168
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002169 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002170 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002171
2172 /*
2173 * Now forward all shutdown requests between both sides of the buffer
2174 */
2175
Willy Tarreau520d95e2009-09-19 21:04:57 +02002176 /* first, let's check if the request buffer needs to shutdown(write), which may
2177 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002178 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2179 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002180 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002181 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002182 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002183 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002184 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002185
2186 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002187 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2188 channel_is_empty(req))) {
2189 if (req->flags & CF_READ_ERROR)
2190 si_b->flags |= SI_FL_NOLINGER;
2191 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002192 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002193 res->rto = s->be->timeout.serverfin;
2194 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002195 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002196 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002197
2198 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002199 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2200 !req->analysers))
2201 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002202
2203 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002204 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2205 if (si_f->flags & SI_FL_NOHALF)
2206 si_f->flags |= SI_FL_NOLINGER;
2207 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002208 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002209
Willy Tarreau520d95e2009-09-19 21:04:57 +02002210 /* it's possible that an upper layer has requested a connection setup or abort.
2211 * There are 2 situations where we decide to establish a new connection :
2212 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002213 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002214 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002215 if (si_b->state == SI_ST_INI) {
2216 if (!(req->flags & CF_SHUTW)) {
2217 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002218 /* If we have an appctx, there is no connect method, so we
2219 * immediately switch to the connected state, otherwise we
2220 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002221 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002222 si_b->state = SI_ST_REQ; /* new connection requested */
2223 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002224 }
Willy Tarreau73201222009-08-16 18:27:24 +02002225 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002226 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002227 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2228 channel_shutw_now(req); /* fix buffer flags upon abort */
2229 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002230 }
Willy Tarreau92795622009-03-06 12:51:23 +01002231 }
2232
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002233
2234 /* we may have a pending connection request, or a connection waiting
2235 * for completion.
2236 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002237 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002238
2239 /* prune the request variables and swap to the response variables. */
2240 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreau6204cd92016-03-10 16:33:04 +01002241 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002242 vars_init(&s->vars_reqres, SCOPE_RES);
2243 }
2244
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002245 do {
2246 /* nb: step 1 might switch from QUE to ASS, but we first want
2247 * to give a chance to step 2 to perform a redirect if needed.
2248 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002250 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002251 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002252 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002253
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002254 /* applets directly go to the ESTABLISHED state. Similarly,
2255 * servers experience the same fate when their connection
2256 * is reused.
2257 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002258 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002259 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002260
Willy Tarreaub44c8732013-12-31 23:16:50 +01002261 /* Now we can add the server name to a header (if requested) */
2262 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002263 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002264 (s->be->server_id_hdr_name != NULL) &&
2265 (s->be->mode == PR_MODE_HTTP) &&
2266 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002267 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002268 }
2269
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002270 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002271 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002272 http_perform_server_redirect(s, si_b);
2273 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002274 }
2275
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002276 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002277 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002278 goto resync_stream_interface;
2279
Willy Tarreau815a9b22010-07-27 17:15:12 +02002280 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002281 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002282 goto resync_request;
2283
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002284 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002285
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002286 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002287 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002288 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002289 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002290 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002291 if (unlikely(!res->analysers &&
2292 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2293 (si_b->state >= SI_ST_EST) &&
2294 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002295 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002296 * attached to it. If any data are left in, we'll permit them to
2297 * move.
2298 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 channel_auto_read(res);
2300 channel_auto_close(res);
2301 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002302
2303 /* We'll let data flow between the producer (if still connected)
2304 * to the consumer.
2305 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002306 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2307 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002308
Willy Tarreau42529c32015-07-09 18:38:57 +02002309 /* Just in order to support fetching HTTP contents after start
2310 * of forwarding when the HTTP forwarding analyser is not used,
2311 * we simply reset msg->sov so that HTTP rewinding points to the
2312 * headers.
2313 */
2314 if (s->txn)
2315 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2316
Willy Tarreauce887fd2012-05-12 12:50:00 +02002317 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002318 * tunnel timeout set, use it now. Note that we must respect
2319 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002320 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002321 if (!req->analysers && s->be->timeout.tunnel) {
2322 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002323 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002324
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002325 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2326 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002327 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2328 res->rto = s->be->timeout.serverfin;
2329 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2330 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002331 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2332 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002333
Willy Tarreau8f128b42014-11-28 15:07:47 +01002334 req->rex = tick_add(now_ms, req->rto);
2335 req->wex = tick_add(now_ms, req->wto);
2336 res->rex = tick_add(now_ms, res->rto);
2337 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002338 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002339 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002340
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002341 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002342 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2343 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002344 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002345 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2346 (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 +01002347 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002348 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2349 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002350 (res->flags & CF_STREAMER_FAST)))) {
2351 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002352 }
2353
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002354 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002355 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002356
2357 /*
2358 * Now forward all shutdown requests between both sides of the buffer
2359 */
2360
2361 /*
2362 * FIXME: this is probably where we should produce error responses.
2363 */
2364
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002365 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002366 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002367 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002368 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002369 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002370
2371 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002372 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2373 channel_is_empty(res))) {
2374 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002375 if (tick_isset(sess->fe->timeout.clientfin)) {
2376 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002377 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002378 }
2379 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002380
2381 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002382 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2383 !res->analysers)
2384 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002385
2386 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002387 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2388 if (si_b->flags & SI_FL_NOHALF)
2389 si_b->flags |= SI_FL_NOLINGER;
2390 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002391 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002392
Willy Tarreau8f128b42014-11-28 15:07:47 +01002393 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002394 goto resync_stream_interface;
2395
Willy Tarreau8f128b42014-11-28 15:07:47 +01002396 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002397 goto resync_request;
2398
Willy Tarreau8f128b42014-11-28 15:07:47 +01002399 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002400 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002401
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002402 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002403 si_f->flags &= ~SI_FL_DONT_WAKE;
2404 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002405
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002406 /* This is needed only when debugging is enabled, to indicate
2407 * client-side or server-side close. Please note that in the unlikely
2408 * event where both sides would close at once, the sequence is reported
2409 * on the server side first.
2410 */
2411 if (unlikely((global.mode & MODE_DEBUG) &&
2412 (!(global.mode & MODE_QUIET) ||
2413 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002414 if (si_b->state == SI_ST_CLO &&
2415 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002416 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002417 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002418 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2419 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002420 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002421 }
2422
Willy Tarreau8f128b42014-11-28 15:07:47 +01002423 if (si_f->state == SI_ST_CLO &&
2424 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002425 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002426 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002427 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2428 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002429 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002430 }
2431 }
2432
Willy Tarreau8f128b42014-11-28 15:07:47 +01002433 if (likely((si_f->state != SI_ST_CLO) ||
2434 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002435
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002436 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002437 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002438
Willy Tarreau563cc372015-04-19 18:13:56 +02002439 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002440 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002441
Willy Tarreau563cc372015-04-19 18:13:56 +02002442 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002443 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002444
Willy Tarreau8f128b42014-11-28 15:07:47 +01002445 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2446 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2447 si_f->prev_state = si_f->state;
2448 si_b->prev_state = si_b->state;
2449 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2450 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002451
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002452 /* Trick: if a request is being waiting for the server to respond,
2453 * and if we know the server can timeout, we don't want the timeout
2454 * to expire on the client side first, but we're still interested
2455 * in passing data from the client to the server (eg: POST). Thus,
2456 * we can cancel the client's request timeout if the server's
2457 * request timeout is set and the server has not yet sent a response.
2458 */
2459
Willy Tarreau8f128b42014-11-28 15:07:47 +01002460 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2461 (tick_isset(req->wex) || tick_isset(res->rex))) {
2462 req->flags |= CF_READ_NOEXP;
2463 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002464 }
2465
Willy Tarreau798f4322012-11-08 14:49:17 +01002466 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002467 t->expire = tick_first(tick_first(req->rex, req->wex),
2468 tick_first(res->rex, res->wex));
2469 if (req->analysers)
2470 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002471
Willy Tarreau8f128b42014-11-28 15:07:47 +01002472 if (si_f->exp)
2473 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002474
Willy Tarreau8f128b42014-11-28 15:07:47 +01002475 if (si_b->exp)
2476 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002477
2478#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002479 fprintf(stderr,
2480 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2481 " 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 +01002482 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2483 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002484#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002485
2486#ifdef DEBUG_DEV
2487 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002488 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002489 ABORT_NOW();
2490#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002491 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002492 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002493 }
2494
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002495 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002496 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002497 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002498 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002499 if (sess->listener) {
2500 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002501 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002502 sess->listener->nbconn--;
2503 if (sess->listener->state == LI_FULL)
2504 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002505
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002506 /* Dequeues all of the listeners waiting for a resource */
2507 if (!LIST_ISEMPTY(&global_listener_queue))
2508 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002509
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002510 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2511 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2512 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002513 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002514
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002515 if (unlikely((global.mode & MODE_DEBUG) &&
2516 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002517 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002518 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002519 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2520 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002521 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002522 }
2523
2524 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002525 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002526
Willy Tarreaueee5b512015-04-03 23:46:31 +02002527 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002528 int n;
2529
Willy Tarreaueee5b512015-04-03 23:46:31 +02002530 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002531 if (n < 1 || n > 5)
2532 n = 0;
2533
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002534 if (sess->fe->mode == PR_MODE_HTTP) {
2535 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002536 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002537 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002538 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002539 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002540 s->be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002541 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002542 }
2543
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002544 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002545 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002546 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002547 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002548 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002549 }
2550
Willy Tarreau87b09662015-04-03 00:22:06 +02002551 /* update time stats for this stream */
2552 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002553
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002554 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002555 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002556 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002557 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002558 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002559}
2560
Willy Tarreau87b09662015-04-03 00:22:06 +02002561/* Update the stream's backend and server time stats */
2562void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002563{
2564 int t_request;
2565 int t_queue;
2566 int t_connect;
2567 int t_data;
2568 int t_close;
2569 struct server *srv;
2570
2571 t_request = 0;
2572 t_queue = s->logs.t_queue;
2573 t_connect = s->logs.t_connect;
2574 t_close = s->logs.t_close;
2575 t_data = s->logs.t_data;
2576
2577 if (s->be->mode != PR_MODE_HTTP)
2578 t_data = t_connect;
2579
2580 if (t_connect < 0 || t_data < 0)
2581 return;
2582
2583 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2584 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2585
2586 t_data -= t_connect;
2587 t_connect -= t_queue;
2588 t_queue -= t_request;
2589
2590 srv = objt_server(s->target);
2591 if (srv) {
2592 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2593 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2594 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2595 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2596 }
2597 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2598 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2599 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2600 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2601}
2602
Willy Tarreau7c669d72008-06-20 15:04:11 +02002603/*
2604 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002605 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002606 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002607 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002608 * server.
2609 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002610void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002611{
2612 if (sess->srv_conn == newsrv)
2613 return;
2614
2615 if (sess->srv_conn) {
2616 sess->srv_conn->served--;
2617 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2618 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002619 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002620 }
2621
2622 if (newsrv) {
2623 newsrv->served++;
2624 if (newsrv->proxy->lbprm.server_take_conn)
2625 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002626 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002627 }
2628}
2629
Willy Tarreau84455332009-03-15 22:34:05 +01002630/* Handle server-side errors for default protocols. It is called whenever a a
2631 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002632 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002633 * them. It's installed as ->srv_error for the server-side stream_interface.
2634 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002635void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002636{
2637 int err_type = si->err_type;
2638 int err = 0, fin = 0;
2639
2640 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002641 err = SF_ERR_CLICL;
2642 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002643 }
2644 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002645 err = SF_ERR_CLICL;
2646 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002647 }
2648 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002649 err = SF_ERR_SRVTO;
2650 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002651 }
2652 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002653 err = SF_ERR_SRVCL;
2654 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002655 }
2656 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002657 err = SF_ERR_SRVTO;
2658 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002659 }
2660 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002661 err = SF_ERR_SRVCL;
2662 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002663 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002664 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002665 err = SF_ERR_RESOURCE;
2666 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002667 }
Willy Tarreau84455332009-03-15 22:34:05 +01002668 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002669 err = SF_ERR_INTERNAL;
2670 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002671 }
2672
Willy Tarreaue7dff022015-04-03 01:14:29 +02002673 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002674 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002675 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002676 s->flags |= fin;
2677}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002678
Willy Tarreaue7dff022015-04-03 01:14:29 +02002679/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002680void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002681{
Willy Tarreau87b09662015-04-03 00:22:06 +02002682 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002683 return;
2684
Willy Tarreau87b09662015-04-03 00:22:06 +02002685 channel_shutw_now(&stream->req);
2686 channel_shutr_now(&stream->res);
2687 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002688 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002689 stream->flags |= why;
2690 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002691}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002692
Willy Tarreau8b22a712010-06-18 17:46:06 +02002693/************************************************************************/
2694/* All supported ACL keywords must be declared here. */
2695/************************************************************************/
2696
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002697/* Returns a pointer to a stkctr depending on the fetch keyword name.
2698 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002699 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002700 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002701 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002702 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002703 * NULL may be returned if the designated stkctr is not tracked. For
2704 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2705 * passed. When present, the currently tracked key is then looked up
2706 * in the specified table instead of the current table. The purpose is
2707 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002708 * multiple tables). <strm> is allowed to be NULL, in which case only
2709 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002710 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002711struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002712smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002713{
2714 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002715 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002716 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002717 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002718 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002719
Willy Tarreau0f791d42013-07-23 19:56:43 +02002720 if (num == '_' - '0') {
2721 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002722 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002723 if (num >= MAX_SESS_STKCTR)
2724 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002725 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002726 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002727 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002728 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002729 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002730
2731 if (!conn)
2732 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002733
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002734 /* Fetch source adress in a sample. */
2735 smp.px = NULL;
2736 smp.sess = sess;
2737 smp.strm = strm;
2738 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2739 return NULL;
2740
2741 /* Converts into key. */
2742 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002743 if (!key)
2744 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002745
Willy Tarreaua65536c2013-07-22 22:40:11 +02002746 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002747 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002748 return &stkctr;
2749 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002750
2751 /* Here, <num> contains the counter number from 0 to 9 for
2752 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002753 * args[arg] is the first optional argument. We first lookup the
2754 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002755 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002756
Thierry Fournier40e1d512016-03-29 21:27:36 +02002757 if (strm)
2758 stkptr = &strm->stkctr[num];
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002759 if (!strm || !stkctr_entry(stkptr)) {
2760 stkptr = &sess->stkctr[num];
2761 if (!stkctr_entry(stkptr))
2762 return NULL;
2763 }
2764
2765 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002766 if (!stksess)
2767 return NULL;
2768
Willy Tarreau0f791d42013-07-23 19:56:43 +02002769 if (unlikely(args[arg].type == ARGT_TAB)) {
2770 /* an alternate table was specified, let's look up the same key there */
2771 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002772 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002773 return &stkctr;
2774 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002775 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002776}
2777
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002778/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2779 * the entry if it doesn't exist yet. This is needed for a few fetch
2780 * functions which need to create an entry, such as src_inc_gpc* and
2781 * src_clr_gpc*.
2782 */
2783struct stkctr *
2784smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2785{
2786 static struct stkctr stkctr;
2787 struct stktable_key *key;
2788 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002789 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002790
2791 if (strncmp(kw, "src_", 4) != 0)
2792 return NULL;
2793
2794 if (!conn)
2795 return NULL;
2796
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002797 /* Fetch source adress in a sample. */
2798 smp.px = NULL;
2799 smp.sess = sess;
2800 smp.strm = strm;
2801 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2802 return NULL;
2803
2804 /* Converts into key. */
2805 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002806 if (!key)
2807 return NULL;
2808
2809 stkctr.table = &args->data.prx->table;
2810 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2811 return &stkctr;
2812}
2813
Willy Tarreau87b09662015-04-03 00:22:06 +02002814/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002815 * currently being tracked or not.
2816 * Supports being called as "sc[0-9]_tracked" only.
2817 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002818static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002819smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002820{
2821 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002822 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002823 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002824 return 1;
2825}
2826
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002827/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2828 * frontend counters or from the src.
2829 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2830 * zero is returned if the key is new.
2831 */
2832static int
2833smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2834{
Willy Tarreaube508f12016-03-10 11:47:01 +01002835 struct stkctr *stkctr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002836
Willy Tarreaube508f12016-03-10 11:47:01 +01002837 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002838 if (!stkctr)
2839 return 0;
2840
2841 smp->flags = SMP_F_VOL_TEST;
2842 smp->data.type = SMP_T_SINT;
2843 smp->data.u.sint = 0;
2844
2845 if (stkctr_entry(stkctr) != NULL) {
2846 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2847 if (!ptr)
2848 return 0; /* parameter not stored */
2849 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2850 }
2851 return 1;
2852}
2853
Willy Tarreau87b09662015-04-03 00:22:06 +02002854/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002855 * frontend counters or from the src.
2856 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2857 * zero is returned if the key is new.
2858 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002859static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002860smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002861{
Willy Tarreaube508f12016-03-10 11:47:01 +01002862 struct stkctr *stkctr;
2863
Willy Tarreaube508f12016-03-10 11:47:01 +01002864 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002865 if (!stkctr)
2866 return 0;
2867
Willy Tarreau37406352012-04-23 16:16:37 +02002868 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002869 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002870 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002871
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002872 if (stkctr_entry(stkctr) != NULL) {
2873 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002874 if (!ptr)
2875 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002876 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002877 }
2878 return 1;
2879}
2880
Willy Tarreau87b09662015-04-03 00:22:06 +02002881/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002882 * tracked frontend counters or from the src.
2883 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2884 * Value zero is returned if the key is new.
2885 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002886static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002887smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002888{
Willy Tarreaube508f12016-03-10 11:47:01 +01002889 struct stkctr *stkctr;
2890
Willy Tarreaube508f12016-03-10 11:47:01 +01002891 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002892 if (!stkctr)
2893 return 0;
2894
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002895 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002896 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002897 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002898 if (stkctr_entry(stkctr) != NULL) {
2899 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002900 if (!ptr)
2901 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002902 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002903 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002904 }
2905 return 1;
2906}
2907
Willy Tarreau87b09662015-04-03 00:22:06 +02002908/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002909 * frontend counters and return it into temp integer.
2910 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002911 */
2912static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002913smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002914{
Willy Tarreaube508f12016-03-10 11:47:01 +01002915 struct stkctr *stkctr;
2916
Willy Tarreaube508f12016-03-10 11:47:01 +01002917 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002918 if (!stkctr)
2919 return 0;
2920
Willy Tarreau37406352012-04-23 16:16:37 +02002921 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002922 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002923 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002924
2925 if (stkctr_entry(stkctr) == NULL)
2926 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2927
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002928 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002929 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002930
2931 /* First, update gpc0_rate if it's tracked. Second, update its
2932 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2933 */
Emeric Brun57056f02015-06-15 18:29:57 +02002934 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2935 if (ptr1) {
2936 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002937 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002938 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002939 }
2940
Emeric Brun57056f02015-06-15 18:29:57 +02002941 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2942 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002943 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002944
Emeric Brun57056f02015-06-15 18:29:57 +02002945 /* If data was modified, we need to touch to re-schedule sync */
2946 if (ptr1 || ptr2)
2947 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002948 }
2949 return 1;
2950}
2951
Willy Tarreau87b09662015-04-03 00:22:06 +02002952/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002953 * frontend counters and return its previous value into temp integer.
2954 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002955 */
2956static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002957smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002958{
Willy Tarreaube508f12016-03-10 11:47:01 +01002959 struct stkctr *stkctr;
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002960
Willy Tarreaube508f12016-03-10 11:47:01 +01002961 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002962 if (!stkctr)
2963 return 0;
2964
Willy Tarreau37406352012-04-23 16:16:37 +02002965 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002966 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002967 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002968
2969 if (stkctr_entry(stkctr) == NULL)
2970 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2971
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002972 if (stkctr_entry(stkctr) != NULL) {
2973 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002974 if (!ptr)
2975 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002976 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002977 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002978 /* If data was modified, we need to touch to re-schedule sync */
2979 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002980 }
2981 return 1;
2982}
2983
Willy Tarreau87b09662015-04-03 00:22:06 +02002984/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002985 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2986 * "src_conn_cnt" only.
2987 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002988static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002989smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002990{
Willy Tarreaube508f12016-03-10 11:47:01 +01002991 struct stkctr *stkctr;
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002992
Willy Tarreaube508f12016-03-10 11:47:01 +01002993 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002994 if (!stkctr)
2995 return 0;
2996
Willy Tarreau37406352012-04-23 16:16:37 +02002997 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002998 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002999 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003000 if (stkctr_entry(stkctr) != NULL) {
3001 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003002 if (!ptr)
3003 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003004 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003005 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02003006 return 1;
3007}
3008
Willy Tarreau87b09662015-04-03 00:22:06 +02003009/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02003010 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3011 * only.
3012 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003013static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003014smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003015{
Willy Tarreaube508f12016-03-10 11:47:01 +01003016 struct stkctr *stkctr;
3017
Willy Tarreaube508f12016-03-10 11:47:01 +01003018 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02003019 if (!stkctr)
3020 return 0;
3021
Willy Tarreau37406352012-04-23 16:16:37 +02003022 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003023 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003024 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003025 if (stkctr_entry(stkctr) != NULL) {
3026 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003027 if (!ptr)
3028 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003029 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02003030 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003031 }
3032 return 1;
3033}
3034
Willy Tarreau87b09662015-04-03 00:22:06 +02003035/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02003036 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003037 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02003038 */
3039static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003040smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003041{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003042 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003043 struct stksess *ts;
3044 struct stktable_key *key;
3045 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003046 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003047
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003048 if (!conn)
3049 return 0;
3050
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02003051 /* Fetch source adress in a sample. */
3052 if (!smp_fetch_src(NULL, smp, NULL, NULL))
3053 return 0;
3054
3055 /* Converts into key. */
3056 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003057 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003058 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003059
Willy Tarreau24e32d82012-04-23 23:55:44 +02003060 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003061
Willy Tarreau1f7e9252010-06-20 12:27:21 +02003062 if ((ts = stktable_update_key(&px->table, key)) == NULL)
3063 /* entry does not exist and could not be created */
3064 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003065
3066 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
3067 if (!ptr)
3068 return 0; /* parameter not stored in this table */
3069
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003070 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003071 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02003072 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02003073 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003074 return 1;
3075}
3076
Willy Tarreau87b09662015-04-03 00:22:06 +02003077/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02003078 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3079 * "src_conn_cur" only.
3080 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003081static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003082smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003083{
Willy Tarreaube508f12016-03-10 11:47:01 +01003084 struct stkctr *stkctr;
3085
Willy Tarreaube508f12016-03-10 11:47:01 +01003086 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003087 if (!stkctr)
3088 return 0;
3089
Willy Tarreau37406352012-04-23 16:16:37 +02003090 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003091 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003092 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003093 if (stkctr_entry(stkctr) != NULL) {
3094 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003095 if (!ptr)
3096 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003097 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003098 }
3099 return 1;
3100}
3101
Willy Tarreau87b09662015-04-03 00:22:06 +02003102/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003103 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3104 * "src_sess_cnt" only.
3105 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003106static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003107smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003108{
Willy Tarreaube508f12016-03-10 11:47:01 +01003109 struct stkctr *stkctr;
3110
Willy Tarreaube508f12016-03-10 11:47:01 +01003111 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003112 if (!stkctr)
3113 return 0;
3114
Willy Tarreau37406352012-04-23 16:16:37 +02003115 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003116 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003117 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003118 if (stkctr_entry(stkctr) != NULL) {
3119 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003120 if (!ptr)
3121 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003122 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003123 }
3124 return 1;
3125}
3126
Willy Tarreau87b09662015-04-03 00:22:06 +02003127/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003128 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3129 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003130static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003131smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003132{
Willy Tarreaube508f12016-03-10 11:47:01 +01003133 struct stkctr *stkctr;
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003134
Willy Tarreaube508f12016-03-10 11:47:01 +01003135 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003136 if (!stkctr)
3137 return 0;
3138
Willy Tarreau37406352012-04-23 16:16:37 +02003139 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003140 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003141 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003142 if (stkctr_entry(stkctr) != NULL) {
3143 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003144 if (!ptr)
3145 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003146 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003147 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003148 }
3149 return 1;
3150}
3151
Willy Tarreau87b09662015-04-03 00:22:06 +02003152/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003153 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3154 * "src_http_req_cnt" only.
3155 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003156static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003157smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003158{
Willy Tarreaube508f12016-03-10 11:47:01 +01003159 struct stkctr *stkctr;
Willy Tarreau91200da2013-07-23 15:55:19 +02003160
Willy Tarreaube508f12016-03-10 11:47:01 +01003161 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003162 if (!stkctr)
3163 return 0;
3164
Willy Tarreau37406352012-04-23 16:16:37 +02003165 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003166 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003167 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003168 if (stkctr_entry(stkctr) != NULL) {
3169 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003170 if (!ptr)
3171 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003172 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003173 }
3174 return 1;
3175}
3176
Willy Tarreau87b09662015-04-03 00:22:06 +02003177/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003178 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3179 * "src_http_req_rate" only.
3180 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003181static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003182smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003183{
Willy Tarreaube508f12016-03-10 11:47:01 +01003184 struct stkctr *stkctr;
3185
Willy Tarreaube508f12016-03-10 11:47:01 +01003186 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003187 if (!stkctr)
3188 return 0;
3189
Willy Tarreau37406352012-04-23 16:16:37 +02003190 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003191 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003192 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003193 if (stkctr_entry(stkctr) != NULL) {
3194 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003195 if (!ptr)
3196 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003197 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003198 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003199 }
3200 return 1;
3201}
3202
Willy Tarreau87b09662015-04-03 00:22:06 +02003203/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003204 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3205 * "src_http_err_cnt" only.
3206 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003207static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003208smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003209{
Willy Tarreaube508f12016-03-10 11:47:01 +01003210 struct stkctr *stkctr;
3211
Willy Tarreaube508f12016-03-10 11:47:01 +01003212 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003213 if (!stkctr)
3214 return 0;
3215
Willy Tarreau37406352012-04-23 16:16:37 +02003216 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003217 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003218 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003219 if (stkctr_entry(stkctr) != NULL) {
3220 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003221 if (!ptr)
3222 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003223 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003224 }
3225 return 1;
3226}
3227
Willy Tarreau87b09662015-04-03 00:22:06 +02003228/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003229 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3230 * "src_http_err_rate" only.
3231 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003232static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003233smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003234{
Willy Tarreaube508f12016-03-10 11:47:01 +01003235 struct stkctr *stkctr;
3236
Willy Tarreaube508f12016-03-10 11:47:01 +01003237 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003238 if (!stkctr)
3239 return 0;
3240
Willy Tarreau37406352012-04-23 16:16:37 +02003241 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003242 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003243 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003244 if (stkctr_entry(stkctr) != NULL) {
3245 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003246 if (!ptr)
3247 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003248 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003249 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003250 }
3251 return 1;
3252}
3253
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003254/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003255 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003256 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3257 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003258static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003259smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003260{
Willy Tarreaube508f12016-03-10 11:47:01 +01003261 struct stkctr *stkctr;
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003262
Willy Tarreaube508f12016-03-10 11:47:01 +01003263 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003264 if (!stkctr)
3265 return 0;
3266
Willy Tarreau37406352012-04-23 16:16:37 +02003267 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003268 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003269 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003270 if (stkctr_entry(stkctr) != NULL) {
3271 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003272 if (!ptr)
3273 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003274 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003275 }
3276 return 1;
3277}
3278
Willy Tarreau613fe992013-07-23 17:39:19 +02003279/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003280 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003281 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003282 */
3283static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003284smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003285{
Willy Tarreaube508f12016-03-10 11:47:01 +01003286 struct stkctr *stkctr;
Willy Tarreau613fe992013-07-23 17:39:19 +02003287
Willy Tarreaube508f12016-03-10 11:47:01 +01003288 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003289 if (!stkctr)
3290 return 0;
3291
Willy Tarreau37406352012-04-23 16:16:37 +02003292 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003293 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003294 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003295 if (stkctr_entry(stkctr) != NULL) {
3296 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003297 if (!ptr)
3298 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003299 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003300 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003301 }
3302 return 1;
3303}
3304
Willy Tarreau53aea102013-07-23 17:39:02 +02003305/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003306 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003307 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3308 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003309static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003310smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003311{
Willy Tarreaube508f12016-03-10 11:47:01 +01003312 struct stkctr *stkctr;
3313
Willy Tarreaube508f12016-03-10 11:47:01 +01003314 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003315 if (!stkctr)
3316 return 0;
3317
Willy Tarreau37406352012-04-23 16:16:37 +02003318 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003319 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003320 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003321 if (stkctr_entry(stkctr) != NULL) {
3322 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003323 if (!ptr)
3324 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003325 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003326 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003327 return 1;
3328}
3329
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003330/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003331 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003332 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003333 */
3334static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003335smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003336{
Willy Tarreaube508f12016-03-10 11:47:01 +01003337 struct stkctr *stkctr;
3338
Willy Tarreaube508f12016-03-10 11:47:01 +01003339 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003340 if (!stkctr)
3341 return 0;
3342
Willy Tarreau37406352012-04-23 16:16:37 +02003343 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003344 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003345 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003346 if (stkctr_entry(stkctr) != NULL) {
3347 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003348 if (!ptr)
3349 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003350 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003351 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003352 }
3353 return 1;
3354}
3355
Willy Tarreau87b09662015-04-03 00:22:06 +02003356/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003357 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3358 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003359static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003360smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003361{
Willy Tarreaube508f12016-03-10 11:47:01 +01003362 struct stkctr *stkctr;
3363
Willy Tarreaube508f12016-03-10 11:47:01 +01003364 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau563eef42013-07-23 18:32:02 +02003365 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003366 return 0;
3367
Willy Tarreau563eef42013-07-23 18:32:02 +02003368 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003369 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003370 smp->data.u.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003371 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003372}
3373
Willy Tarreau34db1082012-04-19 17:16:54 +02003374/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003375 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003376 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003377static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003378smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003379{
Willy Tarreau37406352012-04-23 16:16:37 +02003380 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003381 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003382 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003383 return 1;
3384}
3385
Willy Tarreau34db1082012-04-19 17:16:54 +02003386/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003387 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003388 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003389static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003390smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003391{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003392 struct proxy *px;
3393
Willy Tarreau24e32d82012-04-23 23:55:44 +02003394 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003395 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003396 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003397 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003398 return 1;
3399}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003400
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003401/* 0=OK, <0=Alert, >0=Warning */
3402static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3403 struct proxy *px, struct act_rule *rule,
3404 char **err)
3405{
3406 struct action_kw *kw;
3407
3408 /* Check if the service name exists. */
3409 if (*(args[*cur_arg]) == 0) {
3410 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003411 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003412 }
3413
3414 /* lookup for keyword corresponding to a service. */
3415 kw = action_lookup(&service_keywords, args[*cur_arg]);
3416 if (!kw) {
3417 memprintf(err, "'%s' unknown service name.", args[1]);
3418 return ACT_RET_PRS_ERR;
3419 }
3420 (*cur_arg)++;
3421
3422 /* executes specific rule parser. */
3423 rule->kw = kw;
3424 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3425 return ACT_RET_PRS_ERR;
3426
3427 /* Register processing function. */
3428 rule->action_ptr = process_use_service;
3429 rule->action = ACT_CUSTOM;
3430
3431 return ACT_RET_PRS_OK;
3432}
3433
3434void service_keywords_register(struct action_kw_list *kw_list)
3435{
3436 LIST_ADDQ(&service_keywords, &kw_list->list);
3437}
3438
3439/* main configuration keyword registration. */
3440static struct action_kw_list stream_tcp_keywords = { ILH, {
3441 { "use-service", stream_parse_use_service },
3442 { /* END */ }
3443}};
3444
3445static struct action_kw_list stream_http_keywords = { ILH, {
3446 { "use-service", stream_parse_use_service },
3447 { /* END */ }
3448}};
3449
Willy Tarreau61612d42012-04-19 18:42:05 +02003450/* Note: must not be declared <const> as its list will be overwritten.
3451 * Please take care of keeping this list alphabetically sorted.
3452 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003453static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003454 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003455}};
3456
Willy Tarreau281c7992013-01-08 01:23:27 +01003457/* Note: must not be declared <const> as its list will be overwritten.
3458 * Please take care of keeping this list alphabetically sorted.
3459 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003460static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003461 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3462 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3463 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3464 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3465 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3466 { "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 +02003467 { "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 +02003468 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3469 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3470 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3471 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3472 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3473 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3474 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3475 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3476 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3477 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3478 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3479 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3480 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003481 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3482 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3483 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3484 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3485 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3486 { "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 +02003487 { "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 +02003488 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3489 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3490 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3491 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3492 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3493 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3494 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3495 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3496 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3497 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3498 { "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 +02003499 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003500 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3501 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3502 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3503 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3504 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3505 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3506 { "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 +02003507 { "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 +02003508 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3509 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3510 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3511 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3512 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3513 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3514 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3515 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3516 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3517 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3518 { "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 +02003519 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003520 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3521 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3522 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3523 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3524 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3525 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3526 { "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 +02003527 { "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 +02003528 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3529 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3530 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3531 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3532 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3533 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3534 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3535 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3536 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3537 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3538 { "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 +02003539 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003540 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3541 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3542 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3543 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3544 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3545 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3546 { "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 +02003547 { "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 +02003548 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3549 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3550 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3551 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3552 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3553 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3554 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3555 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3556 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3557 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3558 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3559 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3560 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3561 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003562 { /* END */ },
3563}};
3564
Willy Tarreau8b22a712010-06-18 17:46:06 +02003565__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003566static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003567{
Willy Tarreau281c7992013-01-08 01:23:27 +01003568 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003569 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003570 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3571 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003572}
3573
Willy Tarreaubaaee002006-06-26 02:48:02 +02003574/*
3575 * Local variables:
3576 * c-indent-level: 8
3577 * c-basic-offset: 8
3578 * End:
3579 */