blob: 346e7b61b4e24072ad531d88145cb95b1a9dec0a [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>
William Lallemand9ed62032016-11-21 17:49:11 +010027#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020029#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020030#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020031#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010032#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020033#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010034#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020035#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010036#include <proto/stats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020037#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020038#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020039#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020040#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010041#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010042#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020043#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020044#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020045#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020046#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020047#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010048#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010049#include <proto/proto_http.h>
50#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020051#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020052#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010053#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020054#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010055#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010056#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010057#include <proto/task.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020058#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020059
Willy Tarreau87b09662015-04-03 00:22:06 +020060struct pool_head *pool2_stream;
61struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020062
Willy Tarreau87b09662015-04-03 00:22:06 +020063/* list of streams waiting for at least one buffer */
Willy Tarreaubf883e02014-11-25 21:10:35 +010064struct list buffer_wq = LIST_HEAD_INIT(buffer_wq);
65
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020066/* List of all use-service keywords. */
67static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
68
Willy Tarreau9903f0e2015-04-04 18:50:31 +020069/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020070 * handshake, in order to complete initialization of a valid stream. It must be
71 * called with a session (which may be embryonic). It returns the pointer to
72 * the newly created stream, or NULL in case of fatal error. The client-facing
73 * end point is assigned to <origin>, which must be valid. The task's context
74 * is set to the new stream, and its function is set to process_stream().
75 * Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020076 */
Willy Tarreau73b65ac2015-04-08 18:26:29 +020077struct stream *stream_new(struct session *sess, struct task *t, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020078{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020079 struct stream *s;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020080 struct connection *conn = objt_conn(origin);
81 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020082
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020083 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau02d86382015-04-05 12:00:52 +020084 return s;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020085
86 /* minimum stream initialization required for an embryonic stream is
87 * fairly low. We need very little to execute L4 ACLs, then we need a
88 * task to make the client-side connection live on its own.
89 * - flags
90 * - stick-entry tracking
91 */
92 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020093 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020094 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020095 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
96 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +020097 /* This function is called just after the handshake, so the handshake duration is
98 * between the accept time and now.
99 */
100 s->logs.t_handshake = tv_ms_elapsed(&sess->tv_accept, &now);
101 s->logs.t_idle = -1;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200102 tv_zero(&s->logs.tv_request);
103 s->logs.t_queue = -1;
104 s->logs.t_connect = -1;
105 s->logs.t_data = -1;
106 s->logs.t_close = 0;
107 s->logs.bytes_in = s->logs.bytes_out = 0;
108 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
109 s->logs.srv_queue_size = 0; /* we will get this number soon */
110
111 /* default logging function */
112 s->do_log = strm_log;
113
114 /* default error reporting function, may be changed by analysers */
115 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200116
117 /* Initialise the current rule list pointer to NULL. We are sure that
118 * any rulelist match the NULL pointer.
119 */
120 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200121 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200122
Willy Tarreaua68f7622015-09-21 17:48:24 +0200123 /* Copy SC counters for the stream. We don't touch refcounts because
124 * any reference we have is inherited from the session. Since the stream
125 * doesn't exist without the session, the session's existence guarantees
126 * we don't lose the entry. During the store operation, the stream won't
127 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200128 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200129 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200130
131 s->sess = sess;
132 s->si[0].flags = SI_FL_NONE;
133 s->si[1].flags = SI_FL_ISBACK;
134
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200135 s->uniq_id = global.req_count++;
136
Willy Tarreau87b09662015-04-03 00:22:06 +0200137 /* OK, we're keeping the stream, so let's properly initialize the stream */
138 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2542b532012-08-31 16:01:23 +0200139 LIST_INIT(&s->back_refs);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100140 LIST_INIT(&s->buffer_wait);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200141
Willy Tarreaue7dff022015-04-03 01:14:29 +0200142 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200143 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200144
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200145 s->task = t;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200146 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200147 t->context = s;
148 t->expire = TICK_ETERNITY;
149
Willy Tarreau87b09662015-04-03 00:22:06 +0200150 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200151 * This changes later when switching rules are executed or
152 * when the default backend is assigned.
153 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200154 s->be = sess->fe;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100155 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200156 s->req_cap = NULL;
157 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200158
Willy Tarreauebcd4842015-06-19 11:59:02 +0200159 /* Initialise all the variables contexts even if not used.
160 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200161 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200162 vars_init(&s->vars_txn, SCOPE_TXN);
163 vars_init(&s->vars_reqres, SCOPE_REQ);
164
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200165 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100166 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200167 si_set_state(&s->si[0], SI_ST_EST);
168
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200169 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200170 if (conn)
171 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200172 else if (appctx)
173 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200174
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200175 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200176 s->si[0].flags |= SI_FL_INDEP_STR;
177
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200178 /* pre-initialize the other side's stream interface to an INIT state. The
179 * callbacks will be initialized before attempting to connect.
180 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100181 si_reset(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200182
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200183 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200184 s->si[1].flags |= SI_FL_INDEP_STR;
185
Willy Tarreau87b09662015-04-03 00:22:06 +0200186 stream_init_srv_conn(s);
Willy Tarreaud1769b82015-04-06 00:25:48 +0200187 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200188 s->pend_pos = NULL;
189
190 /* init store persistence */
191 s->store_count = 0;
192
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100193 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100194 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreaud1769b82015-04-06 00:25:48 +0200195 s->req.analysers = 0;
196 channel_auto_connect(&s->req); /* don't wait to establish connection */
197 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200198
199 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100200 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100201 s->req.rex = TICK_ETERNITY;
202 s->req.wex = TICK_ETERNITY;
203 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200204
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100205 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100206 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100207 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200208
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200209 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100210 s->req.flags |= CF_NEVER_WAIT;
211 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200212 }
213
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200214 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100215 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100216 s->res.rex = TICK_ETERNITY;
217 s->res.wex = TICK_ETERNITY;
218 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200219
Willy Tarreaueee5b512015-04-03 23:46:31 +0200220 s->txn = NULL;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100221
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100222 HLUA_INIT(&s->hlua);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100223
Christopher Faulet92d36382015-11-05 13:35:03 +0100224 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200225 goto out_fail_accept;
226
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200227 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200228 if (conn)
229 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200230 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200231 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200232
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200233 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200234 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200235
236 /* it is important not to call the wakeup function directly but to
237 * pass through task_wakeup(), because this one knows how to apply
238 * priorities to tasks.
239 */
240 task_wakeup(t, TASK_WOKEN_INIT);
Willy Tarreau02d86382015-04-05 12:00:52 +0200241 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200242
243 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200244 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100245 flt_stream_release(s, 0);
Willy Tarreau3b246412014-11-25 17:10:33 +0100246 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200247 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200248 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200249}
250
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200252 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200253 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200254static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200255{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200256 struct session *sess = strm_sess(s);
257 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100258 struct bref *bref, *back;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200259 struct connection *cli_conn = objt_conn(sess->origin);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200260 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100261
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262 if (s->pend_pos)
263 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100264
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100265 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200266 if (s->flags & SF_CURR_SESS) {
267 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100268 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100269 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100270 if (may_dequeue_tasks(objt_server(s->target), s->be))
271 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100272 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100273
Willy Tarreau7c669d72008-06-20 15:04:11 +0200274 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200275 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200276 * it should normally be only the same as the one above,
277 * so this should not happen in fact.
278 */
279 sess_change_server(s, NULL);
280 }
281
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100282 if (s->req.pipe)
283 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100284
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100285 if (s->res.pipe)
286 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100287
Willy Tarreaubf883e02014-11-25 21:10:35 +0100288 /* We may still be present in the buffer wait queue */
289 if (!LIST_ISEMPTY(&s->buffer_wait)) {
290 LIST_DEL(&s->buffer_wait);
291 LIST_INIT(&s->buffer_wait);
292 }
293
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100294 b_drop(&s->req.buf);
295 b_drop(&s->res.buf);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100296 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200297 stream_offer_buffers();
Willy Tarreau9b28e032012-10-12 23:49:43 +0200298
Thierry FOURNIERa718b292015-03-04 16:48:34 +0100299 hlua_ctx_destroy(&s->hlua);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200300 if (s->txn)
301 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100302
Willy Tarreau1e954912012-10-12 17:50:05 +0200303 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200304 if (cli_conn)
305 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200306
Willy Tarreaua4cda672010-06-06 18:28:49 +0200307 for (i = 0; i < s->store_count; i++) {
308 if (!s->store[i].ts)
309 continue;
310 stksess_free(s->store[i].table, s->store[i].ts);
311 s->store[i].ts = NULL;
312 }
313
Willy Tarreaueee5b512015-04-03 23:46:31 +0200314 if (s->txn) {
315 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
316 pool_free2(pool2_http_txn, s->txn);
317 s->txn = NULL;
318 }
319
Christopher Fauletd7c91962015-04-30 11:48:27 +0200320 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100321 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200322
Willy Tarreau92fb9832007-10-16 17:34:28 +0200323 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200324 pool_free2(fe->rsp_cap_pool, s->res_cap);
325 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200326 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100327
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200328 /* Cleanup all variable contexts. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100329 vars_prune(&s->vars_txn, s->sess, s);
330 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200331
Willy Tarreau87b09662015-04-03 00:22:06 +0200332 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200333
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100334 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100335 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200336 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100337 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100338 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100339 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200340 if (s->list.n != &streams)
341 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100342 bref->ref = s->list.n;
343 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100344 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200345 si_release_endpoint(&s->si[1]);
346 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200347
348 /* FIXME: for now we have a 1:1 relation between stream and session so
349 * the stream must free the session.
350 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200351 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200352 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200353
354 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200355 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200356 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200357 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200358 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200359 pool_flush2(pool2_requri);
360 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200361 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200362 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100363 pool_flush2(pool2_connection);
364 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200365 pool_flush2(fe->req_cap_pool);
366 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200367 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200368}
369
Willy Tarreau78955f42014-12-28 13:09:02 +0100370/* Allocates a receive buffer for channel <chn>, but only if it's guaranteed
371 * that it's not the last available buffer or it's the response buffer. Unless
372 * the buffer is the response buffer, an extra control is made so that we always
373 * keep <tune.buffers.reserved> buffers available after this allocation. To be
374 * called at the beginning of recv() callbacks to ensure that the required
375 * buffers are properly allocated. Returns 0 in case of failure, non-zero
376 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100377 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200378int stream_alloc_recv_buffer(struct channel *chn)
Willy Tarreau656859d2014-11-25 19:46:36 +0100379{
Willy Tarreau87b09662015-04-03 00:22:06 +0200380 struct stream *s;
Willy Tarreau656859d2014-11-25 19:46:36 +0100381 struct buffer *b;
Willy Tarreaua24adf02014-11-27 01:11:56 +0100382 int margin = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100383
Willy Tarreau78955f42014-12-28 13:09:02 +0100384 if (!(chn->flags & CF_ISRESP))
Willy Tarreaua24adf02014-11-27 01:11:56 +0100385 margin = global.tune.reserved_bufs;
386
Thierry FOURNIER27929fb2015-09-25 08:36:11 +0200387 s = chn_strm(chn);
Willy Tarreau78955f42014-12-28 13:09:02 +0100388
389 b = b_alloc_margin(&chn->buf, margin);
Willy Tarreau656859d2014-11-25 19:46:36 +0100390 if (b)
391 return 1;
392
Willy Tarreaubf883e02014-11-25 21:10:35 +0100393 if (LIST_ISEMPTY(&s->buffer_wait))
394 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100395 return 0;
396}
397
Willy Tarreau87b09662015-04-03 00:22:06 +0200398/* Allocates a work buffer for stream <s>. It is meant to be called inside
399 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200400 * to work fine, which is the response buffer so that an error message may be
401 * built and returned. Response buffers may be allocated from the reserve, this
402 * is critical to ensure that a response may always flow and will never block a
403 * server from releasing a connection. Returns 0 in case of failure, non-zero
404 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100405 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200406int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100407{
Willy Tarreaubf883e02014-11-25 21:10:35 +0100408 if (!LIST_ISEMPTY(&s->buffer_wait)) {
409 LIST_DEL(&s->buffer_wait);
410 LIST_INIT(&s->buffer_wait);
411 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100412
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200413 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100414 return 1;
415
Willy Tarreaubf883e02014-11-25 21:10:35 +0100416 LIST_ADDQ(&buffer_wq, &s->buffer_wait);
Willy Tarreau656859d2014-11-25 19:46:36 +0100417 return 0;
418}
419
420/* releases unused buffers after processing. Typically used at the end of the
Willy Tarreaubf883e02014-11-25 21:10:35 +0100421 * update() functions. It will try to wake up as many tasks as the number of
Willy Tarreau87b09662015-04-03 00:22:06 +0200422 * buffers that it releases. In practice, most often streams are blocked on
Willy Tarreaubf883e02014-11-25 21:10:35 +0100423 * a single buffer, so it makes sense to try to wake two up when two buffers
424 * are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100425 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200426void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100427{
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100428 if (s->req.buf->size && buffer_empty(s->req.buf))
429 b_free(&s->req.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100430
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100431 if (s->res.buf->size && buffer_empty(s->res.buf))
432 b_free(&s->res.buf);
Willy Tarreau656859d2014-11-25 19:46:36 +0100433
Willy Tarreaubf883e02014-11-25 21:10:35 +0100434 /* if we're certain to have at least 1 buffer available, and there is
435 * someone waiting, we can wake up a waiter and offer them.
436 */
Willy Tarreaua24adf02014-11-27 01:11:56 +0100437 if (!LIST_ISEMPTY(&buffer_wq))
Willy Tarreau87b09662015-04-03 00:22:06 +0200438 stream_offer_buffers();
Willy Tarreaubf883e02014-11-25 21:10:35 +0100439}
440
Willy Tarreau87b09662015-04-03 00:22:06 +0200441/* Runs across the list of pending streams waiting for a buffer and wakes one
Willy Tarreaua24adf02014-11-27 01:11:56 +0100442 * up if buffers are available. Will stop when the run queue reaches <rqlimit>.
Willy Tarreau87b09662015-04-03 00:22:06 +0200443 * Should not be called directly, use stream_offer_buffers() instead.
Willy Tarreaubf883e02014-11-25 21:10:35 +0100444 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200445void __stream_offer_buffers(int rqlimit)
Willy Tarreaubf883e02014-11-25 21:10:35 +0100446{
Willy Tarreau87b09662015-04-03 00:22:06 +0200447 struct stream *sess, *bak;
Willy Tarreaubf883e02014-11-25 21:10:35 +0100448
449 list_for_each_entry_safe(sess, bak, &buffer_wq, buffer_wait) {
Willy Tarreaua24adf02014-11-27 01:11:56 +0100450 if (rqlimit <= run_queue)
451 break;
452
Willy Tarreaubf883e02014-11-25 21:10:35 +0100453 if (sess->task->state & TASK_RUNNING)
454 continue;
455
456 LIST_DEL(&sess->buffer_wait);
457 LIST_INIT(&sess->buffer_wait);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100458 task_wakeup(sess->task, TASK_WOKEN_RES);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100459 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100460}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200461
462/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200463int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200464{
Willy Tarreau87b09662015-04-03 00:22:06 +0200465 LIST_INIT(&streams);
466 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
467 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200468}
469
Willy Tarreau87b09662015-04-03 00:22:06 +0200470void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100471{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200472 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100473 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200474 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100475 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100476
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100477 bytes = s->req.total - s->logs.bytes_in;
478 s->logs.bytes_in = s->req.total;
479 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200480 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100481
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100482 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100483
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100484 if (objt_server(s->target))
485 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200486
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200487 if (sess->listener && sess->listener->counters)
488 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200489
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100490 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200491 struct stkctr *stkctr = &s->stkctr[i];
492
493 if (!stkctr_entry(stkctr)) {
494 stkctr = &sess->stkctr[i];
495 if (!stkctr_entry(stkctr))
496 continue;
497 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200498
Emeric Brun57056f02015-06-15 18:29:57 +0200499 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
500 if (ptr1)
501 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200502
Emeric Brun57056f02015-06-15 18:29:57 +0200503 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
504 if (ptr2)
505 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200506 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200507
508 /* If data was modified, we need to touch to re-schedule sync */
509 if (ptr1 || ptr2)
510 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100511 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100512 }
513
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100514 bytes = s->res.total - s->logs.bytes_out;
515 s->logs.bytes_out = s->res.total;
516 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200517 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100518
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100519 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100520
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100521 if (objt_server(s->target))
522 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200523
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200524 if (sess->listener && sess->listener->counters)
525 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200526
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100527 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200528 struct stkctr *stkctr = &s->stkctr[i];
529
530 if (!stkctr_entry(stkctr)) {
531 stkctr = &sess->stkctr[i];
532 if (!stkctr_entry(stkctr))
533 continue;
534 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200535
Emeric Brun57056f02015-06-15 18:29:57 +0200536 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
537 if (ptr1)
538 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200539
Emeric Brun57056f02015-06-15 18:29:57 +0200540 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
541 if (ptr2)
542 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200543 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200544
545 /* If data was modified, we need to touch to re-schedule sync */
546 if (ptr1 || ptr2)
547 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100548 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100549 }
550}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200551
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100552/* This function is called with (si->state == SI_ST_CON) meaning that a
553 * connection was attempted and that the file descriptor is already allocated.
554 * We must check for establishment, error and abort. Possible output states
555 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
556 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200557 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100558 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200559static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100560{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100561 struct stream_interface *si = &s->si[1];
562 struct channel *req = &s->req;
563 struct channel *rep = &s->res;
Willy Tarreau350135c2016-11-17 12:05:13 +0100564 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100565
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100566 /* If we got an error, or if nothing happened and the connection timed
567 * out, we must give up. The CER state handler will take care of retry
568 * attempts and error reports.
569 */
570 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100571 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100572 /* Some data were sent past the connection establishment,
573 * so we need to pretend we're established to log correctly
574 * and let later states handle the failure.
575 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100576 si->state = SI_ST_EST;
577 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100578 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100579 return 1;
580 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100581 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100582 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100583
Willy Tarreau350135c2016-11-17 12:05:13 +0100584 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100585
586 if (si->err_type)
587 return 0;
588
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100589 if (si->flags & SI_FL_ERR)
590 si->err_type = SI_ET_CONN_ERR;
591 else
592 si->err_type = SI_ET_CONN_TO;
593 return 0;
594 }
595
596 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100597 if (!(req->flags & CF_WRITE_PARTIAL) &&
598 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200599 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
600 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100601 s->be->options & PR_O_ABRT_CLOSE)))) {
602 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200603 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100605 if (s->srv_error)
606 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100607 return 1;
608 }
609
610 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200611 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612 return 1;
613
614 /* OK, this means that a connection succeeded. The caller will be
615 * responsible for handling the transition from CON to EST.
616 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100617 si->state = SI_ST_EST;
618 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619 return 1;
620}
621
622/* This function is called with (si->state == SI_ST_CER) meaning that a
623 * previous connection attempt has failed and that the file descriptor
624 * has already been released. Possible causes include asynchronous error
625 * notification and time out. Possible output states are SI_ST_CLO when
626 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
627 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
628 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
629 * marked as in error state. It returns 0.
630 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200631static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100632{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100633 struct stream_interface *si = &s->si[1];
634
Willy Tarreau87b09662015-04-03 00:22:06 +0200635 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100636 if (objt_server(s->target)) {
637 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100638
Willy Tarreaue7dff022015-04-03 01:14:29 +0200639 if (s->flags & SF_CURR_SESS) {
640 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100641 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100642 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100643 }
644
645 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200646 si->conn_retries--;
647 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100648 if (!si->err_type) {
649 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100650 }
651
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100652 if (objt_server(s->target))
653 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100654 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100655 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100656 if (may_dequeue_tasks(objt_server(s->target), s->be))
657 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100658
659 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200660 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100661 s->req.flags |= CF_WRITE_ERROR;
662 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100663
664 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100665 if (s->srv_error)
666 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100667 return 0;
668 }
669
670 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700671 * retry on another server. By default this redispatch occurs on the
672 * last retry, but if configured we allow redispatches to occur on
673 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200674 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100675 * bit to ignore any persistence cookie. We won't count a retry nor a
676 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200677 * If the connection is not persistent, the balancing algorithm is not
678 * determinist (round robin) and there is more than one active server,
679 * we accept to perform an immediate redispatch without waiting since
680 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200682 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700683 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100684 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
685 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700686 ((s->be->conn_retries - si->conn_retries) %
687 s->be->redispatch_after == 0)) ||
688 ((s->be->redispatch_after < 0) &&
689 ((s->be->conn_retries - si->conn_retries) %
690 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200691 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700692 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100693 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100694 if (may_dequeue_tasks(objt_server(s->target), s->be))
695 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696
Willy Tarreaue7dff022015-04-03 01:14:29 +0200697 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100698 si->state = SI_ST_REQ;
699 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100700 if (objt_server(s->target))
701 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100702 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100703 si->state = SI_ST_ASS;
704 }
705
706 if (si->flags & SI_FL_ERR) {
707 /* The error was an asynchronous connection error, and we will
708 * likely have to retry connecting to the same server, most
709 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200710 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100711 */
712
Willy Tarreaub0290662014-06-13 17:04:44 +0200713 int delay = 1000;
714
715 if (s->be->timeout.connect && s->be->timeout.connect < delay)
716 delay = s->be->timeout.connect;
717
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100718 if (!si->err_type)
719 si->err_type = SI_ET_CONN_ERR;
720
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200721 /* only wait when we're retrying on the same server */
722 if (si->state == SI_ST_ASS ||
723 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
724 (s->be->srv_act <= 1)) {
725 si->state = SI_ST_TAR;
726 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
727 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728 return 0;
729 }
730 return 0;
731}
732
733/*
734 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200735 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200736 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200738static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100739{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100740 struct stream_interface *si = &s->si[1];
741 struct channel *req = &s->req;
742 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100744 /* First, centralize the timers information */
745 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
746 si->exp = TICK_ETERNITY;
747
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100748 if (objt_server(s->target))
749 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100750
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100751 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 /* if the user wants to log as soon as possible, without counting
753 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200754 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100756 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100757 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100758 }
759 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100760 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100761 }
762
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200763 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100764
765 /* Be sure to filter response headers if the backend is an HTTP proxy
766 * and if there are filters attached to the stream. */
767 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
768 rep->analysers |= AN_FLT_HTTP_HDRS;
769
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200770 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200771 if (req->flags & CF_WAKE_CONNECT) {
772 req->flags |= CF_WAKE_ONCE;
773 req->flags &= ~CF_WAKE_CONNECT;
774 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200775 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200776 /* real connections have timeouts */
777 req->wto = s->be->timeout.server;
778 rep->rto = s->be->timeout.server;
779 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100780 req->wex = TICK_ETERNITY;
781}
782
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700783/* Check if the connection request is in such a state that it can be aborted. */
784static int check_req_may_abort(struct channel *req, struct stream *s)
785{
786 return ((req->flags & (CF_READ_ERROR)) ||
787 ((req->flags & CF_SHUTW_NOW) && /* empty and client aborted */
788 (channel_is_empty(req) || s->be->options & PR_O_ABRT_CLOSE)));
789}
790
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100791/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
792 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100793 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
794 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
795 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100796 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200797static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100798{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100799 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100800 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100801 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100802
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100803 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 +0100804 now_ms, __FUNCTION__,
805 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400806 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100807 req->rex, s->res.wex,
808 req->flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -0400809 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 +0100810
811 if (si->state == SI_ST_ASS) {
812 /* Server assigned to connection request, we have to try to connect now */
813 int conn_err;
814
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700815 /* Before we try to initiate the connection, see if the
816 * request may be aborted instead.
817 */
818 if (check_req_may_abort(req, s)) {
819 si->err_type |= SI_ET_CONN_ABRT;
820 goto abort_connection;
821 }
822
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100823 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100824 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100825
Willy Tarreaue7dff022015-04-03 01:14:29 +0200826 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100827 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100828 if (srv)
829 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500830 if (srv)
831 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100832 return;
833 }
834
835 /* We have received a synchronous error. We might have to
836 * abort, retry immediately or redispatch.
837 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200838 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100839 if (!si->err_type) {
840 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100841 }
842
Willy Tarreau827aee92011-03-10 16:55:02 +0100843 if (srv)
844 srv_inc_sess_ctr(srv);
845 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500846 srv_set_sess_last(srv);
847 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +0100848 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100849 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100850
Willy Tarreau87b09662015-04-03 00:22:06 +0200851 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100852 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100853 if (may_dequeue_tasks(srv, s->be))
854 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100855
856 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200857 si_shutr(si);
858 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100859 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100860
861 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
862
Willy Tarreau87b09662015-04-03 00:22:06 +0200863 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100864 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100865 if (s->srv_error)
866 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100867 return;
868 }
869
870 /* We are facing a retryable error, but we don't want to run a
871 * turn-around now, as the problem is likely a source port
872 * allocation problem, so we want to retry now.
873 */
874 si->state = SI_ST_CER;
875 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100876 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100877 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
878 return;
879 }
880 else if (si->state == SI_ST_QUE) {
881 /* connection request was queued, check for any update */
882 if (!s->pend_pos) {
883 /* The connection is not in the queue anymore. Either
884 * we have a server connection slot available and we
885 * go directly to the assigned state, or we need to
886 * load-balance first and go to the INI state.
887 */
888 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200889 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100890 si->state = SI_ST_REQ;
891 else {
892 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
893 si->state = SI_ST_ASS;
894 }
895 return;
896 }
897
898 /* Connection request still in queue... */
899 if (si->flags & SI_FL_EXP) {
900 /* ... and timeout expired */
901 si->exp = TICK_ETERNITY;
902 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +0100903 if (srv)
904 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100905 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +0200906 si_shutr(si);
907 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100908 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100909 if (!si->err_type)
910 si->err_type = SI_ET_QUEUE_TO;
911 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100912 if (s->srv_error)
913 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100914 return;
915 }
916
917 /* Connection remains in queue, check if we have to abort it */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700918 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100920 si->err_type |= SI_ET_QUEUE_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700921 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100922 }
923
924 /* Nothing changed */
925 return;
926 }
927 else if (si->state == SI_ST_TAR) {
928 /* Connection request might be aborted */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700929 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100930 si->err_type |= SI_ET_CONN_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700931 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100932 }
933
934 if (!(si->flags & SI_FL_EXP))
935 return; /* still in turn-around */
936
937 si->exp = TICK_ETERNITY;
938
Willy Tarreau87b09662015-04-03 00:22:06 +0200939 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100940 * marked "assigned".
941 * FIXME: Should we force a redispatch attempt when the server is down ?
942 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200943 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100944 si->state = SI_ST_ASS;
945 else
946 si->state = SI_ST_REQ;
947 return;
948 }
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700949 return;
950
951abort_connection:
952 /* give up */
953 si->exp = TICK_ETERNITY;
954 si_shutr(si);
955 si_shutw(si);
956 si->state = SI_ST_CLO;
957 if (s->srv_error)
958 s->srv_error(s, si);
959 return;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100960}
961
Willy Tarreau87b09662015-04-03 00:22:06 +0200962/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +0900963 * also counts a failed request if the server state has not reached the request
964 * stage.
965 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200966static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +0900967{
Willy Tarreaue7dff022015-04-03 01:14:29 +0200968 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +0900969 if (s->si[1].state < SI_ST_REQ) {
970
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200971 strm_fe(s)->fe_counters.failed_req++;
Willy Tarreau2c1068c2015-09-23 12:21:21 +0200972 if (strm_li(s) && strm_li(s)->counters)
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200973 strm_li(s)->counters->failed_req++;
Simon Hormandec5be42011-06-08 09:19:07 +0900974
Willy Tarreaue7dff022015-04-03 01:14:29 +0200975 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +0900976 }
977 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200978 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +0900979 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200980 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +0900981 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +0200982 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +0900983 else
Willy Tarreaue7dff022015-04-03 01:14:29 +0200984 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +0900985 }
986}
987
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100988/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +0100989 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
990 * a real connection to a server, indicating that a server has been assigned,
991 * or SI_ST_EST for a successful connection to an applet. It may also return
992 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100993 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200994static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100995{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100996 struct stream_interface *si = &s->si[1];
997
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100998 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100999 now_ms, __FUNCTION__,
1000 s,
CJ Ess6eac32e2015-05-02 16:35:24 -04001001 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001002 s->req.rex, s->res.wex,
1003 s->req.flags, s->res.flags,
CJ Ess6eac32e2015-05-02 16:35:24 -04001004 s->req.buf->i, s->req.buf->o, s->res.buf->i, s->res.buf->o, s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001005
1006 if (si->state != SI_ST_REQ)
1007 return;
1008
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001009 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1010 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001011 struct appctx *appctx = objt_appctx(si->end);
1012
1013 if (!appctx || appctx->applet != __objt_applet(s->target))
1014 appctx = stream_int_register_handler(si, objt_applet(s->target));
1015
1016 if (!appctx) {
1017 /* No more memory, let's immediately abort. Force the
1018 * error code to ignore the ERR_LOCAL which is not a
1019 * real error.
1020 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001021 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001022
1023 si_shutr(si);
1024 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001025 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001026 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001027 si->state = SI_ST_CLO;
1028 if (s->srv_error)
1029 s->srv_error(s, si);
1030 return;
1031 }
1032
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001033 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001034 si->state = SI_ST_EST;
1035 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001036 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001037 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001038 return;
1039 }
1040
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001041 /* Try to assign a server */
1042 if (srv_redispatch_connect(s) != 0) {
1043 /* We did not get a server. Either we queued the
1044 * connection request, or we encountered an error.
1045 */
1046 if (si->state == SI_ST_QUE)
1047 return;
1048
1049 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001050 si_shutr(si);
1051 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001052 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001053 if (!si->err_type)
1054 si->err_type = SI_ET_CONN_OTHER;
1055 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001056 if (s->srv_error)
1057 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001058 return;
1059 }
1060
1061 /* The server is assigned */
1062 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1063 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001064 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001065}
1066
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001067/* This function parses the use-service action ruleset. It executes
1068 * the associated ACL and set an applet as a stream or txn final node.
1069 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1070 * consistent state. It returns ACT_RET_STOP in succes case because
1071 * use-service must be a terminal action. Returns ACT_RET_YIELD
1072 * if the initialisation function require more data.
1073 */
1074enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1075 struct session *sess, struct stream *s, int flags)
1076
1077{
1078 struct appctx *appctx;
1079
1080 /* Initialises the applet if it is required. */
1081 if (flags & ACT_FLAG_FIRST) {
1082 /* Register applet. this function schedules the applet. */
1083 s->target = &rule->applet.obj_type;
1084 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1085 return ACT_RET_ERR;
1086
1087 /* Initialise the context. */
1088 appctx = si_appctx(&s->si[1]);
1089 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1090 appctx->rule = rule;
1091 }
1092 else
1093 appctx = si_appctx(&s->si[1]);
1094
1095 /* Stops the applet sheduling, in case of the init function miss
1096 * some data.
1097 */
1098 appctx_pause(appctx);
1099 si_applet_stop_get(&s->si[1]);
1100
1101 /* Call initialisation. */
1102 if (rule->applet.init)
1103 switch (rule->applet.init(appctx, px, s)) {
1104 case 0: return ACT_RET_ERR;
1105 case 1: break;
1106 default: return ACT_RET_YIELD;
1107 }
1108
1109 /* Now we can schedule the applet. */
1110 si_applet_cant_get(&s->si[1]);
1111 appctx_wakeup(appctx);
1112
1113 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1114 sess->fe->fe_counters.intercepted_req++;
1115
1116 /* The flag SF_ASSIGNED prevent from server assignment. */
1117 s->flags |= SF_ASSIGNED;
1118
1119 return ACT_RET_STOP;
1120}
1121
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001122/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001123 * if appropriate. The default_backend rule is also considered, then the
1124 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001125 * It returns 1 if the processing can continue on next analysers, or zero if it
1126 * either needs more data or wants to immediately abort the request.
1127 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001128static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001129{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001130 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001131 struct session *sess = s->sess;
1132 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001133
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001134 req->analysers &= ~an_bit;
1135 req->analyse_exp = TICK_ETERNITY;
1136
Willy Tarreau87b09662015-04-03 00:22:06 +02001137 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001138 now_ms, __FUNCTION__,
1139 s,
1140 req,
1141 req->rex, req->wex,
1142 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001143 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001144 req->analysers);
1145
1146 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001147 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001148 struct switching_rule *rule;
1149
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001150 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001151 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001152
Willy Tarreauf51658d2014-04-23 01:21:56 +02001153 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001154 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001155 ret = acl_pass(ret);
1156 if (rule->cond->pol == ACL_COND_UNLESS)
1157 ret = !ret;
1158 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001159
1160 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001161 /* If the backend name is dynamic, try to resolve the name.
1162 * If we can't resolve the name, or if any error occurs, break
1163 * the loop and fallback to the default backend.
1164 */
1165 struct proxy *backend;
1166
1167 if (rule->dynamic) {
1168 struct chunk *tmp = get_trash_chunk();
1169 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1170 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001171 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001172 if (!backend)
1173 break;
1174 }
1175 else
1176 backend = rule->be.backend;
1177
Willy Tarreau87b09662015-04-03 00:22:06 +02001178 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001179 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001180 break;
1181 }
1182 }
1183
1184 /* To ensure correct connection accounting on the backend, we
1185 * have to assign one if it was not set (eg: a listen). This
1186 * measure also takes care of correctly setting the default
1187 * backend if any.
1188 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001189 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001190 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001191 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001192 }
1193
Willy Tarreaufb356202010-08-03 14:02:05 +02001194 /* we don't want to run the TCP or HTTP filters again if the backend has not changed */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001195 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001196 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1197 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001198 s->req.analysers &= ~AN_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001199 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001200
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001201 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau87b09662015-04-03 00:22:06 +02001202 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001203 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001204 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001205 int ret = 1;
1206
1207 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001208 ret = acl_exec_cond(prst_rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4de91492010-01-22 19:10:05 +01001209 ret = acl_pass(ret);
1210 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1211 ret = !ret;
1212 }
1213
1214 if (ret) {
1215 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001216 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001217 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001218 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001219 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001220 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001221 break;
1222 }
1223 }
1224
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001225 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001226
1227 sw_failed:
1228 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001229 channel_abort(&s->req);
1230 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001231
Willy Tarreaue7dff022015-04-03 01:14:29 +02001232 if (!(s->flags & SF_ERR_MASK))
1233 s->flags |= SF_ERR_RESOURCE;
1234 if (!(s->flags & SF_FINST_MASK))
1235 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001236
Willy Tarreaueee5b512015-04-03 23:46:31 +02001237 if (s->txn)
1238 s->txn->status = 500;
Christopher Fauletd7c91962015-04-30 11:48:27 +02001239 s->req.analysers &= AN_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001240 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001241 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001242}
1243
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001244/* This stream analyser works on a request. It applies all use-server rules on
1245 * it then returns 1. The data must already be present in the buffer otherwise
1246 * they won't match. It always returns 1.
1247 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001248static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001249{
1250 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001251 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001252 struct server_rule *rule;
1253
Willy Tarreau87b09662015-04-03 00:22:06 +02001254 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001255 now_ms, __FUNCTION__,
1256 s,
1257 req,
1258 req->rex, req->wex,
1259 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001260 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001261 req->analysers);
1262
Willy Tarreaue7dff022015-04-03 01:14:29 +02001263 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001264 list_for_each_entry(rule, &px->server_rules, list) {
1265 int ret;
1266
Willy Tarreau192252e2015-04-04 01:47:55 +02001267 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001268 ret = acl_pass(ret);
1269 if (rule->cond->pol == ACL_COND_UNLESS)
1270 ret = !ret;
1271
1272 if (ret) {
1273 struct server *srv = rule->srv.ptr;
1274
Willy Tarreau892337c2014-05-13 23:41:20 +02001275 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001276 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001277 (s->flags & SF_FORCE_PRST)) {
1278 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001279 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001280 break;
1281 }
1282 /* if the server is not UP, let's go on with next rules
1283 * just in case another one is suited.
1284 */
1285 }
1286 }
1287 }
1288
1289 req->analysers &= ~an_bit;
1290 req->analyse_exp = TICK_ETERNITY;
1291 return 1;
1292}
1293
Emeric Brun1d33b292010-01-04 15:47:17 +01001294/* This stream analyser works on a request. It applies all sticking rules on
1295 * it then returns 1. The data must already be present in the buffer otherwise
1296 * they won't match. It always returns 1.
1297 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001298static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001299{
1300 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001301 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001302 struct sticking_rule *rule;
1303
Willy Tarreau87b09662015-04-03 00:22:06 +02001304 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001305 now_ms, __FUNCTION__,
1306 s,
1307 req,
1308 req->rex, req->wex,
1309 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001310 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001311 req->analysers);
1312
1313 list_for_each_entry(rule, &px->sticking_rules, list) {
1314 int ret = 1 ;
1315 int i;
1316
Willy Tarreau9667a802013-12-09 12:52:13 +01001317 /* Only the first stick store-request of each table is applied
1318 * and other ones are ignored. The purpose is to allow complex
1319 * configurations which look for multiple entries by decreasing
1320 * order of precision and to stop at the first which matches.
1321 * An example could be a store of the IP address from an HTTP
1322 * header first, then from the source if not found.
1323 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001324 for (i = 0; i < s->store_count; i++) {
1325 if (rule->table.t == s->store[i].table)
1326 break;
1327 }
1328
1329 if (i != s->store_count)
1330 continue;
1331
1332 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001333 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001334 ret = acl_pass(ret);
1335 if (rule->cond->pol == ACL_COND_UNLESS)
1336 ret = !ret;
1337 }
1338
1339 if (ret) {
1340 struct stktable_key *key;
1341
Willy Tarreau192252e2015-04-04 01:47:55 +02001342 key = stktable_fetch_key(rule->table.t, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001343 if (!key)
1344 continue;
1345
1346 if (rule->flags & STK_IS_MATCH) {
1347 struct stksess *ts;
1348
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001349 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001350 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001351 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001352 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001353
1354 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001355 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1356 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001357 if (node) {
1358 struct server *srv;
1359
1360 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001361 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001362 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001363 (s->flags & SF_FORCE_PRST)) {
1364 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001365 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001366 }
1367 }
1368 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001369 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001370 }
1371 }
1372 if (rule->flags & STK_IS_STORE) {
1373 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1374 struct stksess *ts;
1375
1376 ts = stksess_new(rule->table.t, key);
1377 if (ts) {
1378 s->store[s->store_count].table = rule->table.t;
1379 s->store[s->store_count++].ts = ts;
1380 }
1381 }
1382 }
1383 }
1384 }
1385
1386 req->analysers &= ~an_bit;
1387 req->analyse_exp = TICK_ETERNITY;
1388 return 1;
1389}
1390
1391/* This stream analyser works on a response. It applies all store rules on it
1392 * then returns 1. The data must already be present in the buffer otherwise
1393 * they won't match. It always returns 1.
1394 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001395static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001396{
1397 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001398 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001399 struct sticking_rule *rule;
1400 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001401 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001402
Willy Tarreau87b09662015-04-03 00:22:06 +02001403 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001404 now_ms, __FUNCTION__,
1405 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001406 rep,
1407 rep->rex, rep->wex,
1408 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001409 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001410 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001411
1412 list_for_each_entry(rule, &px->storersp_rules, list) {
1413 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001414
Willy Tarreau9667a802013-12-09 12:52:13 +01001415 /* Only the first stick store-response of each table is applied
1416 * and other ones are ignored. The purpose is to allow complex
1417 * configurations which look for multiple entries by decreasing
1418 * order of precision and to stop at the first which matches.
1419 * An example could be a store of a set-cookie value, with a
1420 * fallback to a parameter found in a 302 redirect.
1421 *
1422 * The store-response rules are not allowed to override the
1423 * store-request rules for the same table, but they may coexist.
1424 * Thus we can have up to one store-request entry and one store-
1425 * response entry for the same table at any time.
1426 */
1427 for (i = nbreq; i < s->store_count; i++) {
1428 if (rule->table.t == s->store[i].table)
1429 break;
1430 }
1431
1432 /* skip existing entries for this table */
1433 if (i < s->store_count)
1434 continue;
1435
Emeric Brun1d33b292010-01-04 15:47:17 +01001436 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001437 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001438 ret = acl_pass(ret);
1439 if (rule->cond->pol == ACL_COND_UNLESS)
1440 ret = !ret;
1441 }
1442
1443 if (ret) {
1444 struct stktable_key *key;
1445
Willy Tarreau192252e2015-04-04 01:47:55 +02001446 key = stktable_fetch_key(rule->table.t, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001447 if (!key)
1448 continue;
1449
Willy Tarreau37e340c2013-12-06 23:05:21 +01001450 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001451 struct stksess *ts;
1452
1453 ts = stksess_new(rule->table.t, key);
1454 if (ts) {
1455 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001456 s->store[s->store_count++].ts = ts;
1457 }
1458 }
1459 }
1460 }
1461
1462 /* process store request and store response */
1463 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001464 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001465 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001466
Willy Tarreauc93cd162014-05-13 15:54:22 +02001467 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001468 stksess_free(s->store[i].table, s->store[i].ts);
1469 s->store[i].ts = NULL;
1470 continue;
1471 }
1472
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001473 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1474 if (ts) {
1475 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001476 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001477 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001478 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001479 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001480 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001481
1482 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001483 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001484 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001485 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001486 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001487
1488 rep->analysers &= ~an_bit;
1489 rep->analyse_exp = TICK_ETERNITY;
1490 return 1;
1491}
1492
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001493/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001494 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001495 */
1496#define UPDATE_ANALYSERS(real, list, back, flag) { \
1497 list = (((list) & ~(flag)) | ~(back)) & (real); \
1498 back = real; \
1499 if (!(list)) \
1500 break; \
1501 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1502 continue; \
1503}
1504
Christopher Fauleta9215b72016-05-11 17:06:28 +02001505/* These 2 following macros call an analayzer for the specified channel if the
1506 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001507 * stream has some registered filters, pre and post analyaze callbacks are
1508 * called. The second are used for other analyzers (AN_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001509 * AN_REQ/RES_HTTP_XFER_BODY) */
1510#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1511 { \
1512 if ((list) & (flag)) { \
1513 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001514 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001515 break; \
1516 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1517 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001518 if (!flt_post_analyze((strm), (chn), (flag))) \
1519 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001520 } \
1521 else { \
1522 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1523 break; \
1524 } \
1525 UPDATE_ANALYSERS((chn)->analysers, (list), \
1526 (back), (flag)); \
1527 } \
1528 }
1529
1530#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1531 { \
1532 if ((list) & (flag)) { \
1533 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1534 break; \
1535 UPDATE_ANALYSERS((chn)->analysers, (list), \
1536 (back), (flag)); \
1537 } \
1538 }
1539
Willy Tarreau87b09662015-04-03 00:22:06 +02001540/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001541 * then puts it back to the wait queue in a clean state, or cleans up its
1542 * resources if it must be deleted. Returns in <next> the date the task wants
1543 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1544 * nothing too many times, the request and response buffers flags are monitored
1545 * and each function is called only if at least another function has changed at
1546 * least one flag it is interested in.
1547 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001548struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001549{
Willy Tarreau827aee92011-03-10 16:55:02 +01001550 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001551 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001552 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001553 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001554 unsigned int rq_prod_last, rq_cons_last;
1555 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001556 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001557 struct channel *req, *res;
1558 struct stream_interface *si_f, *si_b;
1559
1560 req = &s->req;
1561 res = &s->res;
1562
1563 si_f = &s->si[0];
1564 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001565
1566 //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 +01001567 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001568
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001569 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001570 if (s->txn)
1571 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001572
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001573 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001574 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1575 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001576
1577 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001578 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1579 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001580
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001581 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001582 si_f->flags |= SI_FL_DONT_WAKE;
1583 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001584
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001585 /* 1a: Check for low level timeouts if needed. We just set a flag on
1586 * stream interfaces when their timeouts have expired.
1587 */
1588 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001589 stream_int_check_timeouts(si_f);
1590 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001591
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001592 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001593 * for future reads or writes. Note: this will also concern upper layers
1594 * but we do not touch any other flag. We must be careful and correctly
1595 * detect state changes when calling them.
1596 */
1597
Willy Tarreau8f128b42014-11-28 15:07:47 +01001598 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001599
Willy Tarreau8f128b42014-11-28 15:07:47 +01001600 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1601 si_b->flags |= SI_FL_NOLINGER;
1602 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001603 }
1604
Willy Tarreau8f128b42014-11-28 15:07:47 +01001605 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1606 if (si_f->flags & SI_FL_NOHALF)
1607 si_f->flags |= SI_FL_NOLINGER;
1608 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001609 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001610
Willy Tarreau8f128b42014-11-28 15:07:47 +01001611 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001612
Willy Tarreau8f128b42014-11-28 15:07:47 +01001613 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1614 si_f->flags |= SI_FL_NOLINGER;
1615 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001616 }
1617
Willy Tarreau8f128b42014-11-28 15:07:47 +01001618 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1619 if (si_b->flags & SI_FL_NOHALF)
1620 si_b->flags |= SI_FL_NOLINGER;
1621 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001622 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001623
Christopher Fauleta00d8172016-11-10 14:58:05 +01001624 if (HAS_FILTERS(s))
1625 flt_stream_check_timeouts(s);
1626
Willy Tarreau798f4322012-11-08 14:49:17 +01001627 /* Once in a while we're woken up because the task expires. But
1628 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001629 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001630 * timeout needs to be refreshed.
1631 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001632 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001633 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1634 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001635 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau5fb04712016-05-04 10:18:37 +02001636 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
1637 si_f->flags &= ~SI_FL_DONT_WAKE;
1638 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001639 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02001640 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001641 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001642
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001643 /* below we may emit error messages so we have to ensure that we have
1644 * our buffers properly allocated.
1645 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001646 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001647 /* No buffer available, we've been subscribed to the list of
1648 * buffer waiters, let's wait for our turn.
1649 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02001650 si_f->flags &= ~SI_FL_DONT_WAKE;
1651 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001652 goto update_exp_and_leave;
1653 }
1654
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001655 /* 1b: check for low-level errors reported at the stream interface.
1656 * First we check if it's a retryable error (in which case we don't
1657 * want to tell the buffer). Otherwise we report the error one level
1658 * upper by setting flags into the buffers. Note that the side towards
1659 * the client cannot have connect (hence retryable) errors. Also, the
1660 * connection setup code must be able to deal with any type of abort.
1661 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001662 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001663 if (unlikely(si_f->flags & SI_FL_ERR)) {
1664 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1665 si_shutr(si_f);
1666 si_shutw(si_f);
1667 stream_int_report_error(si_f);
1668 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001669 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001670 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001671 if (srv)
1672 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001673 if (!(s->flags & SF_ERR_MASK))
1674 s->flags |= SF_ERR_CLICL;
1675 if (!(s->flags & SF_FINST_MASK))
1676 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001677 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001678 }
1679 }
1680
Willy Tarreau8f128b42014-11-28 15:07:47 +01001681 if (unlikely(si_b->flags & SI_FL_ERR)) {
1682 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1683 si_shutr(si_b);
1684 si_shutw(si_b);
1685 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001686 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001687 if (srv)
1688 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001689 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001690 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001691 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001692 if (srv)
1693 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001694 if (!(s->flags & SF_ERR_MASK))
1695 s->flags |= SF_ERR_SRVCL;
1696 if (!(s->flags & SF_FINST_MASK))
1697 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001698 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001699 }
1700 /* note: maybe we should process connection errors here ? */
1701 }
1702
Willy Tarreau8f128b42014-11-28 15:07:47 +01001703 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001704 /* we were trying to establish a connection on the server side,
1705 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1706 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001707 if (unlikely(!sess_update_st_con_tcp(s)))
1708 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001709 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001710 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001711
1712 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1713 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1714 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1715 */
1716 }
1717
Willy Tarreau8f128b42014-11-28 15:07:47 +01001718 rq_prod_last = si_f->state;
1719 rq_cons_last = si_b->state;
1720 rp_cons_last = si_f->state;
1721 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001722
1723 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001724 /* Check for connection closure */
1725
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001726 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001727 "[%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 +01001728 now_ms, __FUNCTION__, __LINE__,
1729 t,
1730 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001731 req, res,
1732 req->rex, res->wex,
1733 req->flags, res->flags,
1734 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1735 si_f->err_type, si_b->err_type,
1736 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001737
1738 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001739 if (unlikely(si_f->state == SI_ST_DIS))
1740 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001741
1742 /* When a server-side connection is released, we have to count it and
1743 * check for pending connections on this server.
1744 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001745 if (unlikely(si_b->state == SI_ST_DIS)) {
1746 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001747 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001748 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001749 if (s->flags & SF_CURR_SESS) {
1750 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001751 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001752 }
1753 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001754 if (may_dequeue_tasks(srv, s->be))
1755 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001756 }
1757 }
1758
1759 /*
1760 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1761 * at this point.
1762 */
1763
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001764 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001765 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001766 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1767 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1768 si_f->state != rq_prod_last ||
1769 si_b->state != rq_cons_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001770 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001771 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001772
Willy Tarreau8f128b42014-11-28 15:07:47 +01001773 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001774 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001775 unsigned int ana_list;
1776 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001777
Willy Tarreau90deb182010-01-07 00:20:41 +01001778 /* it's up to the analysers to stop new connections,
1779 * disable reading or closing. Note: if an analyser
1780 * disables any of these bits, it is responsible for
1781 * enabling them again when it disables itself, so
1782 * that other analysers are called in similar conditions.
1783 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001784 channel_auto_read(req);
1785 channel_auto_connect(req);
1786 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001787
1788 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001789 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001790 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001791 * the list when not needed. Any analyser may return 0
1792 * to break out of the loop, either because of missing
1793 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001794 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001795 * analyser, and we may loop again if other analysers
1796 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001797 *
1798 * We build a list of analysers to run. We evaluate all
1799 * of these analysers in the order of the lower bit to
1800 * the higher bit. This ordering is very important.
1801 * An analyser will often add/remove other analysers,
1802 * including itself. Any changes to itself have no effect
1803 * on the loop. If it removes any other analysers, we
1804 * want those analysers not to be called anymore during
1805 * this loop. If it adds an analyser that is located
1806 * after itself, we want it to be scheduled for being
1807 * processed during the loop. If it adds an analyser
1808 * which is located before it, we want it to switch to
1809 * it immediately, even if it has already been called
1810 * once but removed since.
1811 *
1812 * In order to achieve this, we compare the analyser
1813 * list after the call with a copy of it before the
1814 * call. The work list is fed with analyser bits that
1815 * appeared during the call. Then we compare previous
1816 * work list with the new one, and check the bits that
1817 * appeared. If the lowest of these bits is lower than
1818 * the current bit, it means we have enabled a previous
1819 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001820 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001821
Willy Tarreau8f128b42014-11-28 15:07:47 +01001822 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001823 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001824 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001825 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1826 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
1827 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
1828 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
1829 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
1830 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
1831 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1832 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
1833 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
1834 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
1835 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
1836 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
1837 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
1838 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
1839 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1840 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1841 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
1842 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001843 break;
1844 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001845 }
Willy Tarreau84455332009-03-15 22:34:05 +01001846
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 rq_prod_last = si_f->state;
1848 rq_cons_last = si_b->state;
1849 req->flags &= ~CF_WAKE_ONCE;
1850 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001851
Willy Tarreau8f128b42014-11-28 15:07:47 +01001852 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001853 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001854 }
1855
Willy Tarreau576507f2010-01-07 00:09:04 +01001856 /* we'll monitor the request analysers while parsing the response,
1857 * because some response analysers may indirectly enable new request
1858 * analysers (eg: HTTP keep-alive).
1859 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001860 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001861
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001862 resync_response:
1863 /* Analyse response */
1864
Willy Tarreau8f128b42014-11-28 15:07:47 +01001865 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1866 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1867 si_f->state != rp_cons_last ||
1868 si_b->state != rp_prod_last ||
Thierry FOURNIER2e05a8c2014-11-24 14:49:56 +01001869 s->task->state & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001870 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001871
Willy Tarreau8f128b42014-11-28 15:07:47 +01001872 if ((res->flags & CF_MASK_ANALYSER) &&
1873 (res->analysers & AN_REQ_ALL)) {
Willy Tarreau0499e352010-12-17 07:13:42 +01001874 /* Due to HTTP pipelining, the HTTP request analyser might be waiting
1875 * for some free space in the response buffer, so we might need to call
1876 * it when something changes in the response buffer, but still we pass
1877 * it the request buffer. Note that the SI state might very well still
1878 * be zero due to us returning a flow of redirects!
1879 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001880 res->analysers &= ~AN_REQ_ALL;
1881 req->flags |= CF_WAKE_ONCE;
Willy Tarreau0499e352010-12-17 07:13:42 +01001882 }
1883
Willy Tarreau8f128b42014-11-28 15:07:47 +01001884 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001885 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001886 unsigned int ana_list;
1887 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001888
Willy Tarreau90deb182010-01-07 00:20:41 +01001889 /* it's up to the analysers to stop disable reading or
1890 * closing. Note: if an analyser disables any of these
1891 * bits, it is responsible for enabling them again when
1892 * it disables itself, so that other analysers are called
1893 * in similar conditions.
1894 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001895 channel_auto_read(res);
1896 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001897
1898 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001899 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001900 * to MSB. The analysers must remove themselves from
1901 * the list when not needed. Any analyser may return 0
1902 * to break out of the loop, either because of missing
1903 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001904 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001905 * analyser, and we may loop again if other analysers
1906 * are added in the middle.
1907 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001908
Willy Tarreau8f128b42014-11-28 15:07:47 +01001909 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001910 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001911 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Fauleta9215b72016-05-11 17:06:28 +02001912 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_FE);
1913 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_FLT_START_BE);
1914 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
1915 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
1916 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
1917 FLT_ANALYZE(s, res, http_process_res_common, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE, s->be);
1918 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_FLT_HTTP_HDRS);
1919 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_FLT_XFER_DATA);
1920 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
1921 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001922 break;
1923 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001924 }
1925
Willy Tarreau8f128b42014-11-28 15:07:47 +01001926 rp_cons_last = si_f->state;
1927 rp_prod_last = si_b->state;
1928 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001929
Willy Tarreau8f128b42014-11-28 15:07:47 +01001930 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001931 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001932 }
1933
Willy Tarreau576507f2010-01-07 00:09:04 +01001934 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001935 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01001936 goto resync_request;
1937
Willy Tarreau8f128b42014-11-28 15:07:47 +01001938 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01001939 goto resync_request;
1940
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001941 /* FIXME: here we should call protocol handlers which rely on
1942 * both buffers.
1943 */
1944
1945
1946 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02001947 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01001948 * we're just in a data phase here since it means we have not
1949 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001950 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001951 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001952 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001953 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001954 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001955 req->analysers = 0;
1956 if (req->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001957 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001958 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001959 if (srv)
1960 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001961 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01001962 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001963 else if (req->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001964 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001965 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001966 if (srv)
1967 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001968 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01001969 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001970 else if (req->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001971 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001972 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001973 if (srv)
1974 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001975 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001976 }
1977 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001978 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001979 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001980 if (srv)
1981 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001982 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01001983 }
Willy Tarreau84455332009-03-15 22:34:05 +01001984 sess_set_term_flags(s);
1985 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001986 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001987 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001988 res->analysers = 0;
1989 if (res->flags & CF_READ_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001990 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001991 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001992 if (srv)
1993 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001994 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01001995 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01001996 else if (res->flags & CF_READ_TIMEOUT) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001997 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001998 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001999 if (srv)
2000 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002001 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002002 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002003 else if (res->flags & CF_WRITE_ERROR) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002004 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002005 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002006 if (srv)
2007 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002008 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002009 }
2010 else {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002011 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002012 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01002013 if (srv)
2014 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002015 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002016 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002017 sess_set_term_flags(s);
2018 }
Willy Tarreau84455332009-03-15 22:34:05 +01002019 }
2020
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002021 /*
2022 * Here we take care of forwarding unhandled data. This also includes
2023 * connection establishments and shutdown requests.
2024 */
2025
2026
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002027 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002028 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002029 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002030 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002031 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002032 if (unlikely(!req->analysers &&
2033 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2034 (si_f->state >= SI_ST_EST) &&
2035 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002036 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002037 * attached to it. If any data are left in, we'll permit them to
2038 * move.
2039 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002040 channel_auto_read(req);
2041 channel_auto_connect(req);
2042 channel_auto_close(req);
2043 buffer_flush(req->buf);
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002044
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002045 /* We'll let data flow between the producer (if still connected)
2046 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002047 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002048 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002049 channel_forward_forever(req);
Willy Tarreau42529c32015-07-09 18:38:57 +02002050
2051 /* Just in order to support fetching HTTP contents after start
2052 * of forwarding when the HTTP forwarding analyser is not used,
2053 * we simply reset msg->sov so that HTTP rewinding points to the
2054 * headers.
2055 */
2056 if (s->txn)
2057 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - req->buf->o;
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002058 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002059
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002060 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002061 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2062 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002063 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002064 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->rcv_pipe) &&
2065 (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 +01002066 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002067 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2068 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002069 (req->flags & CF_STREAMER_FAST)))) {
2070 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002071 }
2072
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002073 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002074 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002075
2076 /*
2077 * Now forward all shutdown requests between both sides of the buffer
2078 */
2079
Willy Tarreau520d95e2009-09-19 21:04:57 +02002080 /* first, let's check if the request buffer needs to shutdown(write), which may
2081 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002082 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2083 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002084 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002085 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002086 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002087 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002088 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002089
2090 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002091 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2092 channel_is_empty(req))) {
2093 if (req->flags & CF_READ_ERROR)
2094 si_b->flags |= SI_FL_NOLINGER;
2095 si_shutw(si_b);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002096 if (tick_isset(s->be->timeout.serverfin)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002097 res->rto = s->be->timeout.serverfin;
2098 res->rex = tick_add(now_ms, res->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002099 }
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002100 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002101
2102 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002103 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2104 !req->analysers))
2105 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002106
2107 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002108 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2109 if (si_f->flags & SI_FL_NOHALF)
2110 si_f->flags |= SI_FL_NOLINGER;
2111 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002112 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002113
Willy Tarreau520d95e2009-09-19 21:04:57 +02002114 /* it's possible that an upper layer has requested a connection setup or abort.
2115 * There are 2 situations where we decide to establish a new connection :
2116 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002117 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002118 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002119 if (si_b->state == SI_ST_INI) {
2120 if (!(req->flags & CF_SHUTW)) {
2121 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002122 /* If we have an appctx, there is no connect method, so we
2123 * immediately switch to the connected state, otherwise we
2124 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002125 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002126 si_b->state = SI_ST_REQ; /* new connection requested */
2127 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002128 }
Willy Tarreau73201222009-08-16 18:27:24 +02002129 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002130 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002131 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2132 channel_shutw_now(req); /* fix buffer flags upon abort */
2133 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002134 }
Willy Tarreau92795622009-03-06 12:51:23 +01002135 }
2136
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002137
2138 /* we may have a pending connection request, or a connection waiting
2139 * for completion.
2140 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002141 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002142
2143 /* prune the request variables and swap to the response variables. */
2144 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreau6204cd92016-03-10 16:33:04 +01002145 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002146 vars_init(&s->vars_reqres, SCOPE_RES);
2147 }
2148
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002149 do {
2150 /* nb: step 1 might switch from QUE to ASS, but we first want
2151 * to give a chance to step 2 to perform a redirect if needed.
2152 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002153 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002154 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002155 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002156 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002157
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002158 /* applets directly go to the ESTABLISHED state. Similarly,
2159 * servers experience the same fate when their connection
2160 * is reused.
2161 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002162 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002163 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002164
Willy Tarreaub44c8732013-12-31 23:16:50 +01002165 /* Now we can add the server name to a header (if requested) */
2166 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002167 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002168 (s->be->server_id_hdr_name != NULL) &&
2169 (s->be->mode == PR_MODE_HTTP) &&
2170 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002171 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002172 }
2173
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002174 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002175 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002176 http_perform_server_redirect(s, si_b);
2177 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002178 }
2179
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002180 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002181 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002182 goto resync_stream_interface;
2183
Willy Tarreau815a9b22010-07-27 17:15:12 +02002184 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002185 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002186 goto resync_request;
2187
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002188 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002189
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002190 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002191 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002192 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002193 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002194 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002195 if (unlikely(!res->analysers &&
2196 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2197 (si_b->state >= SI_ST_EST) &&
2198 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002199 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002200 * attached to it. If any data are left in, we'll permit them to
2201 * move.
2202 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002203 channel_auto_read(res);
2204 channel_auto_close(res);
2205 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002206
2207 /* We'll let data flow between the producer (if still connected)
2208 * to the consumer.
2209 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002210 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002211 channel_forward_forever(res);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002212
Willy Tarreau42529c32015-07-09 18:38:57 +02002213 /* Just in order to support fetching HTTP contents after start
2214 * of forwarding when the HTTP forwarding analyser is not used,
2215 * we simply reset msg->sov so that HTTP rewinding points to the
2216 * headers.
2217 */
2218 if (s->txn)
2219 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2220
Willy Tarreauce887fd2012-05-12 12:50:00 +02002221 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002222 * tunnel timeout set, use it now. Note that we must respect
2223 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002224 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002225 if (!req->analysers && s->be->timeout.tunnel) {
2226 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002227 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002228
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002229 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2230 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002231 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2232 res->rto = s->be->timeout.serverfin;
2233 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2234 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002235 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2236 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002237
Willy Tarreau8f128b42014-11-28 15:07:47 +01002238 req->rex = tick_add(now_ms, req->rto);
2239 req->wex = tick_add(now_ms, req->wto);
2240 res->rex = tick_add(now_ms, res->rto);
2241 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002242 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002243 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002244
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002245 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002246 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2247 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002248 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002249 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2250 (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 +01002251 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002252 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2253 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002254 (res->flags & CF_STREAMER_FAST)))) {
2255 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002256 }
2257
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002258 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002260
2261 /*
2262 * Now forward all shutdown requests between both sides of the buffer
2263 */
2264
2265 /*
2266 * FIXME: this is probably where we should produce error responses.
2267 */
2268
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002269 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002270 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002271 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002272 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002273 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002274
2275 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002276 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2277 channel_is_empty(res))) {
2278 si_shutw(si_f);
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002279 if (tick_isset(sess->fe->timeout.clientfin)) {
2280 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002281 req->rex = tick_add(now_ms, req->rto);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002282 }
2283 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002284
2285 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002286 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2287 !res->analysers)
2288 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002289
2290 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002291 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2292 if (si_b->flags & SI_FL_NOHALF)
2293 si_b->flags |= SI_FL_NOLINGER;
2294 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002295 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002296
Willy Tarreau8f128b42014-11-28 15:07:47 +01002297 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002298 goto resync_stream_interface;
2299
Willy Tarreau8f128b42014-11-28 15:07:47 +01002300 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002301 goto resync_request;
2302
Willy Tarreau8f128b42014-11-28 15:07:47 +01002303 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002304 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002305
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002306 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002307 si_f->flags &= ~SI_FL_DONT_WAKE;
2308 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002309
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002310 /* This is needed only when debugging is enabled, to indicate
2311 * client-side or server-side close. Please note that in the unlikely
2312 * event where both sides would close at once, the sequence is reported
2313 * on the server side first.
2314 */
2315 if (unlikely((global.mode & MODE_DEBUG) &&
2316 (!(global.mode & MODE_QUIET) ||
2317 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002318 if (si_b->state == SI_ST_CLO &&
2319 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002320 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002321 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002322 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2323 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002324 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002325 }
2326
Willy Tarreau8f128b42014-11-28 15:07:47 +01002327 if (si_f->state == SI_ST_CLO &&
2328 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002329 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002330 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002331 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2332 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002333 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002334 }
2335 }
2336
Willy Tarreau8f128b42014-11-28 15:07:47 +01002337 if (likely((si_f->state != SI_ST_CLO) ||
2338 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002339
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002340 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002341 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002342
Willy Tarreau563cc372015-04-19 18:13:56 +02002343 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002344 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002345
Willy Tarreau563cc372015-04-19 18:13:56 +02002346 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002347 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002348
Willy Tarreau8f128b42014-11-28 15:07:47 +01002349 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2350 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2351 si_f->prev_state = si_f->state;
2352 si_b->prev_state = si_b->state;
2353 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2354 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002355
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002356 /* Trick: if a request is being waiting for the server to respond,
2357 * and if we know the server can timeout, we don't want the timeout
2358 * to expire on the client side first, but we're still interested
2359 * in passing data from the client to the server (eg: POST). Thus,
2360 * we can cancel the client's request timeout if the server's
2361 * request timeout is set and the server has not yet sent a response.
2362 */
2363
Willy Tarreau8f128b42014-11-28 15:07:47 +01002364 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2365 (tick_isset(req->wex) || tick_isset(res->rex))) {
2366 req->flags |= CF_READ_NOEXP;
2367 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002368 }
2369
Willy Tarreau798f4322012-11-08 14:49:17 +01002370 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002371 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Christopher Fauleta00d8172016-11-10 14:58:05 +01002372 t->expire = tick_first((tick_is_expired(t->expire, now_ms) ? 0 : t->expire),
2373 tick_first(tick_first(req->rex, req->wex),
2374 tick_first(res->rex, res->wex)));
Willy Tarreaudef0d222016-11-08 22:03:00 +01002375 if (!req->analysers)
2376 req->analyse_exp = TICK_ETERNITY;
2377
2378 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) &&
2379 (!tick_isset(req->analyse_exp) || tick_is_expired(req->analyse_exp, now_ms)))
2380 req->analyse_exp = tick_add(now_ms, 5000);
2381
2382 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002383
Willy Tarreau8f128b42014-11-28 15:07:47 +01002384 if (si_f->exp)
2385 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002386
Willy Tarreau8f128b42014-11-28 15:07:47 +01002387 if (si_b->exp)
2388 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002389
2390#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002391 fprintf(stderr,
2392 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2393 " 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 +01002394 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2395 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002396#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002397
2398#ifdef DEBUG_DEV
2399 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002400 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002401 ABORT_NOW();
2402#endif
Willy Tarreau87b09662015-04-03 00:22:06 +02002403 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002404 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002405 }
2406
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002407 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002408 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002409 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002410 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002411 if (sess->listener) {
2412 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002413 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002414 sess->listener->nbconn--;
2415 if (sess->listener->state == LI_FULL)
2416 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002417
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002418 /* Dequeues all of the listeners waiting for a resource */
2419 if (!LIST_ISEMPTY(&global_listener_queue))
2420 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002421
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002422 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2423 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2424 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002425 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002426
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002427 if (unlikely((global.mode & MODE_DEBUG) &&
2428 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002429 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002430 s->uniq_id, s->be->id,
Willy Tarreau8f128b42014-11-28 15:07:47 +01002431 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->t.sock.fd : -1,
2432 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->t.sock.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002433 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434 }
2435
2436 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002437 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002438
Willy Tarreaueee5b512015-04-03 23:46:31 +02002439 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002440 int n;
2441
Willy Tarreaueee5b512015-04-03 23:46:31 +02002442 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002443 if (n < 1 || n > 5)
2444 n = 0;
2445
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002446 if (sess->fe->mode == PR_MODE_HTTP) {
2447 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002448 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002449 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002450 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002451 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002452 s->be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002453 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002454 }
2455
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002456 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002457 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002458 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002459 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002460 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002461 }
2462
Willy Tarreau87b09662015-04-03 00:22:06 +02002463 /* update time stats for this stream */
2464 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002465
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002466 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002467 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002468 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002469 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002470 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002471}
2472
Willy Tarreau87b09662015-04-03 00:22:06 +02002473/* Update the stream's backend and server time stats */
2474void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002475{
2476 int t_request;
2477 int t_queue;
2478 int t_connect;
2479 int t_data;
2480 int t_close;
2481 struct server *srv;
2482
2483 t_request = 0;
2484 t_queue = s->logs.t_queue;
2485 t_connect = s->logs.t_connect;
2486 t_close = s->logs.t_close;
2487 t_data = s->logs.t_data;
2488
2489 if (s->be->mode != PR_MODE_HTTP)
2490 t_data = t_connect;
2491
2492 if (t_connect < 0 || t_data < 0)
2493 return;
2494
2495 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2496 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2497
2498 t_data -= t_connect;
2499 t_connect -= t_queue;
2500 t_queue -= t_request;
2501
2502 srv = objt_server(s->target);
2503 if (srv) {
2504 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2505 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2506 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2507 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2508 }
2509 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2510 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2511 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2512 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2513}
2514
Willy Tarreau7c669d72008-06-20 15:04:11 +02002515/*
2516 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002517 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002518 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002519 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002520 * server.
2521 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002522void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002523{
2524 if (sess->srv_conn == newsrv)
2525 return;
2526
2527 if (sess->srv_conn) {
2528 sess->srv_conn->served--;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002529 sess->srv_conn->proxy->served--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002530 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2531 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002532 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002533 }
2534
2535 if (newsrv) {
2536 newsrv->served++;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002537 newsrv->proxy->served++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002538 if (newsrv->proxy->lbprm.server_take_conn)
2539 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002540 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002541 }
2542}
2543
Willy Tarreau84455332009-03-15 22:34:05 +01002544/* Handle server-side errors for default protocols. It is called whenever a a
2545 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002546 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002547 * them. It's installed as ->srv_error for the server-side stream_interface.
2548 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002549void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002550{
2551 int err_type = si->err_type;
2552 int err = 0, fin = 0;
2553
2554 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002555 err = SF_ERR_CLICL;
2556 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002557 }
2558 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002559 err = SF_ERR_CLICL;
2560 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002561 }
2562 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002563 err = SF_ERR_SRVTO;
2564 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002565 }
2566 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002567 err = SF_ERR_SRVCL;
2568 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002569 }
2570 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002571 err = SF_ERR_SRVTO;
2572 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002573 }
2574 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002575 err = SF_ERR_SRVCL;
2576 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002577 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002578 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002579 err = SF_ERR_RESOURCE;
2580 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002581 }
Willy Tarreau84455332009-03-15 22:34:05 +01002582 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002583 err = SF_ERR_INTERNAL;
2584 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002585 }
2586
Willy Tarreaue7dff022015-04-03 01:14:29 +02002587 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002588 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002589 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002590 s->flags |= fin;
2591}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002592
Willy Tarreaue7dff022015-04-03 01:14:29 +02002593/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002594void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002595{
Willy Tarreau87b09662015-04-03 00:22:06 +02002596 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002597 return;
2598
Willy Tarreau87b09662015-04-03 00:22:06 +02002599 channel_shutw_now(&stream->req);
2600 channel_shutr_now(&stream->res);
2601 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002602 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002603 stream->flags |= why;
2604 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002605}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002606
Willy Tarreau8b22a712010-06-18 17:46:06 +02002607/************************************************************************/
2608/* All supported ACL keywords must be declared here. */
2609/************************************************************************/
2610
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002611/* Returns a pointer to a stkctr depending on the fetch keyword name.
2612 * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002613 * sc[0-9]_* will return a pointer to the respective field in the
Willy Tarreau87b09662015-04-03 00:22:06 +02002614 * stream <l4>. sc_* requires an UINT argument specifying the stick
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002615 * counter number. src_* will fill a locally allocated structure with
Willy Tarreaua65536c2013-07-22 22:40:11 +02002616 * the table and entry corresponding to what is specified with src_*.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002617 * NULL may be returned if the designated stkctr is not tracked. For
2618 * the sc_* and sc[0-9]_* forms, an optional table argument may be
2619 * passed. When present, the currently tracked key is then looked up
2620 * in the specified table instead of the current table. The purpose is
2621 * to be able to convery multiple values per key (eg: have gpc0 from
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002622 * multiple tables). <strm> is allowed to be NULL, in which case only
2623 * the session will be consulted.
Willy Tarreaua65536c2013-07-22 22:40:11 +02002624 */
Willy Tarreaue12704b2014-07-15 19:06:18 +02002625struct stkctr *
Willy Tarreau192252e2015-04-04 01:47:55 +02002626smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
Willy Tarreaua65536c2013-07-22 22:40:11 +02002627{
2628 static struct stkctr stkctr;
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002629 struct stkctr *stkptr;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002630 struct stksess *stksess;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002631 unsigned int num = kw[2] - '0';
Willy Tarreau0f791d42013-07-23 19:56:43 +02002632 int arg = 0;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002633
Willy Tarreau0f791d42013-07-23 19:56:43 +02002634 if (num == '_' - '0') {
2635 /* sc_* variant, args[0] = ctr# (mandatory) */
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02002636 num = args[arg++].data.sint;
Willy Tarreau4d4149c2013-07-23 19:33:46 +02002637 if (num >= MAX_SESS_STKCTR)
2638 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002639 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002640 else if (num > 9) { /* src_* variant, args[0] = table */
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002641 struct stktable_key *key;
Willy Tarreau9ad7bd42015-04-03 19:19:59 +02002642 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002643 struct sample smp;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002644
2645 if (!conn)
2646 return NULL;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002647
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002648 /* Fetch source adress in a sample. */
2649 smp.px = NULL;
2650 smp.sess = sess;
2651 smp.strm = strm;
2652 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2653 return NULL;
2654
2655 /* Converts into key. */
2656 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreaua65536c2013-07-22 22:40:11 +02002657 if (!key)
2658 return NULL;
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002659
Willy Tarreaua65536c2013-07-22 22:40:11 +02002660 stkctr.table = &args->data.prx->table;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002661 stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key));
Willy Tarreaua65536c2013-07-22 22:40:11 +02002662 return &stkctr;
2663 }
Willy Tarreau0f791d42013-07-23 19:56:43 +02002664
2665 /* Here, <num> contains the counter number from 0 to 9 for
2666 * the sc[0-9]_ form, or even higher using sc_(num) if needed.
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002667 * args[arg] is the first optional argument. We first lookup the
2668 * ctr form the stream, then from the session if it was not there.
Willy Tarreau0f791d42013-07-23 19:56:43 +02002669 */
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002670
Thierry Fournier40e1d512016-03-29 21:27:36 +02002671 if (strm)
2672 stkptr = &strm->stkctr[num];
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002673 if (!strm || !stkctr_entry(stkptr)) {
2674 stkptr = &sess->stkctr[num];
2675 if (!stkctr_entry(stkptr))
2676 return NULL;
2677 }
2678
2679 stksess = stkctr_entry(stkptr);
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002680 if (!stksess)
2681 return NULL;
2682
Willy Tarreau0f791d42013-07-23 19:56:43 +02002683 if (unlikely(args[arg].type == ARGT_TAB)) {
2684 /* an alternate table was specified, let's look up the same key there */
2685 stkctr.table = &args[arg].data.prx->table;
Willy Tarreau6a0b6bd2014-04-09 13:25:42 +02002686 stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess));
Willy Tarreau0f791d42013-07-23 19:56:43 +02002687 return &stkctr;
2688 }
Willy Tarreau8b7f8682015-04-04 16:29:12 +02002689 return stkptr;
Willy Tarreaua65536c2013-07-22 22:40:11 +02002690}
2691
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002692/* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create
2693 * the entry if it doesn't exist yet. This is needed for a few fetch
2694 * functions which need to create an entry, such as src_inc_gpc* and
2695 * src_clr_gpc*.
2696 */
2697struct stkctr *
2698smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw)
2699{
2700 static struct stkctr stkctr;
2701 struct stktable_key *key;
2702 struct connection *conn = objt_conn(sess->origin);
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002703 struct sample smp;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002704
2705 if (strncmp(kw, "src_", 4) != 0)
2706 return NULL;
2707
2708 if (!conn)
2709 return NULL;
2710
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002711 /* Fetch source adress in a sample. */
2712 smp.px = NULL;
2713 smp.sess = sess;
2714 smp.strm = strm;
2715 if (!smp_fetch_src(NULL, &smp, NULL, NULL))
2716 return NULL;
2717
2718 /* Converts into key. */
2719 key = smp_to_stkey(&smp, &args->data.prx->table);
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002720 if (!key)
2721 return NULL;
2722
2723 stkctr.table = &args->data.prx->table;
2724 stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key));
2725 return &stkctr;
2726}
2727
Willy Tarreau87b09662015-04-03 00:22:06 +02002728/* set return a boolean indicating if the requested stream counter is
Willy Tarreau88821242013-07-22 18:29:29 +02002729 * currently being tracked or not.
2730 * Supports being called as "sc[0-9]_tracked" only.
2731 */
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002732static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002733smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002734{
2735 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002736 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002737 smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau6f1615f2013-06-03 15:15:22 +02002738 return 1;
2739}
2740
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002741/* set <smp> to the General Purpose Flag 0 value from the stream's tracked
2742 * frontend counters or from the src.
2743 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value
2744 * zero is returned if the key is new.
2745 */
2746static int
2747smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private)
2748{
Willy Tarreaube508f12016-03-10 11:47:01 +01002749 struct stkctr *stkctr;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002750
Willy Tarreaube508f12016-03-10 11:47:01 +01002751 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002752 if (!stkctr)
2753 return 0;
2754
2755 smp->flags = SMP_F_VOL_TEST;
2756 smp->data.type = SMP_T_SINT;
2757 smp->data.u.sint = 0;
2758
2759 if (stkctr_entry(stkctr) != NULL) {
2760 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0);
2761 if (!ptr)
2762 return 0; /* parameter not stored */
2763 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
2764 }
2765 return 1;
2766}
2767
Willy Tarreau87b09662015-04-03 00:22:06 +02002768/* set <smp> to the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau30b20462013-07-22 19:46:52 +02002769 * frontend counters or from the src.
2770 * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value
2771 * zero is returned if the key is new.
2772 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002773static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002774smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002775{
Willy Tarreaube508f12016-03-10 11:47:01 +01002776 struct stkctr *stkctr;
2777
Willy Tarreaube508f12016-03-10 11:47:01 +01002778 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30b20462013-07-22 19:46:52 +02002779 if (!stkctr)
2780 return 0;
2781
Willy Tarreau37406352012-04-23 16:16:37 +02002782 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002783 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002784 smp->data.u.sint = 0;
Willy Tarreau30b20462013-07-22 19:46:52 +02002785
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002786 if (stkctr_entry(stkctr) != NULL) {
2787 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002788 if (!ptr)
2789 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002790 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002791 }
2792 return 1;
2793}
2794
Willy Tarreau87b09662015-04-03 00:22:06 +02002795/* set <smp> to the General Purpose Counter 0's event rate from the stream's
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002796 * tracked frontend counters or from the src.
2797 * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only.
2798 * Value zero is returned if the key is new.
2799 */
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002800static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002801smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002802{
Willy Tarreaube508f12016-03-10 11:47:01 +01002803 struct stkctr *stkctr;
2804
Willy Tarreaube508f12016-03-10 11:47:01 +01002805 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub5e0af02013-07-22 23:47:07 +02002806 if (!stkctr)
2807 return 0;
2808
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002809 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002810 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002811 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002812 if (stkctr_entry(stkctr) != NULL) {
2813 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002814 if (!ptr)
2815 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002816 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002817 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002818 }
2819 return 1;
2820}
2821
Willy Tarreau87b09662015-04-03 00:22:06 +02002822/* Increment the General Purpose Counter 0 value from the stream's tracked
Willy Tarreau710d38c2013-07-23 00:07:04 +02002823 * frontend counters and return it into temp integer.
2824 * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only.
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002825 */
2826static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002827smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002828{
Willy Tarreaube508f12016-03-10 11:47:01 +01002829 struct stkctr *stkctr;
2830
Willy Tarreaube508f12016-03-10 11:47:01 +01002831 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau710d38c2013-07-23 00:07:04 +02002832 if (!stkctr)
2833 return 0;
2834
Willy Tarreau37406352012-04-23 16:16:37 +02002835 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002836 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002837 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002838
2839 if (stkctr_entry(stkctr) == NULL)
2840 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2841
Thierry Fournier6fc340f2016-06-06 18:28:05 +02002842 if (stkctr && stkctr_entry(stkctr)) {
Emeric Brun57056f02015-06-15 18:29:57 +02002843 void *ptr1,*ptr2;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002844
2845 /* First, update gpc0_rate if it's tracked. Second, update its
2846 * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr.
2847 */
Emeric Brun57056f02015-06-15 18:29:57 +02002848 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE);
2849 if (ptr1) {
2850 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
Willy Tarreau710d38c2013-07-23 00:07:04 +02002851 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002852 smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr;
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002853 }
2854
Emeric Brun57056f02015-06-15 18:29:57 +02002855 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
2856 if (ptr2)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002857 smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0);
Willy Tarreauba2ffd12013-05-29 15:54:14 +02002858
Emeric Brun57056f02015-06-15 18:29:57 +02002859 /* If data was modified, we need to touch to re-schedule sync */
2860 if (ptr1 || ptr2)
2861 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauc3bd9722010-06-20 12:47:25 +02002862 }
2863 return 1;
2864}
2865
Willy Tarreau87b09662015-04-03 00:22:06 +02002866/* Clear the General Purpose Counter 0 value from the stream's tracked
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002867 * frontend counters and return its previous value into temp integer.
2868 * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only.
Willy Tarreauf73cd112011-08-13 01:45:16 +02002869 */
2870static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002871smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf73cd112011-08-13 01:45:16 +02002872{
Willy Tarreaube508f12016-03-10 11:47:01 +01002873 struct stkctr *stkctr;
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002874
Willy Tarreaube508f12016-03-10 11:47:01 +01002875 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaub9f441d2013-07-23 00:10:35 +02002876 if (!stkctr)
2877 return 0;
2878
Willy Tarreau37406352012-04-23 16:16:37 +02002879 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002880 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002881 smp->data.u.sint = 0;
Willy Tarreau0f4eadd2015-08-18 17:15:20 +02002882
2883 if (stkctr_entry(stkctr) == NULL)
2884 stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw);
2885
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002886 if (stkctr_entry(stkctr) != NULL) {
2887 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002888 if (!ptr)
2889 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002890 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002891 stktable_data_cast(ptr, gpc0) = 0;
Emeric Brun57056f02015-06-15 18:29:57 +02002892 /* If data was modified, we need to touch to re-schedule sync */
2893 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreauf73cd112011-08-13 01:45:16 +02002894 }
2895 return 1;
2896}
2897
Willy Tarreau87b09662015-04-03 00:22:06 +02002898/* set <smp> to the cumulated number of connections from the stream's tracked
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002899 * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or
2900 * "src_conn_cnt" only.
2901 */
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002902static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002903smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002904{
Willy Tarreaube508f12016-03-10 11:47:01 +01002905 struct stkctr *stkctr;
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002906
Willy Tarreaube508f12016-03-10 11:47:01 +01002907 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3b46c5c2013-07-23 00:22:50 +02002908 if (!stkctr)
2909 return 0;
2910
Willy Tarreau37406352012-04-23 16:16:37 +02002911 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002912 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002913 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002914 if (stkctr_entry(stkctr) != NULL) {
2915 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002916 if (!ptr)
2917 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002918 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002919 }
Willy Tarreau9a3f8492010-06-18 19:53:25 +02002920 return 1;
2921}
2922
Willy Tarreau87b09662015-04-03 00:22:06 +02002923/* set <smp> to the connection rate from the stream's tracked frontend
Willy Tarreauc8c65702013-07-23 15:09:35 +02002924 * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate"
2925 * only.
2926 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02002927static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002928smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02002929{
Willy Tarreaube508f12016-03-10 11:47:01 +01002930 struct stkctr *stkctr;
2931
Willy Tarreaube508f12016-03-10 11:47:01 +01002932 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauc8c65702013-07-23 15:09:35 +02002933 if (!stkctr)
2934 return 0;
2935
Willy Tarreau37406352012-04-23 16:16:37 +02002936 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002937 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002938 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01002939 if (stkctr_entry(stkctr) != NULL) {
2940 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002941 if (!ptr)
2942 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002943 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreauc8c65702013-07-23 15:09:35 +02002944 stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02002945 }
2946 return 1;
2947}
2948
Willy Tarreau87b09662015-04-03 00:22:06 +02002949/* set temp integer to the number of connections from the stream's source address
Willy Tarreau8b22a712010-06-18 17:46:06 +02002950 * in the table pointed to by expr, after updating it.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02002951 * Accepts exactly 1 argument of type table.
Willy Tarreau8b22a712010-06-18 17:46:06 +02002952 */
2953static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002954smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8b22a712010-06-18 17:46:06 +02002955{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002956 struct connection *conn = objt_conn(smp->sess->origin);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002957 struct stksess *ts;
2958 struct stktable_key *key;
2959 void *ptr;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02002960 struct proxy *px;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002961
Willy Tarreaub363a1f2013-10-01 10:45:07 +02002962 if (!conn)
2963 return 0;
2964
Thierry FOURNIER7e25df32015-07-24 19:31:59 +02002965 /* Fetch source adress in a sample. */
2966 if (!smp_fetch_src(NULL, smp, NULL, NULL))
2967 return 0;
2968
2969 /* Converts into key. */
2970 key = smp_to_stkey(smp, &args->data.prx->table);
Willy Tarreau8b22a712010-06-18 17:46:06 +02002971 if (!key)
David du Colombier4f92d322011-03-24 11:09:31 +01002972 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002973
Willy Tarreau24e32d82012-04-23 23:55:44 +02002974 px = args->data.prx;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002975
Willy Tarreau1f7e9252010-06-20 12:27:21 +02002976 if ((ts = stktable_update_key(&px->table, key)) == NULL)
2977 /* entry does not exist and could not be created */
2978 return 0;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002979
2980 ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT);
2981 if (!ptr)
2982 return 0; /* parameter not stored in this table */
2983
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02002984 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02002985 smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt);
Emeric Brun57056f02015-06-15 18:29:57 +02002986 /* Touch was previously performed by stktable_update_key */
Willy Tarreau37406352012-04-23 16:16:37 +02002987 smp->flags = SMP_F_VOL_TEST;
Willy Tarreau8b22a712010-06-18 17:46:06 +02002988 return 1;
2989}
2990
Willy Tarreau87b09662015-04-03 00:22:06 +02002991/* set <smp> to the number of concurrent connections from the stream's tracked
Willy Tarreauf44a5532013-07-23 15:17:53 +02002992 * frontend counters. Supports being called as "sc[0-9]_conn_cur" or
2993 * "src_conn_cur" only.
2994 */
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002995static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02002996smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02002997{
Willy Tarreaube508f12016-03-10 11:47:01 +01002998 struct stkctr *stkctr;
2999
Willy Tarreaube508f12016-03-10 11:47:01 +01003000 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreauf44a5532013-07-23 15:17:53 +02003001 if (!stkctr)
3002 return 0;
3003
Willy Tarreau37406352012-04-23 16:16:37 +02003004 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003005 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003006 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003007 if (stkctr_entry(stkctr) != NULL) {
3008 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003009 if (!ptr)
3010 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003011 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreau9b0ddcf2010-06-18 21:14:36 +02003012 }
3013 return 1;
3014}
3015
Willy Tarreau87b09662015-04-03 00:22:06 +02003016/* set <smp> to the cumulated number of streams from the stream's tracked
Willy Tarreau20843082013-07-23 15:35:33 +02003017 * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or
3018 * "src_sess_cnt" only.
3019 */
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003020static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003021smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003022{
Willy Tarreaube508f12016-03-10 11:47:01 +01003023 struct stkctr *stkctr;
3024
Willy Tarreaube508f12016-03-10 11:47:01 +01003025 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau20843082013-07-23 15:35:33 +02003026 if (!stkctr)
3027 return 0;
3028
Willy Tarreau37406352012-04-23 16:16:37 +02003029 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003030 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003031 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003032 if (stkctr_entry(stkctr) != NULL) {
3033 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003034 if (!ptr)
3035 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003036 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreauf4d17d92010-06-18 22:10:12 +02003037 }
3038 return 1;
3039}
3040
Willy Tarreau87b09662015-04-03 00:22:06 +02003041/* set <smp> to the stream rate from the stream's tracked frontend counters.
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003042 * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only.
3043 */
Willy Tarreau91c43d72010-06-20 11:19:22 +02003044static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003045smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau91c43d72010-06-20 11:19:22 +02003046{
Willy Tarreaube508f12016-03-10 11:47:01 +01003047 struct stkctr *stkctr;
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003048
Willy Tarreaube508f12016-03-10 11:47:01 +01003049 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003050 if (!stkctr)
3051 return 0;
3052
Willy Tarreau37406352012-04-23 16:16:37 +02003053 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003054 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003055 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003056 if (stkctr_entry(stkctr) != NULL) {
3057 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003058 if (!ptr)
3059 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003060 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau3a96f3f2013-07-23 15:48:01 +02003061 stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u);
Willy Tarreau91c43d72010-06-20 11:19:22 +02003062 }
3063 return 1;
3064}
3065
Willy Tarreau87b09662015-04-03 00:22:06 +02003066/* set <smp> to the cumulated number of HTTP requests from the stream's tracked
Willy Tarreau91200da2013-07-23 15:55:19 +02003067 * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or
3068 * "src_http_req_cnt" only.
3069 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003070static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003071smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003072{
Willy Tarreaube508f12016-03-10 11:47:01 +01003073 struct stkctr *stkctr;
Willy Tarreau91200da2013-07-23 15:55:19 +02003074
Willy Tarreaube508f12016-03-10 11:47:01 +01003075 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau91200da2013-07-23 15:55:19 +02003076 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_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +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, http_req_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003087 }
3088 return 1;
3089}
3090
Willy Tarreau87b09662015-04-03 00:22:06 +02003091/* set <smp> to the HTTP request rate from the stream's tracked frontend
Willy Tarreaucf477632013-07-23 16:04:37 +02003092 * counters. Supports being called as "sc[0-9]_http_req_rate" or
3093 * "src_http_req_rate" only.
3094 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003095static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003096smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003097{
Willy Tarreaube508f12016-03-10 11:47:01 +01003098 struct stkctr *stkctr;
3099
Willy Tarreaube508f12016-03-10 11:47:01 +01003100 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaucf477632013-07-23 16:04:37 +02003101 if (!stkctr)
3102 return 0;
3103
Willy Tarreau37406352012-04-23 16:16:37 +02003104 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003105 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003106 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003107 if (stkctr_entry(stkctr) != NULL) {
3108 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003109 if (!ptr)
3110 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003111 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaucf477632013-07-23 16:04:37 +02003112 stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003113 }
3114 return 1;
3115}
3116
Willy Tarreau87b09662015-04-03 00:22:06 +02003117/* set <smp> to the cumulated number of HTTP requests errors from the stream's
Willy Tarreau30d07c32013-07-23 16:45:38 +02003118 * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or
3119 * "src_http_err_cnt" only.
3120 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003121static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003122smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003123{
Willy Tarreaube508f12016-03-10 11:47:01 +01003124 struct stkctr *stkctr;
3125
Willy Tarreaube508f12016-03-10 11:47:01 +01003126 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau30d07c32013-07-23 16:45:38 +02003127 if (!stkctr)
3128 return 0;
3129
Willy Tarreau37406352012-04-23 16:16:37 +02003130 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003131 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003132 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003133 if (stkctr_entry(stkctr) != NULL) {
3134 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003135 if (!ptr)
3136 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003137 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003138 }
3139 return 1;
3140}
3141
Willy Tarreau87b09662015-04-03 00:22:06 +02003142/* set <smp> to the HTTP request error rate from the stream's tracked frontend
Willy Tarreau9daf2622013-07-23 16:48:54 +02003143 * counters. Supports being called as "sc[0-9]_http_err_rate" or
3144 * "src_http_err_rate" only.
3145 */
Willy Tarreauda7ff642010-06-23 11:44:09 +02003146static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003147smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauda7ff642010-06-23 11:44:09 +02003148{
Willy Tarreaube508f12016-03-10 11:47:01 +01003149 struct stkctr *stkctr;
3150
Willy Tarreaube508f12016-03-10 11:47:01 +01003151 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau9daf2622013-07-23 16:48:54 +02003152 if (!stkctr)
3153 return 0;
3154
Willy Tarreau37406352012-04-23 16:16:37 +02003155 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003156 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003157 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003158 if (stkctr_entry(stkctr) != NULL) {
3159 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003160 if (!ptr)
3161 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003162 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau9daf2622013-07-23 16:48:54 +02003163 stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
Willy Tarreauda7ff642010-06-23 11:44:09 +02003164 }
3165 return 1;
3166}
3167
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003168/* set <smp> to the number of kbytes received from clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003169 * stream's tracked frontend counters. Supports being called as
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003170 * "sc[0-9]_kbytes_in" or "src_kbytes_in" only.
3171 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003172static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003173smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003174{
Willy Tarreaube508f12016-03-10 11:47:01 +01003175 struct stkctr *stkctr;
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003176
Willy Tarreaube508f12016-03-10 11:47:01 +01003177 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau5077d4b2013-07-23 17:17:10 +02003178 if (!stkctr)
3179 return 0;
3180
Willy Tarreau37406352012-04-23 16:16:37 +02003181 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003182 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003183 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003184 if (stkctr_entry(stkctr) != NULL) {
3185 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003186 if (!ptr)
3187 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003188 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003189 }
3190 return 1;
3191}
3192
Willy Tarreau613fe992013-07-23 17:39:19 +02003193/* set <smp> to the data rate received from clients in bytes/s, as found
Willy Tarreau87b09662015-04-03 00:22:06 +02003194 * in the stream's tracked frontend counters. Supports being called as
Willy Tarreau613fe992013-07-23 17:39:19 +02003195 * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003196 */
3197static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003198smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003199{
Willy Tarreaube508f12016-03-10 11:47:01 +01003200 struct stkctr *stkctr;
Willy Tarreau613fe992013-07-23 17:39:19 +02003201
Willy Tarreaube508f12016-03-10 11:47:01 +01003202 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau613fe992013-07-23 17:39:19 +02003203 if (!stkctr)
3204 return 0;
3205
Willy Tarreau37406352012-04-23 16:16:37 +02003206 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003207 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003208 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003209 if (stkctr_entry(stkctr) != NULL) {
3210 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003211 if (!ptr)
3212 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003213 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau613fe992013-07-23 17:39:19 +02003214 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003215 }
3216 return 1;
3217}
3218
Willy Tarreau53aea102013-07-23 17:39:02 +02003219/* set <smp> to the number of kbytes sent to clients, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003220 * stream's tracked frontend counters. Supports being called as
Willy Tarreau53aea102013-07-23 17:39:02 +02003221 * "sc[0-9]_kbytes_out" or "src_kbytes_out" only.
3222 */
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003223static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003224smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau1aa006f2010-06-18 21:52:52 +02003225{
Willy Tarreaube508f12016-03-10 11:47:01 +01003226 struct stkctr *stkctr;
3227
Willy Tarreaube508f12016-03-10 11:47:01 +01003228 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau53aea102013-07-23 17:39:02 +02003229 if (!stkctr)
3230 return 0;
3231
Willy Tarreau37406352012-04-23 16:16:37 +02003232 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003233 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003234 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003235 if (stkctr_entry(stkctr) != NULL) {
3236 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003237 if (!ptr)
3238 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003239 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003240 }
Willy Tarreau855e4bb2010-06-18 18:33:32 +02003241 return 1;
3242}
3243
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003244/* set <smp> to the data rate sent to clients in bytes/s, as found in the
Willy Tarreau87b09662015-04-03 00:22:06 +02003245 * stream's tracked frontend counters. Supports being called as
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003246 * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only.
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003247 */
3248static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003249smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003250{
Willy Tarreaube508f12016-03-10 11:47:01 +01003251 struct stkctr *stkctr;
3252
Willy Tarreaube508f12016-03-10 11:47:01 +01003253 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003254 if (!stkctr)
3255 return 0;
3256
Willy Tarreau37406352012-04-23 16:16:37 +02003257 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003258 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003259 smp->data.u.sint = 0;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +01003260 if (stkctr_entry(stkctr) != NULL) {
3261 void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003262 if (!ptr)
3263 return 0; /* parameter not stored */
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003264 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaua0b68ed2013-07-23 18:26:32 +02003265 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +02003266 }
3267 return 1;
3268}
3269
Willy Tarreau87b09662015-04-03 00:22:06 +02003270/* set <smp> to the number of active trackers on the SC entry in the stream's
Willy Tarreau563eef42013-07-23 18:32:02 +02003271 * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only.
3272 */
Willy Tarreau2406db42012-12-09 12:16:43 +01003273static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003274smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau2406db42012-12-09 12:16:43 +01003275{
Willy Tarreaube508f12016-03-10 11:47:01 +01003276 struct stkctr *stkctr;
3277
Willy Tarreaube508f12016-03-10 11:47:01 +01003278 stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw);
Willy Tarreau563eef42013-07-23 18:32:02 +02003279 if (!stkctr)
Willy Tarreau2406db42012-12-09 12:16:43 +01003280 return 0;
3281
Willy Tarreau563eef42013-07-23 18:32:02 +02003282 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003283 smp->data.type = SMP_T_SINT;
Willy Tarreau4d03ef72016-08-14 12:02:55 +02003284 smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0;
Willy Tarreau563eef42013-07-23 18:32:02 +02003285 return 1;
Willy Tarreaue25c9172013-05-28 18:32:20 +02003286}
3287
Willy Tarreau34db1082012-04-19 17:16:54 +02003288/* set temp integer to the number of used entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003289 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003290 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003291static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003292smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003293{
Willy Tarreau37406352012-04-23 16:16:37 +02003294 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003295 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003296 smp->data.u.sint = args->data.prx->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003297 return 1;
3298}
3299
Willy Tarreau34db1082012-04-19 17:16:54 +02003300/* set temp integer to the number of free entries in the table pointed to by expr.
Willy Tarreau0146c2e2012-04-20 11:37:56 +02003301 * Accepts exactly 1 argument of type table.
Willy Tarreau34db1082012-04-19 17:16:54 +02003302 */
Willy Tarreauc735a072011-03-29 00:57:02 +02003303static int
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003304smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreauc735a072011-03-29 00:57:02 +02003305{
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003306 struct proxy *px;
3307
Willy Tarreau24e32d82012-04-23 23:55:44 +02003308 px = args->data.prx;
Willy Tarreau37406352012-04-23 16:16:37 +02003309 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003310 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02003311 smp->data.u.sint = px->table.size - px->table.current;
Willy Tarreauc735a072011-03-29 00:57:02 +02003312 return 1;
3313}
Willy Tarreau8b22a712010-06-18 17:46:06 +02003314
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003315/* 0=OK, <0=Alert, >0=Warning */
3316static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
3317 struct proxy *px, struct act_rule *rule,
3318 char **err)
3319{
3320 struct action_kw *kw;
3321
3322 /* Check if the service name exists. */
3323 if (*(args[*cur_arg]) == 0) {
3324 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01003325 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003326 }
3327
3328 /* lookup for keyword corresponding to a service. */
3329 kw = action_lookup(&service_keywords, args[*cur_arg]);
3330 if (!kw) {
3331 memprintf(err, "'%s' unknown service name.", args[1]);
3332 return ACT_RET_PRS_ERR;
3333 }
3334 (*cur_arg)++;
3335
3336 /* executes specific rule parser. */
3337 rule->kw = kw;
3338 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
3339 return ACT_RET_PRS_ERR;
3340
3341 /* Register processing function. */
3342 rule->action_ptr = process_use_service;
3343 rule->action = ACT_CUSTOM;
3344
3345 return ACT_RET_PRS_OK;
3346}
3347
3348void service_keywords_register(struct action_kw_list *kw_list)
3349{
3350 LIST_ADDQ(&service_keywords, &kw_list->list);
3351}
3352
3353/* main configuration keyword registration. */
3354static struct action_kw_list stream_tcp_keywords = { ILH, {
3355 { "use-service", stream_parse_use_service },
3356 { /* END */ }
3357}};
3358
3359static struct action_kw_list stream_http_keywords = { ILH, {
3360 { "use-service", stream_parse_use_service },
3361 { /* END */ }
3362}};
3363
Willy Tarreau61612d42012-04-19 18:42:05 +02003364/* Note: must not be declared <const> as its list will be overwritten.
3365 * Please take care of keeping this list alphabetically sorted.
3366 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003367static struct acl_kw_list acl_kws = {ILH, {
Willy Tarreau281c7992013-01-08 01:23:27 +01003368 { /* END */ },
Willy Tarreau8b22a712010-06-18 17:46:06 +02003369}};
3370
Willy Tarreau281c7992013-01-08 01:23:27 +01003371/* Note: must not be declared <const> as its list will be overwritten.
3372 * Please take care of keeping this list alphabetically sorted.
3373 */
Willy Tarreaudc13c112013-06-21 23:16:39 +02003374static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +02003375 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3376 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3377 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3378 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3379 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3380 { "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 +02003381 { "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 +02003382 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3383 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3384 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3385 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3386 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3387 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3388 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3389 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3390 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3391 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3392 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3393 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
3394 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003395 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3396 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3397 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3398 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3399 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3400 { "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 +02003401 { "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 +02003402 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3403 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3404 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3405 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3406 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3407 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3408 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3409 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3410 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3411 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3412 { "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 +02003413 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003414 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3415 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3416 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3417 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3418 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3419 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3420 { "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 +02003421 { "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 +02003422 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3423 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3424 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3425 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3426 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3427 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3428 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3429 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3430 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3431 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3432 { "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 +02003433 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003434 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3435 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3436 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3437 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3438 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3439 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3440 { "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 +02003441 { "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 +02003442 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3443 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3444 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3445 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3446 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3447 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3448 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3449 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3450 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3451 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3452 { "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 +02003453 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02003454 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3455 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3456 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3457 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3458 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3459 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3460 { "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 +02003461 { "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 +02003462 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3463 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3464 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3465 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3466 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3467 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3468 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3469 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3470 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3471 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3472 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3473 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
3474 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
3475 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
Willy Tarreau281c7992013-01-08 01:23:27 +01003476 { /* END */ },
3477}};
3478
Willy Tarreau8b22a712010-06-18 17:46:06 +02003479__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003480static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003481{
Willy Tarreau281c7992013-01-08 01:23:27 +01003482 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003483 acl_register_keywords(&acl_kws);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003484 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3485 http_req_keywords_register(&stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003486}
3487
Willy Tarreaubaaee002006-06-26 02:48:02 +02003488/*
3489 * Local variables:
3490 * c-indent-level: 8
3491 * c-basic-offset: 8
3492 * End:
3493 */