blob: 5c5091c4052d26c9cd82def6f7546d6812ddc800 [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>
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +020021#include <common/hathreads.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010022#include <common/initcall.h>
23#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau8a8d83b2015-04-13 13:24:54 +020025#include <types/applet.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <types/capture.h>
William Lallemand4c5b4d52016-11-21 08:51:11 +010027#include <types/cli.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020028#include <types/filters.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010029#include <types/global.h>
William Lallemand9ed62032016-11-21 17:49:11 +010030#include <types/stats.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020032#include <proto/acl.h>
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020033#include <proto/action.h>
Willy Tarreau609aad92018-11-22 08:31:09 +010034#include <proto/activity.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020035#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010036#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020037#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010038#include <proto/checks.h>
William Lallemand4c5b4d52016-11-21 08:51:11 +010039#include <proto/cli.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020040#include <proto/connection.h>
William Lallemand9ed62032016-11-21 17:49:11 +010041#include <proto/stats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020042#include <proto/fd.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020043#include <proto/filters.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020044#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020045#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010046#include <proto/hdr_idx.h>
Thierry FOURNIER65f34c62015-02-16 20:11:43 +010047#include <proto/hlua.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020048#include <proto/http_rules.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020049#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020050#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020051#include <proto/raw_sock.h>
Willy Tarreaufeb76402015-04-03 14:10:06 +020052#include <proto/session.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020053#include <proto/stream.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010054#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010055#include <proto/proto_http.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020056#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020057#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010058#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020059#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010060#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010061#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010062#include <proto/task.h>
Willy Tarreau39713102016-11-25 15:49:32 +010063#include <proto/tcp_rules.h>
Thierry FOURNIER4834bc72015-06-06 19:29:07 +020064#include <proto/vars.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020065
Willy Tarreau8ceae722018-11-26 11:58:30 +010066DECLARE_POOL(pool_head_stream, "stream", sizeof(struct stream));
67
Willy Tarreaub6b3df32018-11-26 16:31:20 +010068struct list streams = LIST_HEAD_INIT(streams);
Willy Tarreau86abe442018-11-25 20:12:18 +010069__decl_spinlock(streams_lock);
Willy Tarreaubaaee002006-06-26 02:48:02 +020070
Thierry FOURNIER5a363e72015-09-27 19:29:33 +020071/* List of all use-service keywords. */
72static struct list service_keywords = LIST_HEAD_INIT(service_keywords);
73
Willy Tarreau5790eb02017-08-28 17:18:36 +020074
75/* Create a new stream for connection <conn>. Return < 0 on error. This is only
76 * valid right after the handshake, before the connection's data layer is
77 * initialized, because it relies on the session to be in conn->owner.
78 */
Olivier Houchard9aaf7782017-09-13 18:30:23 +020079int stream_create_from_cs(struct conn_stream *cs)
Willy Tarreau5790eb02017-08-28 17:18:36 +020080{
81 struct stream *strm;
82
Olivier Houchard9aaf7782017-09-13 18:30:23 +020083 strm = stream_new(cs->conn->owner, &cs->obj_type);
Willy Tarreau5790eb02017-08-28 17:18:36 +020084 if (strm == NULL)
85 return -1;
86
87 task_wakeup(strm->task, TASK_WOKEN_INIT);
88 return 0;
89}
90
Willy Tarreaub882dd82018-11-06 15:50:21 +010091/* Callback used to wake up a stream when an input buffer is available. The
92 * stream <s>'s stream interfaces are checked for a failed buffer allocation
Willy Tarreaud0f5bbc2018-11-14 11:10:26 +010093 * as indicated by the presence of the SI_FL_RXBLK_ROOM flag and the lack of a
Willy Tarreaub882dd82018-11-06 15:50:21 +010094 * buffer, and and input buffer is assigned there (at most one). The function
95 * returns 1 and wakes the stream up if a buffer was taken, otherwise zero.
96 * It's designed to be called from __offer_buffer().
97 */
98int stream_buf_available(void *arg)
99{
100 struct stream *s = arg;
101
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100102 if (!s->req.buf.size && !s->req.pipe && (s->si[0].flags & SI_FL_RXBLK_BUFF) &&
Willy Tarreaub882dd82018-11-06 15:50:21 +0100103 b_alloc_margin(&s->req.buf, global.tune.reserved_bufs))
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100104 si_rx_buff_rdy(&s->si[0]);
105 else if (!s->res.buf.size && !s->res.pipe && (s->si[1].flags & SI_FL_RXBLK_BUFF) &&
Willy Tarreaub882dd82018-11-06 15:50:21 +0100106 b_alloc_margin(&s->res.buf, 0))
Willy Tarreau8be7cd72018-11-14 15:12:08 +0100107 si_rx_buff_rdy(&s->si[1]);
Willy Tarreaub882dd82018-11-06 15:50:21 +0100108 else
109 return 0;
110
111 task_wakeup(s->task, TASK_WOKEN_RES);
112 return 1;
113
114}
115
Willy Tarreau9903f0e2015-04-04 18:50:31 +0200116/* This function is called from the session handler which detects the end of
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200117 * handshake, in order to complete initialization of a valid stream. It must be
Joseph Herlant4cc8d0d2018-11-15 09:14:14 -0800118 * called with a completely initialized session. It returns the pointer to
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200119 * the newly created stream, or NULL in case of fatal error. The client-facing
Willy Tarreau87787ac2017-08-28 16:22:54 +0200120 * end point is assigned to <origin>, which must be valid. The stream's task
121 * is configured with a nice value inherited from the listener's nice if any.
122 * The task's context is set to the new stream, and its function is set to
123 * process_stream(). Target and analysers are null.
Willy Tarreau2542b532012-08-31 16:01:23 +0200124 */
Willy Tarreau87787ac2017-08-28 16:22:54 +0200125struct stream *stream_new(struct session *sess, enum obj_type *origin)
Willy Tarreau2542b532012-08-31 16:01:23 +0200126{
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200127 struct stream *s;
Willy Tarreau87787ac2017-08-28 16:22:54 +0200128 struct task *t;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200129 struct conn_stream *cs = objt_cs(origin);
Willy Tarreau73b65ac2015-04-08 18:26:29 +0200130 struct appctx *appctx = objt_appctx(origin);
Willy Tarreau2542b532012-08-31 16:01:23 +0200131
Willy Tarreaubafbe012017-11-24 17:34:44 +0100132 if (unlikely((s = pool_alloc(pool_head_stream)) == NULL))
Willy Tarreau87787ac2017-08-28 16:22:54 +0200133 goto out_fail_alloc;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200134
135 /* minimum stream initialization required for an embryonic stream is
136 * fairly low. We need very little to execute L4 ACLs, then we need a
137 * task to make the client-side connection live on its own.
138 * - flags
139 * - stick-entry tracking
140 */
141 s->flags = 0;
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200142 s->logs.logwait = sess->fe->to_log;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200143 s->logs.level = 0;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200144 s->logs.accept_date = sess->accept_date; /* user-visible date for logging */
145 s->logs.tv_accept = sess->tv_accept; /* corrected date for internal use */
Willy Tarreau590a0512018-09-05 11:56:48 +0200146 s->logs.t_handshake = sess->t_handshake;
Thierry FOURNIER / OZON.IO4cac3592016-07-28 17:19:45 +0200147 s->logs.t_idle = -1;
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200148 tv_zero(&s->logs.tv_request);
149 s->logs.t_queue = -1;
150 s->logs.t_connect = -1;
151 s->logs.t_data = -1;
152 s->logs.t_close = 0;
153 s->logs.bytes_in = s->logs.bytes_out = 0;
Patrick Hemmerffe5e8c2018-05-11 12:52:31 -0400154 s->logs.prx_queue_pos = 0; /* we get the number of pending conns before us */
155 s->logs.srv_queue_pos = 0; /* we will get this number soon */
Willy Tarreau99eb0f12015-04-05 12:03:54 +0200156
157 /* default logging function */
158 s->do_log = strm_log;
159
160 /* default error reporting function, may be changed by analysers */
161 s->srv_error = default_srv_error;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200162
163 /* Initialise the current rule list pointer to NULL. We are sure that
164 * any rulelist match the NULL pointer.
165 */
166 s->current_rule_list = NULL;
Remi Gacogne7fb9de22015-07-22 17:10:58 +0200167 s->current_rule = NULL;
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200168
Willy Tarreaua68f7622015-09-21 17:48:24 +0200169 /* Copy SC counters for the stream. We don't touch refcounts because
170 * any reference we have is inherited from the session. Since the stream
171 * doesn't exist without the session, the session's existence guarantees
172 * we don't lose the entry. During the store operation, the stream won't
173 * touch these ones.
Thierry FOURNIER827752e2015-08-18 11:34:18 +0200174 */
Thierry FOURNIERc8fdb982015-08-16 12:03:39 +0200175 memcpy(s->stkctr, sess->stkctr, sizeof(s->stkctr));
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200176
177 s->sess = sess;
178 s->si[0].flags = SI_FL_NONE;
179 s->si[1].flags = SI_FL_ISBACK;
180
Willy Tarreau90a7c032018-09-05 16:21:29 +0200181 s->uniq_id = HA_ATOMIC_XADD(&global.req_count, 1);
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200182
Willy Tarreau87b09662015-04-03 00:22:06 +0200183 /* OK, we're keeping the stream, so let's properly initialize the stream */
Willy Tarreau2542b532012-08-31 16:01:23 +0200184 LIST_INIT(&s->back_refs);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100185
186 LIST_INIT(&s->buffer_wait.list);
187 s->buffer_wait.target = s;
Willy Tarreaub882dd82018-11-06 15:50:21 +0100188 s->buffer_wait.wakeup_cb = stream_buf_available;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200189
Willy Tarreaue7dff022015-04-03 01:14:29 +0200190 s->flags |= SF_INITIALIZED;
William Lallemandcf62f7e2018-10-26 14:47:40 +0200191 s->pcli_next_pid = 0;
Willy Tarreau2542b532012-08-31 16:01:23 +0200192 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200193
Emeric Brunc60def82017-09-27 14:59:38 +0200194 if ((t = task_new(tid_bit)) == NULL)
Willy Tarreau87787ac2017-08-28 16:22:54 +0200195 goto out_fail_alloc;
196
Willy Tarreau02a0c0e2015-04-04 18:08:21 +0200197 s->task = t;
Christopher Faulet9d810ca2016-12-08 22:33:52 +0100198 s->pending_events = 0;
Willy Tarreaud1769b82015-04-06 00:25:48 +0200199 t->process = process_stream;
Willy Tarreau2542b532012-08-31 16:01:23 +0200200 t->context = s;
201 t->expire = TICK_ETERNITY;
Willy Tarreau87787ac2017-08-28 16:22:54 +0200202 if (sess->listener)
203 t->nice = sess->listener->nice;
Willy Tarreau2542b532012-08-31 16:01:23 +0200204
Willy Tarreau87b09662015-04-03 00:22:06 +0200205 /* Note: initially, the stream's backend points to the frontend.
Willy Tarreau2542b532012-08-31 16:01:23 +0200206 * This changes later when switching rules are executed or
207 * when the default backend is assigned.
208 */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200209 s->be = sess->fe;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200210 s->req.buf = BUF_NULL;
211 s->res.buf = BUF_NULL;
Willy Tarreaucb7dd012015-04-03 22:16:32 +0200212 s->req_cap = NULL;
213 s->res_cap = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200214
Willy Tarreauebcd4842015-06-19 11:59:02 +0200215 /* Initialise all the variables contexts even if not used.
216 * This permits to prune these contexts without errors.
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200217 */
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200218 vars_init(&s->vars_txn, SCOPE_TXN);
219 vars_init(&s->vars_reqres, SCOPE_REQ);
220
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200221 /* this part should be common with other protocols */
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200222 if (si_reset(&s->si[0]) < 0)
223 goto out_fail_alloc;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200224 si_set_state(&s->si[0], SI_ST_EST);
Hongbo Longe39683c2017-03-10 18:41:51 +0100225 s->si[0].hcto = sess->fe->timeout.clientfin;
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200226
Willy Tarreau984fca92017-12-20 16:31:43 +0100227 if (cs && cs->conn->mux && cs->conn->mux->flags & MX_FL_CLEAN_ABRT)
228 s->si[0].flags |= SI_FL_CLEAN_ABRT;
229
Willy Tarreau1f52bb22015-04-04 14:28:46 +0200230 /* attach the incoming connection to the stream interface now. */
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200231 if (cs)
232 si_attach_cs(&s->si[0], cs);
Willy Tarreau470280e2015-04-05 01:33:13 +0200233 else if (appctx)
234 si_attach_appctx(&s->si[0], appctx);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200235
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200236 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200237 s->si[0].flags |= SI_FL_INDEP_STR;
238
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200239 /* pre-initialize the other side's stream interface to an INIT state. The
240 * callbacks will be initialized before attempting to connect.
241 */
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200242 if (si_reset(&s->si[1]) < 0)
243 goto out_fail_alloc_si1;
Hongbo Longe39683c2017-03-10 18:41:51 +0100244 s->si[1].hcto = TICK_ETERNITY;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200245
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200246 if (likely(sess->fe->options2 & PR_O2_INDEPSTR))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200247 s->si[1].flags |= SI_FL_INDEP_STR;
248
Willy Tarreau87b09662015-04-03 00:22:06 +0200249 stream_init_srv_conn(s);
Willy Tarreau9b82d942016-12-05 00:26:31 +0100250 s->target = sess->listener ? sess->listener->default_target : NULL;
251
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200252 s->pend_pos = NULL;
Patrick Hemmer268a7072018-05-11 12:52:31 -0400253 s->priority_class = 0;
254 s->priority_offset = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200255
256 /* init store persistence */
257 s->store_count = 0;
258
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100259 channel_init(&s->req);
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100260 s->req.flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau9b82d942016-12-05 00:26:31 +0100261 s->req.analysers = sess->listener ? sess->listener->analysers : 0;
Willy Tarreaue223e3b2017-11-18 15:39:10 +0100262
263 if (!sess->fe->fe_req_ana) {
264 channel_auto_connect(&s->req); /* don't wait to establish connection */
265 channel_auto_close(&s->req); /* let the producer forward close requests */
266 }
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200267
268 s->req.rto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100269 s->req.wto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100270 s->req.rex = TICK_ETERNITY;
271 s->req.wex = TICK_ETERNITY;
272 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200273
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100274 channel_init(&s->res);
Willy Tarreauef573c02014-11-28 14:17:09 +0100275 s->res.flags |= CF_ISRESP;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100276 s->res.analysers = 0;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200277
Willy Tarreaue36cbcb2015-04-03 15:40:56 +0200278 if (sess->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100279 s->req.flags |= CF_NEVER_WAIT;
280 s->res.flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200281 }
282
Willy Tarreauc8815ef2015-04-05 18:15:59 +0200283 s->res.wto = sess->fe->timeout.client;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100284 s->res.rto = TICK_ETERNITY;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100285 s->res.rex = TICK_ETERNITY;
286 s->res.wex = TICK_ETERNITY;
287 s->res.analyse_exp = TICK_ETERNITY;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200288
Willy Tarreaueee5b512015-04-03 23:46:31 +0200289 s->txn = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100290 s->hlua = NULL;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100291
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100292 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200293 LIST_ADDQ(&streams, &s->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100294 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200295
Christopher Faulet92d36382015-11-05 13:35:03 +0100296 if (flt_stream_init(s) < 0 || flt_stream_start(s) < 0)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200297 goto out_fail_accept;
298
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200299 /* finish initialization of the accepted file descriptor */
Olivier Houchard53216e72018-10-10 15:46:36 +0200300 if (appctx)
Willy Tarreau0cd3bd62018-11-06 18:46:37 +0100301 si_want_get(&s->si[0]);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200302
Willy Tarreaufb9f5842015-04-05 18:19:23 +0200303 if (sess->fe->accept && sess->fe->accept(s) < 0)
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200304 goto out_fail_accept;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200305
306 /* it is important not to call the wakeup function directly but to
307 * pass through task_wakeup(), because this one knows how to apply
Emeric Brun5f77fef2017-05-29 15:26:51 +0200308 * priorities to tasks. Using multi thread we must be sure that
309 * stream is fully initialized before calling task_wakeup. So
310 * the caller must handle the task_wakeup
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200311 */
Willy Tarreau02d86382015-04-05 12:00:52 +0200312 return s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200313
314 /* Error unrolling */
Willy Tarreauc5a7ff42015-04-05 11:52:08 +0200315 out_fail_accept:
Christopher Faulet92d36382015-11-05 13:35:03 +0100316 flt_stream_release(s, 0);
Willy Tarreau87787ac2017-08-28 16:22:54 +0200317 task_free(t);
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200318 tasklet_free(s->si[1].wait_event.task);
Willy Tarreaue5f229e2018-10-15 11:08:55 +0200319 LIST_DEL(&s->list);
Olivier Houchard8f0b4c62018-08-02 18:21:38 +0200320out_fail_alloc_si1:
Olivier Houchardfa8aa862018-10-10 18:25:41 +0200321 tasklet_free(s->si[0].wait_event.task);
Willy Tarreau87787ac2017-08-28 16:22:54 +0200322 out_fail_alloc:
Willy Tarreaubafbe012017-11-24 17:34:44 +0100323 pool_free(pool_head_stream, s);
Willy Tarreau02d86382015-04-05 12:00:52 +0200324 return NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200325}
326
Willy Tarreaubaaee002006-06-26 02:48:02 +0200327/*
Willy Tarreau87b09662015-04-03 00:22:06 +0200328 * frees the context associated to a stream. It must have been removed first.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200330static void stream_free(struct stream *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200331{
Willy Tarreau9ad7bd42015-04-03 19:19:59 +0200332 struct session *sess = strm_sess(s);
333 struct proxy *fe = sess->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100334 struct bref *bref, *back;
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200335 struct conn_stream *cli_cs = objt_cs(s->si[0].end);
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100336 int must_free_sess;
Willy Tarreaua4cda672010-06-06 18:28:49 +0200337 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100338
Willy Tarreaud0ad4a82018-07-25 11:13:53 +0200339 pendconn_free(s);
Willy Tarreau922a8062008-12-04 09:33:58 +0100340
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100341 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200342 if (s->flags & SF_CURR_SESS) {
343 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +0200344 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau1e62de62008-11-11 20:20:02 +0100345 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100346 if (may_dequeue_tasks(objt_server(s->target), s->be))
347 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100348 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100349
Willy Tarreau7c669d72008-06-20 15:04:11 +0200350 if (unlikely(s->srv_conn)) {
Willy Tarreau87b09662015-04-03 00:22:06 +0200351 /* the stream still has a reserved slot on a server, but
Willy Tarreau7c669d72008-06-20 15:04:11 +0200352 * it should normally be only the same as the one above,
353 * so this should not happen in fact.
354 */
355 sess_change_server(s, NULL);
356 }
357
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100358 if (s->req.pipe)
359 put_pipe(s->req.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100360
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100361 if (s->res.pipe)
362 put_pipe(s->res.pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100363
Willy Tarreaubf883e02014-11-25 21:10:35 +0100364 /* We may still be present in the buffer wait queue */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100365 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100366 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100367 LIST_DEL(&s->buffer_wait.list);
368 LIST_INIT(&s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100369 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100370 }
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200371 if (s->req.buf.size || s->res.buf.size) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100372 b_drop(&s->req.buf);
373 b_drop(&s->res.buf);
Olivier Houchard673867c2018-05-25 16:58:52 +0200374 offer_buffers(NULL, tasks_run_queue);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100375 }
Willy Tarreau9b28e032012-10-12 23:49:43 +0200376
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +0100377 hlua_ctx_destroy(s->hlua);
378 s->hlua = NULL;
Willy Tarreaueee5b512015-04-03 23:46:31 +0200379 if (s->txn)
380 http_end_txn(s);
Willy Tarreau46023632010-01-07 22:51:47 +0100381
Willy Tarreau1e954912012-10-12 17:50:05 +0200382 /* ensure the client-side transport layer is destroyed */
Willy Tarreau32560732017-10-08 11:33:44 +0200383 if (cli_cs)
384 cs_close(cli_cs);
Willy Tarreau1e954912012-10-12 17:50:05 +0200385
Willy Tarreaua4cda672010-06-06 18:28:49 +0200386 for (i = 0; i < s->store_count; i++) {
387 if (!s->store[i].ts)
388 continue;
389 stksess_free(s->store[i].table, s->store[i].ts);
390 s->store[i].ts = NULL;
391 }
392
Willy Tarreaueee5b512015-04-03 23:46:31 +0200393 if (s->txn) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100394 pool_free(pool_head_hdr_idx, s->txn->hdr_idx.v);
395 pool_free(pool_head_http_txn, s->txn);
Willy Tarreaueee5b512015-04-03 23:46:31 +0200396 s->txn = NULL;
397 }
398
Christopher Fauletd7c91962015-04-30 11:48:27 +0200399 flt_stream_stop(s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100400 flt_stream_release(s, 0);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200401
Willy Tarreau92fb9832007-10-16 17:34:28 +0200402 if (fe) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100403 pool_free(fe->rsp_cap_pool, s->res_cap);
404 pool_free(fe->req_cap_pool, s->req_cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100406
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200407 /* Cleanup all variable contexts. */
Willy Tarreaucda7f3f2018-10-28 13:44:36 +0100408 if (!LIST_ISEMPTY(&s->vars_txn.head))
409 vars_prune(&s->vars_txn, s->sess, s);
410 if (!LIST_ISEMPTY(&s->vars_reqres.head))
411 vars_prune(&s->vars_reqres, s->sess, s);
Thierry FOURNIER4834bc72015-06-06 19:29:07 +0200412
Willy Tarreau87b09662015-04-03 00:22:06 +0200413 stream_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200414
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100415 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100416 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100417 /* we have to unlink all watchers. We must not relink them if
Willy Tarreau87b09662015-04-03 00:22:06 +0200418 * this stream was the last one in the list.
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100419 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100420 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100421 LIST_INIT(&bref->users);
Willy Tarreau87b09662015-04-03 00:22:06 +0200422 if (s->list.n != &streams)
423 LIST_ADDQ(&LIST_ELEM(s->list.n, struct stream *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100424 bref->ref = s->list.n;
425 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100426 LIST_DEL(&s->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100427 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
Emeric Brun6b35e9b2017-06-30 16:23:45 +0200428
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100429 /* applets do not release session yet */
430 must_free_sess = objt_appctx(sess->origin) && sess->origin == s->si[0].end;
431
Olivier Houchard4fdec7a2018-10-11 17:09:14 +0200432
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200433 si_release_endpoint(&s->si[1]);
434 si_release_endpoint(&s->si[0]);
Willy Tarreaufeb76402015-04-03 14:10:06 +0200435
Olivier Houchardd7bd3e32018-10-18 16:22:02 +0200436 tasklet_free(s->si[0].wait_event.task);
437 tasklet_free(s->si[1].wait_event.task);
438
Olivier Houchard25607af2018-11-23 14:50:47 +0100439 if (must_free_sess) {
440 sess->origin = NULL;
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100441 session_free(sess);
Olivier Houchard25607af2018-11-23 14:50:47 +0100442 }
Willy Tarreau5bcfd562017-11-29 14:05:38 +0100443
Willy Tarreaubafbe012017-11-24 17:34:44 +0100444 pool_free(pool_head_stream, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200445
446 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200447 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreaubafbe012017-11-24 17:34:44 +0100448 pool_flush(pool_head_buffer);
449 pool_flush(pool_head_http_txn);
450 pool_flush(pool_head_hdr_idx);
451 pool_flush(pool_head_requri);
452 pool_flush(pool_head_capture);
453 pool_flush(pool_head_stream);
454 pool_flush(pool_head_session);
455 pool_flush(pool_head_connection);
456 pool_flush(pool_head_pendconn);
457 pool_flush(fe->req_cap_pool);
458 pool_flush(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200459 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200460}
461
Willy Tarreau656859d2014-11-25 19:46:36 +0100462
Willy Tarreau87b09662015-04-03 00:22:06 +0200463/* Allocates a work buffer for stream <s>. It is meant to be called inside
464 * process_stream(). It will only allocate the side needed for the function
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200465 * to work fine, which is the response buffer so that an error message may be
466 * built and returned. Response buffers may be allocated from the reserve, this
467 * is critical to ensure that a response may always flow and will never block a
468 * server from releasing a connection. Returns 0 in case of failure, non-zero
469 * otherwise.
Willy Tarreau656859d2014-11-25 19:46:36 +0100470 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100471static int stream_alloc_work_buffer(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100472{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100473 if (!LIST_ISEMPTY(&s->buffer_wait.list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100474 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100475 LIST_DEL(&s->buffer_wait.list);
476 LIST_INIT(&s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100477 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreaubf883e02014-11-25 21:10:35 +0100478 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100479
Willy Tarreaubc39a5d2015-04-20 15:52:18 +0200480 if (b_alloc_margin(&s->res.buf, 0))
Willy Tarreau656859d2014-11-25 19:46:36 +0100481 return 1;
482
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100483 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100484 LIST_ADDQ(&buffer_wq, &s->buffer_wait.list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100485 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Willy Tarreau656859d2014-11-25 19:46:36 +0100486 return 0;
487}
488
489/* releases unused buffers after processing. Typically used at the end of the
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100490 * update() functions. It will try to wake up as many tasks/applets as the
491 * number of buffers that it releases. In practice, most often streams are
492 * blocked on a single buffer, so it makes sense to try to wake two up when two
493 * buffers are released at once.
Willy Tarreau656859d2014-11-25 19:46:36 +0100494 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200495void stream_release_buffers(struct stream *s)
Willy Tarreau656859d2014-11-25 19:46:36 +0100496{
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100497 int offer = 0;
Willy Tarreau656859d2014-11-25 19:46:36 +0100498
Willy Tarreau0c7ed5d2018-07-10 09:53:31 +0200499 if (c_size(&s->req) && c_empty(&s->req)) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100500 offer = 1;
501 b_free(&s->req.buf);
502 }
Willy Tarreau0c7ed5d2018-07-10 09:53:31 +0200503 if (c_size(&s->res) && c_empty(&s->res)) {
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100504 offer = 1;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100505 b_free(&s->res.buf);
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100506 }
Willy Tarreau656859d2014-11-25 19:46:36 +0100507
Willy Tarreaubf883e02014-11-25 21:10:35 +0100508 /* if we're certain to have at least 1 buffer available, and there is
509 * someone waiting, we can wake up a waiter and offer them.
510 */
Christopher Fauleta73e59b2016-12-09 17:30:18 +0100511 if (offer)
Olivier Houchard673867c2018-05-25 16:58:52 +0200512 offer_buffers(s, tasks_run_queue);
Willy Tarreau656859d2014-11-25 19:46:36 +0100513}
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200514
Willy Tarreau87b09662015-04-03 00:22:06 +0200515void stream_process_counters(struct stream *s)
Willy Tarreau30e71012007-11-26 20:15:35 +0100516{
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200517 struct session *sess = s->sess;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100518 unsigned long long bytes;
Emeric Brun57056f02015-06-15 18:29:57 +0200519 void *ptr1,*ptr2;
Emeric Brun819fc6f2017-06-13 19:37:32 +0200520 struct stksess *ts;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100521 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100522
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100523 bytes = s->req.total - s->logs.bytes_in;
524 s->logs.bytes_in = s->req.total;
525 if (bytes) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200526 HA_ATOMIC_ADD(&sess->fe->fe_counters.bytes_in, bytes);
527 HA_ATOMIC_ADD(&s->be->be_counters.bytes_in, bytes);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100528
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100529 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +0200530 HA_ATOMIC_ADD(&objt_server(s->target)->counters.bytes_in, bytes);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200531
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200532 if (sess->listener && sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200533 HA_ATOMIC_ADD(&sess->listener->counters->bytes_in, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200534
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100535 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200536 struct stkctr *stkctr = &s->stkctr[i];
537
Emeric Brun819fc6f2017-06-13 19:37:32 +0200538 ts = stkctr_entry(stkctr);
539 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200540 stkctr = &sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200541 ts = stkctr_entry(stkctr);
542 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200543 continue;
544 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200545
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100546 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200547 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_CNT);
Emeric Brun57056f02015-06-15 18:29:57 +0200548 if (ptr1)
549 stktable_data_cast(ptr1, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200550
Emeric Brun819fc6f2017-06-13 19:37:32 +0200551 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_IN_RATE);
Emeric Brun57056f02015-06-15 18:29:57 +0200552 if (ptr2)
553 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_in_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200554 stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100555 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun57056f02015-06-15 18:29:57 +0200556
557 /* If data was modified, we need to touch to re-schedule sync */
558 if (ptr1 || ptr2)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200559 stktable_touch_local(stkctr->table, ts, 0);
Willy Tarreau30e71012007-11-26 20:15:35 +0100560 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100561 }
562
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100563 bytes = s->res.total - s->logs.bytes_out;
564 s->logs.bytes_out = s->res.total;
565 if (bytes) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200566 HA_ATOMIC_ADD(&sess->fe->fe_counters.bytes_out, bytes);
567 HA_ATOMIC_ADD(&s->be->be_counters.bytes_out, bytes);
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100568
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100569 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +0200570 HA_ATOMIC_ADD(&objt_server(s->target)->counters.bytes_out, bytes);
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200571
Willy Tarreaufb0afa72015-04-03 14:46:27 +0200572 if (sess->listener && sess->listener->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +0200573 HA_ATOMIC_ADD(&sess->listener->counters->bytes_out, bytes);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200574
Willy Tarreau22ec1ea2014-11-27 20:45:39 +0100575 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200576 struct stkctr *stkctr = &s->stkctr[i];
577
Emeric Brun819fc6f2017-06-13 19:37:32 +0200578 ts = stkctr_entry(stkctr);
579 if (!ts) {
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200580 stkctr = &sess->stkctr[i];
Emeric Brun819fc6f2017-06-13 19:37:32 +0200581 ts = stkctr_entry(stkctr);
582 if (!ts)
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200583 continue;
584 }
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200585
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100586 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200587 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_CNT);
Emeric Brun57056f02015-06-15 18:29:57 +0200588 if (ptr1)
589 stktable_data_cast(ptr1, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200590
Emeric Brun819fc6f2017-06-13 19:37:32 +0200591 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_BYTES_OUT_RATE);
Emeric Brun57056f02015-06-15 18:29:57 +0200592 if (ptr2)
593 update_freq_ctr_period(&stktable_data_cast(ptr2, bytes_out_rate),
Willy Tarreau8b7f8682015-04-04 16:29:12 +0200594 stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100595 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun57056f02015-06-15 18:29:57 +0200596
597 /* If data was modified, we need to touch to re-schedule sync */
598 if (ptr1 || ptr2)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200599 stktable_touch_local(stkctr->table, stkctr_entry(stkctr), 0);
Willy Tarreau30e71012007-11-26 20:15:35 +0100600 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100601 }
602}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200603
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100604/* This function is called with (si->state == SI_ST_CON) meaning that a
605 * connection was attempted and that the file descriptor is already allocated.
606 * We must check for establishment, error and abort. Possible output states
607 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
608 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreau87b09662015-04-03 00:22:06 +0200609 * otherwise 1. This only works with connection-based streams.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100610 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200611static int sess_update_st_con_tcp(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100612{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100613 struct stream_interface *si = &s->si[1];
614 struct channel *req = &s->req;
615 struct channel *rep = &s->res;
Olivier Houchard14547b22018-11-29 18:01:46 +0100616 struct conn_stream *srv_cs = objt_cs(si->end);
617 struct connection *conn = NULL;
618
619 if (!srv_cs)
620 conn = objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100621
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100622 /* If we got an error, or if nothing happened and the connection timed
623 * out, we must give up. The CER state handler will take care of retry
624 * attempts and error reports.
625 */
626 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Bin Wang95fad5b2017-09-15 14:56:40 +0800627 if (unlikely(req->flags & CF_WROTE_DATA)) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100628 /* Some data were sent past the connection establishment,
629 * so we need to pretend we're established to log correctly
630 * and let later states handle the failure.
631 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100632 si->state = SI_ST_EST;
633 si->err_type = SI_ET_DATA_ERR;
Willy Tarreau103197d2014-11-28 15:26:12 +0100634 rep->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100635 return 1;
636 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100637 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100638 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100639
Olivier Houchard201b9f42018-11-21 00:16:29 +0100640 if (srv_cs)
641 cs_close(srv_cs);
Olivier Houchard14547b22018-11-29 18:01:46 +0100642 else if (conn) {
643 conn_stop_tracking(conn);
644 conn_full_close(conn);
645 conn_free(conn);
646 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100647
648 if (si->err_type)
649 return 0;
650
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100651 if (si->flags & SI_FL_ERR)
652 si->err_type = SI_ET_CONN_ERR;
653 else
654 si->err_type = SI_ET_CONN_TO;
655 return 0;
656 }
657
658 /* OK, maybe we want to abort */
Bin Wang95fad5b2017-09-15 14:56:40 +0800659 if (!(req->flags & CF_WROTE_DATA) &&
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100660 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200661 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
Willy Tarreauede3d882018-10-24 17:17:56 +0200662 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau7aa15b02017-12-20 16:56:50 +0100663 ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)))))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100664 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200665 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100666 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100667 if (s->srv_error)
668 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100669 return 1;
670 }
671
672 /* we need to wait a bit more if there was no activity either */
Christopher Faulet3b88b8d2018-10-26 17:36:03 +0200673 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100674 return 1;
675
676 /* OK, this means that a connection succeeded. The caller will be
677 * responsible for handling the transition from CON to EST.
678 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100679 si->state = SI_ST_EST;
680 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100681 return 1;
682}
683
684/* This function is called with (si->state == SI_ST_CER) meaning that a
685 * previous connection attempt has failed and that the file descriptor
686 * has already been released. Possible causes include asynchronous error
687 * notification and time out. Possible output states are SI_ST_CLO when
688 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
689 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
690 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
691 * marked as in error state. It returns 0.
692 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200693static int sess_update_st_cer(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100694{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100695 struct stream_interface *si = &s->si[1];
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200696 struct conn_stream *cs = objt_cs(si->end);
697 struct connection *conn = cs_conn(cs);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100698
Willy Tarreau87b09662015-04-03 00:22:06 +0200699 /* we probably have to release last stream from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100700 if (objt_server(s->target)) {
701 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100702
Willy Tarreaue7dff022015-04-03 01:14:29 +0200703 if (s->flags & SF_CURR_SESS) {
704 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +0200705 HA_ATOMIC_SUB(&objt_server(s->target)->cur_sess, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100706 }
Willy Tarreau46d5b082017-07-26 20:13:37 +0200707
708 if ((si->flags & SI_FL_ERR) &&
709 conn && conn->err_code == CO_ER_SSL_MISMATCH_SNI) {
710 /* We tried to connect to a server which is configured
711 * with "verify required" and which doesn't have the
712 * "verifyhost" directive. The server presented a wrong
713 * certificate (a certificate for an unexpected name),
714 * which implies that we have used SNI in the handshake,
715 * and that the server doesn't have the associated cert
716 * and presented a default one.
717 *
718 * This is a serious enough issue not to retry. It's
719 * especially important because this wrong name might
720 * either be the result of a configuration error, and
721 * retrying will only hammer the server, or is caused
722 * by the use of a wrong SNI value, most likely
723 * provided by the client and we don't want to let the
724 * client provoke retries.
725 */
726 si->conn_retries = 0;
727 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100728 }
729
730 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200731 si->conn_retries--;
732 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100733 if (!si->err_type) {
734 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100735 }
736
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100737 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +0200738 HA_ATOMIC_ADD(&objt_server(s->target)->counters.failed_conns, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200739 HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100740 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100741 if (may_dequeue_tasks(objt_server(s->target), s->be))
742 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100743
744 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200745 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100746 s->req.flags |= CF_WRITE_ERROR;
747 s->res.flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100748
749 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100750 if (s->srv_error)
751 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100752 return 0;
753 }
754
755 /* If the "redispatch" option is set on the backend, we are allowed to
Joseph Lynch726ab712015-05-11 23:25:34 -0700756 * retry on another server. By default this redispatch occurs on the
757 * last retry, but if configured we allow redispatches to occur on
758 * configurable intervals, e.g. on every retry. In order to achieve this,
Willy Tarreau87b09662015-04-03 00:22:06 +0200759 * we must mark the stream unassigned, and eventually clear the DIRECT
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100760 * bit to ignore any persistence cookie. We won't count a retry nor a
761 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200762 * If the connection is not persistent, the balancing algorithm is not
763 * determinist (round robin) and there is more than one active server,
764 * we accept to perform an immediate redispatch without waiting since
765 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100766 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200767 if (objt_server(s->target) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700768 (s->be->options & PR_O_REDISP) && !(s->flags & SF_FORCE_PRST) &&
Emeric Brun52a91d32017-08-31 14:41:55 +0200769 ((__objt_server(s->target)->cur_state < SRV_ST_RUNNING) ||
Willy Tarreau49008c12016-01-13 07:58:44 +0100770 (((s->be->redispatch_after > 0) &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700771 ((s->be->conn_retries - si->conn_retries) %
772 s->be->redispatch_after == 0)) ||
773 ((s->be->redispatch_after < 0) &&
774 ((s->be->conn_retries - si->conn_retries) %
775 (s->be->conn_retries + 1 + s->be->redispatch_after) == 0))) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +0200776 (!(s->flags & SF_DIRECT) && s->be->srv_act > 1 &&
Joseph Lynch726ab712015-05-11 23:25:34 -0700777 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR)))) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100778 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100779 if (may_dequeue_tasks(objt_server(s->target), s->be))
780 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100781
Willy Tarreaue7dff022015-04-03 01:14:29 +0200782 s->flags &= ~(SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100783 si->state = SI_ST_REQ;
784 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100785 if (objt_server(s->target))
Christopher Faulet29f77e82017-06-08 14:04:45 +0200786 HA_ATOMIC_ADD(&objt_server(s->target)->counters.retries, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200787 HA_ATOMIC_ADD(&s->be->be_counters.retries, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100788 si->state = SI_ST_ASS;
789 }
790
791 if (si->flags & SI_FL_ERR) {
792 /* The error was an asynchronous connection error, and we will
793 * likely have to retry connecting to the same server, most
794 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200795 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100796 */
797
Willy Tarreaub0290662014-06-13 17:04:44 +0200798 int delay = 1000;
799
800 if (s->be->timeout.connect && s->be->timeout.connect < delay)
801 delay = s->be->timeout.connect;
802
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100803 if (!si->err_type)
804 si->err_type = SI_ET_CONN_ERR;
805
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200806 /* only wait when we're retrying on the same server */
807 if (si->state == SI_ST_ASS ||
808 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
809 (s->be->srv_act <= 1)) {
810 si->state = SI_ST_TAR;
811 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
812 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100813 return 0;
814 }
815 return 0;
816}
817
818/*
819 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200820 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200821 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200823static void sess_establish(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100824{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100825 struct stream_interface *si = &s->si[1];
826 struct channel *req = &s->req;
827 struct channel *rep = &s->res;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100828
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100829 /* First, centralize the timers information */
830 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
831 si->exp = TICK_ETERNITY;
832
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100833 if (objt_server(s->target))
834 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100835
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100836 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100837 /* if the user wants to log as soon as possible, without counting
838 * bytes from the server, then this is the right moment. */
Willy Tarreaud0d8da92015-04-04 02:10:38 +0200839 if (!LIST_ISEMPTY(&strm_fe(s)->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau66425e32018-07-25 06:55:12 +0200840 /* note: no pend_pos here, session is established */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100841 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100842 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100843 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100844 }
845 else {
Willy Tarreaud81ca042013-12-31 22:33:13 +0100846 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100847 }
848
Willy Tarreaud62b98c2016-12-13 15:26:56 +0100849 if (!(s->flags & SF_TUNNEL)) {
850 rep->analysers |= strm_fe(s)->fe_rsp_ana | s->be->be_rsp_ana;
Christopher Faulet309c6412015-12-02 09:57:32 +0100851
Willy Tarreaud62b98c2016-12-13 15:26:56 +0100852 /* Be sure to filter response headers if the backend is an HTTP proxy
853 * and if there are filters attached to the stream. */
854 if (s->be->mode == PR_MODE_HTTP && HAS_FILTERS(s))
855 rep->analysers |= AN_RES_FLT_HTTP_HDRS;
856 }
Christopher Faulet309c6412015-12-02 09:57:32 +0100857
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +0100858 si_rx_endp_more(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200859 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200860 if (req->flags & CF_WAKE_CONNECT) {
861 req->flags |= CF_WAKE_ONCE;
862 req->flags &= ~CF_WAKE_CONNECT;
863 }
Olivier Houchard9aaf7782017-09-13 18:30:23 +0200864 if (objt_cs(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200865 /* real connections have timeouts */
866 req->wto = s->be->timeout.server;
867 rep->rto = s->be->timeout.server;
Olivier Houchard47e9a1a2018-11-07 17:55:19 +0100868 /* The connection is now established, try to read data from the
869 * underlying layer, and subscribe to recv events. We use a
870 * delayed recv here to give a chance to the data to flow back
871 * by the time we process other tasks.
872 */
873 si_chk_rcv(si);
Willy Tarreaud04e8582010-05-31 12:31:35 +0200874 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100875 req->wex = TICK_ETERNITY;
876}
877
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700878/* Check if the connection request is in such a state that it can be aborted. */
879static int check_req_may_abort(struct channel *req, struct stream *s)
880{
881 return ((req->flags & (CF_READ_ERROR)) ||
Willy Tarreaud651ba12018-01-12 10:42:12 +0100882 ((req->flags & (CF_SHUTW_NOW|CF_SHUTW)) && /* empty and client aborted */
Willy Tarreau7aa15b02017-12-20 16:56:50 +0100883 (channel_is_empty(req) ||
884 ((s->be->options & PR_O_ABRT_CLOSE) && !(s->si[0].flags & SI_FL_CLEAN_ABRT)))));
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700885}
886
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100887/* Update back stream interface status for input states SI_ST_ASS, SI_ST_QUE,
888 * SI_ST_TAR. Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100889 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
890 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
891 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100892 */
Willy Tarreau87b09662015-04-03 00:22:06 +0200893static void sess_update_stream_int(struct stream *s)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100894{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100895 struct server *srv = objt_server(s->target);
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100896 struct stream_interface *si = &s->si[1];
Willy Tarreau103197d2014-11-28 15:26:12 +0100897 struct channel *req = &s->req;
Willy Tarreau827aee92011-03-10 16:55:02 +0100898
Christopher Faulet45073512018-07-20 10:16:29 +0200899 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%lu rqt=%lu rph=%lu rpt=%lu cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100900 now_ms, __FUNCTION__,
901 s,
CJ Ess6eac32e2015-05-02 16:35:24 -0400902 req, &s->res,
Willy Tarreau103197d2014-11-28 15:26:12 +0100903 req->rex, s->res.wex,
904 req->flags, s->res.flags,
Christopher Faulet45073512018-07-20 10:16:29 +0200905 ci_data(req), co_data(req), ci_data(&s->res), co_data(&s->res), s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100906
907 if (si->state == SI_ST_ASS) {
908 /* Server assigned to connection request, we have to try to connect now */
909 int conn_err;
910
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -0700911 /* Before we try to initiate the connection, see if the
912 * request may be aborted instead.
913 */
914 if (check_req_may_abort(req, s)) {
915 si->err_type |= SI_ET_CONN_ABRT;
916 goto abort_connection;
917 }
918
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100920 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100921
Willy Tarreaue7dff022015-04-03 01:14:29 +0200922 if (conn_err == SF_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100923 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +0100924 if (srv)
925 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500926 if (srv)
927 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100928 return;
929 }
930
931 /* We have received a synchronous error. We might have to
932 * abort, retry immediately or redispatch.
933 */
Willy Tarreaue7dff022015-04-03 01:14:29 +0200934 if (conn_err == SF_ERR_INTERNAL) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100935 if (!si->err_type) {
936 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100937 }
938
Willy Tarreau827aee92011-03-10 16:55:02 +0100939 if (srv)
940 srv_inc_sess_ctr(srv);
941 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -0500942 srv_set_sess_last(srv);
943 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +0200944 HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +0200945 HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100946
Willy Tarreau87b09662015-04-03 00:22:06 +0200947 /* release other streams waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100948 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +0100949 if (may_dequeue_tasks(srv, s->be))
950 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100951
952 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200953 si_shutr(si);
954 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +0100955 req->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100956
957 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
958
Willy Tarreau66425e32018-07-25 06:55:12 +0200959 /* we may need to know the position in the queue for logging */
960 pendconn_cond_unlink(s->pend_pos);
961
Willy Tarreau87b09662015-04-03 00:22:06 +0200962 /* no stream was ever accounted for this server */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100963 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100964 if (s->srv_error)
965 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100966 return;
967 }
968
969 /* We are facing a retryable error, but we don't want to run a
970 * turn-around now, as the problem is likely a source port
971 * allocation problem, so we want to retry now.
972 */
973 si->state = SI_ST_CER;
974 si->flags &= ~SI_FL_ERR;
Willy Tarreau7b8c4f92014-11-28 15:15:44 +0100975 sess_update_st_cer(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100976 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
977 return;
978 }
979 else if (si->state == SI_ST_QUE) {
980 /* connection request was queued, check for any update */
Christopher Faulet5cd4bbd2018-03-14 16:18:06 +0100981 if (!pendconn_dequeue(s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100982 /* The connection is not in the queue anymore. Either
983 * we have a server connection slot available and we
984 * go directly to the assigned state, or we need to
985 * load-balance first and go to the INI state.
986 */
987 si->exp = TICK_ETERNITY;
Willy Tarreaue7dff022015-04-03 01:14:29 +0200988 if (unlikely(!(s->flags & SF_ASSIGNED)))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100989 si->state = SI_ST_REQ;
990 else {
991 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
992 si->state = SI_ST_ASS;
993 }
994 return;
995 }
996
997 /* Connection request still in queue... */
998 if (si->flags & SI_FL_EXP) {
999 /* ... and timeout expired */
1000 si->exp = TICK_ETERNITY;
1001 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau66425e32018-07-25 06:55:12 +02001002
1003 /* we may need to know the position in the queue for logging */
1004 pendconn_cond_unlink(s->pend_pos);
1005
Willy Tarreau827aee92011-03-10 16:55:02 +01001006 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02001007 HA_ATOMIC_ADD(&srv->counters.failed_conns, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001008 HA_ATOMIC_ADD(&s->be->be_counters.failed_conns, 1);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001009 si_shutr(si);
1010 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001011 req->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001012 if (!si->err_type)
1013 si->err_type = SI_ET_QUEUE_TO;
1014 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001015 if (s->srv_error)
1016 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001017 return;
1018 }
1019
1020 /* Connection remains in queue, check if we have to abort it */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001021 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001022 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau66425e32018-07-25 06:55:12 +02001023
1024 /* we may need to know the position in the queue for logging */
1025 pendconn_cond_unlink(s->pend_pos);
1026
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001027 si->err_type |= SI_ET_QUEUE_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001028 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001029 }
1030
1031 /* Nothing changed */
1032 return;
1033 }
1034 else if (si->state == SI_ST_TAR) {
1035 /* Connection request might be aborted */
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001036 if (check_req_may_abort(req, s)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001037 si->err_type |= SI_ET_CONN_ABRT;
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001038 goto abort_connection;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001039 }
1040
1041 if (!(si->flags & SI_FL_EXP))
1042 return; /* still in turn-around */
1043
1044 si->exp = TICK_ETERNITY;
1045
Willy Tarreau87b09662015-04-03 00:22:06 +02001046 /* we keep trying on the same server as long as the stream is
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001047 * marked "assigned".
1048 * FIXME: Should we force a redispatch attempt when the server is down ?
1049 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001050 if (s->flags & SF_ASSIGNED)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001051 si->state = SI_ST_ASS;
1052 else
1053 si->state = SI_ST_REQ;
1054 return;
1055 }
Frederik Deweerdt6cd8d132016-04-07 09:01:04 -07001056 return;
1057
1058abort_connection:
1059 /* give up */
1060 si->exp = TICK_ETERNITY;
1061 si_shutr(si);
1062 si_shutw(si);
1063 si->state = SI_ST_CLO;
1064 if (s->srv_error)
1065 s->srv_error(s, si);
1066 return;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001067}
1068
Willy Tarreau87b09662015-04-03 00:22:06 +02001069/* Set correct stream termination flags in case no analyser has done it. It
Simon Hormandec5be42011-06-08 09:19:07 +09001070 * also counts a failed request if the server state has not reached the request
1071 * stage.
1072 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001073static void sess_set_term_flags(struct stream *s)
Simon Hormandec5be42011-06-08 09:19:07 +09001074{
Willy Tarreaue7dff022015-04-03 01:14:29 +02001075 if (!(s->flags & SF_FINST_MASK)) {
Simon Hormandec5be42011-06-08 09:19:07 +09001076 if (s->si[1].state < SI_ST_REQ) {
1077
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001078 HA_ATOMIC_ADD(&strm_fe(s)->fe_counters.failed_req, 1);
Willy Tarreau2c1068c2015-09-23 12:21:21 +02001079 if (strm_li(s) && strm_li(s)->counters)
Christopher Faulet8d8aa0d2017-05-30 15:36:50 +02001080 HA_ATOMIC_ADD(&strm_li(s)->counters->failed_req, 1);
Simon Hormandec5be42011-06-08 09:19:07 +09001081
Willy Tarreaue7dff022015-04-03 01:14:29 +02001082 s->flags |= SF_FINST_R;
Simon Hormandec5be42011-06-08 09:19:07 +09001083 }
1084 else if (s->si[1].state == SI_ST_QUE)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001085 s->flags |= SF_FINST_Q;
Simon Hormandec5be42011-06-08 09:19:07 +09001086 else if (s->si[1].state < SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001087 s->flags |= SF_FINST_C;
Simon Hormandec5be42011-06-08 09:19:07 +09001088 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
Willy Tarreaue7dff022015-04-03 01:14:29 +02001089 s->flags |= SF_FINST_D;
Simon Hormandec5be42011-06-08 09:19:07 +09001090 else
Willy Tarreaue7dff022015-04-03 01:14:29 +02001091 s->flags |= SF_FINST_L;
Simon Hormandec5be42011-06-08 09:19:07 +09001092 }
1093}
1094
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001095/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001096 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
1097 * a real connection to a server, indicating that a server has been assigned,
1098 * or SI_ST_EST for a successful connection to an applet. It may also return
1099 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001100 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001101static void sess_prepare_conn_req(struct stream *s)
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001102{
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001103 struct stream_interface *si = &s->si[1];
1104
Christopher Faulet45073512018-07-20 10:16:29 +02001105 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%lu rqt=%lu rph=%lu rpt=%lu cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001106 now_ms, __FUNCTION__,
1107 s,
CJ Ess6eac32e2015-05-02 16:35:24 -04001108 &s->req, &s->res,
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001109 s->req.rex, s->res.wex,
1110 s->req.flags, s->res.flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001111 ci_data(&s->req), co_data(&s->req), ci_data(&s->res), co_data(&s->res), s->si[0].state, s->si[1].state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001112
1113 if (si->state != SI_ST_REQ)
1114 return;
1115
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001116 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1117 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001118 struct appctx *appctx = objt_appctx(si->end);
1119
1120 if (!appctx || appctx->applet != __objt_applet(s->target))
1121 appctx = stream_int_register_handler(si, objt_applet(s->target));
1122
1123 if (!appctx) {
1124 /* No more memory, let's immediately abort. Force the
1125 * error code to ignore the ERR_LOCAL which is not a
1126 * real error.
1127 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001128 s->flags &= ~(SF_ERR_MASK | SF_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001129
1130 si_shutr(si);
1131 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001132 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001133 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001134 si->state = SI_ST_CLO;
1135 if (s->srv_error)
1136 s->srv_error(s, si);
1137 return;
1138 }
1139
Willy Tarreauee8269e2017-11-23 17:34:29 +01001140 if (tv_iszero(&s->logs.tv_request))
1141 s->logs.tv_request = now;
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001142 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001143 si->state = SI_ST_EST;
1144 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001145 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001146 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001147 return;
1148 }
1149
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001150 /* Try to assign a server */
1151 if (srv_redispatch_connect(s) != 0) {
1152 /* We did not get a server. Either we queued the
1153 * connection request, or we encountered an error.
1154 */
1155 if (si->state == SI_ST_QUE)
1156 return;
1157
1158 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001159 si_shutr(si);
1160 si_shutw(si);
Willy Tarreau103197d2014-11-28 15:26:12 +01001161 s->req.flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001162 if (!si->err_type)
1163 si->err_type = SI_ET_CONN_OTHER;
1164 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001165 if (s->srv_error)
1166 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001167 return;
1168 }
1169
1170 /* The server is assigned */
1171 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1172 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001173 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001174}
1175
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001176/* This function parses the use-service action ruleset. It executes
1177 * the associated ACL and set an applet as a stream or txn final node.
1178 * it returns ACT_RET_ERR if an error occurs, the proxy left in
1179 * consistent state. It returns ACT_RET_STOP in succes case because
1180 * use-service must be a terminal action. Returns ACT_RET_YIELD
1181 * if the initialisation function require more data.
1182 */
1183enum act_return process_use_service(struct act_rule *rule, struct proxy *px,
1184 struct session *sess, struct stream *s, int flags)
1185
1186{
1187 struct appctx *appctx;
1188
1189 /* Initialises the applet if it is required. */
1190 if (flags & ACT_FLAG_FIRST) {
1191 /* Register applet. this function schedules the applet. */
1192 s->target = &rule->applet.obj_type;
1193 if (unlikely(!stream_int_register_handler(&s->si[1], objt_applet(s->target))))
1194 return ACT_RET_ERR;
1195
1196 /* Initialise the context. */
1197 appctx = si_appctx(&s->si[1]);
1198 memset(&appctx->ctx, 0, sizeof(appctx->ctx));
1199 appctx->rule = rule;
Willy Tarreau85cb0ae2017-08-23 10:52:20 +02001200
1201 /* enable the minimally required analyzers in case of HTTP
1202 * keep-alive to properly handle keep-alive and compression
1203 * on the HTTP response.
1204 */
1205 if (rule->from == ACT_F_HTTP_REQ) {
1206 s->req.analysers &= AN_REQ_FLT_HTTP_HDRS | AN_REQ_FLT_END;
1207 s->req.analysers |= AN_REQ_HTTP_XFER_BODY;
1208 }
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001209 }
1210 else
1211 appctx = si_appctx(&s->si[1]);
1212
1213 /* Stops the applet sheduling, in case of the init function miss
1214 * some data.
1215 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001216 si_stop_get(&s->si[1]);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001217
1218 /* Call initialisation. */
1219 if (rule->applet.init)
1220 switch (rule->applet.init(appctx, px, s)) {
1221 case 0: return ACT_RET_ERR;
1222 case 1: break;
1223 default: return ACT_RET_YIELD;
1224 }
1225
1226 /* Now we can schedule the applet. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001227 si_cant_get(&s->si[1]);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001228 appctx_wakeup(appctx);
1229
1230 if (sess->fe == s->be) /* report it if the request was intercepted by the frontend */
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001231 HA_ATOMIC_ADD(&sess->fe->fe_counters.intercepted_req, 1);
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02001232
1233 /* The flag SF_ASSIGNED prevent from server assignment. */
1234 s->flags |= SF_ASSIGNED;
1235
1236 return ACT_RET_STOP;
1237}
1238
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001239/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001240 * if appropriate. The default_backend rule is also considered, then the
1241 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001242 * It returns 1 if the processing can continue on next analysers, or zero if it
1243 * either needs more data or wants to immediately abort the request.
1244 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001245static int process_switching_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001246{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001247 struct persist_rule *prst_rule;
Willy Tarreau192252e2015-04-04 01:47:55 +02001248 struct session *sess = s->sess;
1249 struct proxy *fe = sess->fe;
Willy Tarreau4de91492010-01-22 19:10:05 +01001250
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001251 req->analysers &= ~an_bit;
1252 req->analyse_exp = TICK_ETERNITY;
1253
Christopher Faulet45073512018-07-20 10:16:29 +02001254 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001255 now_ms, __FUNCTION__,
1256 s,
1257 req,
1258 req->rex, req->wex,
1259 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001260 ci_data(req),
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001261 req->analysers);
1262
1263 /* now check whether we have some switching rules for this request */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001264 if (!(s->flags & SF_BE_ASSIGNED)) {
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001265 struct switching_rule *rule;
1266
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001267 list_for_each_entry(rule, &fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001268 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001269
Willy Tarreauf51658d2014-04-23 01:21:56 +02001270 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001271 ret = acl_exec_cond(rule->cond, fe, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreauf51658d2014-04-23 01:21:56 +02001272 ret = acl_pass(ret);
1273 if (rule->cond->pol == ACL_COND_UNLESS)
1274 ret = !ret;
1275 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001276
1277 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001278 /* If the backend name is dynamic, try to resolve the name.
1279 * If we can't resolve the name, or if any error occurs, break
1280 * the loop and fallback to the default backend.
1281 */
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001282 struct proxy *backend = NULL;
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001283
1284 if (rule->dynamic) {
Willy Tarreau83061a82018-07-13 11:56:34 +02001285 struct buffer *tmp;
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001286
1287 tmp = alloc_trash_chunk();
1288 if (!tmp)
1289 goto sw_failed;
1290
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001291 if (build_logline(s, tmp->area, tmp->size, &rule->be.expr))
1292 backend = proxy_be_by_name(tmp->area);
Dragan Dosen2ae327e2017-10-26 11:25:10 +02001293
1294 free_trash_chunk(tmp);
1295 tmp = NULL;
1296
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001297 if (!backend)
1298 break;
1299 }
1300 else
1301 backend = rule->be.backend;
1302
Willy Tarreau87b09662015-04-03 00:22:06 +02001303 if (!stream_set_backend(s, backend))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001304 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001305 break;
1306 }
1307 }
1308
1309 /* To ensure correct connection accounting on the backend, we
1310 * have to assign one if it was not set (eg: a listen). This
1311 * measure also takes care of correctly setting the default
1312 * backend if any.
1313 */
Willy Tarreaue7dff022015-04-03 01:14:29 +02001314 if (!(s->flags & SF_BE_ASSIGNED))
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02001315 if (!stream_set_backend(s, fe->defbe.be ? fe->defbe.be : s->be))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001316 goto sw_failed;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001317 }
1318
Willy Tarreaufb356202010-08-03 14:02:05 +02001319 /* 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 +02001320 if (fe == s->be) {
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001321 s->req.analysers &= ~AN_REQ_INSPECT_BE;
1322 s->req.analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001323 s->req.analysers &= ~AN_REQ_FLT_START_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001324 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001325
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001326 /* 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 +02001327 * persistence rule, and report that in the stream.
Willy Tarreau4de91492010-01-22 19:10:05 +01001328 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001329 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001330 int ret = 1;
1331
1332 if (prst_rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001333 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 +01001334 ret = acl_pass(ret);
1335 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1336 ret = !ret;
1337 }
1338
1339 if (ret) {
1340 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001341 if (prst_rule->type == PERSIST_TYPE_FORCE) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001342 s->flags |= SF_FORCE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001343 } else {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001344 s->flags |= SF_IGNORE_PRST;
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001345 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001346 break;
1347 }
1348 }
1349
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001350 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001351
1352 sw_failed:
1353 /* immediately abort this request in case of allocation failure */
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001354 channel_abort(&s->req);
1355 channel_abort(&s->res);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001356
Willy Tarreaue7dff022015-04-03 01:14:29 +02001357 if (!(s->flags & SF_ERR_MASK))
1358 s->flags |= SF_ERR_RESOURCE;
1359 if (!(s->flags & SF_FINST_MASK))
1360 s->flags |= SF_FINST_R;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001361
Willy Tarreaueee5b512015-04-03 23:46:31 +02001362 if (s->txn)
1363 s->txn->status = 500;
Christopher Faulet0184ea72017-01-05 14:06:34 +01001364 s->req.analysers &= AN_REQ_FLT_END;
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01001365 s->req.analyse_exp = TICK_ETERNITY;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001366 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001367}
1368
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001369/* This stream analyser works on a request. It applies all use-server rules on
1370 * it then returns 1. The data must already be present in the buffer otherwise
1371 * they won't match. It always returns 1.
1372 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001373static int process_server_rules(struct stream *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001374{
1375 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001376 struct session *sess = s->sess;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001377 struct server_rule *rule;
1378
Christopher Faulet45073512018-07-20 10:16:29 +02001379 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%lu analysers=%02x\n",
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001380 now_ms, __FUNCTION__,
1381 s,
1382 req,
1383 req->rex, req->wex,
1384 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001385 c_data(req),
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001386 req->analysers);
1387
Willy Tarreaue7dff022015-04-03 01:14:29 +02001388 if (!(s->flags & SF_ASSIGNED)) {
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001389 list_for_each_entry(rule, &px->server_rules, list) {
1390 int ret;
1391
Willy Tarreau192252e2015-04-04 01:47:55 +02001392 ret = acl_exec_cond(rule->cond, s->be, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001393 ret = acl_pass(ret);
1394 if (rule->cond->pol == ACL_COND_UNLESS)
1395 ret = !ret;
1396
1397 if (ret) {
1398 struct server *srv = rule->srv.ptr;
1399
Emeric Brun52a91d32017-08-31 14:41:55 +02001400 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001401 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001402 (s->flags & SF_FORCE_PRST)) {
1403 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001404 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001405 break;
1406 }
1407 /* if the server is not UP, let's go on with next rules
1408 * just in case another one is suited.
1409 */
1410 }
1411 }
1412 }
1413
1414 req->analysers &= ~an_bit;
1415 req->analyse_exp = TICK_ETERNITY;
1416 return 1;
1417}
1418
Emeric Brun1d33b292010-01-04 15:47:17 +01001419/* This stream analyser works on a request. It applies all sticking rules on
1420 * it then returns 1. The data must already be present in the buffer otherwise
1421 * they won't match. It always returns 1.
1422 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001423static int process_sticking_rules(struct stream *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001424{
1425 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001426 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001427 struct sticking_rule *rule;
1428
Christopher Faulet45073512018-07-20 10:16:29 +02001429 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001430 now_ms, __FUNCTION__,
1431 s,
1432 req,
1433 req->rex, req->wex,
1434 req->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001435 ci_data(req),
Emeric Brun1d33b292010-01-04 15:47:17 +01001436 req->analysers);
1437
1438 list_for_each_entry(rule, &px->sticking_rules, list) {
1439 int ret = 1 ;
1440 int i;
1441
Willy Tarreau9667a802013-12-09 12:52:13 +01001442 /* Only the first stick store-request of each table is applied
1443 * and other ones are ignored. The purpose is to allow complex
1444 * configurations which look for multiple entries by decreasing
1445 * order of precision and to stop at the first which matches.
1446 * An example could be a store of the IP address from an HTTP
1447 * header first, then from the source if not found.
1448 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001449 for (i = 0; i < s->store_count; i++) {
1450 if (rule->table.t == s->store[i].table)
1451 break;
1452 }
1453
1454 if (i != s->store_count)
1455 continue;
1456
1457 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001458 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001459 ret = acl_pass(ret);
1460 if (rule->cond->pol == ACL_COND_UNLESS)
1461 ret = !ret;
1462 }
1463
1464 if (ret) {
1465 struct stktable_key *key;
1466
Willy Tarreau192252e2015-04-04 01:47:55 +02001467 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 +01001468 if (!key)
1469 continue;
1470
1471 if (rule->flags & STK_IS_MATCH) {
1472 struct stksess *ts;
1473
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001474 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001475 if (!(s->flags & SF_ASSIGNED)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001476 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001477 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001478
1479 /* srv found in table */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001480 HA_RWLOCK_RDLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaub8d42d02018-09-20 11:11:15 +02001481 ptr = __stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau13c29de2010-06-06 16:40:39 +02001482 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001483 HA_RWLOCK_RDUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun1d33b292010-01-04 15:47:17 +01001484 if (node) {
1485 struct server *srv;
1486
1487 srv = container_of(node, struct server, conf.id);
Emeric Brun52a91d32017-08-31 14:41:55 +02001488 if ((srv->cur_state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001489 (px->options & PR_O_PERSIST) ||
Willy Tarreaue7dff022015-04-03 01:14:29 +02001490 (s->flags & SF_FORCE_PRST)) {
1491 s->flags |= SF_DIRECT | SF_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001492 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001493 }
1494 }
1495 }
Emeric Brun819fc6f2017-06-13 19:37:32 +02001496 stktable_touch_local(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001497 }
1498 }
1499 if (rule->flags & STK_IS_STORE) {
1500 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1501 struct stksess *ts;
1502
1503 ts = stksess_new(rule->table.t, key);
1504 if (ts) {
1505 s->store[s->store_count].table = rule->table.t;
1506 s->store[s->store_count++].ts = ts;
1507 }
1508 }
1509 }
1510 }
1511 }
1512
1513 req->analysers &= ~an_bit;
1514 req->analyse_exp = TICK_ETERNITY;
1515 return 1;
1516}
1517
1518/* This stream analyser works on a response. It applies all store rules on it
1519 * then returns 1. The data must already be present in the buffer otherwise
1520 * they won't match. It always returns 1.
1521 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001522static int process_store_rules(struct stream *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001523{
1524 struct proxy *px = s->be;
Willy Tarreau192252e2015-04-04 01:47:55 +02001525 struct session *sess = s->sess;
Emeric Brun1d33b292010-01-04 15:47:17 +01001526 struct sticking_rule *rule;
1527 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001528 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001529
Christopher Faulet45073512018-07-20 10:16:29 +02001530 DPRINTF(stderr,"[%u] %s: stream=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%lu analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001531 now_ms, __FUNCTION__,
1532 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001533 rep,
1534 rep->rex, rep->wex,
1535 rep->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001536 ci_data(rep),
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001537 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001538
1539 list_for_each_entry(rule, &px->storersp_rules, list) {
1540 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001541
Willy Tarreau9667a802013-12-09 12:52:13 +01001542 /* Only the first stick store-response of each table is applied
1543 * and other ones are ignored. The purpose is to allow complex
1544 * configurations which look for multiple entries by decreasing
1545 * order of precision and to stop at the first which matches.
1546 * An example could be a store of a set-cookie value, with a
1547 * fallback to a parameter found in a 302 redirect.
1548 *
1549 * The store-response rules are not allowed to override the
1550 * store-request rules for the same table, but they may coexist.
1551 * Thus we can have up to one store-request entry and one store-
1552 * response entry for the same table at any time.
1553 */
1554 for (i = nbreq; i < s->store_count; i++) {
1555 if (rule->table.t == s->store[i].table)
1556 break;
1557 }
1558
1559 /* skip existing entries for this table */
1560 if (i < s->store_count)
1561 continue;
1562
Emeric Brun1d33b292010-01-04 15:47:17 +01001563 if (rule->cond) {
Willy Tarreau192252e2015-04-04 01:47:55 +02001564 ret = acl_exec_cond(rule->cond, px, sess, s, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001565 ret = acl_pass(ret);
1566 if (rule->cond->pol == ACL_COND_UNLESS)
1567 ret = !ret;
1568 }
1569
1570 if (ret) {
1571 struct stktable_key *key;
1572
Willy Tarreau192252e2015-04-04 01:47:55 +02001573 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 +01001574 if (!key)
1575 continue;
1576
Willy Tarreau37e340c2013-12-06 23:05:21 +01001577 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001578 struct stksess *ts;
1579
1580 ts = stksess_new(rule->table.t, key);
1581 if (ts) {
1582 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001583 s->store[s->store_count++].ts = ts;
1584 }
1585 }
1586 }
1587 }
1588
1589 /* process store request and store response */
1590 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001591 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001592 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001593
Willy Tarreauc93cd162014-05-13 15:54:22 +02001594 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001595 stksess_free(s->store[i].table, s->store[i].ts);
1596 s->store[i].ts = NULL;
1597 continue;
1598 }
1599
Emeric Brun819fc6f2017-06-13 19:37:32 +02001600 ts = stktable_set_entry(s->store[i].table, s->store[i].ts);
1601 if (ts != s->store[i].ts) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001602 /* the entry already existed, we can free ours */
Emeric Brun1d33b292010-01-04 15:47:17 +01001603 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001604 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001605 s->store[i].ts = NULL;
Emeric Brun819fc6f2017-06-13 19:37:32 +02001606
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001607 HA_RWLOCK_WRLOCK(STK_SESS_LOCK, &ts->lock);
Willy Tarreaub8d42d02018-09-20 11:11:15 +02001608 ptr = __stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
1609 stktable_data_cast(ptr, server_id) = __objt_server(s->target)->puid;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001610 HA_RWLOCK_WRUNLOCK(STK_SESS_LOCK, &ts->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +02001611 stktable_touch_local(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001612 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001613 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001614
1615 rep->analysers &= ~an_bit;
1616 rep->analyse_exp = TICK_ETERNITY;
1617 return 1;
1618}
1619
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001620/* This macro is very specific to the function below. See the comments in
Willy Tarreau87b09662015-04-03 00:22:06 +02001621 * process_stream() below to understand the logic and the tests.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001622 */
1623#define UPDATE_ANALYSERS(real, list, back, flag) { \
1624 list = (((list) & ~(flag)) | ~(back)) & (real); \
1625 back = real; \
1626 if (!(list)) \
1627 break; \
1628 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1629 continue; \
1630}
1631
Christopher Fauleta9215b72016-05-11 17:06:28 +02001632/* These 2 following macros call an analayzer for the specified channel if the
1633 * right flag is set. The first one is used for "filterable" analyzers. If a
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001634 * stream has some registered filters, pre and post analyaze callbacks are
Christopher Faulet0184ea72017-01-05 14:06:34 +01001635 * called. The second are used for other analyzers (AN_REQ/RES_FLT_* and
Christopher Fauleta9215b72016-05-11 17:06:28 +02001636 * AN_REQ/RES_HTTP_XFER_BODY) */
1637#define FLT_ANALYZE(strm, chn, fun, list, back, flag, ...) \
1638 { \
1639 if ((list) & (flag)) { \
1640 if (HAS_FILTERS(strm)) { \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001641 if (!flt_pre_analyze((strm), (chn), (flag))) \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001642 break; \
1643 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1644 break; \
Christopher Faulet3a394fa2016-05-11 17:13:39 +02001645 if (!flt_post_analyze((strm), (chn), (flag))) \
1646 break; \
Christopher Fauleta9215b72016-05-11 17:06:28 +02001647 } \
1648 else { \
1649 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1650 break; \
1651 } \
1652 UPDATE_ANALYSERS((chn)->analysers, (list), \
1653 (back), (flag)); \
1654 } \
1655 }
1656
1657#define ANALYZE(strm, chn, fun, list, back, flag, ...) \
1658 { \
1659 if ((list) & (flag)) { \
1660 if (!fun((strm), (chn), (flag), ##__VA_ARGS__)) \
1661 break; \
1662 UPDATE_ANALYSERS((chn)->analysers, (list), \
1663 (back), (flag)); \
1664 } \
1665 }
1666
Willy Tarreau87b09662015-04-03 00:22:06 +02001667/* Processes the client, server, request and response jobs of a stream task,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001668 * then puts it back to the wait queue in a clean state, or cleans up its
1669 * resources if it must be deleted. Returns in <next> the date the task wants
1670 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1671 * nothing too many times, the request and response buffers flags are monitored
1672 * and each function is called only if at least another function has changed at
1673 * least one flag it is interested in.
1674 */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001675struct task *process_stream(struct task *t, void *context, unsigned short state)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001676{
Willy Tarreau827aee92011-03-10 16:55:02 +01001677 struct server *srv;
Olivier Houchard9f6af332018-05-25 14:04:04 +02001678 struct stream *s = context;
Willy Tarreaufb0afa72015-04-03 14:46:27 +02001679 struct session *sess = s->sess;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001680 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001681 unsigned int rq_prod_last, rq_cons_last;
1682 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001683 unsigned int req_ana_back;
Willy Tarreau8f128b42014-11-28 15:07:47 +01001684 struct channel *req, *res;
1685 struct stream_interface *si_f, *si_b;
1686
Willy Tarreaud80cb4e2018-01-20 19:30:13 +01001687 activity[tid].stream++;
1688
Willy Tarreau8f128b42014-11-28 15:07:47 +01001689 req = &s->req;
1690 res = &s->res;
1691
1692 si_f = &s->si[0];
1693 si_b = &s->si[1];
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001694
Willy Tarreau0f8d3ab2018-10-25 10:42:39 +02001695 /* First, attempt to receive pending data from I/O layers */
Willy Tarreauade64782018-11-17 19:51:07 +01001696 si_sync_recv(si_f);
1697 si_sync_recv(si_b);
Willy Tarreau0f8d3ab2018-10-25 10:42:39 +02001698
Olivier Houchard53216e72018-10-10 15:46:36 +02001699redo:
Olivier Houchardc2aa7112018-09-11 18:27:21 +02001700
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001701 //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 +01001702 // si_f->state, si_b->state, si_b->err_type, req->flags, res->flags);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001703
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001704 /* this data may be no longer valid, clear it */
Willy Tarreaueee5b512015-04-03 23:46:31 +02001705 if (s->txn)
1706 memset(&s->txn->auth, 0, sizeof(s->txn->auth));
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001707
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02001708 /* This flag must explicitly be set every time */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001709 req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1710 res->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001711
1712 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001713 rqf_last = req->flags & ~CF_MASK_ANALYSER;
1714 rpf_last = res->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001715
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001716 /* we don't want the stream interface functions to recursively wake us up */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001717 si_f->flags |= SI_FL_DONT_WAKE;
1718 si_b->flags |= SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001719
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001720 /* update pending events */
Olivier Houchard9f6af332018-05-25 14:04:04 +02001721 s->pending_events |= (state & TASK_WOKEN_ANY);
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001722
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001723 /* 1a: Check for low level timeouts if needed. We just set a flag on
1724 * stream interfaces when their timeouts have expired.
1725 */
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001726 if (unlikely(s->pending_events & TASK_WOKEN_TIMER)) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001727 stream_int_check_timeouts(si_f);
1728 stream_int_check_timeouts(si_b);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001729
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001730 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001731 * for future reads or writes. Note: this will also concern upper layers
1732 * but we do not touch any other flag. We must be careful and correctly
1733 * detect state changes when calling them.
1734 */
1735
Willy Tarreau8f128b42014-11-28 15:07:47 +01001736 channel_check_timeouts(req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001737
Willy Tarreau8f128b42014-11-28 15:07:47 +01001738 if (unlikely((req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1739 si_b->flags |= SI_FL_NOLINGER;
1740 si_shutw(si_b);
Willy Tarreau14641402009-12-29 14:49:56 +01001741 }
1742
Willy Tarreau8f128b42014-11-28 15:07:47 +01001743 if (unlikely((req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1744 if (si_f->flags & SI_FL_NOHALF)
1745 si_f->flags |= SI_FL_NOLINGER;
1746 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001747 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001748
Willy Tarreau8f128b42014-11-28 15:07:47 +01001749 channel_check_timeouts(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001750
Willy Tarreau8f128b42014-11-28 15:07:47 +01001751 if (unlikely((res->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
1752 si_f->flags |= SI_FL_NOLINGER;
1753 si_shutw(si_f);
Willy Tarreau14641402009-12-29 14:49:56 +01001754 }
1755
Willy Tarreau8f128b42014-11-28 15:07:47 +01001756 if (unlikely((res->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
1757 if (si_b->flags & SI_FL_NOHALF)
1758 si_b->flags |= SI_FL_NOLINGER;
1759 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001760 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001761
Christopher Fauleta00d8172016-11-10 14:58:05 +01001762 if (HAS_FILTERS(s))
1763 flt_stream_check_timeouts(s);
1764
Willy Tarreau798f4322012-11-08 14:49:17 +01001765 /* Once in a while we're woken up because the task expires. But
1766 * this does not necessarily mean that a timeout has been reached.
Willy Tarreau87b09662015-04-03 00:22:06 +02001767 * So let's not run a whole stream processing if only an expiration
Willy Tarreau798f4322012-11-08 14:49:17 +01001768 * timeout needs to be refreshed.
1769 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001770 if (!((req->flags | res->flags) &
Willy Tarreau798f4322012-11-08 14:49:17 +01001771 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
Willy Tarreauede3d882018-10-24 17:17:56 +02001772 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01001773 !((si_f->flags | si_b->flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001774 ((s->pending_events & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
Willy Tarreau5fb04712016-05-04 10:18:37 +02001775 si_f->flags &= ~SI_FL_DONT_WAKE;
1776 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001777 goto update_exp_and_leave;
Willy Tarreau5fb04712016-05-04 10:18:37 +02001778 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001779 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001780
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001781 /* below we may emit error messages so we have to ensure that we have
1782 * our buffers properly allocated.
1783 */
Willy Tarreau87b09662015-04-03 00:22:06 +02001784 if (!stream_alloc_work_buffer(s)) {
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001785 /* No buffer available, we've been subscribed to the list of
1786 * buffer waiters, let's wait for our turn.
1787 */
Willy Tarreau5fb04712016-05-04 10:18:37 +02001788 si_f->flags &= ~SI_FL_DONT_WAKE;
1789 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau10fc09e2014-11-25 19:46:36 +01001790 goto update_exp_and_leave;
1791 }
1792
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001793 /* 1b: check for low-level errors reported at the stream interface.
1794 * First we check if it's a retryable error (in which case we don't
1795 * want to tell the buffer). Otherwise we report the error one level
1796 * upper by setting flags into the buffers. Note that the side towards
1797 * the client cannot have connect (hence retryable) errors. Also, the
1798 * connection setup code must be able to deal with any type of abort.
1799 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001800 srv = objt_server(s->target);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001801 if (unlikely(si_f->flags & SI_FL_ERR)) {
1802 if (si_f->state == SI_ST_EST || si_f->state == SI_ST_DIS) {
1803 si_shutr(si_f);
1804 si_shutw(si_f);
1805 stream_int_report_error(si_f);
1806 if (!(req->analysers) && !(res->analysers)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001807 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
1808 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01001809 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02001810 HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001811 if (!(s->flags & SF_ERR_MASK))
1812 s->flags |= SF_ERR_CLICL;
1813 if (!(s->flags & SF_FINST_MASK))
1814 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001815 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001816 }
1817 }
1818
Willy Tarreau8f128b42014-11-28 15:07:47 +01001819 if (unlikely(si_b->flags & SI_FL_ERR)) {
1820 if (si_b->state == SI_ST_EST || si_b->state == SI_ST_DIS) {
1821 si_shutr(si_b);
1822 si_shutw(si_b);
1823 stream_int_report_error(si_b);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001824 HA_ATOMIC_ADD(&s->be->be_counters.failed_resp, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01001825 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02001826 HA_ATOMIC_ADD(&srv->counters.failed_resp, 1);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001827 if (!(req->analysers) && !(res->analysers)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02001828 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
1829 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01001830 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02001831 HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02001832 if (!(s->flags & SF_ERR_MASK))
1833 s->flags |= SF_ERR_SRVCL;
1834 if (!(s->flags & SF_FINST_MASK))
1835 s->flags |= SF_FINST_D;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001836 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001837 }
1838 /* note: maybe we should process connection errors here ? */
1839 }
1840
Willy Tarreau8f128b42014-11-28 15:07:47 +01001841 if (si_b->state == SI_ST_CON) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001842 /* we were trying to establish a connection on the server side,
1843 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1844 */
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001845 if (unlikely(!sess_update_st_con_tcp(s)))
1846 sess_update_st_cer(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01001847 else if (si_b->state == SI_ST_EST)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01001848 sess_establish(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001849
1850 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1851 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1852 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1853 */
1854 }
1855
Willy Tarreau8f128b42014-11-28 15:07:47 +01001856 rq_prod_last = si_f->state;
1857 rq_cons_last = si_b->state;
1858 rp_cons_last = si_f->state;
1859 rp_prod_last = si_b->state;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001860
1861 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001862 /* Check for connection closure */
1863
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001864 DPRINTF(stderr,
Christopher Faulet45073512018-07-20 10:16:29 +02001865 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%lu rqt=%lu rph=%lu rpt=%lu cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001866 now_ms, __FUNCTION__, __LINE__,
1867 t,
1868 s, s->flags,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001869 req, res,
1870 req->rex, res->wex,
1871 req->flags, res->flags,
Christopher Faulet45073512018-07-20 10:16:29 +02001872 ci_data(req), co_data(req), ci_data(res), co_data(res), si_f->state, si_b->state,
Willy Tarreau8f128b42014-11-28 15:07:47 +01001873 si_f->err_type, si_b->err_type,
1874 si_b->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001875
1876 /* nothing special to be done on client side */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001877 if (unlikely(si_f->state == SI_ST_DIS))
1878 si_f->state = SI_ST_CLO;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001879
1880 /* When a server-side connection is released, we have to count it and
1881 * check for pending connections on this server.
1882 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001883 if (unlikely(si_b->state == SI_ST_DIS)) {
1884 si_b->state = SI_ST_CLO;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001885 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001886 if (srv) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02001887 if (s->flags & SF_CURR_SESS) {
1888 s->flags &= ~SF_CURR_SESS;
Christopher Faulet29f77e82017-06-08 14:04:45 +02001889 HA_ATOMIC_SUB(&srv->cur_sess, 1);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001890 }
1891 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001892 if (may_dequeue_tasks(srv, s->be))
1893 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001894 }
1895 }
1896
1897 /*
1898 * Note: of the transient states (REQ, CER, DIS), only REQ may remain
1899 * at this point.
1900 */
1901
Willy Tarreau0be0ef92009-03-08 19:20:25 +01001902 resync_request:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001903 /* Analyse request */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001904 if (((req->flags & ~rqf_last) & CF_MASK_ANALYSER) ||
1905 ((req->flags ^ rqf_last) & CF_MASK_STATIC) ||
Willy Tarreau33982cb2017-11-20 15:37:13 +01001906 (req->analysers && (req->flags & CF_SHUTW)) ||
Willy Tarreau8f128b42014-11-28 15:07:47 +01001907 si_f->state != rq_prod_last ||
1908 si_b->state != rq_cons_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01001909 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01001910 unsigned int flags = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001911
Willy Tarreau8f128b42014-11-28 15:07:47 +01001912 if (si_f->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01001913 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001914 unsigned int ana_list;
1915 unsigned int ana_back;
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001916
Willy Tarreau90deb182010-01-07 00:20:41 +01001917 /* it's up to the analysers to stop new connections,
1918 * disable reading or closing. Note: if an analyser
1919 * disables any of these bits, it is responsible for
1920 * enabling them again when it disables itself, so
1921 * that other analysers are called in similar conditions.
1922 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01001923 channel_auto_read(req);
1924 channel_auto_connect(req);
1925 channel_auto_close(req);
Willy Tarreauedcf6682008-11-30 23:15:34 +01001926
1927 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01001928 * req->analysers, following the bit order from LSB
Willy Tarreauedcf6682008-11-30 23:15:34 +01001929 * to MSB. The analysers must remove themselves from
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001930 * the list when not needed. Any analyser may return 0
1931 * to break out of the loop, either because of missing
1932 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02001933 * kill the stream. We loop at least once through each
Willy Tarreau1a52dbd2009-06-28 19:37:53 +02001934 * analyser, and we may loop again if other analysers
1935 * are added in the middle.
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001936 *
1937 * We build a list of analysers to run. We evaluate all
1938 * of these analysers in the order of the lower bit to
1939 * the higher bit. This ordering is very important.
1940 * An analyser will often add/remove other analysers,
1941 * including itself. Any changes to itself have no effect
1942 * on the loop. If it removes any other analysers, we
1943 * want those analysers not to be called anymore during
1944 * this loop. If it adds an analyser that is located
1945 * after itself, we want it to be scheduled for being
1946 * processed during the loop. If it adds an analyser
1947 * which is located before it, we want it to switch to
1948 * it immediately, even if it has already been called
1949 * once but removed since.
1950 *
1951 * In order to achieve this, we compare the analyser
1952 * list after the call with a copy of it before the
1953 * call. The work list is fed with analyser bits that
1954 * appeared during the call. Then we compare previous
1955 * work list with the new one, and check the bits that
1956 * appeared. If the lowest of these bits is lower than
1957 * the current bit, it means we have enabled a previous
1958 * analyser and must immediately loop again.
Willy Tarreauedcf6682008-11-30 23:15:34 +01001959 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001960
Willy Tarreau8f128b42014-11-28 15:07:47 +01001961 ana_list = ana_back = req->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01001962 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001963 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01001964 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_FE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001965 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_FE);
1966 FLT_ANALYZE(s, req, http_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_HTTP);
1967 FLT_ANALYZE(s, req, http_wait_for_request_body, ana_list, ana_back, AN_REQ_HTTP_BODY);
1968 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_FE, sess->fe);
1969 FLT_ANALYZE(s, req, process_switching_rules, ana_list, ana_back, AN_REQ_SWITCHING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001970 ANALYZE (s, req, flt_start_analyze, ana_list, ana_back, AN_REQ_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001971 FLT_ANALYZE(s, req, tcp_inspect_request, ana_list, ana_back, AN_REQ_INSPECT_BE);
1972 FLT_ANALYZE(s, req, http_process_req_common, ana_list, ana_back, AN_REQ_HTTP_PROCESS_BE, s->be);
1973 FLT_ANALYZE(s, req, http_process_tarpit, ana_list, ana_back, AN_REQ_HTTP_TARPIT);
1974 FLT_ANALYZE(s, req, process_server_rules, ana_list, ana_back, AN_REQ_SRV_RULES);
1975 FLT_ANALYZE(s, req, http_process_request, ana_list, ana_back, AN_REQ_HTTP_INNER);
1976 FLT_ANALYZE(s, req, tcp_persist_rdp_cookie, ana_list, ana_back, AN_REQ_PRST_RDP_COOKIE);
1977 FLT_ANALYZE(s, req, process_sticking_rules, ana_list, ana_back, AN_REQ_STICKING_RULES);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001978 ANALYZE (s, req, flt_analyze_http_headers, ana_list, ana_back, AN_REQ_FLT_HTTP_HDRS);
Christopher Fauleta9215b72016-05-11 17:06:28 +02001979 ANALYZE (s, req, http_request_forward_body, ana_list, ana_back, AN_REQ_HTTP_XFER_BODY);
William Lallemandcf62f7e2018-10-26 14:47:40 +02001980 ANALYZE (s, req, pcli_wait_for_request, ana_list, ana_back, AN_REQ_WAIT_CLI);
Christopher Faulet0184ea72017-01-05 14:06:34 +01001981 ANALYZE (s, req, flt_xfer_data, ana_list, ana_back, AN_REQ_FLT_XFER_DATA);
1982 ANALYZE (s, req, flt_end_analyze, ana_list, ana_back, AN_REQ_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01001983 break;
1984 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001985 }
Willy Tarreau84455332009-03-15 22:34:05 +01001986
Willy Tarreau8f128b42014-11-28 15:07:47 +01001987 rq_prod_last = si_f->state;
1988 rq_cons_last = si_b->state;
1989 req->flags &= ~CF_WAKE_ONCE;
1990 rqf_last = req->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001991
Willy Tarreau8f128b42014-11-28 15:07:47 +01001992 if ((req->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001993 goto resync_request;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02001994 }
1995
Willy Tarreau576507f2010-01-07 00:09:04 +01001996 /* we'll monitor the request analysers while parsing the response,
1997 * because some response analysers may indirectly enable new request
1998 * analysers (eg: HTTP keep-alive).
1999 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002000 req_ana_back = req->analysers;
Willy Tarreau576507f2010-01-07 00:09:04 +01002001
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002002 resync_response:
2003 /* Analyse response */
2004
Willy Tarreau8f128b42014-11-28 15:07:47 +01002005 if (((res->flags & ~rpf_last) & CF_MASK_ANALYSER) ||
2006 (res->flags ^ rpf_last) & CF_MASK_STATIC ||
Willy Tarreau33982cb2017-11-20 15:37:13 +01002007 (res->analysers && (res->flags & CF_SHUTW)) ||
Willy Tarreau8f128b42014-11-28 15:07:47 +01002008 si_f->state != rp_cons_last ||
2009 si_b->state != rp_prod_last ||
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002010 s->pending_events & TASK_WOKEN_MSG) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002011 unsigned int flags = res->flags;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002012
Willy Tarreau8f128b42014-11-28 15:07:47 +01002013 if (si_b->state >= SI_ST_EST) {
Willy Tarreaue34070e2010-01-08 00:32:27 +01002014 int max_loops = global.tune.maxpollevents;
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002015 unsigned int ana_list;
2016 unsigned int ana_back;
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002017
Willy Tarreau90deb182010-01-07 00:20:41 +01002018 /* it's up to the analysers to stop disable reading or
2019 * closing. Note: if an analyser disables any of these
2020 * bits, it is responsible for enabling them again when
2021 * it disables itself, so that other analysers are called
2022 * in similar conditions.
2023 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002024 channel_auto_read(res);
2025 channel_auto_close(res);
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002026
2027 /* We will call all analysers for which a bit is set in
Willy Tarreau8f128b42014-11-28 15:07:47 +01002028 * res->analysers, following the bit order from LSB
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002029 * to MSB. The analysers must remove themselves from
2030 * the list when not needed. Any analyser may return 0
2031 * to break out of the loop, either because of missing
2032 * data to take a decision, or because it decides to
Willy Tarreau87b09662015-04-03 00:22:06 +02002033 * kill the stream. We loop at least once through each
Willy Tarreaub37c27e2009-10-18 22:53:08 +02002034 * analyser, and we may loop again if other analysers
2035 * are added in the middle.
2036 */
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002037
Willy Tarreau8f128b42014-11-28 15:07:47 +01002038 ana_list = ana_back = res->analysers;
Willy Tarreaue34070e2010-01-08 00:32:27 +01002039 while (ana_list && max_loops--) {
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01002040 /* Warning! ensure that analysers are always placed in ascending order! */
Christopher Faulet0184ea72017-01-05 14:06:34 +01002041 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_FE);
2042 ANALYZE (s, res, flt_start_analyze, ana_list, ana_back, AN_RES_FLT_START_BE);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002043 FLT_ANALYZE(s, res, tcp_inspect_response, ana_list, ana_back, AN_RES_INSPECT);
2044 FLT_ANALYZE(s, res, http_wait_for_response, ana_list, ana_back, AN_RES_WAIT_HTTP);
2045 FLT_ANALYZE(s, res, process_store_rules, ana_list, ana_back, AN_RES_STORE_RULES);
2046 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 +01002047 ANALYZE (s, res, flt_analyze_http_headers, ana_list, ana_back, AN_RES_FLT_HTTP_HDRS);
Christopher Fauleta9215b72016-05-11 17:06:28 +02002048 ANALYZE (s, res, http_response_forward_body, ana_list, ana_back, AN_RES_HTTP_XFER_BODY);
William Lallemandcf62f7e2018-10-26 14:47:40 +02002049 ANALYZE (s, res, pcli_wait_for_response, ana_list, ana_back, AN_RES_WAIT_CLI);
Christopher Faulet0184ea72017-01-05 14:06:34 +01002050 ANALYZE (s, res, flt_xfer_data, ana_list, ana_back, AN_RES_FLT_XFER_DATA);
2051 ANALYZE (s, res, flt_end_analyze, ana_list, ana_back, AN_RES_FLT_END);
Willy Tarreaue34070e2010-01-08 00:32:27 +01002052 break;
2053 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002054 }
2055
Willy Tarreau8f128b42014-11-28 15:07:47 +01002056 rp_cons_last = si_f->state;
2057 rp_prod_last = si_b->state;
Christopher Fauletcdaea892017-07-06 15:49:30 +02002058 res->flags &= ~CF_WAKE_ONCE;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002059 rpf_last = res->flags;
Willy Tarreau815a9b22010-07-27 17:15:12 +02002060
Willy Tarreau8f128b42014-11-28 15:07:47 +01002061 if ((res->flags ^ flags) & CF_MASK_STATIC)
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002062 goto resync_response;
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002063 }
2064
Willy Tarreau576507f2010-01-07 00:09:04 +01002065 /* maybe someone has added some request analysers, so we must check and loop */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002066 if (req->analysers & ~req_ana_back)
Willy Tarreau576507f2010-01-07 00:09:04 +01002067 goto resync_request;
2068
Willy Tarreau8f128b42014-11-28 15:07:47 +01002069 if ((req->flags & ~rqf_last) & CF_MASK_ANALYSER)
Willy Tarreau0499e352010-12-17 07:13:42 +01002070 goto resync_request;
2071
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002072 /* FIXME: here we should call protocol handlers which rely on
2073 * both buffers.
2074 */
2075
2076
2077 /*
Willy Tarreau87b09662015-04-03 00:22:06 +02002078 * Now we propagate unhandled errors to the stream. Normally
Willy Tarreauae526782010-03-04 20:34:23 +01002079 * we're just in a data phase here since it means we have not
2080 * seen any analyser who could set an error status.
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002081 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002082 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002083 if (unlikely(!(s->flags & SF_ERR_MASK))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002084 if (req->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002085 /* Report it if the client got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002086 req->analysers = 0;
2087 if (req->flags & CF_READ_ERROR) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002088 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2089 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002090 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002091 HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002092 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002093 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002094 else if (req->flags & CF_READ_TIMEOUT) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002095 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2096 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002097 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002098 HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002099 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002100 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002101 else if (req->flags & CF_WRITE_ERROR) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002102 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2103 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002104 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002105 HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002106 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002107 }
2108 else {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002109 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2110 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002111 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002112 HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002113 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002114 }
Willy Tarreau84455332009-03-15 22:34:05 +01002115 sess_set_term_flags(s);
2116 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002117 else if (res->flags & (CF_READ_ERROR|CF_READ_TIMEOUT|CF_WRITE_ERROR|CF_WRITE_TIMEOUT)) {
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002118 /* Report it if the server got an error or a read timeout expired */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002119 res->analysers = 0;
2120 if (res->flags & CF_READ_ERROR) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002121 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2122 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002123 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002124 HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002125 s->flags |= SF_ERR_SRVCL;
Willy Tarreauae526782010-03-04 20:34:23 +01002126 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002127 else if (res->flags & CF_READ_TIMEOUT) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002128 HA_ATOMIC_ADD(&s->be->be_counters.srv_aborts, 1);
2129 HA_ATOMIC_ADD(&sess->fe->fe_counters.srv_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002130 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002131 HA_ATOMIC_ADD(&srv->counters.srv_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002132 s->flags |= SF_ERR_SRVTO;
Willy Tarreauae526782010-03-04 20:34:23 +01002133 }
Willy Tarreau8f128b42014-11-28 15:07:47 +01002134 else if (res->flags & CF_WRITE_ERROR) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002135 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2136 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002137 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002138 HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002139 s->flags |= SF_ERR_CLICL;
Willy Tarreauae526782010-03-04 20:34:23 +01002140 }
2141 else {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002142 HA_ATOMIC_ADD(&s->be->be_counters.cli_aborts, 1);
2143 HA_ATOMIC_ADD(&sess->fe->fe_counters.cli_aborts, 1);
Willy Tarreau827aee92011-03-10 16:55:02 +01002144 if (srv)
Christopher Faulet29f77e82017-06-08 14:04:45 +02002145 HA_ATOMIC_ADD(&srv->counters.cli_aborts, 1);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002146 s->flags |= SF_ERR_CLITO;
Willy Tarreauae526782010-03-04 20:34:23 +01002147 }
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002148 sess_set_term_flags(s);
2149 }
Willy Tarreau84455332009-03-15 22:34:05 +01002150 }
2151
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002152 /*
2153 * Here we take care of forwarding unhandled data. This also includes
2154 * connection establishments and shutdown requests.
2155 */
2156
2157
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002158 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002159 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002160 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002161 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002162 */
Christopher Faulet35fe6992017-08-29 16:06:38 +02002163 if (unlikely((!req->analysers || (req->analysers == AN_REQ_FLT_END && !(req->flags & CF_FLT_ANALYZE))) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002164 !(req->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2165 (si_f->state >= SI_ST_EST) &&
2166 (req->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002167 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002168 * attached to it. If any data are left in, we'll permit them to
2169 * move.
2170 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002171 channel_auto_read(req);
2172 channel_auto_connect(req);
2173 channel_auto_close(req);
Willy Tarreau144c5c42018-06-15 15:16:51 +02002174 c_adv(req, ci_data(req));
Willy Tarreau5bd8c372009-01-19 00:32:22 +01002175
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002176 /* We'll let data flow between the producer (if still connected)
2177 * to the consumer (which might possibly not be connected yet).
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002178 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002179 if (!(req->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002180 channel_forward_forever(req);
Willy Tarreau42529c32015-07-09 18:38:57 +02002181
2182 /* Just in order to support fetching HTTP contents after start
2183 * of forwarding when the HTTP forwarding analyser is not used,
2184 * we simply reset msg->sov so that HTTP rewinding points to the
2185 * headers.
2186 */
Christopher Fauleted28da52018-10-31 13:43:33 +01002187 if (IS_HTX_STRM(s) && s->txn)
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02002188 s->txn->req.sov = s->txn->req.eoh + s->txn->req.eol - co_data(req);
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002189 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002190
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002191 /* check if it is wise to enable kernel splicing to forward request data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002192 if (!(req->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2193 req->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002194 (global.tune.options & GTUNE_USE_SPLICE) &&
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002195 (objt_cs(si_f->end) && __objt_cs(si_f->end)->conn->xprt && __objt_cs(si_f->end)->conn->xprt->rcv_pipe) &&
2196 (objt_cs(si_b->end) && __objt_cs(si_b->end)->conn->xprt && __objt_cs(si_b->end)->conn->xprt->snd_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002197 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002198 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_REQ) ||
2199 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002200 (req->flags & CF_STREAMER_FAST)))) {
2201 req->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002202 }
2203
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002204 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002205 rqf_last = req->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002206
2207 /*
2208 * Now forward all shutdown requests between both sides of the buffer
2209 */
2210
Willy Tarreau520d95e2009-09-19 21:04:57 +02002211 /* first, let's check if the request buffer needs to shutdown(write), which may
2212 * happen either because the input is closed or because we want to force a close
Willy Tarreau05cdd962014-05-10 14:30:07 +02002213 * once the server has begun to respond. If a half-closed timeout is set, we adjust
2214 * the other side's timeout as well.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002215 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002216 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002217 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002218 channel_shutw_now(req);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002219 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002220
2221 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002222 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2223 channel_is_empty(req))) {
2224 if (req->flags & CF_READ_ERROR)
2225 si_b->flags |= SI_FL_NOLINGER;
2226 si_shutw(si_b);
Willy Tarreau8615c2a2013-06-21 08:20:19 +02002227 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002228
2229 /* shutdown(write) done on server side, we must stop the client too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002230 if (unlikely((req->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW &&
2231 !req->analysers))
2232 channel_shutr_now(req);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002233
2234 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002235 if (unlikely((req->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2236 if (si_f->flags & SI_FL_NOHALF)
2237 si_f->flags |= SI_FL_NOLINGER;
2238 si_shutr(si_f);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002239 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002240
Willy Tarreau520d95e2009-09-19 21:04:57 +02002241 /* it's possible that an upper layer has requested a connection setup or abort.
2242 * There are 2 situations where we decide to establish a new connection :
2243 * - there are data scheduled for emission in the buffer
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002244 * - the CF_AUTO_CONNECT flag is set (active connection)
Willy Tarreau520d95e2009-09-19 21:04:57 +02002245 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002246 if (si_b->state == SI_ST_INI) {
2247 if (!(req->flags & CF_SHUTW)) {
2248 if ((req->flags & CF_AUTO_CONNECT) || !channel_is_empty(req)) {
Willy Tarreaucf644ed2013-09-29 17:19:56 +02002249 /* If we have an appctx, there is no connect method, so we
2250 * immediately switch to the connected state, otherwise we
2251 * perform a connection request.
Willy Tarreau520d95e2009-09-19 21:04:57 +02002252 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002253 si_b->state = SI_ST_REQ; /* new connection requested */
2254 si_b->conn_retries = s->be->conn_retries;
Willy Tarreau520d95e2009-09-19 21:04:57 +02002255 }
Willy Tarreau73201222009-08-16 18:27:24 +02002256 }
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002257 else {
Willy Tarreau0542c8b2017-11-24 15:01:10 +01002258 si_release_endpoint(si_b);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002259 si_b->state = SI_ST_CLO; /* shutw+ini = abort */
2260 channel_shutw_now(req); /* fix buffer flags upon abort */
2261 channel_shutr_now(res);
Willy Tarreauf41ffdc2009-09-20 08:19:25 +02002262 }
Willy Tarreau92795622009-03-06 12:51:23 +01002263 }
2264
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002265
2266 /* we may have a pending connection request, or a connection waiting
2267 * for completion.
2268 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002269 if (si_b->state >= SI_ST_REQ && si_b->state < SI_ST_CON) {
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002270
2271 /* prune the request variables and swap to the response variables. */
2272 if (s->vars_reqres.scope != SCOPE_RES) {
Willy Tarreaucda7f3f2018-10-28 13:44:36 +01002273 if (!LIST_ISEMPTY(&s->vars_reqres.head)) {
2274 vars_prune(&s->vars_reqres, s->sess, s);
2275 vars_init(&s->vars_reqres, SCOPE_RES);
2276 }
Thierry FOURNIER4834bc72015-06-06 19:29:07 +02002277 }
2278
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002279 do {
2280 /* nb: step 1 might switch from QUE to ASS, but we first want
2281 * to give a chance to step 2 to perform a redirect if needed.
2282 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002283 if (si_b->state != SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002284 sess_update_stream_int(s);
Willy Tarreau8f128b42014-11-28 15:07:47 +01002285 if (si_b->state == SI_ST_REQ)
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002286 sess_prepare_conn_req(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002287
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01002288 /* applets directly go to the ESTABLISHED state. Similarly,
2289 * servers experience the same fate when their connection
2290 * is reused.
2291 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002292 if (unlikely(si_b->state == SI_ST_EST))
Willy Tarreau7b8c4f92014-11-28 15:15:44 +01002293 sess_establish(s);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01002294
Willy Tarreaub44c8732013-12-31 23:16:50 +01002295 /* Now we can add the server name to a header (if requested) */
2296 /* check for HTTP mode and proxy server_name_hdr_name != NULL */
Willy Tarreau9c03b332015-09-07 19:32:33 +02002297 if ((si_b->state >= SI_ST_CON) && (si_b->state < SI_ST_CLO) &&
Willy Tarreaub44c8732013-12-31 23:16:50 +01002298 (s->be->server_id_hdr_name != NULL) &&
2299 (s->be->mode == PR_MODE_HTTP) &&
2300 objt_server(s->target)) {
Christopher Faulet27a3dc82018-10-23 15:34:07 +02002301 http_send_name_header(s, s->be, objt_server(s->target)->id);
Willy Tarreau1e5dfda2013-04-07 18:19:16 +02002302 }
2303
Willy Tarreau3fdb3662012-11-12 00:42:33 +01002304 srv = objt_server(s->target);
Willy Tarreaue7dff022015-04-03 01:14:29 +02002305 if (si_b->state == SI_ST_ASS && srv && srv->rdr_len && (s->flags & SF_REDIRECTABLE))
Willy Tarreau8f128b42014-11-28 15:07:47 +01002306 http_perform_server_redirect(s, si_b);
2307 } while (si_b->state == SI_ST_ASS);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002308 }
2309
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002310 /* Benchmarks have shown that it's optimal to do a full resync now */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002311 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002312 goto resync_stream_interface;
2313
Willy Tarreau815a9b22010-07-27 17:15:12 +02002314 /* otherwise we want to check if we need to resync the req buffer or not */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002315 if ((req->flags ^ rqf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002316 goto resync_request;
2317
Willy Tarreau3deb3d02009-06-21 22:43:05 +02002318 /* perform output updates to the response buffer */
Willy Tarreau84455332009-03-15 22:34:05 +01002319
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002320 /* If noone is interested in analysing data, it's time to forward
Willy Tarreau31971e52009-09-20 12:07:52 +02002321 * everything. We configure the buffer to forward indefinitely.
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02002322 * Note that we're checking CF_SHUTR_NOW as an indication of a possible
Willy Tarreau8263d2b2012-08-28 00:06:31 +02002323 * recent call to channel_abort().
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002324 */
Christopher Faulet35fe6992017-08-29 16:06:38 +02002325 if (unlikely((!res->analysers || (res->analysers == AN_RES_FLT_END && !(res->flags & CF_FLT_ANALYZE))) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002326 !(res->flags & (CF_SHUTW|CF_SHUTR_NOW)) &&
2327 (si_b->state >= SI_ST_EST) &&
2328 (res->to_forward != CHN_INFINITE_FORWARD))) {
Willy Tarreaub31c9712012-11-11 23:05:39 +01002329 /* This buffer is freewheeling, there's no analyser
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002330 * attached to it. If any data are left in, we'll permit them to
2331 * move.
2332 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002333 channel_auto_read(res);
2334 channel_auto_close(res);
Willy Tarreau144c5c42018-06-15 15:16:51 +02002335 c_adv(res, ci_data(res));
Willy Tarreauda4d9fe2010-11-07 20:26:56 +01002336
2337 /* We'll let data flow between the producer (if still connected)
2338 * to the consumer.
2339 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002340 if (!(res->flags & (CF_SHUTR|CF_SHUTW_NOW)))
Willy Tarreau8bf242b2016-05-04 14:05:58 +02002341 channel_forward_forever(res);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002342
Willy Tarreau42529c32015-07-09 18:38:57 +02002343 /* Just in order to support fetching HTTP contents after start
2344 * of forwarding when the HTTP forwarding analyser is not used,
2345 * we simply reset msg->sov so that HTTP rewinding points to the
2346 * headers.
2347 */
Christopher Fauleted28da52018-10-31 13:43:33 +01002348 if (IS_HTX_STRM(s) && s->txn)
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02002349 s->txn->rsp.sov = s->txn->rsp.eoh + s->txn->rsp.eol - co_data(res);
Willy Tarreau42529c32015-07-09 18:38:57 +02002350
Willy Tarreauce887fd2012-05-12 12:50:00 +02002351 /* if we have no analyser anymore in any direction and have a
Willy Tarreau05cdd962014-05-10 14:30:07 +02002352 * tunnel timeout set, use it now. Note that we must respect
2353 * the half-closed timeouts as well.
Willy Tarreauce887fd2012-05-12 12:50:00 +02002354 */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002355 if (!req->analysers && s->be->timeout.tunnel) {
2356 req->rto = req->wto = res->rto = res->wto =
Willy Tarreauce887fd2012-05-12 12:50:00 +02002357 s->be->timeout.tunnel;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002358
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002359 if ((req->flags & CF_SHUTR) && tick_isset(sess->fe->timeout.clientfin))
2360 res->wto = sess->fe->timeout.clientfin;
Willy Tarreau8f128b42014-11-28 15:07:47 +01002361 if ((req->flags & CF_SHUTW) && tick_isset(s->be->timeout.serverfin))
2362 res->rto = s->be->timeout.serverfin;
2363 if ((res->flags & CF_SHUTR) && tick_isset(s->be->timeout.serverfin))
2364 req->wto = s->be->timeout.serverfin;
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002365 if ((res->flags & CF_SHUTW) && tick_isset(sess->fe->timeout.clientfin))
2366 req->rto = sess->fe->timeout.clientfin;
Willy Tarreau05cdd962014-05-10 14:30:07 +02002367
Willy Tarreau8f128b42014-11-28 15:07:47 +01002368 req->rex = tick_add(now_ms, req->rto);
2369 req->wex = tick_add(now_ms, req->wto);
2370 res->rex = tick_add(now_ms, res->rto);
2371 res->wex = tick_add(now_ms, res->wto);
Willy Tarreauce887fd2012-05-12 12:50:00 +02002372 }
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002373 }
Willy Tarreauf890dc92008-12-13 21:12:26 +01002374
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002375 /* check if it is wise to enable kernel splicing to forward response data */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002376 if (!(res->flags & (CF_KERN_SPLICING|CF_SHUTR)) &&
2377 res->to_forward &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002378 (global.tune.options & GTUNE_USE_SPLICE) &&
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002379 (objt_cs(si_f->end) && __objt_cs(si_f->end)->conn->xprt && __objt_cs(si_f->end)->conn->xprt->snd_pipe) &&
2380 (objt_cs(si_b->end) && __objt_cs(si_b->end)->conn->xprt && __objt_cs(si_b->end)->conn->xprt->rcv_pipe) &&
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002381 (pipes_used < global.maxpipes) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002382 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_RTR) ||
2383 (((sess->fe->options2|s->be->options2) & PR_O2_SPLIC_AUT) &&
Willy Tarreau8f128b42014-11-28 15:07:47 +01002384 (res->flags & CF_STREAMER_FAST)))) {
2385 res->flags |= CF_KERN_SPLICING;
Willy Tarreau7c84bab2009-03-08 21:38:23 +01002386 }
2387
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002388 /* reflect what the L7 analysers have seen last */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002389 rpf_last = res->flags;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002390
2391 /*
2392 * Now forward all shutdown requests between both sides of the buffer
2393 */
2394
2395 /*
2396 * FIXME: this is probably where we should produce error responses.
2397 */
2398
Willy Tarreau6b66f3e2008-12-14 17:31:54 +01002399 /* first, let's check if the response buffer needs to shutdown(write) */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002400 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_AUTO_CLOSE|CF_SHUTR)) ==
Willy Tarreau05cdd962014-05-10 14:30:07 +02002401 (CF_AUTO_CLOSE|CF_SHUTR))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002402 channel_shutw_now(res);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002403 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002404
2405 /* shutdown(write) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002406 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTW_NOW)) == CF_SHUTW_NOW &&
2407 channel_is_empty(res))) {
2408 si_shutw(si_f);
Willy Tarreau05cdd962014-05-10 14:30:07 +02002409 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002410
2411 /* shutdown(write) done on the client side, we must stop the server too */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002412 if (unlikely((res->flags & (CF_SHUTW|CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTW) &&
2413 !res->analysers)
2414 channel_shutr_now(res);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002415
2416 /* shutdown(read) pending */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002417 if (unlikely((res->flags & (CF_SHUTR|CF_SHUTR_NOW)) == CF_SHUTR_NOW)) {
2418 if (si_b->flags & SI_FL_NOHALF)
2419 si_b->flags |= SI_FL_NOLINGER;
2420 si_shutr(si_b);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02002421 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002422
Willy Tarreau8f128b42014-11-28 15:07:47 +01002423 if (si_f->state == SI_ST_DIS || si_b->state == SI_ST_DIS)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002424 goto resync_stream_interface;
2425
Willy Tarreau8f128b42014-11-28 15:07:47 +01002426 if (req->flags != rqf_last)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002427 goto resync_request;
2428
Willy Tarreau8f128b42014-11-28 15:07:47 +01002429 if ((res->flags ^ rpf_last) & CF_MASK_STATIC)
Willy Tarreau0be0ef92009-03-08 19:20:25 +01002430 goto resync_response;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002431
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002432 /* we're interested in getting wakeups again */
Willy Tarreau8f128b42014-11-28 15:07:47 +01002433 si_f->flags &= ~SI_FL_DONT_WAKE;
2434 si_b->flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau89f7ef22009-09-05 20:57:35 +02002435
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002436 /* This is needed only when debugging is enabled, to indicate
2437 * client-side or server-side close. Please note that in the unlikely
2438 * event where both sides would close at once, the sequence is reported
2439 * on the server side first.
2440 */
2441 if (unlikely((global.mode & MODE_DEBUG) &&
2442 (!(global.mode & MODE_QUIET) ||
2443 (global.mode & MODE_VERBOSE)))) {
Willy Tarreau8f128b42014-11-28 15:07:47 +01002444 if (si_b->state == SI_ST_CLO &&
2445 si_b->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002446 chunk_printf(&trash, "%08x:%s.srvcls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002447 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002448 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2449 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002450 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002451 }
2452
Willy Tarreau8f128b42014-11-28 15:07:47 +01002453 if (si_f->state == SI_ST_CLO &&
2454 si_f->prev_state == SI_ST_EST) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002455 chunk_printf(&trash, "%08x:%s.clicls[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002456 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002457 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2458 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002459 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002460 }
2461 }
2462
Willy Tarreau8f128b42014-11-28 15:07:47 +01002463 if (likely((si_f->state != SI_ST_CLO) ||
2464 (si_b->state > SI_ST_INI && si_b->state < SI_ST_CLO))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002465
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002466 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED))
Willy Tarreau87b09662015-04-03 00:22:06 +02002467 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002468
Willy Tarreaud14844a2018-11-08 18:15:29 +01002469 si_update_both(si_f, si_b);
Olivier Houchard53216e72018-10-10 15:46:36 +02002470
Willy Tarreaubf89ff32018-11-09 14:59:25 +01002471 if (si_f->state == SI_ST_DIS || si_f->state != si_f->prev_state ||
2472 si_b->state == SI_ST_DIS || si_b->state != si_b->prev_state ||
Willy Tarreau85f89012018-10-25 11:06:57 +02002473 (((req->flags ^ rqf_last) | (res->flags ^ rpf_last)) & CF_MASK_ANALYSER))
Olivier Houchard53216e72018-10-10 15:46:36 +02002474 goto redo;
2475
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002476 /* Trick: if a request is being waiting for the server to respond,
2477 * and if we know the server can timeout, we don't want the timeout
2478 * to expire on the client side first, but we're still interested
2479 * in passing data from the client to the server (eg: POST). Thus,
2480 * we can cancel the client's request timeout if the server's
2481 * request timeout is set and the server has not yet sent a response.
2482 */
2483
Willy Tarreau8f128b42014-11-28 15:07:47 +01002484 if ((res->flags & (CF_AUTO_CLOSE|CF_SHUTR)) == 0 &&
2485 (tick_isset(req->wex) || tick_isset(res->rex))) {
2486 req->flags |= CF_READ_NOEXP;
2487 req->rex = TICK_ETERNITY;
Willy Tarreau6f0a7ba2014-06-23 15:22:31 +02002488 }
2489
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002490 /* Reset pending events now */
2491 s->pending_events = 0;
2492
Willy Tarreau798f4322012-11-08 14:49:17 +01002493 update_exp_and_leave:
Willy Tarreau5fb04712016-05-04 10:18:37 +02002494 /* Note: please ensure that if you branch here you disable SI_FL_DONT_WAKE */
Christopher Fauleta00d8172016-11-10 14:58:05 +01002495 t->expire = tick_first((tick_is_expired(t->expire, now_ms) ? 0 : t->expire),
2496 tick_first(tick_first(req->rex, req->wex),
2497 tick_first(res->rex, res->wex)));
Willy Tarreaudef0d222016-11-08 22:03:00 +01002498 if (!req->analysers)
2499 req->analyse_exp = TICK_ETERNITY;
2500
2501 if ((sess->fe->options & PR_O_CONTSTATS) && (s->flags & SF_BE_ASSIGNED) &&
2502 (!tick_isset(req->analyse_exp) || tick_is_expired(req->analyse_exp, now_ms)))
2503 req->analyse_exp = tick_add(now_ms, 5000);
2504
2505 t->expire = tick_first(t->expire, req->analyse_exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002506
Willy Tarreau9a398be2017-11-10 17:14:23 +01002507 t->expire = tick_first(t->expire, res->analyse_exp);
2508
Willy Tarreau8f128b42014-11-28 15:07:47 +01002509 if (si_f->exp)
2510 t->expire = tick_first(t->expire, si_f->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002511
Willy Tarreau8f128b42014-11-28 15:07:47 +01002512 if (si_b->exp)
2513 t->expire = tick_first(t->expire, si_b->exp);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002514
Christopher Faulet56803b12017-11-24 16:06:18 +01002515 DPRINTF(stderr,
Willy Tarreau127334e2009-03-28 10:47:26 +01002516 "[%u] queuing with exp=%u req->rex=%u req->wex=%u req->ana_exp=%u"
2517 " 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 +01002518 now_ms, t->expire, req->rex, req->wex, req->analyse_exp,
2519 res->rex, res->wex, si_f->exp, si_b->exp, si_f->state, si_b->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002520
2521#ifdef DEBUG_DEV
2522 /* this may only happen when no timeout is set or in case of an FSM bug */
Willy Tarreaud0a201b2009-03-08 15:53:06 +01002523 if (!tick_isset(t->expire))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002524 ABORT_NOW();
2525#endif
Christopher Faulet9d810ca2016-12-08 22:33:52 +01002526 s->pending_events &= ~(TASK_WOKEN_TIMER | TASK_WOKEN_RES);
Willy Tarreau87b09662015-04-03 00:22:06 +02002527 stream_release_buffers(s);
Olivier Houchardc2aa7112018-09-11 18:27:21 +02002528 /* We may have free'd some space in buffers, or have more to send/recv, try again */
Willy Tarreau26c25062009-03-08 09:38:41 +01002529 return t; /* nothing more to do */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002530 }
2531
Willy Tarreaue7dff022015-04-03 01:14:29 +02002532 if (s->flags & SF_BE_ASSIGNED)
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002533 HA_ATOMIC_SUB(&s->be->beconn, 1);
Willy Tarreau6f5e4b92017-09-15 09:07:56 +02002534
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002535 if (unlikely((global.mode & MODE_DEBUG) &&
2536 (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)))) {
Willy Tarreau19d14ef2012-10-29 16:51:55 +01002537 chunk_printf(&trash, "%08x:%s.closed[%04x:%04x]\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002538 s->uniq_id, s->be->id,
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002539 objt_cs(si_f->end) ? (unsigned short)objt_cs(si_f->end)->conn->handle.fd : -1,
2540 objt_cs(si_b->end) ? (unsigned short)objt_cs(si_b->end)->conn->handle.fd : -1);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02002541 shut_your_big_mouth_gcc(write(1, trash.area, trash.data));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002542 }
2543
2544 s->logs.t_close = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau87b09662015-04-03 00:22:06 +02002545 stream_process_counters(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002546
Willy Tarreaueee5b512015-04-03 23:46:31 +02002547 if (s->txn && s->txn->status) {
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002548 int n;
2549
Willy Tarreaueee5b512015-04-03 23:46:31 +02002550 n = s->txn->status / 100;
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002551 if (n < 1 || n > 5)
2552 n = 0;
2553
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002554 if (sess->fe->mode == PR_MODE_HTTP) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002555 HA_ATOMIC_ADD(&sess->fe->fe_counters.p.http.rsp[n], 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002556 }
Willy Tarreaue7dff022015-04-03 01:14:29 +02002557 if ((s->flags & SF_BE_ASSIGNED) &&
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002558 (s->be->mode == PR_MODE_HTTP)) {
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002559 HA_ATOMIC_ADD(&s->be->be_counters.p.http.rsp[n], 1);
2560 HA_ATOMIC_ADD(&s->be->be_counters.p.http.cum_req, 1);
Willy Tarreau5e16cbc2012-11-24 14:54:13 +01002561 }
Krzysztof Piotr Oledzkide71d162009-10-24 15:36:15 +02002562 }
2563
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002564 /* let's do a final log if we need it */
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002565 if (!LIST_ISEMPTY(&sess->fe->logformat) && s->logs.logwait &&
Willy Tarreaue7dff022015-04-03 01:14:29 +02002566 !(s->flags & SF_MONITOR) &&
Willy Tarreaue36cbcb2015-04-03 15:40:56 +02002567 (!(sess->fe->options & PR_O_NULLNOLOG) || req->total)) {
Willy Tarreau66425e32018-07-25 06:55:12 +02002568 /* we may need to know the position in the queue */
2569 pendconn_free(s);
Willy Tarreaua5555ec2008-11-30 19:02:32 +01002570 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002571 }
2572
Willy Tarreau87b09662015-04-03 00:22:06 +02002573 /* update time stats for this stream */
2574 stream_update_time_stats(s);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002575
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002576 /* the task MUST not be in the run queue anymore */
Willy Tarreau87b09662015-04-03 00:22:06 +02002577 stream_free(s);
Willy Tarreau26c25062009-03-08 09:38:41 +01002578 task_delete(t);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002579 task_free(t);
Willy Tarreau26c25062009-03-08 09:38:41 +01002580 return NULL;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01002581}
2582
Willy Tarreau87b09662015-04-03 00:22:06 +02002583/* Update the stream's backend and server time stats */
2584void stream_update_time_stats(struct stream *s)
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002585{
2586 int t_request;
2587 int t_queue;
2588 int t_connect;
2589 int t_data;
2590 int t_close;
2591 struct server *srv;
2592
2593 t_request = 0;
2594 t_queue = s->logs.t_queue;
2595 t_connect = s->logs.t_connect;
2596 t_close = s->logs.t_close;
2597 t_data = s->logs.t_data;
2598
2599 if (s->be->mode != PR_MODE_HTTP)
2600 t_data = t_connect;
2601
2602 if (t_connect < 0 || t_data < 0)
2603 return;
2604
2605 if (tv_isge(&s->logs.tv_request, &s->logs.tv_accept))
2606 t_request = tv_ms_elapsed(&s->logs.tv_accept, &s->logs.tv_request);
2607
2608 t_data -= t_connect;
2609 t_connect -= t_queue;
2610 t_queue -= t_request;
2611
2612 srv = objt_server(s->target);
2613 if (srv) {
2614 swrate_add(&srv->counters.q_time, TIME_STATS_SAMPLES, t_queue);
2615 swrate_add(&srv->counters.c_time, TIME_STATS_SAMPLES, t_connect);
2616 swrate_add(&srv->counters.d_time, TIME_STATS_SAMPLES, t_data);
2617 swrate_add(&srv->counters.t_time, TIME_STATS_SAMPLES, t_close);
2618 }
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002619 HA_SPIN_LOCK(PROXY_LOCK, &s->be->lock);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002620 swrate_add(&s->be->be_counters.q_time, TIME_STATS_SAMPLES, t_queue);
2621 swrate_add(&s->be->be_counters.c_time, TIME_STATS_SAMPLES, t_connect);
2622 swrate_add(&s->be->be_counters.d_time, TIME_STATS_SAMPLES, t_data);
2623 swrate_add(&s->be->be_counters.t_time, TIME_STATS_SAMPLES, t_close);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002624 HA_SPIN_UNLOCK(PROXY_LOCK, &s->be->lock);
Willy Tarreau4bfc5802014-06-17 12:19:18 +02002625}
2626
Willy Tarreau7c669d72008-06-20 15:04:11 +02002627/*
2628 * This function adjusts sess->srv_conn and maintains the previous and new
Willy Tarreau87b09662015-04-03 00:22:06 +02002629 * server's served stream counts. Setting newsrv to NULL is enough to release
Willy Tarreau7c669d72008-06-20 15:04:11 +02002630 * current connection slot. This function also notifies any LB algo which might
Willy Tarreau87b09662015-04-03 00:22:06 +02002631 * expect to be informed about any change in the number of active streams on a
Willy Tarreau7c669d72008-06-20 15:04:11 +02002632 * server.
2633 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002634void sess_change_server(struct stream *sess, struct server *newsrv)
Willy Tarreau7c669d72008-06-20 15:04:11 +02002635{
2636 if (sess->srv_conn == newsrv)
2637 return;
2638
2639 if (sess->srv_conn) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02002640 HA_ATOMIC_SUB(&sess->srv_conn->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002641 HA_ATOMIC_SUB(&sess->srv_conn->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002642 if (sess->srv_conn->proxy->lbprm.server_drop_conn)
2643 sess->srv_conn->proxy->lbprm.server_drop_conn(sess->srv_conn);
Willy Tarreau87b09662015-04-03 00:22:06 +02002644 stream_del_srv_conn(sess);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002645 }
2646
2647 if (newsrv) {
Christopher Faulet29f77e82017-06-08 14:04:45 +02002648 HA_ATOMIC_ADD(&newsrv->served, 1);
Christopher Fauletff8abcd2017-06-02 15:33:24 +02002649 HA_ATOMIC_ADD(&newsrv->proxy->served, 1);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002650 if (newsrv->proxy->lbprm.server_take_conn)
2651 newsrv->proxy->lbprm.server_take_conn(newsrv);
Willy Tarreau87b09662015-04-03 00:22:06 +02002652 stream_add_srv_conn(sess, newsrv);
Willy Tarreau7c669d72008-06-20 15:04:11 +02002653 }
2654}
2655
Willy Tarreau84455332009-03-15 22:34:05 +01002656/* Handle server-side errors for default protocols. It is called whenever a a
2657 * connection setup is aborted or a request is aborted in queue. It sets the
Willy Tarreau87b09662015-04-03 00:22:06 +02002658 * stream termination flags so that the caller does not have to worry about
Willy Tarreau84455332009-03-15 22:34:05 +01002659 * them. It's installed as ->srv_error for the server-side stream_interface.
2660 */
Willy Tarreau87b09662015-04-03 00:22:06 +02002661void default_srv_error(struct stream *s, struct stream_interface *si)
Willy Tarreau84455332009-03-15 22:34:05 +01002662{
2663 int err_type = si->err_type;
2664 int err = 0, fin = 0;
2665
2666 if (err_type & SI_ET_QUEUE_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002667 err = SF_ERR_CLICL;
2668 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002669 }
2670 else if (err_type & SI_ET_CONN_ABRT) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002671 err = SF_ERR_CLICL;
2672 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002673 }
2674 else if (err_type & SI_ET_QUEUE_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002675 err = SF_ERR_SRVTO;
2676 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002677 }
2678 else if (err_type & SI_ET_QUEUE_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002679 err = SF_ERR_SRVCL;
2680 fin = SF_FINST_Q;
Willy Tarreau84455332009-03-15 22:34:05 +01002681 }
2682 else if (err_type & SI_ET_CONN_TO) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002683 err = SF_ERR_SRVTO;
2684 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002685 }
2686 else if (err_type & SI_ET_CONN_ERR) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002687 err = SF_ERR_SRVCL;
2688 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002689 }
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002690 else if (err_type & SI_ET_CONN_RES) {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002691 err = SF_ERR_RESOURCE;
2692 fin = SF_FINST_C;
Willy Tarreau2d400bb2012-05-14 12:11:47 +02002693 }
Willy Tarreau84455332009-03-15 22:34:05 +01002694 else /* SI_ET_CONN_OTHER and others */ {
Willy Tarreaue7dff022015-04-03 01:14:29 +02002695 err = SF_ERR_INTERNAL;
2696 fin = SF_FINST_C;
Willy Tarreau84455332009-03-15 22:34:05 +01002697 }
2698
Willy Tarreaue7dff022015-04-03 01:14:29 +02002699 if (!(s->flags & SF_ERR_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002700 s->flags |= err;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002701 if (!(s->flags & SF_FINST_MASK))
Willy Tarreau84455332009-03-15 22:34:05 +01002702 s->flags |= fin;
2703}
Willy Tarreau7c669d72008-06-20 15:04:11 +02002704
Willy Tarreaue7dff022015-04-03 01:14:29 +02002705/* kill a stream and set the termination flags to <why> (one of SF_ERR_*) */
Willy Tarreau87b09662015-04-03 00:22:06 +02002706void stream_shutdown(struct stream *stream, int why)
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002707{
Willy Tarreau87b09662015-04-03 00:22:06 +02002708 if (stream->req.flags & (CF_SHUTW|CF_SHUTW_NOW))
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002709 return;
2710
Willy Tarreau87b09662015-04-03 00:22:06 +02002711 channel_shutw_now(&stream->req);
2712 channel_shutr_now(&stream->res);
2713 stream->task->nice = 1024;
Willy Tarreaue7dff022015-04-03 01:14:29 +02002714 if (!(stream->flags & SF_ERR_MASK))
Willy Tarreau87b09662015-04-03 00:22:06 +02002715 stream->flags |= why;
2716 task_wakeup(stream->task, TASK_WOKEN_OTHER);
Willy Tarreaua2a64e92011-09-07 23:01:56 +02002717}
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +02002718
Willy Tarreau8b22a712010-06-18 17:46:06 +02002719/************************************************************************/
2720/* All supported ACL keywords must be declared here. */
2721/************************************************************************/
2722
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02002723/* 0=OK, <0=Alert, >0=Warning */
2724static enum act_parse_ret stream_parse_use_service(const char **args, int *cur_arg,
2725 struct proxy *px, struct act_rule *rule,
2726 char **err)
2727{
2728 struct action_kw *kw;
2729
2730 /* Check if the service name exists. */
2731 if (*(args[*cur_arg]) == 0) {
2732 memprintf(err, "'%s' expects a service name.", args[0]);
Thierry FOURNIER337eae12015-11-26 19:48:04 +01002733 return ACT_RET_PRS_ERR;
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02002734 }
2735
2736 /* lookup for keyword corresponding to a service. */
2737 kw = action_lookup(&service_keywords, args[*cur_arg]);
2738 if (!kw) {
2739 memprintf(err, "'%s' unknown service name.", args[1]);
2740 return ACT_RET_PRS_ERR;
2741 }
2742 (*cur_arg)++;
2743
2744 /* executes specific rule parser. */
2745 rule->kw = kw;
2746 if (kw->parse((const char **)args, cur_arg, px, rule, err) == ACT_RET_PRS_ERR)
2747 return ACT_RET_PRS_ERR;
2748
2749 /* Register processing function. */
2750 rule->action_ptr = process_use_service;
2751 rule->action = ACT_CUSTOM;
2752
2753 return ACT_RET_PRS_OK;
2754}
2755
2756void service_keywords_register(struct action_kw_list *kw_list)
2757{
2758 LIST_ADDQ(&service_keywords, &kw_list->list);
2759}
2760
William Lallemand4c5b4d52016-11-21 08:51:11 +01002761
2762/* This function dumps a complete stream state onto the stream interface's
2763 * read buffer. The stream has to be set in strm. It returns 0 if the output
2764 * buffer is full and it needs to be called again, otherwise non-zero. It is
2765 * designed to be called from stats_dump_strm_to_buffer() below.
2766 */
2767static int stats_dump_full_strm_to_buffer(struct stream_interface *si, struct stream *strm)
2768{
2769 struct appctx *appctx = __objt_appctx(si->end);
2770 struct tm tm;
2771 extern const char *monthname[12];
2772 char pn[INET6_ADDRSTRLEN];
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002773 struct conn_stream *cs;
William Lallemand4c5b4d52016-11-21 08:51:11 +01002774 struct connection *conn;
2775 struct appctx *tmpctx;
2776
2777 chunk_reset(&trash);
2778
2779 if (appctx->ctx.sess.section > 0 && appctx->ctx.sess.uid != strm->uniq_id) {
2780 /* stream changed, no need to go any further */
2781 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
Willy Tarreau06d80a92017-10-19 14:32:15 +02002782 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01002783 si_rx_room_blk(si);
William Lallemand4c5b4d52016-11-21 08:51:11 +01002784 return 0;
2785 }
2786 appctx->ctx.sess.uid = 0;
2787 appctx->ctx.sess.section = 0;
2788 return 1;
2789 }
2790
2791 switch (appctx->ctx.sess.section) {
2792 case 0: /* main status of the stream */
2793 appctx->ctx.sess.uid = strm->uniq_id;
2794 appctx->ctx.sess.section = 1;
2795 /* fall through */
2796
2797 case 1:
2798 get_localtime(strm->logs.accept_date.tv_sec, &tm);
2799 chunk_appendf(&trash,
2800 "%p: [%02d/%s/%04d:%02d:%02d:%02d.%06d] id=%u proto=%s",
2801 strm,
2802 tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2803 tm.tm_hour, tm.tm_min, tm.tm_sec, (int)(strm->logs.accept_date.tv_usec),
2804 strm->uniq_id,
2805 strm_li(strm) ? strm_li(strm)->proto->name : "?");
2806
2807 conn = objt_conn(strm_orig(strm));
2808 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2809 case AF_INET:
2810 case AF_INET6:
2811 chunk_appendf(&trash, " source=%s:%d\n",
2812 pn, get_host_port(&conn->addr.from));
2813 break;
2814 case AF_UNIX:
2815 chunk_appendf(&trash, " source=unix:%d\n", strm_li(strm)->luid);
2816 break;
2817 default:
2818 /* no more information to print right now */
2819 chunk_appendf(&trash, "\n");
2820 break;
2821 }
2822
2823 chunk_appendf(&trash,
2824 " flags=0x%x, conn_retries=%d, srv_conn=%p, pend_pos=%p\n",
2825 strm->flags, strm->si[1].conn_retries, strm->srv_conn, strm->pend_pos);
2826
2827 chunk_appendf(&trash,
2828 " frontend=%s (id=%u mode=%s), listener=%s (id=%u)",
2829 strm_fe(strm)->id, strm_fe(strm)->uuid, strm_fe(strm)->mode ? "http" : "tcp",
2830 strm_li(strm) ? strm_li(strm)->name ? strm_li(strm)->name : "?" : "?",
2831 strm_li(strm) ? strm_li(strm)->luid : 0);
2832
2833 if (conn)
2834 conn_get_to_addr(conn);
2835
2836 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
2837 case AF_INET:
2838 case AF_INET6:
2839 chunk_appendf(&trash, " addr=%s:%d\n",
2840 pn, get_host_port(&conn->addr.to));
2841 break;
2842 case AF_UNIX:
2843 chunk_appendf(&trash, " addr=unix:%d\n", strm_li(strm)->luid);
2844 break;
2845 default:
2846 /* no more information to print right now */
2847 chunk_appendf(&trash, "\n");
2848 break;
2849 }
2850
2851 if (strm->be->cap & PR_CAP_BE)
2852 chunk_appendf(&trash,
2853 " backend=%s (id=%u mode=%s)",
2854 strm->be->id,
2855 strm->be->uuid, strm->be->mode ? "http" : "tcp");
2856 else
2857 chunk_appendf(&trash, " backend=<NONE> (id=-1 mode=-)");
2858
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002859 cs = objt_cs(strm->si[1].end);
2860 conn = cs_conn(cs);
2861
William Lallemand4c5b4d52016-11-21 08:51:11 +01002862 if (conn)
2863 conn_get_from_addr(conn);
2864
2865 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
2866 case AF_INET:
2867 case AF_INET6:
2868 chunk_appendf(&trash, " addr=%s:%d\n",
2869 pn, get_host_port(&conn->addr.from));
2870 break;
2871 case AF_UNIX:
2872 chunk_appendf(&trash, " addr=unix\n");
2873 break;
2874 default:
2875 /* no more information to print right now */
2876 chunk_appendf(&trash, "\n");
2877 break;
2878 }
2879
2880 if (strm->be->cap & PR_CAP_BE)
2881 chunk_appendf(&trash,
2882 " server=%s (id=%u)",
2883 objt_server(strm->target) ? objt_server(strm->target)->id : "<none>",
2884 objt_server(strm->target) ? objt_server(strm->target)->puid : 0);
2885 else
2886 chunk_appendf(&trash, " server=<NONE> (id=-1)");
2887
2888 if (conn)
2889 conn_get_to_addr(conn);
2890
2891 switch (conn ? addr_to_str(&conn->addr.to, pn, sizeof(pn)) : AF_UNSPEC) {
2892 case AF_INET:
2893 case AF_INET6:
2894 chunk_appendf(&trash, " addr=%s:%d\n",
2895 pn, get_host_port(&conn->addr.to));
2896 break;
2897 case AF_UNIX:
2898 chunk_appendf(&trash, " addr=unix\n");
2899 break;
2900 default:
2901 /* no more information to print right now */
2902 chunk_appendf(&trash, "\n");
2903 break;
2904 }
2905
2906 chunk_appendf(&trash,
Christopher Fauletf0205062017-11-15 20:56:43 +01002907 " task=%p (state=0x%02x nice=%d calls=%d exp=%s tmask=0x%lx%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01002908 strm->task,
2909 strm->task->state,
2910 strm->task->nice, strm->task->calls,
2911 strm->task->expire ?
2912 tick_is_expired(strm->task->expire, now_ms) ? "<PAST>" :
2913 human_time(TICKS_TO_MS(strm->task->expire - now_ms),
2914 TICKS_TO_MS(1000)) : "<NEVER>",
Christopher Fauletf0205062017-11-15 20:56:43 +01002915 strm->task->thread_mask,
William Lallemand4c5b4d52016-11-21 08:51:11 +01002916 task_in_rq(strm->task) ? ", running" : "");
2917
2918 chunk_appendf(&trash,
2919 " age=%s)\n",
2920 human_time(now.tv_sec - strm->logs.accept_date.tv_sec, 1));
2921
2922 if (strm->txn)
2923 chunk_appendf(&trash,
2924 " txn=%p flags=0x%x meth=%d status=%d req.st=%s rsp.st=%s waiting=%d\n",
2925 strm->txn, strm->txn->flags, strm->txn->meth, strm->txn->status,
Willy Tarreau0da5b3b2017-09-21 09:30:46 +02002926 h1_msg_state_str(strm->txn->req.msg_state), h1_msg_state_str(strm->txn->rsp.msg_state), !LIST_ISEMPTY(&strm->buffer_wait.list));
William Lallemand4c5b4d52016-11-21 08:51:11 +01002927
2928 chunk_appendf(&trash,
2929 " si[0]=%p (state=%s flags=0x%02x endp0=%s:%p exp=%s, et=0x%03x)\n",
2930 &strm->si[0],
2931 si_state_str(strm->si[0].state),
2932 strm->si[0].flags,
2933 obj_type_name(strm->si[0].end),
2934 obj_base_ptr(strm->si[0].end),
2935 strm->si[0].exp ?
2936 tick_is_expired(strm->si[0].exp, now_ms) ? "<PAST>" :
2937 human_time(TICKS_TO_MS(strm->si[0].exp - now_ms),
2938 TICKS_TO_MS(1000)) : "<NEVER>",
2939 strm->si[0].err_type);
2940
2941 chunk_appendf(&trash,
2942 " si[1]=%p (state=%s flags=0x%02x endp1=%s:%p exp=%s, et=0x%03x)\n",
2943 &strm->si[1],
2944 si_state_str(strm->si[1].state),
2945 strm->si[1].flags,
2946 obj_type_name(strm->si[1].end),
2947 obj_base_ptr(strm->si[1].end),
2948 strm->si[1].exp ?
2949 tick_is_expired(strm->si[1].exp, now_ms) ? "<PAST>" :
2950 human_time(TICKS_TO_MS(strm->si[1].exp - now_ms),
2951 TICKS_TO_MS(1000)) : "<NEVER>",
2952 strm->si[1].err_type);
2953
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002954 if ((cs = objt_cs(strm->si[0].end)) != NULL) {
2955 conn = cs->conn;
2956
William Lallemand4c5b4d52016-11-21 08:51:11 +01002957 chunk_appendf(&trash,
Willy Tarreau53a47662017-08-28 10:53:00 +02002958 " co0=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01002959 conn,
2960 conn_get_ctrl_name(conn),
2961 conn_get_xprt_name(conn),
Willy Tarreau53a47662017-08-28 10:53:00 +02002962 conn_get_mux_name(conn),
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002963 cs_get_data_name(cs),
William Lallemand4c5b4d52016-11-21 08:51:11 +01002964 obj_type_name(conn->target),
2965 obj_base_ptr(conn->target));
2966
2967 chunk_appendf(&trash,
Christopher Fauletf0205062017-11-15 20:56:43 +01002968 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d fd.tmask=0x%lx\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01002969 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02002970 conn->handle.fd,
2971 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
Olivier Houchard12568362018-01-31 18:07:29 +01002972 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].cache.next >= -2 : 0,
Willy Tarreauebc78d72018-01-20 23:53:50 +01002973 conn->handle.fd >= 0 ? !!(fdtab[conn->handle.fd].update_mask & tid_bit) : 0,
Christopher Fauletf0205062017-11-15 20:56:43 +01002974 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].thread_mask: 0);
William Lallemand4c5b4d52016-11-21 08:51:11 +01002975 }
2976 else if ((tmpctx = objt_appctx(strm->si[0].end)) != NULL) {
2977 chunk_appendf(&trash,
Willy Tarreau9efd7452018-05-31 14:48:54 +02002978 " app0=%p st0=%d st1=%d st2=%d applet=%s tmask=0x%lx nice=%d calls=%u cpu=%llu lat=%llu\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01002979 tmpctx,
2980 tmpctx->st0,
2981 tmpctx->st1,
2982 tmpctx->st2,
Christopher Fauletf0205062017-11-15 20:56:43 +01002983 tmpctx->applet->name,
Willy Tarreau34b1fac2018-05-31 14:40:19 +02002984 tmpctx->thread_mask,
Willy Tarreau9efd7452018-05-31 14:48:54 +02002985 tmpctx->t->nice, tmpctx->t->calls,
2986 (unsigned long long)tmpctx->t->cpu_time, (unsigned long long)tmpctx->t->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01002987 }
2988
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002989 if ((cs = objt_cs(strm->si[1].end)) != NULL) {
2990 conn = cs->conn;
2991
William Lallemand4c5b4d52016-11-21 08:51:11 +01002992 chunk_appendf(&trash,
Willy Tarreau53a47662017-08-28 10:53:00 +02002993 " co1=%p ctrl=%s xprt=%s mux=%s data=%s target=%s:%p\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01002994 conn,
2995 conn_get_ctrl_name(conn),
2996 conn_get_xprt_name(conn),
Willy Tarreau53a47662017-08-28 10:53:00 +02002997 conn_get_mux_name(conn),
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002998 cs_get_data_name(cs),
William Lallemand4c5b4d52016-11-21 08:51:11 +01002999 obj_type_name(conn->target),
3000 obj_base_ptr(conn->target));
3001
3002 chunk_appendf(&trash,
Christopher Fauletf0205062017-11-15 20:56:43 +01003003 " flags=0x%08x fd=%d fd.state=%02x fd.cache=%d updt=%d fd.tmask=0x%lx\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003004 conn->flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003005 conn->handle.fd,
3006 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].state : 0,
Olivier Houchard12568362018-01-31 18:07:29 +01003007 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].cache.next >= -2 : 0,
Willy Tarreauebc78d72018-01-20 23:53:50 +01003008 conn->handle.fd >= 0 ? !!(fdtab[conn->handle.fd].update_mask & tid_bit) : 0,
Christopher Fauletf0205062017-11-15 20:56:43 +01003009 conn->handle.fd >= 0 ? fdtab[conn->handle.fd].thread_mask: 0);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003010 }
3011 else if ((tmpctx = objt_appctx(strm->si[1].end)) != NULL) {
3012 chunk_appendf(&trash,
Willy Tarreau9efd7452018-05-31 14:48:54 +02003013 " app1=%p st0=%d st1=%d st2=%d applet=%s tmask=0x%lx, nice=%d, calls=%u, cpu=%llu, lat=%llu\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003014 tmpctx,
3015 tmpctx->st0,
3016 tmpctx->st1,
3017 tmpctx->st2,
Christopher Fauletf0205062017-11-15 20:56:43 +01003018 tmpctx->applet->name,
Willy Tarreau34b1fac2018-05-31 14:40:19 +02003019 tmpctx->thread_mask,
Willy Tarreau9efd7452018-05-31 14:48:54 +02003020 tmpctx->t->nice, tmpctx->t->calls,
3021 (unsigned long long)tmpctx->t->cpu_time, (unsigned long long)tmpctx->t->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003022 }
3023
3024 chunk_appendf(&trash,
3025 " req=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3026 " an_exp=%s",
3027 &strm->req,
3028 strm->req.flags, strm->req.analysers,
3029 strm->req.pipe ? strm->req.pipe->data : 0,
3030 strm->req.to_forward, strm->req.total,
3031 strm->req.analyse_exp ?
3032 human_time(TICKS_TO_MS(strm->req.analyse_exp - now_ms),
3033 TICKS_TO_MS(1000)) : "<NEVER>");
3034
3035 chunk_appendf(&trash,
3036 " rex=%s",
3037 strm->req.rex ?
3038 human_time(TICKS_TO_MS(strm->req.rex - now_ms),
3039 TICKS_TO_MS(1000)) : "<NEVER>");
3040
3041 chunk_appendf(&trash,
3042 " wex=%s\n"
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003043 " buf=%p data=%p o=%u p=%u req.next=%d i=%u size=%u\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003044 strm->req.wex ?
3045 human_time(TICKS_TO_MS(strm->req.wex - now_ms),
3046 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003047 &strm->req.buf,
3048 b_orig(&strm->req.buf), (unsigned int)co_data(&strm->req),
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003049 (unsigned int)ci_head_ofs(&strm->req),
Willy Tarreau591d4452018-06-15 17:21:00 +02003050 strm->txn ? strm->txn->req.next : 0, (unsigned int)ci_data(&strm->req),
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003051 (unsigned int)strm->req.buf.size);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003052
3053 chunk_appendf(&trash,
3054 " res=%p (f=0x%06x an=0x%x pipe=%d tofwd=%d total=%lld)\n"
3055 " an_exp=%s",
3056 &strm->res,
3057 strm->res.flags, strm->res.analysers,
3058 strm->res.pipe ? strm->res.pipe->data : 0,
3059 strm->res.to_forward, strm->res.total,
3060 strm->res.analyse_exp ?
3061 human_time(TICKS_TO_MS(strm->res.analyse_exp - now_ms),
3062 TICKS_TO_MS(1000)) : "<NEVER>");
3063
3064 chunk_appendf(&trash,
3065 " rex=%s",
3066 strm->res.rex ?
3067 human_time(TICKS_TO_MS(strm->res.rex - now_ms),
3068 TICKS_TO_MS(1000)) : "<NEVER>");
3069
3070 chunk_appendf(&trash,
3071 " wex=%s\n"
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003072 " buf=%p data=%p o=%u p=%u rsp.next=%d i=%u size=%u\n",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003073 strm->res.wex ?
3074 human_time(TICKS_TO_MS(strm->res.wex - now_ms),
3075 TICKS_TO_MS(1000)) : "<NEVER>",
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003076 &strm->res.buf,
3077 b_orig(&strm->res.buf), (unsigned int)co_data(&strm->res),
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003078 (unsigned int)ci_head_ofs(&strm->res),
Willy Tarreau591d4452018-06-15 17:21:00 +02003079 strm->txn ? strm->txn->rsp.next : 0, (unsigned int)ci_data(&strm->res),
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003080 (unsigned int)strm->res.buf.size);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003081
Willy Tarreau06d80a92017-10-19 14:32:15 +02003082 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003083 si_rx_room_blk(si);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003084 return 0;
3085 }
3086
3087 /* use other states to dump the contents */
3088 }
3089 /* end of dump */
3090 appctx->ctx.sess.uid = 0;
3091 appctx->ctx.sess.section = 0;
3092 return 1;
3093}
3094
3095
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003096static int cli_parse_show_sess(char **args, char *payload, struct appctx *appctx, void *private)
William Lallemand4c5b4d52016-11-21 08:51:11 +01003097{
William Lallemand4c5b4d52016-11-21 08:51:11 +01003098 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
3099 return 1;
3100
3101 if (*args[2] && strcmp(args[2], "all") == 0)
3102 appctx->ctx.sess.target = (void *)-1;
3103 else if (*args[2])
3104 appctx->ctx.sess.target = (void *)strtoul(args[2], NULL, 0);
3105 else
3106 appctx->ctx.sess.target = NULL;
3107 appctx->ctx.sess.section = 0; /* start with stream status */
3108 appctx->ctx.sess.pos = 0;
3109
3110 return 0;
3111}
3112
3113/* This function dumps all streams' states onto the stream interface's
3114 * read buffer. It returns 0 if the output buffer is full and it needs
3115 * to be called again, otherwise non-zero. It is designed to be called
3116 * from stats_dump_sess_to_buffer() below.
3117 */
3118static int cli_io_handler_dump_sess(struct appctx *appctx)
3119{
3120 struct stream_interface *si = appctx->owner;
3121 struct connection *conn;
3122
3123 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
3124 /* If we're forced to shut down, we might have to remove our
3125 * reference to the last stream being dumped.
3126 */
3127 if (appctx->st2 == STAT_ST_LIST) {
3128 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3129 LIST_DEL(&appctx->ctx.sess.bref.users);
3130 LIST_INIT(&appctx->ctx.sess.bref.users);
3131 }
3132 }
3133 return 1;
3134 }
3135
3136 chunk_reset(&trash);
3137
3138 switch (appctx->st2) {
3139 case STAT_ST_INIT:
3140 /* the function had not been called yet, let's prepare the
3141 * buffer for a response. We initialize the current stream
3142 * pointer to the first in the global list. When a target
3143 * stream is being destroyed, it is responsible for updating
3144 * this pointer. We know we have reached the end when this
3145 * pointer points back to the head of the streams list.
3146 */
3147 LIST_INIT(&appctx->ctx.sess.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003148 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003149 appctx->ctx.sess.bref.ref = streams.n;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003150 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003151 appctx->st2 = STAT_ST_LIST;
3152 /* fall through */
3153
3154 case STAT_ST_LIST:
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003155 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003156 /* first, let's detach the back-ref from a possible previous stream */
3157 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users)) {
3158 LIST_DEL(&appctx->ctx.sess.bref.users);
3159 LIST_INIT(&appctx->ctx.sess.bref.users);
3160 }
3161
3162 /* and start from where we stopped */
3163 while (appctx->ctx.sess.bref.ref != &streams) {
3164 char pn[INET6_ADDRSTRLEN];
3165 struct stream *curr_strm;
3166
3167 curr_strm = LIST_ELEM(appctx->ctx.sess.bref.ref, struct stream *, list);
3168
3169 if (appctx->ctx.sess.target) {
3170 if (appctx->ctx.sess.target != (void *)-1 && appctx->ctx.sess.target != curr_strm)
3171 goto next_sess;
3172
3173 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
3174 /* call the proper dump() function and return if we're missing space */
Emeric Brun6b35e9b2017-06-30 16:23:45 +02003175 if (!stats_dump_full_strm_to_buffer(si, curr_strm)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003176 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003177 return 0;
Emeric Brun6b35e9b2017-06-30 16:23:45 +02003178 }
William Lallemand4c5b4d52016-11-21 08:51:11 +01003179
3180 /* stream dump complete */
3181 LIST_DEL(&appctx->ctx.sess.bref.users);
3182 LIST_INIT(&appctx->ctx.sess.bref.users);
3183 if (appctx->ctx.sess.target != (void *)-1) {
3184 appctx->ctx.sess.target = NULL;
3185 break;
3186 }
3187 else
3188 goto next_sess;
3189 }
3190
3191 chunk_appendf(&trash,
3192 "%p: proto=%s",
3193 curr_strm,
3194 strm_li(curr_strm) ? strm_li(curr_strm)->proto->name : "?");
3195
3196 conn = objt_conn(strm_orig(curr_strm));
3197 switch (conn ? addr_to_str(&conn->addr.from, pn, sizeof(pn)) : AF_UNSPEC) {
3198 case AF_INET:
3199 case AF_INET6:
3200 chunk_appendf(&trash,
3201 " src=%s:%d fe=%s be=%s srv=%s",
3202 pn,
3203 get_host_port(&conn->addr.from),
3204 strm_fe(curr_strm)->id,
3205 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3206 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3207 );
3208 break;
3209 case AF_UNIX:
3210 chunk_appendf(&trash,
3211 " src=unix:%d fe=%s be=%s srv=%s",
3212 strm_li(curr_strm)->luid,
3213 strm_fe(curr_strm)->id,
3214 (curr_strm->be->cap & PR_CAP_BE) ? curr_strm->be->id : "<NONE>",
3215 objt_server(curr_strm->target) ? objt_server(curr_strm->target)->id : "<none>"
3216 );
3217 break;
3218 }
3219
3220 chunk_appendf(&trash,
Willy Tarreau9efd7452018-05-31 14:48:54 +02003221 " ts=%02x age=%s calls=%d cpu=%llu lat=%llu",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003222 curr_strm->task->state,
3223 human_time(now.tv_sec - curr_strm->logs.tv_accept.tv_sec, 1),
Willy Tarreau9efd7452018-05-31 14:48:54 +02003224 curr_strm->task->calls,
3225 (unsigned long long)curr_strm->task->cpu_time, (unsigned long long)curr_strm->task->lat_time);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003226
3227 chunk_appendf(&trash,
Willy Tarreau506a29a2018-07-18 10:07:58 +02003228 " rq[f=%06xh,i=%u,an=%02xh,rx=%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003229 curr_strm->req.flags,
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003230 (unsigned int)ci_data(&curr_strm->req),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003231 curr_strm->req.analysers,
3232 curr_strm->req.rex ?
3233 human_time(TICKS_TO_MS(curr_strm->req.rex - now_ms),
3234 TICKS_TO_MS(1000)) : "");
3235
3236 chunk_appendf(&trash,
3237 ",wx=%s",
3238 curr_strm->req.wex ?
3239 human_time(TICKS_TO_MS(curr_strm->req.wex - now_ms),
3240 TICKS_TO_MS(1000)) : "");
3241
3242 chunk_appendf(&trash,
3243 ",ax=%s]",
3244 curr_strm->req.analyse_exp ?
3245 human_time(TICKS_TO_MS(curr_strm->req.analyse_exp - now_ms),
3246 TICKS_TO_MS(1000)) : "");
3247
3248 chunk_appendf(&trash,
Willy Tarreau506a29a2018-07-18 10:07:58 +02003249 " rp[f=%06xh,i=%u,an=%02xh,rx=%s",
William Lallemand4c5b4d52016-11-21 08:51:11 +01003250 curr_strm->res.flags,
Willy Tarreau7e9c30a2018-06-15 19:24:46 +02003251 (unsigned int)ci_data(&curr_strm->res),
William Lallemand4c5b4d52016-11-21 08:51:11 +01003252 curr_strm->res.analysers,
3253 curr_strm->res.rex ?
3254 human_time(TICKS_TO_MS(curr_strm->res.rex - now_ms),
3255 TICKS_TO_MS(1000)) : "");
3256
3257 chunk_appendf(&trash,
3258 ",wx=%s",
3259 curr_strm->res.wex ?
3260 human_time(TICKS_TO_MS(curr_strm->res.wex - now_ms),
3261 TICKS_TO_MS(1000)) : "");
3262
3263 chunk_appendf(&trash,
3264 ",ax=%s]",
3265 curr_strm->res.analyse_exp ?
3266 human_time(TICKS_TO_MS(curr_strm->res.analyse_exp - now_ms),
3267 TICKS_TO_MS(1000)) : "");
3268
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003269 conn = cs_conn(objt_cs(curr_strm->si[0].end));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003270 chunk_appendf(&trash,
3271 " s0=[%d,%1xh,fd=%d,ex=%s]",
3272 curr_strm->si[0].state,
3273 curr_strm->si[0].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003274 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003275 curr_strm->si[0].exp ?
3276 human_time(TICKS_TO_MS(curr_strm->si[0].exp - now_ms),
3277 TICKS_TO_MS(1000)) : "");
3278
Olivier Houchard9aaf7782017-09-13 18:30:23 +02003279 conn = cs_conn(objt_cs(curr_strm->si[1].end));
William Lallemand4c5b4d52016-11-21 08:51:11 +01003280 chunk_appendf(&trash,
3281 " s1=[%d,%1xh,fd=%d,ex=%s]",
3282 curr_strm->si[1].state,
3283 curr_strm->si[1].flags,
Willy Tarreau585744b2017-08-24 14:31:19 +02003284 conn ? conn->handle.fd : -1,
William Lallemand4c5b4d52016-11-21 08:51:11 +01003285 curr_strm->si[1].exp ?
3286 human_time(TICKS_TO_MS(curr_strm->si[1].exp - now_ms),
3287 TICKS_TO_MS(1000)) : "");
3288
3289 chunk_appendf(&trash,
3290 " exp=%s",
3291 curr_strm->task->expire ?
3292 human_time(TICKS_TO_MS(curr_strm->task->expire - now_ms),
3293 TICKS_TO_MS(1000)) : "");
3294 if (task_in_rq(curr_strm->task))
3295 chunk_appendf(&trash, " run(nice=%d)", curr_strm->task->nice);
3296
3297 chunk_appendf(&trash, "\n");
3298
Willy Tarreau06d80a92017-10-19 14:32:15 +02003299 if (ci_putchk(si_ic(si), &trash) == -1) {
William Lallemand4c5b4d52016-11-21 08:51:11 +01003300 /* let's try again later from this stream. We add ourselves into
3301 * this stream's users so that it can remove us upon termination.
3302 */
Willy Tarreaudb398432018-11-15 11:08:52 +01003303 si_rx_room_blk(si);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003304 LIST_ADDQ(&curr_strm->back_refs, &appctx->ctx.sess.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003305 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003306 return 0;
3307 }
3308
3309 next_sess:
3310 appctx->ctx.sess.bref.ref = curr_strm->list.n;
3311 }
3312
3313 if (appctx->ctx.sess.target && appctx->ctx.sess.target != (void *)-1) {
3314 /* specified stream not found */
3315 if (appctx->ctx.sess.section > 0)
3316 chunk_appendf(&trash, " *** session terminated while we were watching it ***\n");
3317 else
3318 chunk_appendf(&trash, "Session not found.\n");
3319
Willy Tarreau06d80a92017-10-19 14:32:15 +02003320 if (ci_putchk(si_ic(si), &trash) == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01003321 si_rx_room_blk(si);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003322 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003323 return 0;
3324 }
3325
3326 appctx->ctx.sess.target = NULL;
3327 appctx->ctx.sess.uid = 0;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003328 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003329 return 1;
3330 }
3331
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003332 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003333 /* fall through */
3334
3335 default:
3336 appctx->st2 = STAT_ST_FIN;
3337 return 1;
3338 }
3339}
3340
3341static void cli_release_show_sess(struct appctx *appctx)
3342{
3343 if (appctx->st2 == STAT_ST_LIST) {
Willy Tarreau7ce3f092017-11-10 16:24:41 +01003344 HA_SPIN_LOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003345 if (!LIST_ISEMPTY(&appctx->ctx.sess.bref.users))
3346 LIST_DEL(&appctx->ctx.sess.bref.users);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003347 HA_SPIN_UNLOCK(STRMS_LOCK, &streams_lock);
William Lallemand4c5b4d52016-11-21 08:51:11 +01003348 }
3349}
3350
Willy Tarreau61b65212016-11-24 11:09:25 +01003351/* Parses the "shutdown session" directive, it always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003352static int cli_parse_shutdown_session(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau61b65212016-11-24 11:09:25 +01003353{
3354 struct stream *strm, *ptr;
3355
3356 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3357 return 1;
3358
3359 if (!*args[2]) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003360 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau61b65212016-11-24 11:09:25 +01003361 appctx->ctx.cli.msg = "Session pointer expected (use 'show sess').\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003362 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau61b65212016-11-24 11:09:25 +01003363 return 1;
3364 }
3365
3366 ptr = (void *)strtoul(args[2], NULL, 0);
3367
3368 /* first, look for the requested stream in the stream table */
3369 list_for_each_entry(strm, &streams, list) {
3370 if (strm == ptr)
3371 break;
3372 }
3373
3374 /* do we have the stream ? */
3375 if (strm != ptr) {
Andjelko Iharosc3680ec2017-07-20 16:49:14 +02003376 appctx->ctx.cli.severity = LOG_ERR;
Willy Tarreau61b65212016-11-24 11:09:25 +01003377 appctx->ctx.cli.msg = "No such session (use 'show sess').\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01003378 appctx->st0 = CLI_ST_PRINT;
Willy Tarreau61b65212016-11-24 11:09:25 +01003379 return 1;
3380 }
3381
3382 stream_shutdown(strm, SF_ERR_KILLED);
3383 return 1;
3384}
3385
Willy Tarreau4e46b622016-11-23 16:50:48 +01003386/* Parses the "shutdown session server" directive, it always returns 1 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02003387static int cli_parse_shutdown_sessions_server(char **args, char *payload, struct appctx *appctx, void *private)
Willy Tarreau4e46b622016-11-23 16:50:48 +01003388{
3389 struct server *sv;
3390 struct stream *strm, *strm_bck;
3391
3392 if (!cli_has_level(appctx, ACCESS_LVL_ADMIN))
3393 return 1;
3394
3395 sv = cli_find_server(appctx, args[3]);
3396 if (!sv)
3397 return 1;
3398
3399 /* kill all the stream that are on this server */
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003400 HA_SPIN_LOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau4e46b622016-11-23 16:50:48 +01003401 list_for_each_entry_safe(strm, strm_bck, &sv->actconns, by_srv)
3402 if (strm->srv_conn == sv)
3403 stream_shutdown(strm, SF_ERR_KILLED);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003404 HA_SPIN_UNLOCK(SERVER_LOCK, &sv->lock);
Willy Tarreau4e46b622016-11-23 16:50:48 +01003405 return 1;
3406}
3407
William Lallemand4c5b4d52016-11-21 08:51:11 +01003408/* register cli keywords */
3409static struct cli_kw_list cli_kws = {{ },{
3410 { { "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 +01003411 { { "shutdown", "session", NULL }, "shutdown session : kill a specific session", cli_parse_shutdown_session, NULL, NULL },
Willy Tarreau4e46b622016-11-23 16:50:48 +01003412 { { "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 +01003413 {{},}
3414}};
3415
Willy Tarreau0108d902018-11-25 19:14:37 +01003416INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws);
3417
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003418/* main configuration keyword registration. */
3419static struct action_kw_list stream_tcp_keywords = { ILH, {
3420 { "use-service", stream_parse_use_service },
3421 { /* END */ }
3422}};
3423
Willy Tarreau0108d902018-11-25 19:14:37 +01003424INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &stream_tcp_keywords);
3425
Thierry FOURNIER5a363e72015-09-27 19:29:33 +02003426static struct action_kw_list stream_http_keywords = { ILH, {
3427 { "use-service", stream_parse_use_service },
3428 { /* END */ }
3429}};
3430
Willy Tarreau0108d902018-11-25 19:14:37 +01003431INITCALL1(STG_REGISTER, http_req_keywords_register, &stream_http_keywords);
Willy Tarreau8b22a712010-06-18 17:46:06 +02003432
Willy Tarreaubaaee002006-06-26 02:48:02 +02003433/*
3434 * Local variables:
3435 * c-indent-level: 8
3436 * c-basic-offset: 8
3437 * End:
3438 */