blob: c5d6d437b10978542dfa1b02936e9266e3f49b23 [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. */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200323 vars_prune(&s->vars_txn, s);
324 vars_prune(&s->vars_reqres, s);
325
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;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200758 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200759 if (req->flags & CF_WAKE_CONNECT) {
760 req->flags |= CF_WAKE_ONCE;
761 req->flags &= ~CF_WAKE_CONNECT;
762 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200763 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200764 /* real connections have timeouts */
765 req->wto = s->be->timeout.server;
766 rep->rto = s->be->timeout.server;
767 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100768 req->wex = TICK_ETERNITY;
769}
770
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100771/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
772 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100773 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
774 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
775 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100776 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200777static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100778{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100779 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100780 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100781 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100782
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100783 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 +0100784 now_ms, __FUNCTION__,
785 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400786 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100787 req->rex, s->res.wex,
788 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400789 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 +0100790
791 if (si->state == SI_ST_ASS) {
792 /* Server assigned to connection request, we have to try to connect now */
793 int conn_err;
794
795 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100796 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100797
Willy Tarreaue7dff022015-04-03 01:14:29 +0200798 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100799 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100800 if (srv)
801 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500802 if (srv)
803 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100804 return;
805 }
806
807 /* We have received a synchronous error. We might have to
808 * abort, retry immediately or redispatch.
809 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200810 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100811 if (!si->err_type) {
812 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813 }
814
Willy Tarreau827aee92011-03-10 16:55:02 +0100815 if (srv)
816 srv_inc_sess_ctr(srv);
817 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500818 srv_set_sess_last(srv);
819 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100820 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100821 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822
Willy Tarreau87b09662015-04-03 00:22:06 +0200823 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100824 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100825 if (may_dequeue_tasks(srv, s->be))
826 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100827
828 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200829 si_shutr(si);
830 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100831 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832
833 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
834
Willy Tarreau87b09662015-04-03 00:22:06 +0200835 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100836 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100837 if (s->srv_error)
838 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100839 return;
840 }
841
842 /* We are facing a retryable error, but we don't want to run a
843 * turn-around now, as the problem is likely a source port
844 * allocation problem, so we want to retry now.
845 */
846 si->state = SI_ST_CER;
847 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100848 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100849 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
850 return;
851 }
852 else if (si->state == SI_ST_QUE) {
853 /* connection request was queued, check for any update */
854 if (!s->pend_pos) {
855 /* The connection is not in the queue anymore. Either
856 * we have a server connection slot available and we
857 * go directly to the assigned state, or we need to
858 * load-balance first and go to the INI state.
859 */
860 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200861 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100862 si->state = SI_ST_REQ;
863 else {
864 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
865 si->state = SI_ST_ASS;
866 }
867 return;
868 }
869
870 /* Connection request still in queue... */
871 if (si->flags & SI_FL_EXP) {
872 /* ... and timeout expired */
873 si->exp = TICK_ETERNITY;
874 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100875 if (srv)
876 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100877 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200878 si_shutr(si);
879 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100880 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100881 if (!si->err_type)
882 si->err_type = SI_ET_QUEUE_TO;
883 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100884 if (s->srv_error)
885 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100886 return;
887 }
888
889 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau103197d2014-11-28 15:26:12 +0100890 if ((req->flags & (CF_READ_ERROR)) ||
891 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
892 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100893 /* give up */
894 si->exp = TICK_ETERNITY;
895 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +0200896 si_shutr(si);
897 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100898 si->err_type |= SI_ET_QUEUE_ABRT;
899 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100900 if (s->srv_error)
901 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100902 return;
903 }
904
905 /* Nothing changed */
906 return;
907 }
908 else if (si->state == SI_ST_TAR) {
909 /* Connection request might be aborted */
Willy Tarreau103197d2014-11-28 15:26:12 +0100910 if ((req->flags & (CF_READ_ERROR)) ||
911 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
912 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100913 /* give up */
914 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200915 si_shutr(si);
916 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100917 si->err_type |= SI_ET_CONN_ABRT;
918 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100919 if (s->srv_error)
920 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100921 return;
922 }
923
924 if (!(si->flags & SI_FL_EXP))
925 return; /* still in turn-around */
926
927 si->exp = TICK_ETERNITY;
928
Willy Tarreau87b09662015-04-03 00:22:06 +0200929 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100930 * marked "assigned".
931 * FIXME: Should we force a redispatch attempt when the server is down ?
932 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200933 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100934 si->state = SI_ST_ASS;
935 else
936 si->state = SI_ST_REQ;
937 return;
938 }
939}
940
Willy Tarreau87b09662015-04-03 00:22:06 +0200941/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900942 * also counts a failed request if the server state has not reached the request
943 * stage.
944 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200945static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900946{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200947 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900948 if (s->si[1].state < SI_ST_REQ) {
949
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200950 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200951 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200952 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900953
Willy Tarreaue7dff022015-04-03 01:14:29 +0200954 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900955 }
956 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200957 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900958 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200959 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900960 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200961 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900962 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200963 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900964 }
965}
966
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100967/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100968 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
969 * a real connection to a server, indicating that a server has been assigned,
970 * or SI_ST_EST for a successful connection to an applet. It may also return
971 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100972 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200973static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100974{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100975 struct stream_interface *si = &s->si[1];
976
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100977 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 +0100978 now_ms, __FUNCTION__,
979 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400980 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100981 s->req.rex, s->res.wex,
982 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400983 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 +0100984
985 if (si->state != SI_ST_REQ)
986 return;
987
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100988 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
989 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +0100990 struct appctx *appctx = objt_appctx(si->end);
991
992 if (!appctx || appctx->applet != __objt_applet(s->target))
993 appctx = stream_int_register_handler(si, objt_applet(s->target));
994
995 if (!appctx) {
996 /* No more memory, let's immediately abort. Force the
997 * error code to ignore the ERR_LOCAL which is not a
998 * real error.
999 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001000 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001001
1002 si_shutr(si);
1003 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001004 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001005 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001006 si->state = SI_ST_CLO;
1007 if (s->srv_error)
1008 s->srv_error(s, si);
1009 return;
1010 }
1011
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001012 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001013 si->state = SI_ST_EST;
1014 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001015 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001016 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001017 return;
1018 }
1019
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001020 /* Try to assign a server */
1021 if (srv_redispatch_connect(s) != 0) {
1022 /* We did not get a server. Either we queued the
1023 * connection request, or we encountered an error.
1024 */
1025 if (si->state == SI_ST_QUE)
1026 return;
1027
1028 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001029 si_shutr(si);
1030 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001031 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001032 if (!si->err_type)
1033 si->err_type = SI_ET_CONN_OTHER;
1034 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001035 if (s->srv_error)
1036 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001037 return;
1038 }
1039
1040 /* The server is assigned */
1041 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1042 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001043 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001044}
1045
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001046/* This function parses the use-service action ruleset. It executes
1047 * the associated ACL and set an applet as a stream or txn final node.
1048 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1049 * consistent state. It returns ACT_RET_STOP in succes case because
1050 * use-service must be a terminal action. Returns ACT_RET_YIELD
1051 * if the initialisation function require more data.
1052 */
1053enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1054 struct session *sess, struct stream *s, int flags)
1055
1056{
1057 struct appctx *appctx;
1058
1059 /* Initialises the applet if it is required. */
1060 if (flags & ACT_FLAG_FIRST) {
1061 /* Register applet. this function schedules the applet. */
1062 s->target = &rule->applet.obj_type;
1063 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1064 return ACT_RET_ERR;
1065
1066 /* Initialise the context. */
1067 appctx = si_appctx(&s->si[1]);
1068 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1069 appctx->rule = rule;
1070 }
1071 else
1072 appctx = si_appctx(&s->si[1]);
1073
1074 /* Stops the applet sheduling, in case of the init function miss
1075 * some data.
1076 */
1077 appctx_pause(appctx);
1078 si_applet_stop_get(&s->si[1]);
1079
1080 /* Call initialisation. */
1081 if (rule->applet.init)
1082 switch (rule->applet.init(appctx, px, s)) {
1083 case 0: return ACT_RET_ERR;
1084 case 1: break;
1085 default: return ACT_RET_YIELD;
1086 }
1087
1088 /* Now we can schedule the applet. */
1089 si_applet_cant_get(&s->si[1]);
1090 appctx_wakeup(appctx);
1091
1092 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1093 sess->fe->fe_counters.intercepted_req++;
1094
1095 /* The flag SF_ASSIGNED prevent from server assignment. */
1096 s->flags |= SF_ASSIGNED;
1097
1098 return ACT_RET_STOP;
1099}
1100
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001101/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001102 * if appropriate. The default_backend rule is also considered, then the
1103 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001104 * It returns 1 if the processing can continue on next analysers, or zero if it
1105 * either needs more data or wants to immediately abort the request.
1106 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001107static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001108{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001109 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001110 struct session *sess = s->sess;
1111 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001112
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001113 req->analysers &= ~an_bit;
1114 req->analyse_exp = TICK_ETERNITY;
1115
Willy Tarreau87b09662015-04-03 00:22:06 +02001116 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 +02001117 now_ms, __FUNCTION__,
1118 s,
1119 req,
1120 req->rex, req->wex,
1121 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001122 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001123 req->analysers);
1124
1125 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001126 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001127 struct switching_rule *rule;
1128
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001129 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001130 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001131
Willy Tarreauf51658d2014-04-23 01:21:56 +02001132 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001133 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001134 ret = acl_pass(ret);
1135 if (rule->cond->pol == ACL_COND_UNLESS)
1136 ret = !ret;
1137 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001138
1139 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001140 /* If the backend name is dynamic, try to resolve the name.
1141 * If we can't resolve the name, or if any error occurs, break
1142 * the loop and fallback to the default backend.
1143 */
1144 struct proxy *backend;
1145
1146 if (rule->dynamic) {
1147 struct chunk *tmp = get_trash_chunk();
1148 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1149 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001150 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001151 if (!backend)
1152 break;
1153 }
1154 else
1155 backend = rule->be.backend;
1156
Willy Tarreau87b09662015-04-03 00:22:06 +02001157 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001158 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001159 break;
1160 }
1161 }
1162
1163 /* To ensure correct connection accounting on the backend, we
1164 * have to assign one if it was not set (eg: a listen). This
1165 * measure also takes care of correctly setting the default
1166 * backend if any.
1167 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001168 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001169 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001170 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001171 }
1172
Willy Tarreaufb356202010-08-03 14:02:05 +02001173 /* 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 +02001174 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001175 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1176 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001177 s->req.analysers &= ~AN_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001178 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001179
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001180 /* 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 +02001181 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001182 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001183 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001184 int ret = 1;
1185
1186 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001187 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 +01001188 ret = acl_pass(ret);
1189 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1190 ret = !ret;
1191 }
1192
1193 if (ret) {
1194 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001195 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001196 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001197 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001198 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001199 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001200 break;
1201 }
1202 }
1203
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001204 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001205
1206 sw_failed:
1207 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001208 channel_abort(&s->req);
1209 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001210
Willy Tarreaue7dff022015-04-03 01:14:29 +02001211 if (!(s->flags & SF_ERR_MASK))
1212 s->flags |= SF_ERR_RESOURCE;
1213 if (!(s->flags & SF_FINST_MASK))
1214 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001215
Willy Tarreaueee5b512015-04-03 23:46:31 +02001216 if (s->txn)
1217 s->txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001218 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001219 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001220 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001221}
1222
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001223/* This stream analyser works on a request. It applies all use-server rules on
1224 * it then returns 1. The data must already be present in the buffer otherwise
1225 * they won't match. It always returns 1.
1226 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001227static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001228{
1229 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001230 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001231 struct server_rule *rule;
1232
Willy Tarreau87b09662015-04-03 00:22:06 +02001233 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 +02001234 now_ms, __FUNCTION__,
1235 s,
1236 req,
1237 req->rex, req->wex,
1238 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001239 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001240 req->analysers);
1241
Willy Tarreaue7dff022015-04-03 01:14:29 +02001242 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001243 list_for_each_entry(rule, &px->server_rules, list) {
1244 int ret;
1245
Willy Tarreau192252e2015-04-04 01:47:55 +02001246 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001247 ret = acl_pass(ret);
1248 if (rule->cond->pol == ACL_COND_UNLESS)
1249 ret = !ret;
1250
1251 if (ret) {
1252 struct server *srv = rule->srv.ptr;
1253
Willy Tarreau892337c2014-05-13 23:41:20 +02001254 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001255 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001256 (s->flags & SF_FORCE_PRST)) {
1257 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001258 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001259 break;
1260 }
1261 /* if the server is not UP, let's go on with next rules
1262 * just in case another one is suited.
1263 */
1264 }
1265 }
1266 }
1267
1268 req->analysers &= ~an_bit;
1269 req->analyse_exp = TICK_ETERNITY;
1270 return 1;
1271}
1272
Emeric Brun1d33b292010-01-04 15:47:17 +01001273/* This stream analyser works on a request. It applies all sticking rules on
1274 * it then returns 1. The data must already be present in the buffer otherwise
1275 * they won't match. It always returns 1.
1276 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001277static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001278{
1279 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001280 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001281 struct sticking_rule *rule;
1282
Willy Tarreau87b09662015-04-03 00:22:06 +02001283 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 +01001284 now_ms, __FUNCTION__,
1285 s,
1286 req,
1287 req->rex, req->wex,
1288 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001289 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001290 req->analysers);
1291
1292 list_for_each_entry(rule, &px->sticking_rules, list) {
1293 int ret = 1 ;
1294 int i;
1295
Willy Tarreau9667a802013-12-09 12:52:13 +01001296 /* Only the first stick store-request of each table is applied
1297 * and other ones are ignored. The purpose is to allow complex
1298 * configurations which look for multiple entries by decreasing
1299 * order of precision and to stop at the first which matches.
1300 * An example could be a store of the IP address from an HTTP
1301 * header first, then from the source if not found.
1302 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001303 for (i = 0; i < s->store_count; i++) {
1304 if (rule->table.t == s->store[i].table)
1305 break;
1306 }
1307
1308 if (i != s->store_count)
1309 continue;
1310
1311 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001312 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001313 ret = acl_pass(ret);
1314 if (rule->cond->pol == ACL_COND_UNLESS)
1315 ret = !ret;
1316 }
1317
1318 if (ret) {
1319 struct stktable_key *key;
1320
Willy Tarreau192252e2015-04-04 01:47:55 +02001321 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 +01001322 if (!key)
1323 continue;
1324
1325 if (rule->flags & STK_IS_MATCH) {
1326 struct stksess *ts;
1327
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001328 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001329 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001330 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001331 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001332
1333 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001334 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1335 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001336 if (node) {
1337 struct server *srv;
1338
1339 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001340 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001341 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001342 (s->flags & SF_FORCE_PRST)) {
1343 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001344 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001345 }
1346 }
1347 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001348 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001349 }
1350 }
1351 if (rule->flags & STK_IS_STORE) {
1352 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1353 struct stksess *ts;
1354
1355 ts = stksess_new(rule->table.t, key);
1356 if (ts) {
1357 s->store[s->store_count].table = rule->table.t;
1358 s->store[s->store_count++].ts = ts;
1359 }
1360 }
1361 }
1362 }
1363 }
1364
1365 req->analysers &= ~an_bit;
1366 req->analyse_exp = TICK_ETERNITY;
1367 return 1;
1368}
1369
1370/* This stream analyser works on a response. It applies all store rules on it
1371 * then returns 1. The data must already be present in the buffer otherwise
1372 * they won't match. It always returns 1.
1373 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001374static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001375{
1376 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001377 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001378 struct sticking_rule *rule;
1379 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001380 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001381
Willy Tarreau87b09662015-04-03 00:22:06 +02001382 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 +01001383 now_ms, __FUNCTION__,
1384 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001385 rep,
1386 rep->rex, rep->wex,
1387 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001388 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001389 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001390
1391 list_for_each_entry(rule, &px->storersp_rules, list) {
1392 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001393
Willy Tarreau9667a802013-12-09 12:52:13 +01001394 /* Only the first stick store-response of each table is applied
1395 * and other ones are ignored. The purpose is to allow complex
1396 * configurations which look for multiple entries by decreasing
1397 * order of precision and to stop at the first which matches.
1398 * An example could be a store of a set-cookie value, with a
1399 * fallback to a parameter found in a 302 redirect.
1400 *
1401 * The store-response rules are not allowed to override the
1402 * store-request rules for the same table, but they may coexist.
1403 * Thus we can have up to one store-request entry and one store-
1404 * response entry for the same table at any time.
1405 */
1406 for (i = nbreq; i < s->store_count; i++) {
1407 if (rule->table.t == s->store[i].table)
1408 break;
1409 }
1410
1411 /* skip existing entries for this table */
1412 if (i < s->store_count)
1413 continue;
1414
Emeric Brun1d33b292010-01-04 15:47:17 +01001415 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001416 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001417 ret = acl_pass(ret);
1418 if (rule->cond->pol == ACL_COND_UNLESS)
1419 ret = !ret;
1420 }
1421
1422 if (ret) {
1423 struct stktable_key *key;
1424
Willy Tarreau192252e2015-04-04 01:47:55 +02001425 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 +01001426 if (!key)
1427 continue;
1428
Willy Tarreau37e340c2013-12-06 23:05:21 +01001429 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001430 struct stksess *ts;
1431
1432 ts = stksess_new(rule->table.t, key);
1433 if (ts) {
1434 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001435 s->store[s->store_count++].ts = ts;
1436 }
1437 }
1438 }
1439 }
1440
1441 /* process store request and store response */
1442 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001443 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001444 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001445
Willy Tarreauc93cd162014-05-13 15:54:22 +02001446 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001447 stksess_free(s->store[i].table, s->store[i].ts);
1448 s->store[i].ts = NULL;
1449 continue;
1450 }
1451
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001452 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1453 if (ts) {
1454 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001455 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001456 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001457 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001458 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001459 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001460
1461 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001462 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001463 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001464 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001465 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001466
1467 rep->analysers &= ~an_bit;
1468 rep->analyse_exp = TICK_ETERNITY;
1469 return 1;
1470}
1471
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001472/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001473 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001474 */
1475#define UPDATE_ANALYSERS(real, list, back, flag) { \
1476 list = (((list) & ~(flag)) | ~(back)) & (real); \
1477 back = real; \
1478 if (!(list)) \
1479 break; \
1480 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1481 continue; \
1482}
1483
Willy Tarreau87b09662015-04-03 00:22:06 +02001484/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001485 * then puts it back to the wait queue in a clean state, or cleans up its
1486 * resources if it must be deleted. Returns in <next> the date the task wants
1487 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1488 * nothing too many times, the request and response buffers flags are monitored
1489 * and each function is called only if at least another function has changed at
1490 * least one flag it is interested in.
1491 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001492struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001493{
Willy Tarreau827aee92011-03-10 16:55:02 +01001494 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001495 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001496 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001497 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001498 unsigned int rq_prod_last, rq_cons_last;
1499 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001500 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001501 struct channel *req, *res;
1502 struct stream_interface *si_f, *si_b;
1503
1504 req = &s->req;
1505 res = &s->res;
1506
1507 si_f = &s->si[0];
1508 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001509
1510 //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 +01001511 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001512
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001513 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001514 if (s->txn)
1515 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001516
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001517 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001518 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1519 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001520
1521 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001522 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1523 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001524
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001525 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001526 si_f->flags |= SI_FL_DONT_WAKE;
1527 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001528
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001529 /* 1a: Check for low level timeouts if needed. We just set a flag on
1530 * stream interfaces when their timeouts have expired.
1531 */
1532 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001533 stream_int_check_timeouts(si_f);
1534 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001535
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001536 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001537 * for future reads or writes. Note: this will also concern upper layers
1538 * but we do not touch any other flag. We must be careful and correctly
1539 * detect state changes when calling them.
1540 */
1541
Willy Tarreau8f128b42014-11-28 15:07:47 +01001542 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001543
Willy Tarreau8f128b42014-11-28 15:07:47 +01001544 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1545 si_b->flags |= SI_FL_NOLINGER;
1546 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001547 }
1548
Willy Tarreau8f128b42014-11-28 15:07:47 +01001549 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1550 if (si_f->flags & SI_FL_NOHALF)
1551 si_f->flags |= SI_FL_NOLINGER;
1552 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001553 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001554
Willy Tarreau8f128b42014-11-28 15:07:47 +01001555 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001556
Willy Tarreau8f128b42014-11-28 15:07:47 +01001557 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1558 si_f->flags |= SI_FL_NOLINGER;
1559 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001560 }
1561
Willy Tarreau8f128b42014-11-28 15:07:47 +01001562 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1563 if (si_b->flags & SI_FL_NOHALF)
1564 si_b->flags |= SI_FL_NOLINGER;
1565 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001566 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001567
1568 /* Once in a while we're woken up because the task expires. But
1569 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001570 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001571 * timeout needs to be refreshed.
1572 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001573 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001574 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1575 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001576 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau798f4322012-11-08 14:49:17 +01001577 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER))
1578 goto update_exp_and_leave;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001579 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001580
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001581 /* below we may emit error messages so we have to ensure that we have
1582 * our buffers properly allocated.
1583 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001584 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001585 /* No buffer available, we've been subscribed to the list of
1586 * buffer waiters, let's wait for our turn.
1587 */
1588 goto update_exp_and_leave;
1589 }
1590
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001591 /* 1b: check for low-level errors reported at the stream interface.
1592 * First we check if it's a retryable error (in which case we don't
1593 * want to tell the buffer). Otherwise we report the error one level
1594 * upper by setting flags into the buffers. Note that the side towards
1595 * the client cannot have connect (hence retryable) errors. Also, the
1596 * connection setup code must be able to deal with any type of abort.
1597 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001598 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001599 if (unlikely(si_f->flags & SI_FL_ERR)) {
1600 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1601 si_shutr(si_f);
1602 si_shutw(si_f);
1603 stream_int_report_error(si_f);
1604 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001605 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001606 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001607 if (srv)
1608 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001609 if (!(s->flags & SF_ERR_MASK))
1610 s->flags |= SF_ERR_CLICL;
1611 if (!(s->flags & SF_FINST_MASK))
1612 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001613 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001614 }
1615 }
1616
Willy Tarreau8f128b42014-11-28 15:07:47 +01001617 if (unlikely(si_b->flags & SI_FL_ERR)) {
1618 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1619 si_shutr(si_b);
1620 si_shutw(si_b);
1621 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001622 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001623 if (srv)
1624 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001625 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001626 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001627 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001628 if (srv)
1629 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001630 if (!(s->flags & SF_ERR_MASK))
1631 s->flags |= SF_ERR_SRVCL;
1632 if (!(s->flags & SF_FINST_MASK))
1633 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001634 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001635 }
1636 /* note: maybe we should process connection errors here ? */
1637 }
1638
Willy Tarreau8f128b42014-11-28 15:07:47 +01001639 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001640 /* we were trying to establish a connection on the server side,
1641 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1642 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001643 if (unlikely(!sess_update_st_con_tcp(s)))
1644 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001645 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001646 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001647
1648 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1649 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1650 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1651 */
1652 }
1653
Willy Tarreau8f128b42014-11-28 15:07:47 +01001654 rq_prod_last = si_f->state;
1655 rq_cons_last = si_b->state;
1656 rp_cons_last = si_f->state;
1657 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001658
1659 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001660 /* Check for connection closure */
1661
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001662 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001663 "[%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 +01001664 now_ms, __FUNCTION__, __LINE__,
1665 t,
1666 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001667 req, res,
1668 req->rex, res->wex,
1669 req->flags, res->flags,
1670 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1671 si_f->err_type, si_b->err_type,
1672 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001673
1674 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001675 if (unlikely(si_f->state == SI_ST_DIS))
1676 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001677
1678 /* When a server-side connection is released, we have to count it and
1679 * check for pending connections on this server.
1680 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001681 if (unlikely(si_b->state == SI_ST_DIS)) {
1682 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001683 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001684 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001685 if (s->flags & SF_CURR_SESS) {
1686 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001687 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001688 }
1689 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001690 if (may_dequeue_tasks(srv, s->be))
1691 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001692 }
1693 }
1694
1695 /*
1696 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1697 * at this point.
1698 */
1699
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001700 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001701 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001702 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1703 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1704 si_f->state != rq_prod_last ||
1705 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001706 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001707 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001708
Willy Tarreau8f128b42014-11-28 15:07:47 +01001709 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001710 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001711 unsigned int ana_list;
1712 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001713
Willy Tarreau90deb182010-01-07 00:20:41 +01001714 /* it's up to the analysers to stop new connections,
1715 * disable reading or closing. Note: if an analyser
1716 * disables any of these bits, it is responsible for
1717 * enabling them again when it disables itself, so
1718 * that other analysers are called in similar conditions.
1719 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001720 channel_auto_read(req);
1721 channel_auto_connect(req);
1722 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001723
1724 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001725 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001726 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001727 * the list when not needed. Any analyser may return 0
1728 * to break out of the loop, either because of missing
1729 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001730 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001731 * analyser, and we may loop again if other analysers
1732 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001733 *
1734 * We build a list of analysers to run. We evaluate all
1735 * of these analysers in the order of the lower bit to
1736 * the higher bit. This ordering is very important.
1737 * An analyser will often add/remove other analysers,
1738 * including itself. Any changes to itself have no effect
1739 * on the loop. If it removes any other analysers, we
1740 * want those analysers not to be called anymore during
1741 * this loop. If it adds an analyser that is located
1742 * after itself, we want it to be scheduled for being
1743 * processed during the loop. If it adds an analyser
1744 * which is located before it, we want it to switch to
1745 * it immediately, even if it has already been called
1746 * once but removed since.
1747 *
1748 * In order to achieve this, we compare the analyser
1749 * list after the call with a copy of it before the
1750 * call. The work list is fed with analyser bits that
1751 * appeared during the call. Then we compare previous
1752 * work list with the new one, and check the bits that
1753 * appeared. If the lowest of these bits is lower than
1754 * the current bit, it means we have enabled a previous
1755 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001756 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001757
Willy Tarreau8f128b42014-11-28 15:07:47 +01001758 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001759 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001760 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauletd7c91962015-04-30 11:48:27 +02001761 if (ana_list & AN_FLT_START_FE) {
1762 if (!flt_start_analyze(s, req, AN_FLT_START_FE))
1763 break;
1764 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_START_FE);
1765 }
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001766
Willy Tarreaufb356202010-08-03 14:02:05 +02001767 if (ana_list & AN_REQ_INSPECT_FE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001768 if (!flt_analyze(s, req, AN_REQ_INSPECT_FE))
1769 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001770 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_FE))
Willy Tarreauedcf6682008-11-30 23:15:34 +01001771 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001772 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_FE);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001773 }
Willy Tarreauedcf6682008-11-30 23:15:34 +01001774
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001775 if (ana_list & AN_REQ_WAIT_HTTP) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001776 if (!flt_analyze(s, req, AN_REQ_WAIT_HTTP))
1777 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001778 if (!http_wait_for_request(s, req, AN_REQ_WAIT_HTTP))
Willy Tarreaud787e662009-07-07 10:14:51 +02001779 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001780 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_WAIT_HTTP);
Willy Tarreaud787e662009-07-07 10:14:51 +02001781 }
1782
Willy Tarreau748179e2015-05-01 21:52:31 +02001783 if (ana_list & AN_REQ_HTTP_BODY) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001784 if (!flt_analyze(s, req, AN_REQ_HTTP_BODY))
1785 break;
Willy Tarreau748179e2015-05-01 21:52:31 +02001786 if (!http_wait_for_request_body(s, req, AN_REQ_HTTP_BODY))
1787 break;
1788 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_BODY);
1789 }
1790
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001791 if (ana_list & AN_REQ_HTTP_PROCESS_FE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001792 if (!flt_analyze(s, req, AN_REQ_HTTP_PROCESS_FE))
1793 break;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001794 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_FE, sess->fe))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001795 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001796 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001797 }
1798
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001799 if (ana_list & AN_REQ_SWITCHING_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001800 if (!flt_analyze(s, req, AN_REQ_SWITCHING_RULES))
1801 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001802 if (!process_switching_rules(s, req, AN_REQ_SWITCHING_RULES))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001803 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001804 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001805 }
1806
Willy Tarreaufb356202010-08-03 14:02:05 +02001807 if (ana_list & AN_REQ_INSPECT_BE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001808 if (!flt_analyze(s, req, AN_REQ_INSPECT_BE))
1809 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001810 if (!tcp_inspect_request(s, req, AN_REQ_INSPECT_BE))
Willy Tarreaufb356202010-08-03 14:02:05 +02001811 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001812 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_INSPECT_BE);
Willy Tarreaufb356202010-08-03 14:02:05 +02001813 }
1814
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001815 if (ana_list & AN_REQ_HTTP_PROCESS_BE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001816 if (!flt_analyze(s, req, AN_REQ_HTTP_PROCESS_BE))
1817 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001818 if (!http_process_req_common(s, req, AN_REQ_HTTP_PROCESS_BE, s->be))
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001819 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001820 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE);
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001821 }
1822
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001823 if (ana_list & AN_REQ_HTTP_TARPIT) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001824 if (!flt_analyze(s, req, AN_REQ_HTTP_TARPIT))
1825 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001826 if (!http_process_tarpit(s, req, AN_REQ_HTTP_TARPIT))
Willy Tarreau60b85b02008-11-30 23:28:40 +01001827 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001828 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001829 }
Willy Tarreau60b85b02008-11-30 23:28:40 +01001830
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001831 if (ana_list & AN_REQ_SRV_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001832 if (!flt_analyze(s, req, AN_REQ_SRV_RULES))
1833 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001834 if (!process_server_rules(s, req, AN_REQ_SRV_RULES))
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001835 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001836 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_SRV_RULES);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001837 }
1838
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001839 if (ana_list & AN_REQ_HTTP_INNER) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001840 if (!flt_analyze(s, req, AN_REQ_HTTP_INNER))
1841 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001842 if (!http_process_request(s, req, AN_REQ_HTTP_INNER))
Willy Tarreauc465fd72009-08-31 00:17:18 +02001843 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001844 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_HTTP_INNER);
Willy Tarreauc465fd72009-08-31 00:17:18 +02001845 }
1846
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001847 if (ana_list & AN_REQ_PRST_RDP_COOKIE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001848 if (!flt_analyze(s, req, AN_REQ_PRST_RDP_COOKIE))
1849 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001850 if (!tcp_persist_rdp_cookie(s, req, AN_REQ_PRST_RDP_COOKIE))
Emeric Brun647caf12009-06-30 17:57:00 +02001851 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
Emeric Brun647caf12009-06-30 17:57:00 +02001853 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001854
Emeric Brun1d33b292010-01-04 15:47:17 +01001855 if (ana_list & AN_REQ_STICKING_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001856 if (!flt_analyze(s, req, AN_REQ_STICKING_RULES))
1857 break;
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 Fauletd7c91962015-04-30 11:48:27 +02001863 if (ana_list & AN_FLT_START_BE) {
1864 if (!flt_start_analyze(s, req, AN_FLT_START_BE))
1865 break;
1866 UPDATE_ANALYSERS(req->analysers, ana_list, ana_back, AN_FLT_START_BE);
1867 }
1868
1869 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
Emeric Brun97679e72010-09-23 17:56:44 +02001961 if (ana_list & AN_RES_INSPECT) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001962 if (!flt_analyze(s, res, AN_RES_INSPECT))
1963 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001964 if (!tcp_inspect_response(s, res, AN_RES_INSPECT))
Emeric Brun97679e72010-09-23 17:56:44 +02001965 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001966 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_INSPECT);
Emeric Brun97679e72010-09-23 17:56:44 +02001967 }
1968
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001969 if (ana_list & AN_RES_WAIT_HTTP) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001970 if (!flt_analyze(s, res, AN_RES_WAIT_HTTP))
1971 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001972 if (!http_wait_for_response(s, res, AN_RES_WAIT_HTTP))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001973 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001974 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_WAIT_HTTP);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001975 }
1976
Emeric Brun1d33b292010-01-04 15:47:17 +01001977 if (ana_list & AN_RES_STORE_RULES) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001978 if (!flt_analyze(s, res, AN_RES_STORE_RULES))
1979 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001980 if (!process_store_rules(s, res, AN_RES_STORE_RULES))
Emeric Brun1d33b292010-01-04 15:47:17 +01001981 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001982 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_STORE_RULES);
Emeric Brun1d33b292010-01-04 15:47:17 +01001983 }
1984
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001985 if (ana_list & AN_RES_HTTP_PROCESS_BE) {
Christopher Fauletd7c91962015-04-30 11:48:27 +02001986 if (!flt_analyze(s, res, AN_RES_HTTP_PROCESS_BE))
1987 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001988 if (!http_process_res_common(s, res, AN_RES_HTTP_PROCESS_BE, s->be))
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001989 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001990 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001991 }
Willy Tarreaud98cf932009-12-27 22:54:55 +01001992
Christopher Fauletd7c91962015-04-30 11:48:27 +02001993 if (ana_list & AN_FLT_START_BE) {
1994 if (!flt_start_analyze(s, res, AN_FLT_START_BE))
1995 break;
1996 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_START_BE);
1997 }
1998
1999 if (ana_list & AN_FLT_XFER_DATA) {
2000 if (!flt_xfer_data(s, res, AN_FLT_XFER_DATA))
2001 break;
2002 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_XFER_DATA);
2003 }
2004
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002005 if (ana_list & AN_RES_HTTP_XFER_BODY) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002006 if (!http_response_forward_body(s, res, AN_RES_HTTP_XFER_BODY))
Willy Tarreaud98cf932009-12-27 22:54:55 +01002007 break;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002008 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Willy Tarreaud98cf932009-12-27 22:54:55 +01002009 }
Christopher Fauletd7c91962015-04-30 11:48:27 +02002010
2011 if (ana_list & AN_FLT_END) {
2012 if (!flt_end_analyze(s, res, AN_FLT_END))
2013 break;
2014 UPDATE_ANALYSERS(res->analysers, ana_list, ana_back, AN_FLT_END);
2015 }
Willy Tarreaue34070e2010-01-08 00:32:27 +01002016 break;
2017 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002018 }
2019
Willy Tarreau8f128b42014-11-28 15:07:47 +01002020 rp_cons_last = si_f->state;
2021 rp_prod_last = si_b->state;
2022 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002023
Willy Tarreau8f128b42014-11-28 15:07:47 +01002024 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002025 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002026 }
2027
Willy Tarreau576507f2010-01-07 00:09:04 +01002028 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002029 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002030 goto resync_request;
2031
Willy Tarreau8f128b42014-11-28 15:07:47 +01002032 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002033 goto resync_request;
2034
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002035 /* FIXME: here we should call protocol handlers which rely on
2036 * both buffers.
2037 */
2038
2039
2040 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002041 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002042 * we're just in a data phase here since it means we have not
2043 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002044 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002045 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002046 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002047 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002048 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002049 req->analysers = 0;
2050 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002051 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002052 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002053 if (srv)
2054 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002055 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002056 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002057 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002058 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002059 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002060 if (srv)
2061 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002062 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002063 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002064 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002065 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002066 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002067 if (srv)
2068 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002069 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002070 }
2071 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002072 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002073 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002074 if (srv)
2075 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002076 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002077 }
Willy Tarreau84455332009-03-15 22:34:05 +01002078 sess_set_term_flags(s);
2079 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002080 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002081 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002082 res->analysers = 0;
2083 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002084 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002085 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002086 if (srv)
2087 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002088 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002089 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002090 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002091 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002092 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002093 if (srv)
2094 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002095 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002096 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002097 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002098 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002099 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002100 if (srv)
2101 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002102 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002103 }
2104 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002105 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002106 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002107 if (srv)
2108 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002109 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002110 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002111 sess_set_term_flags(s);
2112 }
Willy Tarreau84455332009-03-15 22:34:05 +01002113 }
2114
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002115 /*
2116 * Here we take care of forwarding unhandled data. This also includes
2117 * connection establishments and shutdown requests.
2118 */
2119
2120
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002121 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002122 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002123 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002124 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002125 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002126 if (unlikely(!req->analysers &&
2127 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2128 (si_f->state >= SI_ST_EST) &&
2129 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002130 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002131 * attached to it. If any data are left in, we'll permit them to
2132 * move.
2133 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002134 channel_auto_read(req);
2135 channel_auto_connect(req);
2136 channel_auto_close(req);
2137 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002138
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002139 /* We'll let data flow between the producer (if still connected)
2140 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002141 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002142 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2143 channel_forward(req, CHN_INFINITE_FORWARD);
Willy Tarreau42529c32015-07-09 18:38:57 +02002144
2145 /* Just in order to support fetching HTTP contents after start
2146 * of forwarding when the HTTP forwarding analyser is not used,
2147 * we simply reset msg->sov so that HTTP rewinding points to the
2148 * headers.
2149 */
2150 if (s->txn)
2151 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002152 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002153
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002154 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002155 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2156 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002157 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002158 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2159 (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 +01002160 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002161 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2162 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002163 (req->flags & CF_STREAMER_FAST)))) {
2164 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002165 }
2166
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002167 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002168 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002169
2170 /*
2171 * Now forward all shutdown requests between both sides of the buffer
2172 */
2173
Willy Tarreau520d95e2009-09-19 21:04:57 +02002174 /* first, let's check if the request buffer needs to shutdown(write), which may
2175 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002176 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2177 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002178 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002179 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002180 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002181 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002182 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002183
2184 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002185 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2186 channel_is_empty(req))) {
2187 if (req->flags & CF_READ_ERROR)
2188 si_b->flags |= SI_FL_NOLINGER;
2189 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002190 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002191 res->rto = s->be->timeout.serverfin;
2192 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002193 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002194 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002195
2196 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002197 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2198 !req->analysers))
2199 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002200
2201 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002202 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2203 if (si_f->flags & SI_FL_NOHALF)
2204 si_f->flags |= SI_FL_NOLINGER;
2205 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002206 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002207
Willy Tarreau520d95e2009-09-19 21:04:57 +02002208 /* it's possible that an upper layer has requested a connection setup or abort.
2209 * There are 2 situations where we decide to establish a new connection :
2210 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002211 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002212 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002213 if (si_b->state == SI_ST_INI) {
2214 if (!(req->flags & CF_SHUTW)) {
2215 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002216 /* If we have an appctx, there is no connect method, so we
2217 * immediately switch to the connected state, otherwise we
2218 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002219 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002220 si_b->state = SI_ST_REQ; /* new connection requested */
2221 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002222 }
Willy Tarreau73201222009-08-16 18:27:24 +02002223 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002224 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002225 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2226 channel_shutw_now(req); /* fix buffer flags upon abort */
2227 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002228 }
Willy Tarreau92795622009-03-06 12:51:23 +01002229 }
2230
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002231
2232 /* we may have a pending connection request, or a connection waiting
2233 * for completion.
2234 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002235 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002236
2237 /* prune the request variables and swap to the response variables. */
2238 if (s->vars_reqres.scope != SCOPE_RES) {
2239 vars_prune(&s->vars_reqres, s);
2240 vars_init(&s->vars_reqres, SCOPE_RES);
2241 }
2242
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002243 do {
2244 /* nb: step 1 might switch from QUE to ASS, but we first want
2245 * to give a chance to step 2 to perform a redirect if needed.
2246 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002247 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002248 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002250 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002251
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002252 /* applets directly go to the ESTABLISHED state. Similarly,
2253 * servers experience the same fate when their connection
2254 * is reused.
2255 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002256 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002257 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002258
Willy Tarreaub44c8732013-12-31 23:16:50 +01002259 /* Now we can add the server name to a header (if requested) */
2260 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002261 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002262 (s->be->server_id_hdr_name != NULL) &&
2263 (s->be->mode == PR_MODE_HTTP) &&
2264 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002265 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002266 }
2267
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002268 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002269 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002270 http_perform_server_redirect(s, si_b);
2271 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002272 }
2273
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002274 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002275 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276 goto resync_stream_interface;
2277
Willy Tarreau815a9b22010-07-27 17:15:12 +02002278 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002279 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002280 goto resync_request;
2281
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002282 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002283
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002284 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002285 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002286 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002287 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002288 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002289 if (unlikely(!res->analysers &&
2290 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2291 (si_b->state >= SI_ST_EST) &&
2292 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002293 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002294 * attached to it. If any data are left in, we'll permit them to
2295 * move.
2296 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002297 channel_auto_read(res);
2298 channel_auto_close(res);
2299 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002300
2301 /* We'll let data flow between the producer (if still connected)
2302 * to the consumer.
2303 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002304 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
2305 channel_forward(res, CHN_INFINITE_FORWARD);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002306
Willy Tarreau42529c32015-07-09 18:38:57 +02002307 /* Just in order to support fetching HTTP contents after start
2308 * of forwarding when the HTTP forwarding analyser is not used,
2309 * we simply reset msg->sov so that HTTP rewinding points to the
2310 * headers.
2311 */
2312 if (s->txn)
2313 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2314
Willy Tarreauce887fd2012-05-12 12:50:00 +02002315 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002316 * tunnel timeout set, use it now. Note that we must respect
2317 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002318 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002319 if (!req->analysers && s->be->timeout.tunnel) {
2320 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002321 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002322
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002323 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2324 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002325 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2326 res->rto = s->be->timeout.serverfin;
2327 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2328 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002329 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2330 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002331
Willy Tarreau8f128b42014-11-28 15:07:47 +01002332 req->rex = tick_add(now_ms, req->rto);
2333 req->wex = tick_add(now_ms, req->wto);
2334 res->rex = tick_add(now_ms, res->rto);
2335 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002336 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002337 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002338
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002339 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2341 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002342 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002343 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2344 (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 +01002345 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002346 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2347 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002348 (res->flags & CF_STREAMER_FAST)))) {
2349 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002350 }
2351
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002352 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002353 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002354
2355 /*
2356 * Now forward all shutdown requests between both sides of the buffer
2357 */
2358
2359 /*
2360 * FIXME: this is probably where we should produce error responses.
2361 */
2362
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002363 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002364 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002365 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002366 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002367 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002368
2369 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002370 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2371 channel_is_empty(res))) {
2372 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002373 if (tick_isset(sess->fe->timeout.clientfin)) {
2374 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002375 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002376 }
2377 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002378
2379 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002380 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2381 !res->analysers)
2382 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002383
2384 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002385 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2386 if (si_b->flags & SI_FL_NOHALF)
2387 si_b->flags |= SI_FL_NOLINGER;
2388 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002389 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002390
Willy Tarreau8f128b42014-11-28 15:07:47 +01002391 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002392 goto resync_stream_interface;
2393
Willy Tarreau8f128b42014-11-28 15:07:47 +01002394 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002395 goto resync_request;
2396
Willy Tarreau8f128b42014-11-28 15:07:47 +01002397 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002398 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002399
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002400 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002401 si_f->flags &= ~SI_FL_DONT_WAKE;
2402 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002403
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002404 /* This is needed only when debugging is enabled, to indicate
2405 * client-side or server-side close. Please note that in the unlikely
2406 * event where both sides would close at once, the sequence is reported
2407 * on the server side first.
2408 */
2409 if (unlikely((global.mode & MODE_DEBUG) &&
2410 (!(global.mode & MODE_QUIET) ||
2411 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002412 if (si_b->state == SI_ST_CLO &&
2413 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002414 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002415 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002416 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2417 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002418 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002419 }
2420
Willy Tarreau8f128b42014-11-28 15:07:47 +01002421 if (si_f->state == SI_ST_CLO &&
2422 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002423 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002424 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002425 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2426 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002427 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002428 }
2429 }
2430
Willy Tarreau8f128b42014-11-28 15:07:47 +01002431 if (likely((si_f->state != SI_ST_CLO) ||
2432 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002433
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002434 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002435 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002436
Willy Tarreau563cc372015-04-19 18:13:56 +02002437 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002438 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002439
Willy Tarreau563cc372015-04-19 18:13:56 +02002440 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002441 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002442
Willy Tarreau8f128b42014-11-28 15:07:47 +01002443 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2444 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2445 si_f->prev_state = si_f->state;
2446 si_b->prev_state = si_b->state;
2447 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2448 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002449
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002450 /* Trick: if a request is being waiting for the server to respond,
2451 * and if we know the server can timeout, we don't want the timeout
2452 * to expire on the client side first, but we're still interested
2453 * in passing data from the client to the server (eg: POST). Thus,
2454 * we can cancel the client's request timeout if the server's
2455 * request timeout is set and the server has not yet sent a response.
2456 */
2457
Willy Tarreau8f128b42014-11-28 15:07:47 +01002458 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2459 (tick_isset(req->wex) || tick_isset(res->rex))) {
2460 req->flags |= CF_READ_NOEXP;
2461 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002462 }
2463
Willy Tarreau798f4322012-11-08 14:49:17 +01002464 update_exp_and_leave:
Willy Tarreau8f128b42014-11-28 15:07:47 +01002465 t->expire = tick_first(tick_first(req->rex, req->wex),
2466 tick_first(res->rex, res->wex));
2467 if (req->analysers)
2468 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002469
Willy Tarreau8f128b42014-11-28 15:07:47 +01002470 if (si_f->exp)
2471 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002472
Willy Tarreau8f128b42014-11-28 15:07:47 +01002473 if (si_b->exp)
2474 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002475
2476#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002477 fprintf(stderr,
2478 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2479 " 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 +01002480 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2481 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002482#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002483
2484#ifdef DEBUG_DEV
2485 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002486 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002487 ABORT_NOW();
2488#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002489 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002490 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002491 }
2492
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002493 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002494 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002495 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002496 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002497 if (sess->listener) {
2498 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002499 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002500 sess->listener->nbconn--;
2501 if (sess->listener->state == LI_FULL)
2502 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002503
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002504 /* Dequeues all of the listeners waiting for a resource */
2505 if (!LIST_ISEMPTY(&global_listener_queue))
2506 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002507
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002508 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2509 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2510 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002511 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002512
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002513 if (unlikely((global.mode & MODE_DEBUG) &&
2514 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002515 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002516 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002517 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2518 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002519 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002520 }
2521
2522 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002523 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002524
Willy Tarreaueee5b512015-04-03 23:46:31 +02002525 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002526 int n;
2527
Willy Tarreaueee5b512015-04-03 23:46:31 +02002528 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002529 if (n < 1 || n > 5)
2530 n = 0;
2531
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002532 if (sess->fe->mode == PR_MODE_HTTP) {
2533 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002534 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002535 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002536 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002537 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002538 s->be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002539 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002540 }
2541
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002542 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002543 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002544 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002545 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002546 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002547 }
2548
Willy Tarreau87b09662015-04-03 00:22:06 +02002549 /* update time stats for this stream */
2550 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002551
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002552 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002553 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002554 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002555 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002556 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002557}
2558
Willy Tarreau87b09662015-04-03 00:22:06 +02002559/* Update the stream's backend and server time stats */
2560void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002561{
2562 int t_request;
2563 int t_queue;
2564 int t_connect;
2565 int t_data;
2566 int t_close;
2567 struct server *srv;
2568
2569 t_request = 0;
2570 t_queue = s->logs.t_queue;
2571 t_connect = s->logs.t_connect;
2572 t_close = s->logs.t_close;
2573 t_data = s->logs.t_data;
2574
2575 if (s->be->mode != PR_MODE_HTTP)
2576 t_data = t_connect;
2577
2578 if (t_connect < 0 || t_data < 0)
2579 return;
2580
2581 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2582 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2583
2584 t_data -= t_connect;
2585 t_connect -= t_queue;
2586 t_queue -= t_request;
2587
2588 srv = objt_server(s->target);
2589 if (srv) {
2590 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2591 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2592 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2593 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2594 }
2595 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2596 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2597 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2598 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2599}
2600
Willy Tarreau7c669d72008-06-20 15:04:11 +02002601/*
2602 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002603 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002604 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002605 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002606 * server.
2607 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002608void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002609{
2610 if (sess->srv_conn == newsrv)
2611 return;
2612
2613 if (sess->srv_conn) {
2614 sess->srv_conn->served--;
2615 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2616 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002617 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002618 }
2619
2620 if (newsrv) {
2621 newsrv->served++;
2622 if (newsrv->proxy->lbprm.server_take_conn)
2623 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002624 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002625 }
2626}
2627
Willy Tarreau84455332009-03-15 22:34:05 +01002628/* Handle server-side errors for default protocols. It is called whenever a a
2629 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002630 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002631 * them. It's installed as ->srv_error for the server-side stream_interface.
2632 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002633void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002634{
2635 int err_type = si->err_type;
2636 int err = 0, fin = 0;
2637
2638 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002639 err = SF_ERR_CLICL;
2640 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002641 }
2642 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002643 err = SF_ERR_CLICL;
2644 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002645 }
2646 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002647 err = SF_ERR_SRVTO;
2648 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002649 }
2650 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002651 err = SF_ERR_SRVCL;
2652 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002653 }
2654 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002655 err = SF_ERR_SRVTO;
2656 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002657 }
2658 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002659 err = SF_ERR_SRVCL;
2660 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002661 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002662 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002663 err = SF_ERR_RESOURCE;
2664 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002665 }
Willy Tarreau84455332009-03-15 22:34:05 +01002666 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002667 err = SF_ERR_INTERNAL;
2668 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002669 }
2670
Willy Tarreaue7dff022015-04-03 01:14:29 +02002671 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002672 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002673 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002674 s->flags |= fin;
2675}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002676
Willy Tarreaue7dff022015-04-03 01:14:29 +02002677/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002678void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002679{
Willy Tarreau87b09662015-04-03 00:22:06 +02002680 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002681 return;
2682
Willy Tarreau87b09662015-04-03 00:22:06 +02002683 channel_shutw_now(&stream->req);
2684 channel_shutr_now(&stream->res);
2685 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002686 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002687 stream->flags |= why;
2688 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002689}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002690
Willy Tarreau8b22a712010-06-18 17:46:06 +02002691/************************************************************************/
2692/* All supported ACL keywords must be declared here. */
2693/************************************************************************/
2694
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002695/* Returns a pointer to a stkctr depending on the fetch keyword name.
2696 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002697 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002698 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002699 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002700 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002701 * NULL may be returned if the designated stkctr is not tracked. For
2702 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2703 * passed. When present, the currently tracked key is then looked up
2704 * in the specified table instead of the current table. The purpose is
2705 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002706 * multiple tables). <strm> is allowed to be NULL, in which case only
2707 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002708 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002709struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002710smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002711{
2712 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002713 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002714 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002715 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002716 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002717
Willy Tarreau0f791d42013-07-23 19:56:43 +02002718 if (num == '_' - '0') {
2719 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002720 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002721 if (num >= MAX_SESS_STKCTR)
2722 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002723 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002724 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002725 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002726 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002727 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002728
2729 if (!conn)
2730 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002731
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002732 /* Fetch source adress in a sample. */
2733 smp.px = NULL;
2734 smp.sess = sess;
2735 smp.strm = strm;
2736 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2737 return NULL;
2738
2739 /* Converts into key. */
2740 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002741 if (!key)
2742 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002743
Willy Tarreaua65536c2013-07-22 22:40:11 +02002744 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002745 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002746 return &stkctr;
2747 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002748
2749 /* Here, <num> contains the counter number from 0 to 9 for
2750 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002751 * args[arg] is the first optional argument. We first lookup the
2752 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002753 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002754
2755 stkptr = &strm->stkctr[num];
2756 if (!strm || !stkctr_entry(stkptr)) {
2757 stkptr = &sess->stkctr[num];
2758 if (!stkctr_entry(stkptr))
2759 return NULL;
2760 }
2761
2762 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002763 if (!stksess)
2764 return NULL;
2765
Willy Tarreau0f791d42013-07-23 19:56:43 +02002766 if (unlikely(args[arg].type == ARGT_TAB)) {
2767 /* an alternate table was specified, let's look up the same key there */
2768 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002769 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002770 return &stkctr;
2771 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002772 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002773}
2774
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002775/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2776 * the entry if it doesn't exist yet. This is needed for a few fetch
2777 * functions which need to create an entry, such as src_inc_gpc* and
2778 * src_clr_gpc*.
2779 */
2780struct stkctr *
2781smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2782{
2783 static struct stkctr stkctr;
2784 struct stktable_key *key;
2785 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002786 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002787
2788 if (strncmp(kw, "src_", 4) != 0)
2789 return NULL;
2790
2791 if (!conn)
2792 return NULL;
2793
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002794 /* Fetch source adress in a sample. */
2795 smp.px = NULL;
2796 smp.sess = sess;
2797 smp.strm = strm;
2798 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2799 return NULL;
2800
2801 /* Converts into key. */
2802 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002803 if (!key)
2804 return NULL;
2805
2806 stkctr.table = &args->data.prx->table;
2807 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2808 return &stkctr;
2809}
2810
Willy Tarreau87b09662015-04-03 00:22:06 +02002811/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002812 * currently being tracked or not.
2813 * Supports being called as "sc[0-9]_tracked" only.
2814 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002815static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002816smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002817{
2818 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002819 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002820 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002821 return 1;
2822}
2823
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002824/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2825 * frontend counters or from the src.
2826 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2827 * zero is returned if the key is new.
2828 */
2829static int
2830smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2831{
2832 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
2833
2834 if (!stkctr)
2835 return 0;
2836
2837 smp->flags = SMP_F_VOL_TEST;
2838 smp->data.type = SMP_T_SINT;
2839 smp->data.u.sint = 0;
2840
2841 if (stkctr_entry(stkctr) != NULL) {
2842 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2843 if (!ptr)
2844 return 0; /* parameter not stored */
2845 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2846 }
2847 return 1;
2848}
2849
Willy Tarreau87b09662015-04-03 00:22:06 +02002850/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002851 * frontend counters or from the src.
2852 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2853 * zero is returned if the key is new.
2854 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002855static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002856smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002857{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002858 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002859
2860 if (!stkctr)
2861 return 0;
2862
Willy Tarreau37406352012-04-23 16:16:37 +02002863 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002864 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002865 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002866
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002867 if (stkctr_entry(stkctr) != NULL) {
2868 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002869 if (!ptr)
2870 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002871 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002872 }
2873 return 1;
2874}
2875
Willy Tarreau87b09662015-04-03 00:22:06 +02002876/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002877 * tracked frontend counters or from the src.
2878 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2879 * Value zero is returned if the key is new.
2880 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002881static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002882smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002883{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002884 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002885
2886 if (!stkctr)
2887 return 0;
2888
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002889 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002890 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002891 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002892 if (stkctr_entry(stkctr) != NULL) {
2893 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002894 if (!ptr)
2895 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002896 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002897 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002898 }
2899 return 1;
2900}
2901
Willy Tarreau87b09662015-04-03 00:22:06 +02002902/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002903 * frontend counters and return it into temp integer.
2904 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002905 */
2906static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002907smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002908{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002909 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002910
2911 if (!stkctr)
2912 return 0;
2913
Willy Tarreau37406352012-04-23 16:16:37 +02002914 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002915 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002916 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002917
2918 if (stkctr_entry(stkctr) == NULL)
2919 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2920
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002921 if (stkctr_entry(stkctr) != NULL) {
Emeric Brun57056f02015-06-15 18:29:57 +02002922 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002923
2924 /* First, update gpc0_rate if it's tracked. Second, update its
2925 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2926 */
Emeric Brun57056f02015-06-15 18:29:57 +02002927 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2928 if (ptr1) {
2929 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002930 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002931 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002932 }
2933
Emeric Brun57056f02015-06-15 18:29:57 +02002934 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2935 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002936 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002937
Emeric Brun57056f02015-06-15 18:29:57 +02002938 /* If data was modified, we need to touch to re-schedule sync */
2939 if (ptr1 || ptr2)
2940 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002941 }
2942 return 1;
2943}
2944
Willy Tarreau87b09662015-04-03 00:22:06 +02002945/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002946 * frontend counters and return its previous value into temp integer.
2947 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002948 */
2949static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002950smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002951{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002952 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002953
2954 if (!stkctr)
2955 return 0;
2956
Willy Tarreau37406352012-04-23 16:16:37 +02002957 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002958 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002959 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002960
2961 if (stkctr_entry(stkctr) == NULL)
2962 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2963
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002964 if (stkctr_entry(stkctr) != NULL) {
2965 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002966 if (!ptr)
2967 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002968 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002969 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002970 /* If data was modified, we need to touch to re-schedule sync */
2971 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002972 }
2973 return 1;
2974}
2975
Willy Tarreau87b09662015-04-03 00:22:06 +02002976/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002977 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2978 * "src_conn_cnt" only.
2979 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002980static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002981smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002982{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002983 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002984
2985 if (!stkctr)
2986 return 0;
2987
Willy Tarreau37406352012-04-23 16:16:37 +02002988 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002989 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002990 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002991 if (stkctr_entry(stkctr) != NULL) {
2992 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002993 if (!ptr)
2994 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002995 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002996 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002997 return 1;
2998}
2999
Willy Tarreau87b09662015-04-03 00:22:06 +02003000/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02003001 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
3002 * only.
3003 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003004static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003005smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003006{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003007 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02003008
3009 if (!stkctr)
3010 return 0;
3011
Willy Tarreau37406352012-04-23 16:16:37 +02003012 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003013 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003014 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003015 if (stkctr_entry(stkctr) != NULL) {
3016 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003017 if (!ptr)
3018 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003019 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02003020 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003021 }
3022 return 1;
3023}
3024
Willy Tarreau87b09662015-04-03 00:22:06 +02003025/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02003026 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003027 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02003028 */
3029static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003030smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003031{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003032 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003033 struct stksess *ts;
3034 struct stktable_key *key;
3035 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003036 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003037
Willy Tarreaub363a1f2013-10-01 10:45:07 +02003038 if (!conn)
3039 return 0;
3040
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02003041 /* Fetch source adress in a sample. */
3042 if (!smp_fetch_src(NULL, smp, NULL, NULL))
3043 return 0;
3044
3045 /* Converts into key. */
3046 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003047 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01003048 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003049
Willy Tarreau24e32d82012-04-23 23:55:44 +02003050 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003051
Willy Tarreau1f7e9252010-06-20 12:27:21 +02003052 if ((ts = stktable_update_key(&px->table, key)) == NULL)
3053 /* entry does not exist and could not be created */
3054 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003055
3056 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
3057 if (!ptr)
3058 return 0; /* parameter not stored in this table */
3059
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003060 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003061 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02003062 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02003063 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02003064 return 1;
3065}
3066
Willy Tarreau87b09662015-04-03 00:22:06 +02003067/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02003068 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
3069 * "src_conn_cur" only.
3070 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003071static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003072smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003073{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003074 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003075
3076 if (!stkctr)
3077 return 0;
3078
Willy Tarreau37406352012-04-23 16:16:37 +02003079 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003080 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003081 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003082 if (stkctr_entry(stkctr) != NULL) {
3083 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003084 if (!ptr)
3085 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003086 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003087 }
3088 return 1;
3089}
3090
Willy Tarreau87b09662015-04-03 00:22:06 +02003091/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003092 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3093 * "src_sess_cnt" only.
3094 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003095static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003096smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003097{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003098 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003099
3100 if (!stkctr)
3101 return 0;
3102
Willy Tarreau37406352012-04-23 16:16:37 +02003103 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003104 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003105 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003106 if (stkctr_entry(stkctr) != NULL) {
3107 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003108 if (!ptr)
3109 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003110 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003111 }
3112 return 1;
3113}
3114
Willy Tarreau87b09662015-04-03 00:22:06 +02003115/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003116 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3117 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003118static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003119smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003120{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003121 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003122
3123 if (!stkctr)
3124 return 0;
3125
Willy Tarreau37406352012-04-23 16:16:37 +02003126 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003127 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003128 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003129 if (stkctr_entry(stkctr) != NULL) {
3130 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003131 if (!ptr)
3132 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003133 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003134 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003135 }
3136 return 1;
3137}
3138
Willy Tarreau87b09662015-04-03 00:22:06 +02003139/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003140 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3141 * "src_http_req_cnt" only.
3142 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003143static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003144smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003145{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003146 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003147
3148 if (!stkctr)
3149 return 0;
3150
Willy Tarreau37406352012-04-23 16:16:37 +02003151 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003152 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003153 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003154 if (stkctr_entry(stkctr) != NULL) {
3155 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003156 if (!ptr)
3157 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003158 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003159 }
3160 return 1;
3161}
3162
Willy Tarreau87b09662015-04-03 00:22:06 +02003163/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003164 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3165 * "src_http_req_rate" only.
3166 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003167static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003168smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003169{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003170 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003171
3172 if (!stkctr)
3173 return 0;
3174
Willy Tarreau37406352012-04-23 16:16:37 +02003175 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003176 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003177 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003178 if (stkctr_entry(stkctr) != NULL) {
3179 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003180 if (!ptr)
3181 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003182 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003183 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003184 }
3185 return 1;
3186}
3187
Willy Tarreau87b09662015-04-03 00:22:06 +02003188/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003189 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3190 * "src_http_err_cnt" only.
3191 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003192static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003193smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003194{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003195 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003196
3197 if (!stkctr)
3198 return 0;
3199
Willy Tarreau37406352012-04-23 16:16:37 +02003200 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003201 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003202 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003203 if (stkctr_entry(stkctr) != NULL) {
3204 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003205 if (!ptr)
3206 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003207 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003208 }
3209 return 1;
3210}
3211
Willy Tarreau87b09662015-04-03 00:22:06 +02003212/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003213 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3214 * "src_http_err_rate" only.
3215 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003216static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003217smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003218{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003219 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003220
3221 if (!stkctr)
3222 return 0;
3223
Willy Tarreau37406352012-04-23 16:16:37 +02003224 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003225 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003226 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003227 if (stkctr_entry(stkctr) != NULL) {
3228 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003229 if (!ptr)
3230 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003231 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003232 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003233 }
3234 return 1;
3235}
3236
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003237/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003238 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003239 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3240 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003241static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003242smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003243{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003244 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003245
3246 if (!stkctr)
3247 return 0;
3248
Willy Tarreau37406352012-04-23 16:16:37 +02003249 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003250 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003251 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003252 if (stkctr_entry(stkctr) != NULL) {
3253 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003254 if (!ptr)
3255 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003256 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003257 }
3258 return 1;
3259}
3260
Willy Tarreau613fe992013-07-23 17:39:19 +02003261/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003262 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003263 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003264 */
3265static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003266smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003267{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003268 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003269
3270 if (!stkctr)
3271 return 0;
3272
Willy Tarreau37406352012-04-23 16:16:37 +02003273 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003274 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003275 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003276 if (stkctr_entry(stkctr) != NULL) {
3277 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003278 if (!ptr)
3279 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003280 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003281 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003282 }
3283 return 1;
3284}
3285
Willy Tarreau53aea102013-07-23 17:39:02 +02003286/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003287 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003288 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3289 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003290static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003291smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003292{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003293 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003294
3295 if (!stkctr)
3296 return 0;
3297
Willy Tarreau37406352012-04-23 16:16:37 +02003298 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003299 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003300 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003301 if (stkctr_entry(stkctr) != NULL) {
3302 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003303 if (!ptr)
3304 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003305 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003306 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003307 return 1;
3308}
3309
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003310/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003311 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003312 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003313 */
3314static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003315smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003316{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003317 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003318
3319 if (!stkctr)
3320 return 0;
3321
Willy Tarreau37406352012-04-23 16:16:37 +02003322 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003323 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003324 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003325 if (stkctr_entry(stkctr) != NULL) {
3326 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003327 if (!ptr)
3328 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003329 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003330 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003331 }
3332 return 1;
3333}
3334
Willy Tarreau87b09662015-04-03 00:22:06 +02003335/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003336 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3337 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003338static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003339smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003340{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003341 struct stkctr *stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau2406db42012-12-09 12:16:43 +01003342
Willy Tarreau563eef42013-07-23 18:32:02 +02003343 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003344 return 0;
3345
Willy Tarreau563eef42013-07-23 18:32:02 +02003346 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003347 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003348 smp->data.u.sint = stkctr_entry(stkctr)->ref_cnt;
Willy Tarreau563eef42013-07-23 18:32:02 +02003349 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003350}
3351
Willy Tarreau34db1082012-04-19 17:16:54 +02003352/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003353 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003354 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003355static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003356smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003357{
Willy Tarreau37406352012-04-23 16:16:37 +02003358 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003359 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003360 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003361 return 1;
3362}
3363
Willy Tarreau34db1082012-04-19 17:16:54 +02003364/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003365 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003366 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003367static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003368smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003369{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003370 struct proxy *px;
3371
Willy Tarreau24e32d82012-04-23 23:55:44 +02003372 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003373 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003374 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003375 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003376 return 1;
3377}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003378
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003379/* 0=OK, <0=Alert, >0=Warning */
3380static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3381 struct proxy *px, struct act_rule *rule,
3382 char **err)
3383{
3384 struct action_kw *kw;
3385
3386 /* Check if the service name exists. */
3387 if (*(args[*cur_arg]) == 0) {
3388 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003389 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003390 }
3391
3392 /* lookup for keyword corresponding to a service. */
3393 kw = action_lookup(&service_keywords, args[*cur_arg]);
3394 if (!kw) {
3395 memprintf(err, "'%s' unknown service name.", args[1]);
3396 return ACT_RET_PRS_ERR;
3397 }
3398 (*cur_arg)++;
3399
3400 /* executes specific rule parser. */
3401 rule->kw = kw;
3402 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3403 return ACT_RET_PRS_ERR;
3404
3405 /* Register processing function. */
3406 rule->action_ptr = process_use_service;
3407 rule->action = ACT_CUSTOM;
3408
3409 return ACT_RET_PRS_OK;
3410}
3411
3412void service_keywords_register(struct action_kw_list *kw_list)
3413{
3414 LIST_ADDQ(&service_keywords, &kw_list->list);
3415}
3416
3417/* main configuration keyword registration. */
3418static struct action_kw_list stream_tcp_keywords = { ILH, {
3419 { "use-service", stream_parse_use_service },
3420 { /* END */ }
3421}};
3422
3423static struct action_kw_list stream_http_keywords = { ILH, {
3424 { "use-service", stream_parse_use_service },
3425 { /* END */ }
3426}};
3427
Willy Tarreau61612d42012-04-19 18:42:05 +02003428/* Note: must not be declared <const> as its list will be overwritten.
3429 * Please take care of keeping this list alphabetically sorted.
3430 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003431static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003432 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003433}};
3434
Willy Tarreau281c7992013-01-08 01:23:27 +01003435/* Note: must not be declared <const> as its list will be overwritten.
3436 * Please take care of keeping this list alphabetically sorted.
3437 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003438static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003439 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3440 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3441 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3442 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3443 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3444 { "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 +02003445 { "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 +02003446 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3447 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3448 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3449 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3450 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3451 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3452 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3453 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3454 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3455 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3456 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3457 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3458 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003459 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3460 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3461 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3462 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3463 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3464 { "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 +02003465 { "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 +02003466 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3467 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3468 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3469 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3470 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3471 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3472 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3473 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3474 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3475 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3476 { "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 +02003477 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003478 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3479 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3480 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3481 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3482 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3483 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3484 { "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 +02003485 { "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 +02003486 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3487 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3488 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3489 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3490 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3491 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3492 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3493 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3494 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3495 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3496 { "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 +02003497 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003498 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3499 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3500 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3501 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3502 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3503 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3504 { "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 +02003505 { "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 +02003506 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3507 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3508 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3509 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3510 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3511 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3512 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3513 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3514 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3515 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3516 { "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 +02003517 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003518 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3519 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3520 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3521 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3522 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3523 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3524 { "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 +02003525 { "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 +02003526 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3527 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3528 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3529 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3530 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3531 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3532 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3533 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3534 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3535 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3536 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3537 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3538 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3539 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003540 { /* END */ },
3541}};
3542
Willy Tarreau8b22a712010-06-18 17:46:06 +02003543__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003544static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003545{
Willy Tarreau281c7992013-01-08 01:23:27 +01003546 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003547 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003548 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3549 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003550}
3551
Willy Tarreaubaaee002006-06-26 02:48:02 +02003552/*
3553 * Local variables:
3554 * c-indent-level: 8
3555 * c-basic-offset: 8
3556 * End:
3557 */