blob: 8527c297fc205a7c78ab5ce9930c373a7963d599 [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>
William Lallemand4c5b4d52016-11-21 08:51:11 +010025#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020026#include <types/filters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010028#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020030#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020031#include <proto/action.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020032#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010033#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020034#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010035#include <proto/checks.h>
William Lallemand4c5b4d52016-11-21 08:51:11 +010036#include <proto/cli.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020037#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010038#include <proto/stats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020039#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020040#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020041#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020042#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010043#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010044#include <proto/hlua.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020045#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020046#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020047#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020048#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020049#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010050#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010051#include <proto/proto_http.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020052#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020053#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010054#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020055#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010056#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010057#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010058#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010059#include <proto/tcp_rules.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020060#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020061
Willy Tarreau87b09662015-04-03 00:22:06 +020062struct pool_head *pool2_stream;
63struct list streams;
Willy Tarreaubaaee002006-06-26 02:48:02 +020064
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020065/* List of all use-service keywords. */
66static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
67
Willy Tarreau9903f0e2015-04-04 18:50:31 +020068/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +020069 * handshake, in order to complete initialization of a valid stream. It must be
Willy Tarreau87787ac2017-08-28 16:22:54 +020070 * called with a completley initialized session. It returns the pointer to
Willy Tarreau73b65ac2015-04-08 18:26:29 +020071 * the newly created stream, or NULL in case of fatal error. The client-facing
Willy Tarreau87787ac2017-08-28 16:22:54 +020072 * end point is assigned to <origin>, which must be valid. The stream's task
73 * is configured with a nice value inherited from the listener's nice if any.
74 * The task's context is set to the new stream, and its function is set to
75 * process_stream(). Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +020076 */
Willy Tarreau87787ac2017-08-28 16:22:54 +020077struct stream *stream_new(struct session *sess, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +020078{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020079 struct stream *s;
Willy Tarreau87787ac2017-08-28 16:22:54 +020080 struct task *t;
Willy Tarreau73b65ac2015-04-08 18:26:29 +020081 struct connection *conn = objt_conn(origin);
82 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +020083
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020084 if (unlikely((s = pool_alloc2(pool2_stream)) == NULL))
Willy Tarreau87787ac2017-08-28 16:22:54 +020085 goto out_fail_alloc;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020086
87 /* minimum stream initialization required for an embryonic stream is
88 * fairly low. We need very little to execute L4 ACLs, then we need a
89 * task to make the client-side connection live on its own.
90 * - flags
91 * - stick-entry tracking
92 */
93 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +020094 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +020095 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +020096 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
97 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +020098 /* This function is called just after the handshake, so the handshake duration is
99 * between the accept time and now.
100 */
101 s->logs.t_handshake = tv_ms_elapsed(&sess->tv_accept, &now);
102 s->logs.t_idle = -1;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200103 tv_zero(&s->logs.tv_request);
104 s->logs.t_queue = -1;
105 s->logs.t_connect = -1;
106 s->logs.t_data = -1;
107 s->logs.t_close = 0;
108 s->logs.bytes_in = s->logs.bytes_out = 0;
109 s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */
110 s->logs.srv_queue_size = 0; /* we will get this number soon */
111
112 /* default logging function */
113 s->do_log = strm_log;
114
115 /* default error reporting function, may be changed by analysers */
116 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200117
118 /* Initialise the current rule list pointer to NULL. We are sure that
119 * any rulelist match the NULL pointer.
120 */
121 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200122 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200123
Willy Tarreaua68f7622015-09-21 17:48:24 +0200124 /* Copy SC counters for the stream. We don't touch refcounts because
125 * any reference we have is inherited from the session. Since the stream
126 * doesn't exist without the session, the session's existence guarantees
127 * we don't lose the entry. During the store operation, the stream won't
128 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200129 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200130 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200131
132 s->sess = sess;
133 s->si[0].flags = SI_FL_NONE;
134 s->si[1].flags = SI_FL_ISBACK;
135
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200136 s->uniq_id = global.req_count++;
137
Willy Tarreau87b09662015-04-03 00:22:06 +0200138 /* OK, we're keeping the stream, so let's properly initialize the stream */
139 LIST_ADDQ(&streams, &s->list);
Willy Tarreau2bfd3582017-06-27 15:20:05 +0200140 LIST_ADDQ(&sess->streams, &s->by_sess);
Willy Tarreau2542b532012-08-31 16:01:23 +0200141 LIST_INIT(&s->back_refs);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100142
143 LIST_INIT(&s->buffer_wait.list);
144 s->buffer_wait.target = s;
145 s->buffer_wait.wakeup_cb = (int (*)(void *))stream_res_wakeup;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200146
Willy Tarreaue7dff022015-04-03 01:14:29 +0200147 s->flags |= SF_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200148 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200149
Willy Tarreau87787ac2017-08-28 16:22:54 +0200150 if ((t = task_new()) == NULL)
151 goto out_fail_alloc;
152
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200153 s->task = t;
Christopher Faulet9d810ca2016-12-08 22:33:52 +0100154 s->pending_events = 0;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200155 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200156 t->context = s;
157 t->expire = TICK_ETERNITY;
Willy Tarreau87787ac2017-08-28 16:22:54 +0200158 if (sess->listener)
159 t->nice = sess->listener->nice;
Willy Tarreau2542b532012-08-31 16:01:23 +0200160
Willy Tarreau87b09662015-04-03 00:22:06 +0200161 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200162 * This changes later when switching rules are executed or
163 * when the default backend is assigned.
164 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200165 s->be = sess->fe;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100166 s->req.buf = s->res.buf = NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200167 s->req_cap = NULL;
168 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200169
Willy Tarreauebcd4842015-06-19 11:59:02 +0200170 /* Initialise all the variables contexts even if not used.
171 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200172 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200173 vars_init(&s->vars_txn, SCOPE_TXN);
174 vars_init(&s->vars_reqres, SCOPE_REQ);
175
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200176 /* this part should be common with other protocols */
Willy Tarreau819d3322014-11-28 12:12:34 +0100177 si_reset(&s->si[0]);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200178 si_set_state(&s->si[0], SI_ST_EST);
Hongbo Longe39683c2017-03-10 18:41:51 +0100179 s->si[0].hcto = sess->fe->timeout.clientfin;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200180
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200181 /* attach the incoming connection to the stream interface now. */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200182 if (conn)
183 si_attach_conn(&s->si[0], conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200184 else if (appctx)
185 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200186
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200187 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200188 s->si[0].flags |= SI_FL_INDEP_STR;
189
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200190 /* pre-initialize the other side's stream interface to an INIT state. The
191 * callbacks will be initialized before attempting to connect.
192 */
Willy Tarreau819d3322014-11-28 12:12:34 +0100193 si_reset(&s->si[1]);
Hongbo Longe39683c2017-03-10 18:41:51 +0100194 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200195
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200196 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200197 s->si[1].flags |= SI_FL_INDEP_STR;
198
Willy Tarreau87b09662015-04-03 00:22:06 +0200199 stream_init_srv_conn(s);
Willy Tarreau9b82d942016-12-05 00:26:31 +0100200 s->target = sess->listener ? sess->listener->default_target : NULL;
201
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200202 s->pend_pos = NULL;
203
204 /* init store persistence */
205 s->store_count = 0;
206
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100207 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100208 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau9b82d942016-12-05 00:26:31 +0100209 s->req.analysers = sess->listener ? sess->listener->analysers : 0;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200210 channel_auto_connect(&s->req); /* don't wait to establish connection */
211 channel_auto_close(&s->req); /* let the producer forward close requests */
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200212
213 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100214 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100215 s->req.rex = TICK_ETERNITY;
216 s->req.wex = TICK_ETERNITY;
217 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200218
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100219 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100220 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100221 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200222
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200223 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100224 s->req.flags |= CF_NEVER_WAIT;
225 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200226 }
227
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200228 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100229 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100230 s->res.rex = TICK_ETERNITY;
231 s->res.wex = TICK_ETERNITY;
232 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200233
Willy Tarreaueee5b512015-04-03 23:46:31 +0200234 s->txn = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100235 s->hlua = NULL;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100236
Christopher Faulet92d36382015-11-05 13:35:03 +0100237 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200238 goto out_fail_accept;
239
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200240 /* finish initialization of the accepted file descriptor */
Willy Tarreauf2b98742015-04-05 01:30:42 +0200241 if (conn)
242 conn_data_want_recv(conn);
Willy Tarreau470280e2015-04-05 01:33:13 +0200243 else if (appctx)
Willy Tarreaufe127932015-04-21 19:23:39 +0200244 si_applet_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200245
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200246 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200247 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200248
249 /* it is important not to call the wakeup function directly but to
250 * pass through task_wakeup(), because this one knows how to apply
Emeric Brun5f77fef2017-05-29 15:26:51 +0200251 * priorities to tasks. Using multi thread we must be sure that
252 * stream is fully initialized before calling task_wakeup. So
253 * the caller must handle the task_wakeup
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200254 */
Willy Tarreau02d86382015-04-05 12:00:52 +0200255 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200256
257 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200258 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100259 flt_stream_release(s, 0);
Willy Tarreau87787ac2017-08-28 16:22:54 +0200260 task_free(t);
261 out_fail_alloc:
Willy Tarreau2bfd3582017-06-27 15:20:05 +0200262 LIST_DEL(&s->by_sess);
Willy Tarreau3b246412014-11-25 17:10:33 +0100263 LIST_DEL(&s->list);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200264 pool_free2(pool2_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200265 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200266}
267
Willy Tarreaubaaee002006-06-26 02:48:02 +0200268/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200269 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200270 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200271static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200272{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200273 struct session *sess = strm_sess(s);
274 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100275 struct bref *bref, *back;
Willy Tarreau76325482017-08-17 15:54:46 +0200276 struct connection *cli_conn = objt_conn(s->si[0].end);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200277 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100278
Willy Tarreaubaaee002006-06-26 02:48:02 +0200279 if (s->pend_pos)
280 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100281
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100282 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200283 if (s->flags & SF_CURR_SESS) {
284 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100285 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100286 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100287 if (may_dequeue_tasks(objt_server(s->target), s->be))
288 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100289 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100290
Willy Tarreau7c669d72008-06-20 15:04:11 +0200291 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200292 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200293 * it should normally be only the same as the one above,
294 * so this should not happen in fact.
295 */
296 sess_change_server(s, NULL);
297 }
298
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100299 if (s->req.pipe)
300 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100301
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100302 if (s->res.pipe)
303 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100304
Willy Tarreaubf883e02014-11-25 21:10:35 +0100305 /* We may still be present in the buffer wait queue */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100306 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
307 LIST_DEL(&s->buffer_wait.list);
308 LIST_INIT(&s->buffer_wait.list);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100309 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100310 if (s->req.buf->size || s->res.buf->size) {
311 b_drop(&s->req.buf);
312 b_drop(&s->res.buf);
313 offer_buffers(NULL, tasks_run_queue + applets_active_queue);
314 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200315
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100316 hlua_ctx_destroy(s->hlua);
317 s->hlua = NULL;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200318 if (s->txn)
319 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100320
Willy Tarreau1e954912012-10-12 17:50:05 +0200321 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200322 if (cli_conn)
323 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200324
Willy Tarreaua4cda672010-06-06 18:28:49 +0200325 for (i = 0; i < s->store_count; i++) {
326 if (!s->store[i].ts)
327 continue;
328 stksess_free(s->store[i].table, s->store[i].ts);
329 s->store[i].ts = NULL;
330 }
331
Willy Tarreaueee5b512015-04-03 23:46:31 +0200332 if (s->txn) {
333 pool_free2(pool2_hdr_idx, s->txn->hdr_idx.v);
334 pool_free2(pool2_http_txn, s->txn);
335 s->txn = NULL;
336 }
337
Christopher Fauletd7c91962015-04-30 11:48:27 +0200338 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100339 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200340
Willy Tarreau92fb9832007-10-16 17:34:28 +0200341 if (fe) {
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200342 pool_free2(fe->rsp_cap_pool, s->res_cap);
343 pool_free2(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100345
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200346 /* Cleanup all variable contexts. */
Willy Tarreau6204cd92016-03-10 16:33:04 +0100347 vars_prune(&s->vars_txn, s->sess, s);
348 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200349
Willy Tarreau87b09662015-04-03 00:22:06 +0200350 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200351
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100352 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100353 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200354 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100355 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100356 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100357 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200358 if (s->list.n != &streams)
359 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100360 bref->ref = s->list.n;
361 }
Willy Tarreau2bfd3582017-06-27 15:20:05 +0200362 LIST_DEL(&s->by_sess);
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100363 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200364 si_release_endpoint(&s->si[1]);
365 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200366
367 /* FIXME: for now we have a 1:1 relation between stream and session so
368 * the stream must free the session.
369 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200370 pool_free2(pool2_stream, s);
Willy Tarreau11c36242015-04-04 15:54:03 +0200371 session_free(sess);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200372
373 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200374 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200375 pool_flush2(pool2_buffer);
Willy Tarreau63986c72015-04-03 22:55:33 +0200376 pool_flush2(pool2_http_txn);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200377 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200378 pool_flush2(pool2_requri);
379 pool_flush2(pool2_capture);
Willy Tarreau87b09662015-04-03 00:22:06 +0200380 pool_flush2(pool2_stream);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200381 pool_flush2(pool2_session);
Willy Tarreau3a5e0602014-11-13 16:46:28 +0100382 pool_flush2(pool2_connection);
383 pool_flush2(pool2_pendconn);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200384 pool_flush2(fe->req_cap_pool);
385 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200386 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200387}
388
Willy Tarreau656859d2014-11-25 19:46:36 +0100389
Willy Tarreau87b09662015-04-03 00:22:06 +0200390/* Allocates a work buffer for stream <s>. It is meant to be called inside
391 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200392 * to work fine, which is the response buffer so that an error message may be
393 * built and returned. Response buffers may be allocated from the reserve, this
394 * is critical to ensure that a response may always flow and will never block a
395 * server from releasing a connection. Returns 0 in case of failure, non-zero
396 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100397 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100398static int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100399{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100400 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
401 LIST_DEL(&s->buffer_wait.list);
402 LIST_INIT(&s->buffer_wait.list);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100403 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100404
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200405 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100406 return 1;
407
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100408 LIST_ADDQ(&buffer_wq, &s->buffer_wait.list);
Willy Tarreau656859d2014-11-25 19:46:36 +0100409 return 0;
410}
411
412/* releases unused buffers after processing. Typically used at the end of the
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100413 * update() functions. It will try to wake up as many tasks/applets as the
414 * number of buffers that it releases. In practice, most often streams are
415 * blocked on a single buffer, so it makes sense to try to wake two up when two
416 * buffers are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100417 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200418void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100419{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100420 int offer = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100421
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100422 if (s->req.buf->size && buffer_empty(s->req.buf)) {
423 offer = 1;
424 b_free(&s->req.buf);
425 }
426 if (s->res.buf->size && buffer_empty(s->res.buf)) {
427 offer = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100428 b_free(&s->res.buf);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100429 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100430
Willy Tarreaubf883e02014-11-25 21:10:35 +0100431 /* if we're certain to have at least 1 buffer available, and there is
432 * someone waiting, we can wake up a waiter and offer them.
433 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100434 if (offer)
435 offer_buffers(s, tasks_run_queue + applets_active_queue);
Willy Tarreau656859d2014-11-25 19:46:36 +0100436}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200437
438/* perform minimal intializations, report 0 in case of error, 1 if OK. */
Willy Tarreau87b09662015-04-03 00:22:06 +0200439int init_stream()
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200440{
Willy Tarreau87b09662015-04-03 00:22:06 +0200441 LIST_INIT(&streams);
442 pool2_stream = create_pool("stream", sizeof(struct stream), MEM_F_SHARED);
443 return pool2_stream != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200444}
445
Willy Tarreau87b09662015-04-03 00:22:06 +0200446void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100447{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200448 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100449 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200450 void *ptr1,*ptr2;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100451 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100452
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100453 bytes = s->req.total - s->logs.bytes_in;
454 s->logs.bytes_in = s->req.total;
455 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200456 sess->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100457
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100458 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100459
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100460 if (objt_server(s->target))
461 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200462
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200463 if (sess->listener && sess->listener->counters)
464 sess->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200465
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100466 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200467 struct stkctr *stkctr = &s->stkctr[i];
468
469 if (!stkctr_entry(stkctr)) {
470 stkctr = &sess->stkctr[i];
471 if (!stkctr_entry(stkctr))
472 continue;
473 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200474
Emeric Brun57056f02015-06-15 18:29:57 +0200475 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT);
476 if (ptr1)
477 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200478
Emeric Brun57056f02015-06-15 18:29:57 +0200479 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE);
480 if (ptr2)
481 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200482 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200483
484 /* If data was modified, we need to touch to re-schedule sync */
485 if (ptr1 || ptr2)
486 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100487 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100488 }
489
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100490 bytes = s->res.total - s->logs.bytes_out;
491 s->logs.bytes_out = s->res.total;
492 if (bytes) {
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200493 sess->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100494
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100495 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100496
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100497 if (objt_server(s->target))
498 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200499
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200500 if (sess->listener && sess->listener->counters)
501 sess->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200502
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100503 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200504 struct stkctr *stkctr = &s->stkctr[i];
505
506 if (!stkctr_entry(stkctr)) {
507 stkctr = &sess->stkctr[i];
508 if (!stkctr_entry(stkctr))
509 continue;
510 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200511
Emeric Brun57056f02015-06-15 18:29:57 +0200512 ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT);
513 if (ptr1)
514 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200515
Emeric Brun57056f02015-06-15 18:29:57 +0200516 ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE);
517 if (ptr2)
518 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200519 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Emeric Brun57056f02015-06-15 18:29:57 +0200520
521 /* If data was modified, we need to touch to re-schedule sync */
522 if (ptr1 || ptr2)
523 stktable_touch(stkctr->table, stkctr_entry(stkctr), 1);
Willy Tarreau30e71012007-11-26 20:15:35 +0100524 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100525 }
526}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200527
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100528/* This function is called with (si->state == SI_ST_CON) meaning that a
529 * connection was attempted and that the file descriptor is already allocated.
530 * We must check for establishment, error and abort. Possible output states
531 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
532 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200533 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100534 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200535static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100536{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100537 struct stream_interface *si = &s->si[1];
538 struct channel *req = &s->req;
539 struct channel *rep = &s->res;
Willy Tarreau350135c2016-11-17 12:05:13 +0100540 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100541
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100542 /* If we got an error, or if nothing happened and the connection timed
543 * out, we must give up. The CER state handler will take care of retry
544 * attempts and error reports.
545 */
546 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreau103197d2014-11-28 15:26:12 +0100547 if (unlikely(req->flags & CF_WRITE_PARTIAL)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100548 /* Some data were sent past the connection establishment,
549 * so we need to pretend we're established to log correctly
550 * and let later states handle the failure.
551 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100552 si->state = SI_ST_EST;
553 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100554 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100555 return 1;
556 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100557 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100558 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100559
Willy Tarreau350135c2016-11-17 12:05:13 +0100560 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100561
562 if (si->err_type)
563 return 0;
564
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100565 if (si->flags & SI_FL_ERR)
566 si->err_type = SI_ET_CONN_ERR;
567 else
568 si->err_type = SI_ET_CONN_TO;
569 return 0;
570 }
571
572 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100573 if (!(req->flags & CF_WRITE_PARTIAL) &&
574 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200575 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
576 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100577 s->be->options & PR_O_ABRT_CLOSE)))) {
578 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200579 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100580 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100581 if (s->srv_error)
582 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100583 return 1;
584 }
585
586 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200587 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100588 return 1;
589
590 /* OK, this means that a connection succeeded. The caller will be
591 * responsible for handling the transition from CON to EST.
592 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100593 si->state = SI_ST_EST;
594 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100595 return 1;
596}
597
598/* This function is called with (si->state == SI_ST_CER) meaning that a
599 * previous connection attempt has failed and that the file descriptor
600 * has already been released. Possible causes include asynchronous error
601 * notification and time out. Possible output states are SI_ST_CLO when
602 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
603 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
604 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
605 * marked as in error state. It returns 0.
606 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200607static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100608{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100609 struct stream_interface *si = &s->si[1];
Willy Tarreau46d5b082017-07-26 20:13:37 +0200610 struct connection *conn = objt_conn(si->end);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100611
Willy Tarreau87b09662015-04-03 00:22:06 +0200612 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100613 if (objt_server(s->target)) {
614 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100615
Willy Tarreaue7dff022015-04-03 01:14:29 +0200616 if (s->flags & SF_CURR_SESS) {
617 s->flags &= ~SF_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100618 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100619 }
Willy Tarreau46d5b082017-07-26 20:13:37 +0200620
621 if ((si->flags & SI_FL_ERR) &&
622 conn && conn->err_code == CO_ER_SSL_MISMATCH_SNI) {
623 /* We tried to connect to a server which is configured
624 * with "verify required" and which doesn't have the
625 * "verifyhost" directive. The server presented a wrong
626 * certificate (a certificate for an unexpected name),
627 * which implies that we have used SNI in the handshake,
628 * and that the server doesn't have the associated cert
629 * and presented a default one.
630 *
631 * This is a serious enough issue not to retry. It's
632 * especially important because this wrong name might
633 * either be the result of a configuration error, and
634 * retrying will only hammer the server, or is caused
635 * by the use of a wrong SNI value, most likely
636 * provided by the client and we don't want to let the
637 * client provoke retries.
638 */
639 si->conn_retries = 0;
640 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100641 }
642
643 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200644 si->conn_retries--;
645 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100646 if (!si->err_type) {
647 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100648 }
649
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100650 if (objt_server(s->target))
651 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100652 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100653 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100654 if (may_dequeue_tasks(objt_server(s->target), s->be))
655 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100656
657 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200658 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100659 s->req.flags |= CF_WRITE_ERROR;
660 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100661
662 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100663 if (s->srv_error)
664 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100665 return 0;
666 }
667
668 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700669 * retry on another server. By default this redispatch occurs on the
670 * last retry, but if configured we allow redispatches to occur on
671 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200672 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100673 * bit to ignore any persistence cookie. We won't count a retry nor a
674 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200675 * If the connection is not persistent, the balancing algorithm is not
676 * determinist (round robin) and there is more than one active server,
677 * we accept to perform an immediate redispatch without waiting since
678 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200680 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700681 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Willy Tarreau49008c12016-01-13 07:58:44 +0100682 ((__objt_server(s->target)->state < SRV_ST_RUNNING) ||
683 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700684 ((s->be->conn_retries - si->conn_retries) %
685 s->be->redispatch_after == 0)) ||
686 ((s->be->redispatch_after < 0) &&
687 ((s->be->conn_retries - si->conn_retries) %
688 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200689 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700690 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100691 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100692 if (may_dequeue_tasks(objt_server(s->target), s->be))
693 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694
Willy Tarreaue7dff022015-04-03 01:14:29 +0200695 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100696 si->state = SI_ST_REQ;
697 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100698 if (objt_server(s->target))
699 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100700 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100701 si->state = SI_ST_ASS;
702 }
703
704 if (si->flags & SI_FL_ERR) {
705 /* The error was an asynchronous connection error, and we will
706 * likely have to retry connecting to the same server, most
707 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200708 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100709 */
710
Willy Tarreaub0290662014-06-13 17:04:44 +0200711 int delay = 1000;
712
713 if (s->be->timeout.connect && s->be->timeout.connect < delay)
714 delay = s->be->timeout.connect;
715
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100716 if (!si->err_type)
717 si->err_type = SI_ET_CONN_ERR;
718
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200719 /* only wait when we're retrying on the same server */
720 if (si->state == SI_ST_ASS ||
721 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
722 (s->be->srv_act <= 1)) {
723 si->state = SI_ST_TAR;
724 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
725 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100726 return 0;
727 }
728 return 0;
729}
730
731/*
732 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200733 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200734 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200736static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100737{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100738 struct stream_interface *si = &s->si[1];
739 struct channel *req = &s->req;
740 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100741
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100742 /* First, centralize the timers information */
743 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
744 si->exp = TICK_ETERNITY;
745
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100746 if (objt_server(s->target))
747 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100748
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100749 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100750 /* if the user wants to log as soon as possible, without counting
751 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200752 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100753 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100754 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100755 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100756 }
757 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100758 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100759 }
760
Willy Tarreaud62b98c2016-12-13 15:26:56 +0100761 if (!(s->flags & SF_TUNNEL)) {
762 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100763
Willy Tarreaud62b98c2016-12-13 15:26:56 +0100764 /* Be sure to filter response headers if the backend is an HTTP proxy
765 * and if there are filters attached to the stream. */
766 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
767 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
768 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100769
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;
Willy Tarreau85cb0ae2017-08-23 10:52:20 +02001091
1092 /* enable the minimally required analyzers in case of HTTP
1093 * keep-alive to properly handle keep-alive and compression
1094 * on the HTTP response.
1095 */
1096 if (rule->from == ACT_F_HTTP_REQ) {
1097 s->req.analysers &= AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END;
1098 s->req.analysers |= AN_REQ_HTTP_XFER_BODY;
1099 }
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001100 }
1101 else
1102 appctx = si_appctx(&s->si[1]);
1103
1104 /* Stops the applet sheduling, in case of the init function miss
1105 * some data.
1106 */
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001107 si_applet_stop_get(&s->si[1]);
1108
1109 /* Call initialisation. */
1110 if (rule->applet.init)
1111 switch (rule->applet.init(appctx, px, s)) {
1112 case 0: return ACT_RET_ERR;
1113 case 1: break;
1114 default: return ACT_RET_YIELD;
1115 }
1116
1117 /* Now we can schedule the applet. */
1118 si_applet_cant_get(&s->si[1]);
1119 appctx_wakeup(appctx);
1120
1121 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
1122 sess->fe->fe_counters.intercepted_req++;
1123
1124 /* The flag SF_ASSIGNED prevent from server assignment. */
1125 s->flags |= SF_ASSIGNED;
1126
1127 return ACT_RET_STOP;
1128}
1129
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001130/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001131 * if appropriate. The default_backend rule is also considered, then the
1132 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001133 * It returns 1 if the processing can continue on next analysers, or zero if it
1134 * either needs more data or wants to immediately abort the request.
1135 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001136static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001137{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001138 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001139 struct session *sess = s->sess;
1140 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001141
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001142 req->analysers &= ~an_bit;
1143 req->analyse_exp = TICK_ETERNITY;
1144
Willy Tarreau87b09662015-04-03 00:22:06 +02001145 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 +02001146 now_ms, __FUNCTION__,
1147 s,
1148 req,
1149 req->rex, req->wex,
1150 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001151 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001152 req->analysers);
1153
1154 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001155 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001156 struct switching_rule *rule;
1157
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001158 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001159 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001160
Willy Tarreauf51658d2014-04-23 01:21:56 +02001161 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001162 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001163 ret = acl_pass(ret);
1164 if (rule->cond->pol == ACL_COND_UNLESS)
1165 ret = !ret;
1166 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001167
1168 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001169 /* If the backend name is dynamic, try to resolve the name.
1170 * If we can't resolve the name, or if any error occurs, break
1171 * the loop and fallback to the default backend.
1172 */
1173 struct proxy *backend;
1174
1175 if (rule->dynamic) {
1176 struct chunk *tmp = get_trash_chunk();
1177 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1178 break;
Willy Tarreau9e0bb102015-05-26 11:24:42 +02001179 backend = proxy_be_by_name(tmp->str);
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001180 if (!backend)
1181 break;
1182 }
1183 else
1184 backend = rule->be.backend;
1185
Willy Tarreau87b09662015-04-03 00:22:06 +02001186 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001187 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001188 break;
1189 }
1190 }
1191
1192 /* To ensure correct connection accounting on the backend, we
1193 * have to assign one if it was not set (eg: a listen). This
1194 * measure also takes care of correctly setting the default
1195 * backend if any.
1196 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001197 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001198 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001199 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001200 }
1201
Willy Tarreaufb356202010-08-03 14:02:05 +02001202 /* 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 +02001203 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001204 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1205 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001206 s->req.analysers &= ~AN_REQ_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001207 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001208
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001209 /* 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 +02001210 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001211 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001212 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001213 int ret = 1;
1214
1215 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001216 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 +01001217 ret = acl_pass(ret);
1218 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1219 ret = !ret;
1220 }
1221
1222 if (ret) {
1223 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001224 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001225 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001226 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001227 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001228 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001229 break;
1230 }
1231 }
1232
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001233 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001234
1235 sw_failed:
1236 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001237 channel_abort(&s->req);
1238 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001239
Willy Tarreaue7dff022015-04-03 01:14:29 +02001240 if (!(s->flags & SF_ERR_MASK))
1241 s->flags |= SF_ERR_RESOURCE;
1242 if (!(s->flags & SF_FINST_MASK))
1243 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001244
Willy Tarreaueee5b512015-04-03 23:46:31 +02001245 if (s->txn)
1246 s->txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001247 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001248 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001249 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001250}
1251
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001252/* This stream analyser works on a request. It applies all use-server rules on
1253 * it then returns 1. The data must already be present in the buffer otherwise
1254 * they won't match. It always returns 1.
1255 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001256static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001257{
1258 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001259 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001260 struct server_rule *rule;
1261
Willy Tarreau87b09662015-04-03 00:22:06 +02001262 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 +02001263 now_ms, __FUNCTION__,
1264 s,
1265 req,
1266 req->rex, req->wex,
1267 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001268 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001269 req->analysers);
1270
Willy Tarreaue7dff022015-04-03 01:14:29 +02001271 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001272 list_for_each_entry(rule, &px->server_rules, list) {
1273 int ret;
1274
Willy Tarreau192252e2015-04-04 01:47:55 +02001275 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001276 ret = acl_pass(ret);
1277 if (rule->cond->pol == ACL_COND_UNLESS)
1278 ret = !ret;
1279
1280 if (ret) {
1281 struct server *srv = rule->srv.ptr;
1282
Willy Tarreau892337c2014-05-13 23:41:20 +02001283 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001284 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001285 (s->flags & SF_FORCE_PRST)) {
1286 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001287 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001288 break;
1289 }
1290 /* if the server is not UP, let's go on with next rules
1291 * just in case another one is suited.
1292 */
1293 }
1294 }
1295 }
1296
1297 req->analysers &= ~an_bit;
1298 req->analyse_exp = TICK_ETERNITY;
1299 return 1;
1300}
1301
Emeric Brun1d33b292010-01-04 15:47:17 +01001302/* This stream analyser works on a request. It applies all sticking rules on
1303 * it then returns 1. The data must already be present in the buffer otherwise
1304 * they won't match. It always returns 1.
1305 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001306static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001307{
1308 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001309 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001310 struct sticking_rule *rule;
1311
Willy Tarreau87b09662015-04-03 00:22:06 +02001312 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 +01001313 now_ms, __FUNCTION__,
1314 s,
1315 req,
1316 req->rex, req->wex,
1317 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001318 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001319 req->analysers);
1320
1321 list_for_each_entry(rule, &px->sticking_rules, list) {
1322 int ret = 1 ;
1323 int i;
1324
Willy Tarreau9667a802013-12-09 12:52:13 +01001325 /* Only the first stick store-request of each table is applied
1326 * and other ones are ignored. The purpose is to allow complex
1327 * configurations which look for multiple entries by decreasing
1328 * order of precision and to stop at the first which matches.
1329 * An example could be a store of the IP address from an HTTP
1330 * header first, then from the source if not found.
1331 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001332 for (i = 0; i < s->store_count; i++) {
1333 if (rule->table.t == s->store[i].table)
1334 break;
1335 }
1336
1337 if (i != s->store_count)
1338 continue;
1339
1340 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001341 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001342 ret = acl_pass(ret);
1343 if (rule->cond->pol == ACL_COND_UNLESS)
1344 ret = !ret;
1345 }
1346
1347 if (ret) {
1348 struct stktable_key *key;
1349
Willy Tarreau192252e2015-04-04 01:47:55 +02001350 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 +01001351 if (!key)
1352 continue;
1353
1354 if (rule->flags & STK_IS_MATCH) {
1355 struct stksess *ts;
1356
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001357 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001358 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001359 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001360 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001361
1362 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001363 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1364 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001365 if (node) {
1366 struct server *srv;
1367
1368 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001369 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001370 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001371 (s->flags & SF_FORCE_PRST)) {
1372 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001373 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001374 }
1375 }
1376 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001377 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001378 }
1379 }
1380 if (rule->flags & STK_IS_STORE) {
1381 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1382 struct stksess *ts;
1383
1384 ts = stksess_new(rule->table.t, key);
1385 if (ts) {
1386 s->store[s->store_count].table = rule->table.t;
1387 s->store[s->store_count++].ts = ts;
1388 }
1389 }
1390 }
1391 }
1392 }
1393
1394 req->analysers &= ~an_bit;
1395 req->analyse_exp = TICK_ETERNITY;
1396 return 1;
1397}
1398
1399/* This stream analyser works on a response. It applies all store rules on it
1400 * then returns 1. The data must already be present in the buffer otherwise
1401 * they won't match. It always returns 1.
1402 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001403static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001404{
1405 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001406 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001407 struct sticking_rule *rule;
1408 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001409 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001410
Willy Tarreau87b09662015-04-03 00:22:06 +02001411 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 +01001412 now_ms, __FUNCTION__,
1413 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001414 rep,
1415 rep->rex, rep->wex,
1416 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001417 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001418 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001419
1420 list_for_each_entry(rule, &px->storersp_rules, list) {
1421 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001422
Willy Tarreau9667a802013-12-09 12:52:13 +01001423 /* Only the first stick store-response of each table is applied
1424 * and other ones are ignored. The purpose is to allow complex
1425 * configurations which look for multiple entries by decreasing
1426 * order of precision and to stop at the first which matches.
1427 * An example could be a store of a set-cookie value, with a
1428 * fallback to a parameter found in a 302 redirect.
1429 *
1430 * The store-response rules are not allowed to override the
1431 * store-request rules for the same table, but they may coexist.
1432 * Thus we can have up to one store-request entry and one store-
1433 * response entry for the same table at any time.
1434 */
1435 for (i = nbreq; i < s->store_count; i++) {
1436 if (rule->table.t == s->store[i].table)
1437 break;
1438 }
1439
1440 /* skip existing entries for this table */
1441 if (i < s->store_count)
1442 continue;
1443
Emeric Brun1d33b292010-01-04 15:47:17 +01001444 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001445 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001446 ret = acl_pass(ret);
1447 if (rule->cond->pol == ACL_COND_UNLESS)
1448 ret = !ret;
1449 }
1450
1451 if (ret) {
1452 struct stktable_key *key;
1453
Willy Tarreau192252e2015-04-04 01:47:55 +02001454 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 +01001455 if (!key)
1456 continue;
1457
Willy Tarreau37e340c2013-12-06 23:05:21 +01001458 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001459 struct stksess *ts;
1460
1461 ts = stksess_new(rule->table.t, key);
1462 if (ts) {
1463 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001464 s->store[s->store_count++].ts = ts;
1465 }
1466 }
1467 }
1468 }
1469
1470 /* process store request and store response */
1471 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001472 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001473 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001474
Willy Tarreauc93cd162014-05-13 15:54:22 +02001475 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001476 stksess_free(s->store[i].table, s->store[i].ts);
1477 s->store[i].ts = NULL;
1478 continue;
1479 }
1480
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001481 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1482 if (ts) {
1483 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001484 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001485 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001486 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001487 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001488 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001489
1490 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001491 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001492 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001493 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001494 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001495
1496 rep->analysers &= ~an_bit;
1497 rep->analyse_exp = TICK_ETERNITY;
1498 return 1;
1499}
1500
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001501/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001502 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001503 */
1504#define UPDATE_ANALYSERS(real, list, back, flag) { \
1505 list = (((list) & ~(flag)) | ~(back)) & (real); \
1506 back = real; \
1507 if (!(list)) \
1508 break; \
1509 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1510 continue; \
1511}
1512
Christopher Fauleta9215b72016-05-11 17:06:28 +02001513/* These 2 following macros call an analayzer for the specified channel if the
1514 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001515 * stream has some registered filters, pre and post analyaze callbacks are
Christopher Faulet0184ea72017-01-05 14:06:34 +01001516 * called. The second are used for other analyzers (AN_REQ/RES_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001517 * AN_REQ/RES_HTTP_XFER_BODY) */
1518#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1519 { \
1520 if ((list) & (flag)) { \
1521 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001522 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001523 break; \
1524 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1525 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001526 if (!flt_post_analyze((strm), (chn), (flag))) \
1527 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001528 } \
1529 else { \
1530 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1531 break; \
1532 } \
1533 UPDATE_ANALYSERS((chn)->analysers, (list), \
1534 (back), (flag)); \
1535 } \
1536 }
1537
1538#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1539 { \
1540 if ((list) & (flag)) { \
1541 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1542 break; \
1543 UPDATE_ANALYSERS((chn)->analysers, (list), \
1544 (back), (flag)); \
1545 } \
1546 }
1547
Willy Tarreau87b09662015-04-03 00:22:06 +02001548/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001549 * then puts it back to the wait queue in a clean state, or cleans up its
1550 * resources if it must be deleted. Returns in <next> the date the task wants
1551 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1552 * nothing too many times, the request and response buffers flags are monitored
1553 * and each function is called only if at least another function has changed at
1554 * least one flag it is interested in.
1555 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001556struct task *process_stream(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001557{
Willy Tarreau827aee92011-03-10 16:55:02 +01001558 struct server *srv;
Willy Tarreau87b09662015-04-03 00:22:06 +02001559 struct stream *s = t->context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001560 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001561 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001562 unsigned int rq_prod_last, rq_cons_last;
1563 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001564 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001565 struct channel *req, *res;
1566 struct stream_interface *si_f, *si_b;
1567
1568 req = &s->req;
1569 res = &s->res;
1570
1571 si_f = &s->si[0];
1572 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001573
1574 //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 +01001575 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001576
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001577 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001578 if (s->txn)
1579 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001580
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001581 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001582 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1583 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001584
1585 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001586 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1587 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001588
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001589 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001590 si_f->flags |= SI_FL_DONT_WAKE;
1591 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001592
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001593 /* update pending events */
1594 s->pending_events |= (t->state & TASK_WOKEN_ANY);
1595
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001596 /* 1a: Check for low level timeouts if needed. We just set a flag on
1597 * stream interfaces when their timeouts have expired.
1598 */
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001599 if (unlikely(s->pending_events & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001600 stream_int_check_timeouts(si_f);
1601 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001602
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001603 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001604 * for future reads or writes. Note: this will also concern upper layers
1605 * but we do not touch any other flag. We must be careful and correctly
1606 * detect state changes when calling them.
1607 */
1608
Willy Tarreau8f128b42014-11-28 15:07:47 +01001609 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001610
Willy Tarreau8f128b42014-11-28 15:07:47 +01001611 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1612 si_b->flags |= SI_FL_NOLINGER;
1613 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001614 }
1615
Willy Tarreau8f128b42014-11-28 15:07:47 +01001616 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1617 if (si_f->flags & SI_FL_NOHALF)
1618 si_f->flags |= SI_FL_NOLINGER;
1619 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001620 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001621
Willy Tarreau8f128b42014-11-28 15:07:47 +01001622 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001623
Willy Tarreau8f128b42014-11-28 15:07:47 +01001624 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1625 si_f->flags |= SI_FL_NOLINGER;
1626 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001627 }
1628
Willy Tarreau8f128b42014-11-28 15:07:47 +01001629 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1630 if (si_b->flags & SI_FL_NOHALF)
1631 si_b->flags |= SI_FL_NOLINGER;
1632 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001633 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001634
Christopher Fauleta00d8172016-11-10 14:58:05 +01001635 if (HAS_FILTERS(s))
1636 flt_stream_check_timeouts(s);
1637
Willy Tarreau798f4322012-11-08 14:49:17 +01001638 /* Once in a while we're woken up because the task expires. But
1639 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001640 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001641 * timeout needs to be refreshed.
1642 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001643 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001644 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1645 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001646 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001647 ((s->pending_events & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
Willy Tarreau5fb04712016-05-04 10:18:37 +02001648 si_f->flags &= ~SI_FL_DONT_WAKE;
1649 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001650 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02001651 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001652 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001653
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001654 /* below we may emit error messages so we have to ensure that we have
1655 * our buffers properly allocated.
1656 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001657 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001658 /* No buffer available, we've been subscribed to the list of
1659 * buffer waiters, let's wait for our turn.
1660 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02001661 si_f->flags &= ~SI_FL_DONT_WAKE;
1662 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001663 goto update_exp_and_leave;
1664 }
1665
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001666 /* 1b: check for low-level errors reported at the stream interface.
1667 * First we check if it's a retryable error (in which case we don't
1668 * want to tell the buffer). Otherwise we report the error one level
1669 * upper by setting flags into the buffers. Note that the side towards
1670 * the client cannot have connect (hence retryable) errors. Also, the
1671 * connection setup code must be able to deal with any type of abort.
1672 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001673 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001674 if (unlikely(si_f->flags & SI_FL_ERR)) {
1675 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1676 si_shutr(si_f);
1677 si_shutw(si_f);
1678 stream_int_report_error(si_f);
1679 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001680 s->be->be_counters.cli_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001681 sess->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001682 if (srv)
1683 srv->counters.cli_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001684 if (!(s->flags & SF_ERR_MASK))
1685 s->flags |= SF_ERR_CLICL;
1686 if (!(s->flags & SF_FINST_MASK))
1687 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001688 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001689 }
1690 }
1691
Willy Tarreau8f128b42014-11-28 15:07:47 +01001692 if (unlikely(si_b->flags & SI_FL_ERR)) {
1693 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1694 si_shutr(si_b);
1695 si_shutw(si_b);
1696 stream_int_report_error(si_b);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001697 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001698 if (srv)
1699 srv->counters.failed_resp++;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001700 if (!(req->analysers) && !(res->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001701 s->be->be_counters.srv_aborts++;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001702 sess->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001703 if (srv)
1704 srv->counters.srv_aborts++;
Willy Tarreaue7dff022015-04-03 01:14:29 +02001705 if (!(s->flags & SF_ERR_MASK))
1706 s->flags |= SF_ERR_SRVCL;
1707 if (!(s->flags & SF_FINST_MASK))
1708 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001709 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001710 }
1711 /* note: maybe we should process connection errors here ? */
1712 }
1713
Willy Tarreau8f128b42014-11-28 15:07:47 +01001714 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001715 /* we were trying to establish a connection on the server side,
1716 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1717 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001718 if (unlikely(!sess_update_st_con_tcp(s)))
1719 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001720 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001721 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001722
1723 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1724 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1725 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1726 */
1727 }
1728
Willy Tarreau8f128b42014-11-28 15:07:47 +01001729 rq_prod_last = si_f->state;
1730 rq_cons_last = si_b->state;
1731 rp_cons_last = si_f->state;
1732 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001733
1734 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001735 /* Check for connection closure */
1736
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001737 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001738 "[%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 +01001739 now_ms, __FUNCTION__, __LINE__,
1740 t,
1741 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001742 req, res,
1743 req->rex, res->wex,
1744 req->flags, res->flags,
1745 req->buf->i, req->buf->o, res->buf->i, res->buf->o, si_f->state, si_b->state,
1746 si_f->err_type, si_b->err_type,
1747 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001748
1749 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001750 if (unlikely(si_f->state == SI_ST_DIS))
1751 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001752
1753 /* When a server-side connection is released, we have to count it and
1754 * check for pending connections on this server.
1755 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001756 if (unlikely(si_b->state == SI_ST_DIS)) {
1757 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001758 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001759 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001760 if (s->flags & SF_CURR_SESS) {
1761 s->flags &= ~SF_CURR_SESS;
Willy Tarreau827aee92011-03-10 16:55:02 +01001762 srv->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001763 }
1764 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001765 if (may_dequeue_tasks(srv, s->be))
1766 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001767 }
1768 }
1769
1770 /*
1771 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1772 * at this point.
1773 */
1774
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001775 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001776 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001777 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1778 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
1779 si_f->state != rq_prod_last ||
1780 si_b->state != rq_cons_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001781 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001782 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001783
Willy Tarreau8f128b42014-11-28 15:07:47 +01001784 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001785 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001786 unsigned int ana_list;
1787 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001788
Willy Tarreau90deb182010-01-07 00:20:41 +01001789 /* it's up to the analysers to stop new connections,
1790 * disable reading or closing. Note: if an analyser
1791 * disables any of these bits, it is responsible for
1792 * enabling them again when it disables itself, so
1793 * that other analysers are called in similar conditions.
1794 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001795 channel_auto_read(req);
1796 channel_auto_connect(req);
1797 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001798
1799 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001800 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001801 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001802 * the list when not needed. Any analyser may return 0
1803 * to break out of the loop, either because of missing
1804 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001805 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001806 * analyser, and we may loop again if other analysers
1807 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001808 *
1809 * We build a list of analysers to run. We evaluate all
1810 * of these analysers in the order of the lower bit to
1811 * the higher bit. This ordering is very important.
1812 * An analyser will often add/remove other analysers,
1813 * including itself. Any changes to itself have no effect
1814 * on the loop. If it removes any other analysers, we
1815 * want those analysers not to be called anymore during
1816 * this loop. If it adds an analyser that is located
1817 * after itself, we want it to be scheduled for being
1818 * processed during the loop. If it adds an analyser
1819 * which is located before it, we want it to switch to
1820 * it immediately, even if it has already been called
1821 * once but removed since.
1822 *
1823 * In order to achieve this, we compare the analyser
1824 * list after the call with a copy of it before the
1825 * call. The work list is fed with analyser bits that
1826 * appeared during the call. Then we compare previous
1827 * work list with the new one, and check the bits that
1828 * appeared. If the lowest of these bits is lower than
1829 * the current bit, it means we have enabled a previous
1830 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001831 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001832
Willy Tarreau8f128b42014-11-28 15:07:47 +01001833 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001834 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001835 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01001836 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_FE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001837 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
1838 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
1839 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
1840 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
1841 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001842 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001843 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
1844 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
1845 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
1846 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
1847 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
1848 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
1849 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001850 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_REQ_FLT_HTTP_HDRS);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001851 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001852 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_REQ_FLT_XFER_DATA);
1853 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_REQ_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001854 break;
1855 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001856 }
Willy Tarreau84455332009-03-15 22:34:05 +01001857
Willy Tarreau8f128b42014-11-28 15:07:47 +01001858 rq_prod_last = si_f->state;
1859 rq_cons_last = si_b->state;
1860 req->flags &= ~CF_WAKE_ONCE;
1861 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001862
Willy Tarreau8f128b42014-11-28 15:07:47 +01001863 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001864 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001865 }
1866
Willy Tarreau576507f2010-01-07 00:09:04 +01001867 /* we'll monitor the request analysers while parsing the response,
1868 * because some response analysers may indirectly enable new request
1869 * analysers (eg: HTTP keep-alive).
1870 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001871 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01001872
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001873 resync_response:
1874 /* Analyse response */
1875
Willy Tarreau8f128b42014-11-28 15:07:47 +01001876 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
1877 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
1878 si_f->state != rp_cons_last ||
1879 si_b->state != rp_prod_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001880 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001881 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001882
Willy Tarreau8f128b42014-11-28 15:07:47 +01001883 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001884 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001885 unsigned int ana_list;
1886 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001887
Willy Tarreau90deb182010-01-07 00:20:41 +01001888 /* it's up to the analysers to stop disable reading or
1889 * closing. Note: if an analyser disables any of these
1890 * bits, it is responsible for enabling them again when
1891 * it disables itself, so that other analysers are called
1892 * in similar conditions.
1893 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001894 channel_auto_read(res);
1895 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001896
1897 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001898 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001899 * to MSB. The analysers must remove themselves from
1900 * the list when not needed. Any analyser may return 0
1901 * to break out of the loop, either because of missing
1902 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001903 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02001904 * analyser, and we may loop again if other analysers
1905 * are added in the middle.
1906 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001907
Willy Tarreau8f128b42014-11-28 15:07:47 +01001908 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001909 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001910 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01001911 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_FE);
1912 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001913 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
1914 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
1915 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
1916 FLT_ANALYZE(s, res, http_process_res_common, ana_list, ana_back, AN_RES_HTTP_PROCESS_BE, s->be);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001917 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_RES_FLT_HTTP_HDRS);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001918 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001919 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_RES_FLT_XFER_DATA);
1920 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_RES_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001921 break;
1922 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001923 }
1924
Willy Tarreau8f128b42014-11-28 15:07:47 +01001925 rp_cons_last = si_f->state;
1926 rp_prod_last = si_b->state;
Christopher Fauletcdaea892017-07-06 15:49:30 +02001927 res->flags &= ~CF_WAKE_ONCE;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001928 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 Tarreau8615c2a2013-06-21 08:20:19 +02002096 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002097
2098 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002099 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2100 !req->analysers))
2101 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002102
2103 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002104 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2105 if (si_f->flags & SI_FL_NOHALF)
2106 si_f->flags |= SI_FL_NOLINGER;
2107 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002108 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002109
Willy Tarreau520d95e2009-09-19 21:04:57 +02002110 /* it's possible that an upper layer has requested a connection setup or abort.
2111 * There are 2 situations where we decide to establish a new connection :
2112 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002113 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002114 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002115 if (si_b->state == SI_ST_INI) {
2116 if (!(req->flags & CF_SHUTW)) {
2117 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002118 /* If we have an appctx, there is no connect method, so we
2119 * immediately switch to the connected state, otherwise we
2120 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002121 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002122 si_b->state = SI_ST_REQ; /* new connection requested */
2123 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002124 }
Willy Tarreau73201222009-08-16 18:27:24 +02002125 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002126 else {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002127 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2128 channel_shutw_now(req); /* fix buffer flags upon abort */
2129 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002130 }
Willy Tarreau92795622009-03-06 12:51:23 +01002131 }
2132
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002133
2134 /* we may have a pending connection request, or a connection waiting
2135 * for completion.
2136 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002137 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002138
2139 /* prune the request variables and swap to the response variables. */
2140 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreau6204cd92016-03-10 16:33:04 +01002141 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002142 vars_init(&s->vars_reqres, SCOPE_RES);
2143 }
2144
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002145 do {
2146 /* nb: step 1 might switch from QUE to ASS, but we first want
2147 * to give a chance to step 2 to perform a redirect if needed.
2148 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002149 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002150 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002151 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002152 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002153
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002154 /* applets directly go to the ESTABLISHED state. Similarly,
2155 * servers experience the same fate when their connection
2156 * is reused.
2157 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002158 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002159 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002160
Willy Tarreaub44c8732013-12-31 23:16:50 +01002161 /* Now we can add the server name to a header (if requested) */
2162 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002163 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002164 (s->be->server_id_hdr_name != NULL) &&
2165 (s->be->mode == PR_MODE_HTTP) &&
2166 objt_server(s->target)) {
Willy Tarreaueee5b512015-04-03 23:46:31 +02002167 http_send_name_header(s->txn, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002168 }
2169
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002170 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002171 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002172 http_perform_server_redirect(s, si_b);
2173 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002174 }
2175
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002176 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002177 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002178 goto resync_stream_interface;
2179
Willy Tarreau815a9b22010-07-27 17:15:12 +02002180 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002181 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002182 goto resync_request;
2183
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002184 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002185
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002186 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002187 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002188 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002189 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002190 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002191 if (unlikely(!res->analysers &&
2192 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2193 (si_b->state >= SI_ST_EST) &&
2194 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002195 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002196 * attached to it. If any data are left in, we'll permit them to
2197 * move.
2198 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002199 channel_auto_read(res);
2200 channel_auto_close(res);
2201 buffer_flush(res->buf);
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002202
2203 /* We'll let data flow between the producer (if still connected)
2204 * to the consumer.
2205 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002206 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002207 channel_forward_forever(res);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002208
Willy Tarreau42529c32015-07-09 18:38:57 +02002209 /* Just in order to support fetching HTTP contents after start
2210 * of forwarding when the HTTP forwarding analyser is not used,
2211 * we simply reset msg->sov so that HTTP rewinding points to the
2212 * headers.
2213 */
2214 if (s->txn)
2215 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - res->buf->o;
2216
Willy Tarreauce887fd2012-05-12 12:50:00 +02002217 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002218 * tunnel timeout set, use it now. Note that we must respect
2219 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002220 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002221 if (!req->analysers && s->be->timeout.tunnel) {
2222 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002223 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002224
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002225 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2226 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002227 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2228 res->rto = s->be->timeout.serverfin;
2229 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2230 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002231 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2232 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002233
Willy Tarreau8f128b42014-11-28 15:07:47 +01002234 req->rex = tick_add(now_ms, req->rto);
2235 req->wex = tick_add(now_ms, req->wto);
2236 res->rex = tick_add(now_ms, res->rto);
2237 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002238 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002239 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002240
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002241 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002242 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2243 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002244 (global.tune.options & GTUNE_USE_SPLICE) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002245 (objt_conn(si_f->end) && __objt_conn(si_f->end)->xprt && __objt_conn(si_f->end)->xprt->snd_pipe) &&
2246 (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 +01002247 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002248 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2249 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002250 (res->flags & CF_STREAMER_FAST)))) {
2251 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002252 }
2253
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002254 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002255 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002256
2257 /*
2258 * Now forward all shutdown requests between both sides of the buffer
2259 */
2260
2261 /*
2262 * FIXME: this is probably where we should produce error responses.
2263 */
2264
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002265 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002266 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002267 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002268 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002269 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002270
2271 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002272 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2273 channel_is_empty(res))) {
2274 si_shutw(si_f);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002275 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002276
2277 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002278 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2279 !res->analysers)
2280 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002281
2282 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2284 if (si_b->flags & SI_FL_NOHALF)
2285 si_b->flags |= SI_FL_NOLINGER;
2286 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002287 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002288
Willy Tarreau8f128b42014-11-28 15:07:47 +01002289 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002290 goto resync_stream_interface;
2291
Willy Tarreau8f128b42014-11-28 15:07:47 +01002292 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002293 goto resync_request;
2294
Willy Tarreau8f128b42014-11-28 15:07:47 +01002295 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002296 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002297
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002298 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002299 si_f->flags &= ~SI_FL_DONT_WAKE;
2300 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002301
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002302 /* This is needed only when debugging is enabled, to indicate
2303 * client-side or server-side close. Please note that in the unlikely
2304 * event where both sides would close at once, the sequence is reported
2305 * on the server side first.
2306 */
2307 if (unlikely((global.mode & MODE_DEBUG) &&
2308 (!(global.mode & MODE_QUIET) ||
2309 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002310 if (si_b->state == SI_ST_CLO &&
2311 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002312 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002313 s->uniq_id, s->be->id,
Willy Tarreau585744b2017-08-24 14:31:19 +02002314 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->handle.fd : -1,
2315 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->handle.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002316 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002317 }
2318
Willy Tarreau8f128b42014-11-28 15:07:47 +01002319 if (si_f->state == SI_ST_CLO &&
2320 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002321 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002322 s->uniq_id, s->be->id,
Willy Tarreau585744b2017-08-24 14:31:19 +02002323 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->handle.fd : -1,
2324 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->handle.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002325 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002326 }
2327 }
2328
Willy Tarreau8f128b42014-11-28 15:07:47 +01002329 if (likely((si_f->state != SI_ST_CLO) ||
2330 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002331
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002332 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002333 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002334
Willy Tarreau563cc372015-04-19 18:13:56 +02002335 if (si_f->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002336 si_update(si_f);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002337
Willy Tarreau563cc372015-04-19 18:13:56 +02002338 if (si_b->state == SI_ST_EST)
Willy Tarreau8f128b42014-11-28 15:07:47 +01002339 si_update(si_b);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002340
Willy Tarreau8f128b42014-11-28 15:07:47 +01002341 req->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2342 res->flags &= ~(CF_READ_NULL|CF_READ_PARTIAL|CF_WRITE_NULL|CF_WRITE_PARTIAL|CF_READ_ATTACHED);
2343 si_f->prev_state = si_f->state;
2344 si_b->prev_state = si_b->state;
2345 si_f->flags &= ~(SI_FL_ERR|SI_FL_EXP);
2346 si_b->flags &= ~(SI_FL_ERR|SI_FL_EXP);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002347
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002348 /* Trick: if a request is being waiting for the server to respond,
2349 * and if we know the server can timeout, we don't want the timeout
2350 * to expire on the client side first, but we're still interested
2351 * in passing data from the client to the server (eg: POST). Thus,
2352 * we can cancel the client's request timeout if the server's
2353 * request timeout is set and the server has not yet sent a response.
2354 */
2355
Willy Tarreau8f128b42014-11-28 15:07:47 +01002356 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2357 (tick_isset(req->wex) || tick_isset(res->rex))) {
2358 req->flags |= CF_READ_NOEXP;
2359 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002360 }
2361
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002362 /* Reset pending events now */
2363 s->pending_events = 0;
2364
Willy Tarreau798f4322012-11-08 14:49:17 +01002365 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002366 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Christopher Fauleta00d8172016-11-10 14:58:05 +01002367 t->expire = tick_first((tick_is_expired(t->expire, now_ms) ? 0 : t->expire),
2368 tick_first(tick_first(req->rex, req->wex),
2369 tick_first(res->rex, res->wex)));
Willy Tarreaudef0d222016-11-08 22:03:00 +01002370 if (!req->analysers)
2371 req->analyse_exp = TICK_ETERNITY;
2372
2373 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) &&
2374 (!tick_isset(req->analyse_exp) || tick_is_expired(req->analyse_exp, now_ms)))
2375 req->analyse_exp = tick_add(now_ms, 5000);
2376
2377 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002378
Willy Tarreau8f128b42014-11-28 15:07:47 +01002379 if (si_f->exp)
2380 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002381
Willy Tarreau8f128b42014-11-28 15:07:47 +01002382 if (si_b->exp)
2383 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002384
2385#ifdef DEBUG_FULL
Willy Tarreau127334e2009-03-28 10:47:26 +01002386 fprintf(stderr,
2387 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2388 " 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 +01002389 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2390 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002391#endif
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002392
2393#ifdef DEBUG_DEV
2394 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002395 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002396 ABORT_NOW();
2397#endif
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002398 s->pending_events &= ~(TASK_WOKEN_TIMER | TASK_WOKEN_RES);
Willy Tarreau87b09662015-04-03 00:22:06 +02002399 stream_release_buffers(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002400 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002401 }
2402
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002403 sess->fe->feconn--;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002404 if (s->flags & SF_BE_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002405 s->be->beconn--;
Willy Tarreauaf7ad002010-08-31 15:39:26 +02002406 jobs--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002407 if (sess->listener) {
2408 if (!(sess->listener->options & LI_O_UNLIMITED))
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002409 actconn--;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02002410 sess->listener->nbconn--;
2411 if (sess->listener->state == LI_FULL)
2412 resume_listener(sess->listener);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002413
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002414 /* Dequeues all of the listeners waiting for a resource */
2415 if (!LIST_ISEMPTY(&global_listener_queue))
2416 dequeue_all_listeners(&global_listener_queue);
Willy Tarreau08ceb102011-07-24 22:58:00 +02002417
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002418 if (!LIST_ISEMPTY(&sess->fe->listener_queue) &&
2419 (!sess->fe->fe_sps_lim || freq_ctr_remain(&sess->fe->fe_sess_per_sec, sess->fe->fe_sps_lim, 0) > 0))
2420 dequeue_all_listeners(&sess->fe->listener_queue);
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +01002421 }
Willy Tarreau07687c12011-07-24 23:55:06 +02002422
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002423 if (unlikely((global.mode & MODE_DEBUG) &&
2424 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002425 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002426 s->uniq_id, s->be->id,
Willy Tarreau585744b2017-08-24 14:31:19 +02002427 objt_conn(si_f->end) ? (unsigned short)objt_conn(si_f->end)->handle.fd : -1,
2428 objt_conn(si_b->end) ? (unsigned short)objt_conn(si_b->end)->handle.fd : -1);
Willy Tarreau89efaed2013-12-13 15:14:55 +01002429 shut_your_big_mouth_gcc(write(1, trash.str, trash.len));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002430 }
2431
2432 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002433 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002434
Willy Tarreaueee5b512015-04-03 23:46:31 +02002435 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002436 int n;
2437
Willy Tarreaueee5b512015-04-03 23:46:31 +02002438 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002439 if (n < 1 || n > 5)
2440 n = 0;
2441
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002442 if (sess->fe->mode == PR_MODE_HTTP) {
2443 sess->fe->fe_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002444 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002445 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002446 (s->be->mode == PR_MODE_HTTP)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01002447 s->be->be_counters.p.http.rsp[n]++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002448 s->be->be_counters.p.http.cum_req++;
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002449 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002450 }
2451
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002452 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002453 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002454 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002455 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002456 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002457 }
2458
Willy Tarreau87b09662015-04-03 00:22:06 +02002459 /* update time stats for this stream */
2460 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002461
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002462 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002463 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002464 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002465 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002466 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002467}
2468
Willy Tarreau87b09662015-04-03 00:22:06 +02002469/* Update the stream's backend and server time stats */
2470void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002471{
2472 int t_request;
2473 int t_queue;
2474 int t_connect;
2475 int t_data;
2476 int t_close;
2477 struct server *srv;
2478
2479 t_request = 0;
2480 t_queue = s->logs.t_queue;
2481 t_connect = s->logs.t_connect;
2482 t_close = s->logs.t_close;
2483 t_data = s->logs.t_data;
2484
2485 if (s->be->mode != PR_MODE_HTTP)
2486 t_data = t_connect;
2487
2488 if (t_connect < 0 || t_data < 0)
2489 return;
2490
2491 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2492 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2493
2494 t_data -= t_connect;
2495 t_connect -= t_queue;
2496 t_queue -= t_request;
2497
2498 srv = objt_server(s->target);
2499 if (srv) {
2500 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2501 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2502 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2503 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2504 }
2505 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2506 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2507 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2508 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
2509}
2510
Willy Tarreau7c669d72008-06-20 15:04:11 +02002511/*
2512 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002513 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002514 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002515 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002516 * server.
2517 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002518void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002519{
2520 if (sess->srv_conn == newsrv)
2521 return;
2522
2523 if (sess->srv_conn) {
2524 sess->srv_conn->served--;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002525 sess->srv_conn->proxy->served--;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002526 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2527 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002528 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002529 }
2530
2531 if (newsrv) {
2532 newsrv->served++;
Andrew Rodlande168feb2016-10-25 12:48:17 -04002533 newsrv->proxy->served++;
Willy Tarreau7c669d72008-06-20 15:04:11 +02002534 if (newsrv->proxy->lbprm.server_take_conn)
2535 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002536 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002537 }
2538}
2539
Willy Tarreau84455332009-03-15 22:34:05 +01002540/* Handle server-side errors for default protocols. It is called whenever a a
2541 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002542 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002543 * them. It's installed as ->srv_error for the server-side stream_interface.
2544 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002545void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002546{
2547 int err_type = si->err_type;
2548 int err = 0, fin = 0;
2549
2550 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002551 err = SF_ERR_CLICL;
2552 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002553 }
2554 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002555 err = SF_ERR_CLICL;
2556 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002557 }
2558 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002559 err = SF_ERR_SRVTO;
2560 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002561 }
2562 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002563 err = SF_ERR_SRVCL;
2564 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002565 }
2566 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002567 err = SF_ERR_SRVTO;
2568 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002569 }
2570 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002571 err = SF_ERR_SRVCL;
2572 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002573 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002574 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002575 err = SF_ERR_RESOURCE;
2576 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002577 }
Willy Tarreau84455332009-03-15 22:34:05 +01002578 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002579 err = SF_ERR_INTERNAL;
2580 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002581 }
2582
Willy Tarreaue7dff022015-04-03 01:14:29 +02002583 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002584 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002585 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002586 s->flags |= fin;
2587}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002588
Willy Tarreaue7dff022015-04-03 01:14:29 +02002589/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002590void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002591{
Willy Tarreau87b09662015-04-03 00:22:06 +02002592 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002593 return;
2594
Willy Tarreau87b09662015-04-03 00:22:06 +02002595 channel_shutw_now(&stream->req);
2596 channel_shutr_now(&stream->res);
2597 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002598 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002599 stream->flags |= why;
2600 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002601}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002602
Willy Tarreau8b22a712010-06-18 17:46:06 +02002603/************************************************************************/
2604/* All supported ACL keywords must be declared here. */
2605/************************************************************************/
2606
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02002607/* 0=OK, <0=Alert, >0=Warning */
2608static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
2609 struct proxy *px, struct act_rule *rule,
2610 char **err)
2611{
2612 struct action_kw *kw;
2613
2614 /* Check if the service name exists. */
2615 if (*(args[*cur_arg]) == 0) {
2616 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01002617 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02002618 }
2619
2620 /* lookup for keyword corresponding to a service. */
2621 kw = action_lookup(&service_keywords, args[*cur_arg]);
2622 if (!kw) {
2623 memprintf(err, "'%s' unknown service name.", args[1]);
2624 return ACT_RET_PRS_ERR;
2625 }
2626 (*cur_arg)++;
2627
2628 /* executes specific rule parser. */
2629 rule->kw = kw;
2630 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
2631 return ACT_RET_PRS_ERR;
2632
2633 /* Register processing function. */
2634 rule->action_ptr = process_use_service;
2635 rule->action = ACT_CUSTOM;
2636
2637 return ACT_RET_PRS_OK;
2638}
2639
2640void service_keywords_register(struct action_kw_list *kw_list)
2641{
2642 LIST_ADDQ(&service_keywords, &kw_list->list);
2643}
2644
William Lallemand4c5b4d52016-11-21 08:51:11 +01002645
2646/* This function dumps a complete stream state onto the stream interface's
2647 * read buffer. The stream has to be set in strm. It returns 0 if the output
2648 * buffer is full and it needs to be called again, otherwise non-zero. It is
2649 * designed to be called from stats_dump_strm_to_buffer() below.
2650 */
2651static int stats_dump_full_strm_to_buffer(struct stream_interface *si, struct stream *strm)
2652{
2653 struct appctx *appctx = __objt_appctx(si->end);
2654 struct tm tm;
2655 extern const char *monthname[12];
2656 char pn[INET6_ADDRSTRLEN];
2657 struct connection *conn;
2658 struct appctx *tmpctx;
2659
2660 chunk_reset(&trash);
2661
2662 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != strm->uniq_id) {
2663 /* stream changed, no need to go any further */
2664 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
2665 if (bi_putchk(si_ic(si), &trash) == -1) {
2666 si_applet_cant_put(si);
2667 return 0;
2668 }
2669 appctx->ctx.sess.uid = 0;
2670 appctx->ctx.sess.section = 0;
2671 return 1;
2672 }
2673
2674 switch (appctx->ctx.sess.section) {
2675 case 0: /* main status of the stream */
2676 appctx->ctx.sess.uid = strm->uniq_id;
2677 appctx->ctx.sess.section = 1;
2678 /* fall through */
2679
2680 case 1:
2681 get_localtime(strm->logs.accept_date.tv_sec, &tm);
2682 chunk_appendf(&trash,
2683 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
2684 strm,
2685 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2686 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(strm->logs.accept_date.tv_usec),
2687 strm->uniq_id,
2688 strm_li(strm) ? strm_li(strm)->proto->name : "?");
2689
2690 conn = objt_conn(strm_orig(strm));
2691 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2692 case AF_INET:
2693 case AF_INET6:
2694 chunk_appendf(&trash, " source=%s:%d\n",
2695 pn, get_host_port(&conn->addr.from));
2696 break;
2697 case AF_UNIX:
2698 chunk_appendf(&trash, " source=unix:%d\n", strm_li(strm)->luid);
2699 break;
2700 default:
2701 /* no more information to print right now */
2702 chunk_appendf(&trash, "\n");
2703 break;
2704 }
2705
2706 chunk_appendf(&trash,
2707 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
2708 strm->flags, strm->si[1].conn_retries, strm->srv_conn, strm->pend_pos);
2709
2710 chunk_appendf(&trash,
2711 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
2712 strm_fe(strm)->id, strm_fe(strm)->uuid, strm_fe(strm)->mode ? "http" : "tcp",
2713 strm_li(strm) ? strm_li(strm)->name ? strm_li(strm)->name : "?" : "?",
2714 strm_li(strm) ? strm_li(strm)->luid : 0);
2715
2716 if (conn)
2717 conn_get_to_addr(conn);
2718
2719 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
2720 case AF_INET:
2721 case AF_INET6:
2722 chunk_appendf(&trash, " addr=%s:%d\n",
2723 pn, get_host_port(&conn->addr.to));
2724 break;
2725 case AF_UNIX:
2726 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(strm)->luid);
2727 break;
2728 default:
2729 /* no more information to print right now */
2730 chunk_appendf(&trash, "\n");
2731 break;
2732 }
2733
2734 if (strm->be->cap & PR_CAP_BE)
2735 chunk_appendf(&trash,
2736 " backend=%s (id=%u mode=%s)",
2737 strm->be->id,
2738 strm->be->uuid, strm->be->mode ? "http" : "tcp");
2739 else
2740 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
2741
2742 conn = objt_conn(strm->si[1].end);
2743 if (conn)
2744 conn_get_from_addr(conn);
2745
2746 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2747 case AF_INET:
2748 case AF_INET6:
2749 chunk_appendf(&trash, " addr=%s:%d\n",
2750 pn, get_host_port(&conn->addr.from));
2751 break;
2752 case AF_UNIX:
2753 chunk_appendf(&trash, " addr=unix\n");
2754 break;
2755 default:
2756 /* no more information to print right now */
2757 chunk_appendf(&trash, "\n");
2758 break;
2759 }
2760
2761 if (strm->be->cap & PR_CAP_BE)
2762 chunk_appendf(&trash,
2763 " server=%s (id=%u)",
2764 objt_server(strm->target) ? objt_server(strm->target)->id : "<none>",
2765 objt_server(strm->target) ? objt_server(strm->target)->puid : 0);
2766 else
2767 chunk_appendf(&trash, " server=<NONE> (id=-1)");
2768
2769 if (conn)
2770 conn_get_to_addr(conn);
2771
2772 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
2773 case AF_INET:
2774 case AF_INET6:
2775 chunk_appendf(&trash, " addr=%s:%d\n",
2776 pn, get_host_port(&conn->addr.to));
2777 break;
2778 case AF_UNIX:
2779 chunk_appendf(&trash, " addr=unix\n");
2780 break;
2781 default:
2782 /* no more information to print right now */
2783 chunk_appendf(&trash, "\n");
2784 break;
2785 }
2786
2787 chunk_appendf(&trash,
2788 " task=%p (state=0x%02x nice=%d calls=%d exp=%s%s",
2789 strm->task,
2790 strm->task->state,
2791 strm->task->nice, strm->task->calls,
2792 strm->task->expire ?
2793 tick_is_expired(strm->task->expire, now_ms) ? "<PAST>" :
2794 human_time(TICKS_TO_MS(strm->task->expire - now_ms),
2795 TICKS_TO_MS(1000)) : "<NEVER>",
2796 task_in_rq(strm->task) ? ", running" : "");
2797
2798 chunk_appendf(&trash,
2799 " age=%s)\n",
2800 human_time(now.tv_sec - strm->logs.accept_date.tv_sec, 1));
2801
2802 if (strm->txn)
2803 chunk_appendf(&trash,
2804 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
2805 strm->txn, strm->txn->flags, strm->txn->meth, strm->txn->status,
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002806 http_msg_state_str(strm->txn->req.msg_state), http_msg_state_str(strm->txn->rsp.msg_state), !LIST_ISEMPTY(&strm->buffer_wait.list));
William Lallemand4c5b4d52016-11-21 08:51:11 +01002807
2808 chunk_appendf(&trash,
2809 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
2810 &strm->si[0],
2811 si_state_str(strm->si[0].state),
2812 strm->si[0].flags,
2813 obj_type_name(strm->si[0].end),
2814 obj_base_ptr(strm->si[0].end),
2815 strm->si[0].exp ?
2816 tick_is_expired(strm->si[0].exp, now_ms) ? "<PAST>" :
2817 human_time(TICKS_TO_MS(strm->si[0].exp - now_ms),
2818 TICKS_TO_MS(1000)) : "<NEVER>",
2819 strm->si[0].err_type);
2820
2821 chunk_appendf(&trash,
2822 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
2823 &strm->si[1],
2824 si_state_str(strm->si[1].state),
2825 strm->si[1].flags,
2826 obj_type_name(strm->si[1].end),
2827 obj_base_ptr(strm->si[1].end),
2828 strm->si[1].exp ?
2829 tick_is_expired(strm->si[1].exp, now_ms) ? "<PAST>" :
2830 human_time(TICKS_TO_MS(strm->si[1].exp - now_ms),
2831 TICKS_TO_MS(1000)) : "<NEVER>",
2832 strm->si[1].err_type);
2833
2834 if ((conn = objt_conn(strm->si[0].end)) != NULL) {
2835 chunk_appendf(&trash,
2836 " co0=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
2837 conn,
2838 conn_get_ctrl_name(conn),
2839 conn_get_xprt_name(conn),
2840 conn_get_data_name(conn),
2841 obj_type_name(conn->target),
2842 obj_base_ptr(conn->target));
2843
2844 chunk_appendf(&trash,
2845 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
2846 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02002847 conn->handle.fd,
2848 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
2849 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].cache : 0,
2850 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].updated : 0);
William Lallemand4c5b4d52016-11-21 08:51:11 +01002851 }
2852 else if ((tmpctx = objt_appctx(strm->si[0].end)) != NULL) {
2853 chunk_appendf(&trash,
2854 " app0=%p st0=%d st1=%d st2=%d applet=%s\n",
2855 tmpctx,
2856 tmpctx->st0,
2857 tmpctx->st1,
2858 tmpctx->st2,
2859 tmpctx->applet->name);
2860 }
2861
2862 if ((conn = objt_conn(strm->si[1].end)) != NULL) {
2863 chunk_appendf(&trash,
2864 " co1=%p ctrl=%s xprt=%s data=%s target=%s:%p\n",
2865 conn,
2866 conn_get_ctrl_name(conn),
2867 conn_get_xprt_name(conn),
2868 conn_get_data_name(conn),
2869 obj_type_name(conn->target),
2870 obj_base_ptr(conn->target));
2871
2872 chunk_appendf(&trash,
2873 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d\n",
2874 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02002875 conn->handle.fd,
2876 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
2877 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].cache : 0,
2878 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].updated : 0);
William Lallemand4c5b4d52016-11-21 08:51:11 +01002879 }
2880 else if ((tmpctx = objt_appctx(strm->si[1].end)) != NULL) {
2881 chunk_appendf(&trash,
2882 " app1=%p st0=%d st1=%d st2=%d applet=%s\n",
2883 tmpctx,
2884 tmpctx->st0,
2885 tmpctx->st1,
2886 tmpctx->st2,
2887 tmpctx->applet->name);
2888 }
2889
2890 chunk_appendf(&trash,
2891 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
2892 " an_exp=%s",
2893 &strm->req,
2894 strm->req.flags, strm->req.analysers,
2895 strm->req.pipe ? strm->req.pipe->data : 0,
2896 strm->req.to_forward, strm->req.total,
2897 strm->req.analyse_exp ?
2898 human_time(TICKS_TO_MS(strm->req.analyse_exp - now_ms),
2899 TICKS_TO_MS(1000)) : "<NEVER>");
2900
2901 chunk_appendf(&trash,
2902 " rex=%s",
2903 strm->req.rex ?
2904 human_time(TICKS_TO_MS(strm->req.rex - now_ms),
2905 TICKS_TO_MS(1000)) : "<NEVER>");
2906
2907 chunk_appendf(&trash,
2908 " wex=%s\n"
2909 " buf=%p data=%p o=%d p=%d req.next=%d i=%d size=%d\n",
2910 strm->req.wex ?
2911 human_time(TICKS_TO_MS(strm->req.wex - now_ms),
2912 TICKS_TO_MS(1000)) : "<NEVER>",
2913 strm->req.buf,
2914 strm->req.buf->data, strm->req.buf->o,
2915 (int)(strm->req.buf->p - strm->req.buf->data),
2916 strm->txn ? strm->txn->req.next : 0, strm->req.buf->i,
2917 strm->req.buf->size);
2918
2919 chunk_appendf(&trash,
2920 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
2921 " an_exp=%s",
2922 &strm->res,
2923 strm->res.flags, strm->res.analysers,
2924 strm->res.pipe ? strm->res.pipe->data : 0,
2925 strm->res.to_forward, strm->res.total,
2926 strm->res.analyse_exp ?
2927 human_time(TICKS_TO_MS(strm->res.analyse_exp - now_ms),
2928 TICKS_TO_MS(1000)) : "<NEVER>");
2929
2930 chunk_appendf(&trash,
2931 " rex=%s",
2932 strm->res.rex ?
2933 human_time(TICKS_TO_MS(strm->res.rex - now_ms),
2934 TICKS_TO_MS(1000)) : "<NEVER>");
2935
2936 chunk_appendf(&trash,
2937 " wex=%s\n"
2938 " buf=%p data=%p o=%d p=%d rsp.next=%d i=%d size=%d\n",
2939 strm->res.wex ?
2940 human_time(TICKS_TO_MS(strm->res.wex - now_ms),
2941 TICKS_TO_MS(1000)) : "<NEVER>",
2942 strm->res.buf,
2943 strm->res.buf->data, strm->res.buf->o,
2944 (int)(strm->res.buf->p - strm->res.buf->data),
2945 strm->txn ? strm->txn->rsp.next : 0, strm->res.buf->i,
2946 strm->res.buf->size);
2947
2948 if (bi_putchk(si_ic(si), &trash) == -1) {
2949 si_applet_cant_put(si);
2950 return 0;
2951 }
2952
2953 /* use other states to dump the contents */
2954 }
2955 /* end of dump */
2956 appctx->ctx.sess.uid = 0;
2957 appctx->ctx.sess.section = 0;
2958 return 1;
2959}
2960
2961
2962static int cli_parse_show_sess(char **args, struct appctx *appctx, void *private)
2963{
William Lallemand4c5b4d52016-11-21 08:51:11 +01002964 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2965 return 1;
2966
2967 if (*args[2] && strcmp(args[2], "all") == 0)
2968 appctx->ctx.sess.target = (void *)-1;
2969 else if (*args[2])
2970 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
2971 else
2972 appctx->ctx.sess.target = NULL;
2973 appctx->ctx.sess.section = 0; /* start with stream status */
2974 appctx->ctx.sess.pos = 0;
2975
2976 return 0;
2977}
2978
2979/* This function dumps all streams' states onto the stream interface's
2980 * read buffer. It returns 0 if the output buffer is full and it needs
2981 * to be called again, otherwise non-zero. It is designed to be called
2982 * from stats_dump_sess_to_buffer() below.
2983 */
2984static int cli_io_handler_dump_sess(struct appctx *appctx)
2985{
2986 struct stream_interface *si = appctx->owner;
2987 struct connection *conn;
2988
2989 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
2990 /* If we're forced to shut down, we might have to remove our
2991 * reference to the last stream being dumped.
2992 */
2993 if (appctx->st2 == STAT_ST_LIST) {
2994 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
2995 LIST_DEL(&appctx->ctx.sess.bref.users);
2996 LIST_INIT(&appctx->ctx.sess.bref.users);
2997 }
2998 }
2999 return 1;
3000 }
3001
3002 chunk_reset(&trash);
3003
3004 switch (appctx->st2) {
3005 case STAT_ST_INIT:
3006 /* the function had not been called yet, let's prepare the
3007 * buffer for a response. We initialize the current stream
3008 * pointer to the first in the global list. When a target
3009 * stream is being destroyed, it is responsible for updating
3010 * this pointer. We know we have reached the end when this
3011 * pointer points back to the head of the streams list.
3012 */
3013 LIST_INIT(&appctx->ctx.sess.bref.users);
3014 appctx->ctx.sess.bref.ref = streams.n;
3015 appctx->st2 = STAT_ST_LIST;
3016 /* fall through */
3017
3018 case STAT_ST_LIST:
3019 /* first, let's detach the back-ref from a possible previous stream */
3020 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3021 LIST_DEL(&appctx->ctx.sess.bref.users);
3022 LIST_INIT(&appctx->ctx.sess.bref.users);
3023 }
3024
3025 /* and start from where we stopped */
3026 while (appctx->ctx.sess.bref.ref != &streams) {
3027 char pn[INET6_ADDRSTRLEN];
3028 struct stream *curr_strm;
3029
3030 curr_strm = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
3031
3032 if (appctx->ctx.sess.target) {
3033 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_strm)
3034 goto next_sess;
3035
3036 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
3037 /* call the proper dump() function and return if we're missing space */
3038 if (!stats_dump_full_strm_to_buffer(si, curr_strm))
3039 return 0;
3040
3041 /* stream dump complete */
3042 LIST_DEL(&appctx->ctx.sess.bref.users);
3043 LIST_INIT(&appctx->ctx.sess.bref.users);
3044 if (appctx->ctx.sess.target != (void *)-1) {
3045 appctx->ctx.sess.target = NULL;
3046 break;
3047 }
3048 else
3049 goto next_sess;
3050 }
3051
3052 chunk_appendf(&trash,
3053 "%p: proto=%s",
3054 curr_strm,
3055 strm_li(curr_strm) ? strm_li(curr_strm)->proto->name : "?");
3056
3057 conn = objt_conn(strm_orig(curr_strm));
3058 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3059 case AF_INET:
3060 case AF_INET6:
3061 chunk_appendf(&trash,
3062 " src=%s:%d fe=%s be=%s srv=%s",
3063 pn,
3064 get_host_port(&conn->addr.from),
3065 strm_fe(curr_strm)->id,
3066 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3067 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3068 );
3069 break;
3070 case AF_UNIX:
3071 chunk_appendf(&trash,
3072 " src=unix:%d fe=%s be=%s srv=%s",
3073 strm_li(curr_strm)->luid,
3074 strm_fe(curr_strm)->id,
3075 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3076 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3077 );
3078 break;
3079 }
3080
3081 chunk_appendf(&trash,
3082 " ts=%02x age=%s calls=%d",
3083 curr_strm->task->state,
3084 human_time(now.tv_sec - curr_strm->logs.tv_accept.tv_sec, 1),
3085 curr_strm->task->calls);
3086
3087 chunk_appendf(&trash,
3088 " rq[f=%06xh,i=%d,an=%02xh,rx=%s",
3089 curr_strm->req.flags,
3090 curr_strm->req.buf->i,
3091 curr_strm->req.analysers,
3092 curr_strm->req.rex ?
3093 human_time(TICKS_TO_MS(curr_strm->req.rex - now_ms),
3094 TICKS_TO_MS(1000)) : "");
3095
3096 chunk_appendf(&trash,
3097 ",wx=%s",
3098 curr_strm->req.wex ?
3099 human_time(TICKS_TO_MS(curr_strm->req.wex - now_ms),
3100 TICKS_TO_MS(1000)) : "");
3101
3102 chunk_appendf(&trash,
3103 ",ax=%s]",
3104 curr_strm->req.analyse_exp ?
3105 human_time(TICKS_TO_MS(curr_strm->req.analyse_exp - now_ms),
3106 TICKS_TO_MS(1000)) : "");
3107
3108 chunk_appendf(&trash,
3109 " rp[f=%06xh,i=%d,an=%02xh,rx=%s",
3110 curr_strm->res.flags,
3111 curr_strm->res.buf->i,
3112 curr_strm->res.analysers,
3113 curr_strm->res.rex ?
3114 human_time(TICKS_TO_MS(curr_strm->res.rex - now_ms),
3115 TICKS_TO_MS(1000)) : "");
3116
3117 chunk_appendf(&trash,
3118 ",wx=%s",
3119 curr_strm->res.wex ?
3120 human_time(TICKS_TO_MS(curr_strm->res.wex - now_ms),
3121 TICKS_TO_MS(1000)) : "");
3122
3123 chunk_appendf(&trash,
3124 ",ax=%s]",
3125 curr_strm->res.analyse_exp ?
3126 human_time(TICKS_TO_MS(curr_strm->res.analyse_exp - now_ms),
3127 TICKS_TO_MS(1000)) : "");
3128
3129 conn = objt_conn(curr_strm->si[0].end);
3130 chunk_appendf(&trash,
3131 " s0=[%d,%1xh,fd=%d,ex=%s]",
3132 curr_strm->si[0].state,
3133 curr_strm->si[0].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003134 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003135 curr_strm->si[0].exp ?
3136 human_time(TICKS_TO_MS(curr_strm->si[0].exp - now_ms),
3137 TICKS_TO_MS(1000)) : "");
3138
3139 conn = objt_conn(curr_strm->si[1].end);
3140 chunk_appendf(&trash,
3141 " s1=[%d,%1xh,fd=%d,ex=%s]",
3142 curr_strm->si[1].state,
3143 curr_strm->si[1].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003144 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003145 curr_strm->si[1].exp ?
3146 human_time(TICKS_TO_MS(curr_strm->si[1].exp - now_ms),
3147 TICKS_TO_MS(1000)) : "");
3148
3149 chunk_appendf(&trash,
3150 " exp=%s",
3151 curr_strm->task->expire ?
3152 human_time(TICKS_TO_MS(curr_strm->task->expire - now_ms),
3153 TICKS_TO_MS(1000)) : "");
3154 if (task_in_rq(curr_strm->task))
3155 chunk_appendf(&trash, " run(nice=%d)", curr_strm->task->nice);
3156
3157 chunk_appendf(&trash, "\n");
3158
3159 if (bi_putchk(si_ic(si), &trash) == -1) {
3160 /* let's try again later from this stream. We add ourselves into
3161 * this stream's users so that it can remove us upon termination.
3162 */
3163 si_applet_cant_put(si);
3164 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
3165 return 0;
3166 }
3167
3168 next_sess:
3169 appctx->ctx.sess.bref.ref = curr_strm->list.n;
3170 }
3171
3172 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
3173 /* specified stream not found */
3174 if (appctx->ctx.sess.section > 0)
3175 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3176 else
3177 chunk_appendf(&trash, "Session not found.\n");
3178
3179 if (bi_putchk(si_ic(si), &trash) == -1) {
3180 si_applet_cant_put(si);
3181 return 0;
3182 }
3183
3184 appctx->ctx.sess.target = NULL;
3185 appctx->ctx.sess.uid = 0;
3186 return 1;
3187 }
3188
3189 appctx->st2 = STAT_ST_FIN;
3190 /* fall through */
3191
3192 default:
3193 appctx->st2 = STAT_ST_FIN;
3194 return 1;
3195 }
3196}
3197
3198static void cli_release_show_sess(struct appctx *appctx)
3199{
3200 if (appctx->st2 == STAT_ST_LIST) {
3201 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
3202 LIST_DEL(&appctx->ctx.sess.bref.users);
3203 }
3204}
3205
Willy Tarreau61b65212016-11-24 11:09:25 +01003206/* Parses the "shutdown session" directive, it always returns 1 */
3207static int cli_parse_shutdown_session(char **args, struct appctx *appctx, void *private)
3208{
3209 struct stream *strm, *ptr;
3210
3211 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3212 return 1;
3213
3214 if (!*args[2]) {
3215 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003216 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau61b65212016-11-24 11:09:25 +01003217 return 1;
3218 }
3219
3220 ptr = (void *)strtoul(args[2], NULL, 0);
3221
3222 /* first, look for the requested stream in the stream table */
3223 list_for_each_entry(strm, &streams, list) {
3224 if (strm == ptr)
3225 break;
3226 }
3227
3228 /* do we have the stream ? */
3229 if (strm != ptr) {
3230 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003231 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau61b65212016-11-24 11:09:25 +01003232 return 1;
3233 }
3234
3235 stream_shutdown(strm, SF_ERR_KILLED);
3236 return 1;
3237}
3238
Willy Tarreau4e46b622016-11-23 16:50:48 +01003239/* Parses the "shutdown session server" directive, it always returns 1 */
3240static int cli_parse_shutdown_sessions_server(char **args, struct appctx *appctx, void *private)
3241{
3242 struct server *sv;
3243 struct stream *strm, *strm_bck;
3244
3245 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3246 return 1;
3247
3248 sv = cli_find_server(appctx, args[3]);
3249 if (!sv)
3250 return 1;
3251
3252 /* kill all the stream that are on this server */
3253 list_for_each_entry_safe(strm, strm_bck, &sv->actconns, by_srv)
3254 if (strm->srv_conn == sv)
3255 stream_shutdown(strm, SF_ERR_KILLED);
3256 return 1;
3257}
3258
William Lallemand4c5b4d52016-11-21 08:51:11 +01003259/* register cli keywords */
3260static struct cli_kw_list cli_kws = {{ },{
3261 { { "show", "sess", NULL }, "show sess [id] : report the list of current sessions or dump this session", cli_parse_show_sess, cli_io_handler_dump_sess, cli_release_show_sess },
Willy Tarreau61b65212016-11-24 11:09:25 +01003262 { { "shutdown", "session", NULL }, "shutdown session : kill a specific session", cli_parse_shutdown_session, NULL, NULL },
Willy Tarreau4e46b622016-11-23 16:50:48 +01003263 { { "shutdown", "sessions", "server" }, "shutdown sessions server : kill sessions on a server", cli_parse_shutdown_sessions_server, NULL, NULL },
William Lallemand4c5b4d52016-11-21 08:51:11 +01003264 {{},}
3265}};
3266
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003267/* main configuration keyword registration. */
3268static struct action_kw_list stream_tcp_keywords = { ILH, {
3269 { "use-service", stream_parse_use_service },
3270 { /* END */ }
3271}};
3272
3273static struct action_kw_list stream_http_keywords = { ILH, {
3274 { "use-service", stream_parse_use_service },
3275 { /* END */ }
3276}};
3277
Willy Tarreau8b22a712010-06-18 17:46:06 +02003278__attribute__((constructor))
Willy Tarreau87b09662015-04-03 00:22:06 +02003279static void __stream_init(void)
Willy Tarreau8b22a712010-06-18 17:46:06 +02003280{
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003281 tcp_req_cont_keywords_register(&stream_tcp_keywords);
3282 http_req_keywords_register(&stream_http_keywords);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003283 cli_register_kw(&cli_kws);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003284}
3285
Willy Tarreaubaaee002006-06-26 02:48:02 +02003286/*
3287 * Local variables:
3288 * c-indent-level: 8
3289 * c-basic-offset: 8
3290 * End:
3291 */