blob: 6e4c0753ef27d26a452f1412704168e74a937e94 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * Session 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
17#include <common/config.h>
Willy Tarreau9b28e032012-10-12 23:49:43 +020018#include <common/buffer.h>
Willy Tarreau7c669d72008-06-20 15:04:11 +020019#include <common/debug.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <types/capture.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010023#include <types/global.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020025#include <proto/acl.h>
Willy Tarreau61612d42012-04-19 18:42:05 +020026#include <proto/arg.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010027#include <proto/backend.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028#include <proto/channel.h>
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +010029#include <proto/checks.h>
Willy Tarreaud2274c62012-07-06 14:29:45 +020030#include <proto/connection.h>
Willy Tarreau5ca791d2009-08-16 19:06:42 +020031#include <proto/dumpstats.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020032#include <proto/fd.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020033#include <proto/freq_ctr.h>
Willy Tarreau3041b9f2010-10-15 23:25:20 +020034#include <proto/frontend.h>
Willy Tarreau8d5d7f22007-01-21 19:16:41 +010035#include <proto/hdr_idx.h>
Willy Tarreaud1d54542012-09-12 22:58:11 +020036#include <proto/listener.h>
Willy Tarreau332f8bf2007-05-13 21:36:56 +020037#include <proto/log.h>
Willy Tarreaucbaaec42012-09-06 11:32:07 +020038#include <proto/raw_sock.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020039#include <proto/session.h>
Willy Tarreau3eba98a2009-01-25 13:56:13 +010040#include <proto/pipe.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010041#include <proto/proto_http.h>
42#include <proto/proto_tcp.h>
Willy Tarreau1d0dfb12009-07-07 15:10:31 +020043#include <proto/proxy.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020044#include <proto/queue.h>
Willy Tarreau7f062c42009-03-05 18:43:00 +010045#include <proto/server.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020046#include <proto/sample.h>
Emeric Brun1d33b292010-01-04 15:47:17 +010047#include <proto/stick_table.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010048#include <proto/stream_interface.h>
Willy Tarreau55a8d0e2008-11-30 18:47:21 +010049#include <proto/task.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020050
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020051struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010052struct list sessions;
Willy Tarreaubaaee002006-06-26 02:48:02 +020053
Willy Tarreau071e1372012-10-03 01:39:48 +020054static int conn_session_complete(struct connection *conn);
Willy Tarreau9683e9a2012-10-03 21:17:23 +020055static int conn_session_update(struct connection *conn);
Willy Tarreau2542b532012-08-31 16:01:23 +020056static struct task *expire_mini_session(struct task *t);
57int session_complete(struct session *s);
58
Willy Tarreau5e75e272012-10-02 21:21:20 +020059/* data layer callbacks for an embryonic session */
60struct data_cb sess_conn_cb = {
61 .recv = NULL,
62 .send = NULL,
Willy Tarreau9683e9a2012-10-03 21:17:23 +020063 .wake = conn_session_update,
Willy Tarreau071e1372012-10-03 01:39:48 +020064 .init = conn_session_complete,
Willy Tarreau5e75e272012-10-02 21:21:20 +020065};
66
Willy Tarreau2542b532012-08-31 16:01:23 +020067/* This function is called from the protocol layer accept() in order to
68 * instanciate a new embryonic session on behalf of a given listener and
69 * frontend. It returns a positive value upon success, 0 if the connection
70 * can be ignored, or a negative value upon critical failure. The accepted
71 * file descriptor is closed if we return <= 0.
Willy Tarreau81f9aa32010-06-01 17:45:26 +020072 */
73int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr)
74{
Willy Tarreaub363a1f2013-10-01 10:45:07 +020075 struct connection *cli_conn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020076 struct proxy *p = l->frontend;
77 struct session *s;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020078 struct task *t;
Willy Tarreauabe8ea52010-11-11 10:56:04 +010079 int ret;
80
81
82 ret = -1; /* assume unrecoverable error by default */
Willy Tarreau81f9aa32010-06-01 17:45:26 +020083
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020084 if (unlikely((cli_conn = conn_new()) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +020085 goto out_close;
Willy Tarreau81f9aa32010-06-01 17:45:26 +020086
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020087 conn_prepare(cli_conn, l->proto, l->xprt);
Willy Tarreauf2943dc2012-10-26 20:10:28 +020088
Willy Tarreau32e3c6a2013-10-11 19:34:20 +020089 cli_conn->t.sock.fd = cfd;
90 cli_conn->addr.from = *addr;
91 cli_conn->flags |= CO_FL_ADDR_FROM_SET;
92 cli_conn->target = &l->obj_type;
93
94 if (unlikely((s = pool_alloc2(pool2_session)) == NULL))
95 goto out_free_conn;
Willy Tarreauf2943dc2012-10-26 20:10:28 +020096
Willy Tarreau2542b532012-08-31 16:01:23 +020097 /* minimum session initialization required for an embryonic session is
98 * fairly low. We need very little to execute L4 ACLs, then we need a
99 * task to make the client-side connection live on its own.
100 * - flags
101 * - stick-entry tracking
102 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200103 s->flags = 0;
104 s->logs.logwait = p->to_log;
Willy Tarreau9a355ec2013-06-11 17:45:46 +0200105 s->logs.level = 0;
Willy Tarreaud5ca9ab2013-05-28 17:40:25 +0200106
107 memset(s->stkctr, 0, sizeof(s->stkctr));
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200108
Willy Tarreau2542b532012-08-31 16:01:23 +0200109 s->listener = l;
110 s->fe = p;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100111
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200112 /* On a mini-session, the connection is directly attached to the
113 * session's target so that we don't need to initialize the stream
114 * interfaces. Another benefit is that it's easy to detect a mini-
115 * session in dumps using this : it's the only one which has a
116 * connection in s->target.
117 */
118 s->target = &cli_conn->obj_type;
119
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200120 s->logs.accept_date = date; /* user-visible date for logging */
121 s->logs.tv_accept = now; /* corrected date for internal use */
Willy Tarreau1f0da242014-01-25 11:01:50 +0100122 s->uniq_id = global.req_count++;
Willy Tarreau2542b532012-08-31 16:01:23 +0200123 p->feconn++;
124 /* This session was accepted, count it now */
125 if (p->feconn > p->fe_counters.conn_max)
126 p->fe_counters.conn_max = p->feconn;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200127
Willy Tarreau2542b532012-08-31 16:01:23 +0200128 proxy_inc_fe_conn_ctr(l, p);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200129
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100130 /* Add the minimum callbacks to prepare the connection's control layer.
131 * We need this so that we can safely execute the ACLs used by the
132 * "tcp-request connection" ruleset. We also carefully attach the
133 * connection to the stream interface without initializing the rest,
134 * so that ACLs can use si[0]->end.
135 */
136 si_attach_conn(&s->si[0], cli_conn);
137 conn_attach(cli_conn, s, &sess_conn_cb);
138 conn_ctrl_init(cli_conn);
139
Willy Tarreau2cff2f72013-12-16 10:12:54 +0100140 /* now evaluate the tcp-request layer4 rules. Since we expect to be able
141 * to abort right here as soon as possible, we check the rules before
142 * even initializing the stream interfaces.
143 */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200144 if ((l->options & LI_O_TCP_RULES) && !tcp_exec_req_rules(s)) {
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200145 /* let's do a no-linger now to close with a single RST. */
146 setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100147 ret = 0; /* successful termination */
Willy Tarreau2542b532012-08-31 16:01:23 +0200148 goto out_free_session;
149 }
150
Willy Tarreau82569f92012-09-27 23:48:56 +0200151 /* monitor-net and health mode are processed immediately after TCP
152 * connection rules. This way it's possible to block them, but they
153 * never use the lower data layers, they send directly over the socket,
154 * as they were designed for. We first flush the socket receive buffer
155 * in order to avoid emission of an RST by the system. We ignore any
156 * error.
157 */
158 if (unlikely((p->mode == PR_MODE_HEALTH) ||
159 ((l->options & LI_O_CHK_MONNET) &&
160 addr->ss_family == AF_INET &&
161 (((struct sockaddr_in *)addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr))) {
162 /* we have 4 possibilities here :
163 * - HTTP mode, from monitoring address => send "HTTP/1.0 200 OK"
164 * - HEALTH mode with HTTP check => send "HTTP/1.0 200 OK"
165 * - HEALTH mode without HTTP check => just send "OK"
166 * - TCP mode from monitoring address => just close
167 */
Willy Tarreau2b57cb82013-06-10 19:56:38 +0200168 if (l->proto->drain)
169 l->proto->drain(cfd);
Willy Tarreau82569f92012-09-27 23:48:56 +0200170 if (p->mode == PR_MODE_HTTP ||
171 (p->mode == PR_MODE_HEALTH && (p->options2 & PR_O2_CHK_ANY) == PR_O2_HTTP_CHK))
172 send(cfd, "HTTP/1.0 200 OK\r\n\r\n", 19, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
173 else if (p->mode == PR_MODE_HEALTH)
174 send(cfd, "OK\n", 3, MSG_DONTWAIT|MSG_NOSIGNAL|MSG_MORE);
175 ret = 0;
176 goto out_free_session;
177 }
178
Willy Tarreau22cda212012-08-31 17:43:29 +0200179 /* wait for a PROXY protocol header */
180 if (l->options & LI_O_ACC_PROXY) {
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200181 cli_conn->flags |= CO_FL_ACCEPT_PROXY;
182 conn_sock_want_recv(cli_conn);
Willy Tarreau22cda212012-08-31 17:43:29 +0200183 }
184
Willy Tarreau2542b532012-08-31 16:01:23 +0200185 if (unlikely((t = task_new()) == NULL))
186 goto out_free_session;
187
188 t->context = s;
189 t->nice = l->nice;
190 s->task = t;
191
Willy Tarreau59e3ff42013-12-16 02:16:50 +0100192 /* Finish setting the callbacks. Right now the transport layer is present
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200193 * but not initialized. Also note we need to be careful as the stream
194 * int is not initialized yet.
Willy Tarreau2542b532012-08-31 16:01:23 +0200195 */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200196 conn_data_want_recv(cli_conn);
197 if (conn_xprt_init(cli_conn) < 0)
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100198 goto out_free_task;
Willy Tarreau2542b532012-08-31 16:01:23 +0200199
200 /* OK, now either we have a pending handshake to execute with and
201 * then we must return to the I/O layer, or we can proceed with the
202 * end of the session initialization. In case of handshake, we also
203 * set the I/O timeout to the frontend's client timeout.
204 */
205
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200206 if (cli_conn->flags & CO_FL_HANDSHAKE) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200207 t->process = expire_mini_session;
208 t->expire = tick_add_ifset(now_ms, p->timeout.client);
209 task_queue(t);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200210 cli_conn->flags |= CO_FL_INIT_DATA | CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200211 return 1;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200212 }
213
Willy Tarreau815f5ec2012-11-06 00:14:25 +0100214 /* OK let's complete session initialization since there is no handshake */
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200215 cli_conn->flags |= CO_FL_CONNECTED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200216 ret = session_complete(s);
217 if (ret > 0)
218 return ret;
219
220 /* Error unrolling */
221 out_free_task:
222 task_free(t);
223 out_free_session:
224 p->feconn--;
Willy Tarreaue9101692014-01-28 22:48:24 +0100225 session_store_counters(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200226 pool_free2(pool2_session, s);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200227 out_free_conn:
228 cli_conn->flags &= ~CO_FL_XPRT_TRACKED;
229 conn_xprt_close(cli_conn);
230 conn_free(cli_conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200231 out_close:
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200232 if (ret < 0 && l->xprt == &raw_sock && p->mode == PR_MODE_HTTP) {
Willy Tarreau2542b532012-08-31 16:01:23 +0200233 /* critical error, no more memory, try to emit a 500 response */
Willy Tarreau05bf5e12013-10-20 23:10:28 +0200234 struct chunk *err_msg = &p->errmsg[HTTP_ERR_500];
235 if (!err_msg->str)
236 err_msg = &http_err_chunks[HTTP_ERR_500];
Willy Tarreau2542b532012-08-31 16:01:23 +0200237 send(cfd, err_msg->str, err_msg->len, MSG_DONTWAIT|MSG_NOSIGNAL);
238 }
239
240 if (fdtab[cfd].owner)
241 fd_delete(cfd);
242 else
243 close(cfd);
244 return ret;
245}
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100246
Willy Tarreau0af29122012-12-03 15:35:00 +0100247
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200248/* prepare the trash with a log prefix for session <s>. It only works with
Willy Tarreaub4f98092014-05-08 21:06:11 +0200249 * embryonic sessions based on a real connection. This function requires that
250 * at s->target still points to the incoming connection.
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200251 */
Willy Tarreau0af29122012-12-03 15:35:00 +0100252static void prepare_mini_sess_log_prefix(struct session *s)
253{
254 struct tm tm;
255 char pn[INET6_ADDRSTRLEN];
256 int ret;
257 char *end;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200258 struct connection *cli_conn = __objt_conn(s->target);
Willy Tarreau0af29122012-12-03 15:35:00 +0100259
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200260 ret = addr_to_str(&cli_conn->addr.from, pn, sizeof(pn));
Willy Tarreau0af29122012-12-03 15:35:00 +0100261 if (ret <= 0)
262 chunk_printf(&trash, "unknown [");
263 else if (ret == AF_UNIX)
264 chunk_printf(&trash, "%s:%d [", pn, s->listener->luid);
265 else
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200266 chunk_printf(&trash, "%s:%d [", pn, get_host_port(&cli_conn->addr.from));
Willy Tarreau0af29122012-12-03 15:35:00 +0100267
268 get_localtime(s->logs.accept_date.tv_sec, &tm);
269 end = date2str_log(trash.str + trash.len, &tm, &(s->logs.accept_date), trash.size - trash.len);
270 trash.len = end - trash.str;
271 if (s->listener->name)
272 chunk_appendf(&trash, "] %s/%s", s->fe->id, s->listener->name);
273 else
274 chunk_appendf(&trash, "] %s/%d", s->fe->id, s->listener->luid);
275}
276
Willy Tarreau2542b532012-08-31 16:01:23 +0200277/* This function kills an existing embryonic session. It stops the connection's
Willy Tarreauf7bc57c2012-10-03 00:19:48 +0200278 * transport layer, releases assigned resources, resumes the listener if it was
Willy Tarreaub4f98092014-05-08 21:06:11 +0200279 * disabled and finally kills the file descriptor. This function requires that
280 * at s->target still points to the incoming connection.
Willy Tarreau2542b532012-08-31 16:01:23 +0200281 */
282static void kill_mini_session(struct session *s)
283{
Willy Tarreau0af29122012-12-03 15:35:00 +0100284 int level = LOG_INFO;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200285 struct connection *conn = __objt_conn(s->target);
Willy Tarreau0af29122012-12-03 15:35:00 +0100286 unsigned int log = s->logs.logwait;
287 const char *err_msg;
288
289 if (s->fe->options2 & PR_O2_LOGERRORS)
290 level = LOG_ERR;
291
292 if (log && (s->fe->options & PR_O_NULLNOLOG)) {
293 /* with "option dontlognull", we don't log connections with no transfer */
Willy Tarreau8e3bf692012-12-03 15:41:18 +0100294 if (!conn->err_code ||
Willy Tarreau20879a02012-12-03 16:32:10 +0100295 conn->err_code == CO_ER_PRX_EMPTY || conn->err_code == CO_ER_PRX_ABORT ||
296 conn->err_code == CO_ER_SSL_EMPTY || conn->err_code == CO_ER_SSL_ABORT)
Willy Tarreau0af29122012-12-03 15:35:00 +0100297 log = 0;
298 }
299
300 if (log) {
301 if (!conn->err_code && (s->task->state & TASK_WOKEN_TIMER)) {
302 if (conn->flags & CO_FL_ACCEPT_PROXY)
303 conn->err_code = CO_ER_PRX_TIMEOUT;
304 else if (conn->flags & CO_FL_SSL_WAIT_HS)
305 conn->err_code = CO_ER_SSL_TIMEOUT;
306 }
307
308 prepare_mini_sess_log_prefix(s);
309 err_msg = conn_err_code_str(conn);
310 if (err_msg)
311 send_log(s->fe, level, "%s: %s\n", trash.str, err_msg);
312 else
313 send_log(s->fe, level, "%s: unknown connection error (code=%d flags=%08x)\n",
314 trash.str, conn->err_code, conn->flags);
315 }
316
Willy Tarreau2542b532012-08-31 16:01:23 +0200317 /* kill the connection now */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200318 conn_force_close(conn);
Willy Tarreaua23ee3a2014-02-05 00:18:47 +0100319 conn_free(conn);
Willy Tarreau2542b532012-08-31 16:01:23 +0200320
321 s->fe->feconn--;
Willy Tarreau7af7d592013-07-01 18:07:03 +0200322 session_store_counters(s);
Willy Tarreau2542b532012-08-31 16:01:23 +0200323
324 if (!(s->listener->options & LI_O_UNLIMITED))
325 actconn--;
326 jobs--;
327 s->listener->nbconn--;
328 if (s->listener->state == LI_FULL)
329 resume_listener(s->listener);
330
331 /* Dequeues all of the listeners waiting for a resource */
332 if (!LIST_ISEMPTY(&global_listener_queue))
333 dequeue_all_listeners(&global_listener_queue);
334
335 if (!LIST_ISEMPTY(&s->fe->listener_queue) &&
336 (!s->fe->fe_sps_lim || freq_ctr_remain(&s->fe->fe_sess_per_sec, s->fe->fe_sps_lim, 0) > 0))
337 dequeue_all_listeners(&s->fe->listener_queue);
338
339 task_delete(s->task);
340 task_free(s->task);
Willy Tarreau2542b532012-08-31 16:01:23 +0200341 pool_free2(pool2_session, s);
342}
343
Willy Tarreau22cda212012-08-31 17:43:29 +0200344/* Finish initializing a session from a connection, or kills it if the
345 * connection shows and error. Returns <0 if the connection was killed.
Willy Tarreau2542b532012-08-31 16:01:23 +0200346 */
Willy Tarreau071e1372012-10-03 01:39:48 +0200347static int conn_session_complete(struct connection *conn)
Willy Tarreau2542b532012-08-31 16:01:23 +0200348{
Willy Tarreau5e75e272012-10-02 21:21:20 +0200349 struct session *s = conn->owner;
Willy Tarreau2542b532012-08-31 16:01:23 +0200350
Willy Tarreau22cda212012-08-31 17:43:29 +0200351 if (!(conn->flags & CO_FL_ERROR) && (session_complete(s) > 0)) {
Willy Tarreau071e1372012-10-03 01:39:48 +0200352 conn->flags &= ~CO_FL_INIT_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200353 return 0;
354 }
355
356 /* kill the connection now */
357 kill_mini_session(s);
358 return -1;
359}
360
Willy Tarreau9683e9a2012-10-03 21:17:23 +0200361/* Update an embryonic session status. The connection is killed in case of
362 * error, and <0 will be returned. Otherwise it does nothing.
363 */
364static int conn_session_update(struct connection *conn)
365{
366 if (conn->flags & CO_FL_ERROR) {
367 kill_mini_session(conn->owner);
368 return -1;
369 }
370 return 0;
371}
372
Willy Tarreau2542b532012-08-31 16:01:23 +0200373/* Manages embryonic sessions timeout. It is only called when the timeout
374 * strikes and performs the required cleanup.
375 */
376static struct task *expire_mini_session(struct task *t)
377{
378 struct session *s = t->context;
379
380 if (!(t->state & TASK_WOKEN_TIMER))
381 return t;
382
383 kill_mini_session(s);
384 return NULL;
385}
386
387/* This function is called from the I/O handler which detects the end of
388 * handshake, in order to complete initialization of a valid session. It must
389 * be called with an embryonic session. It returns a positive value upon
390 * success, 0 if the connection can be ignored, or a negative value upon
391 * critical failure. The accepted file descriptor is closed if we return <= 0.
Willy Tarreaub4f98092014-05-08 21:06:11 +0200392 * The client-side end point is assumed to be a connection, whose pointer is
393 * taken from s->target which is assumed to be valid. If the function fails,
394 * it restores s->target.
Willy Tarreau2542b532012-08-31 16:01:23 +0200395 */
396int session_complete(struct session *s)
397{
398 struct listener *l = s->listener;
399 struct proxy *p = s->fe;
400 struct http_txn *txn;
401 struct task *t = s->task;
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200402 struct connection *conn = __objt_conn(s->target);
Willy Tarreau2542b532012-08-31 16:01:23 +0200403 int ret;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100404 int i;
Willy Tarreau2542b532012-08-31 16:01:23 +0200405
406 ret = -1; /* assume unrecoverable error by default */
407
408 /* OK, we're keeping the session, so let's properly initialize the session */
409 LIST_ADDQ(&sessions, &s->list);
410 LIST_INIT(&s->back_refs);
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200411
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200412 s->flags |= SN_INITIALIZED;
Willy Tarreau2542b532012-08-31 16:01:23 +0200413 s->unique_id = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200414
415 t->process = l->handler;
416 t->context = s;
417 t->expire = TICK_ETERNITY;
418
419 /* Note: initially, the session's backend points to the frontend.
420 * This changes later when switching rules are executed or
421 * when the default backend is assigned.
422 */
423 s->be = s->fe;
424 s->req = s->rep = NULL; /* will be allocated later */
William Lallemand82fe75c2012-10-23 10:25:10 +0200425 s->comp_algo = NULL;
Willy Tarreau2542b532012-08-31 16:01:23 +0200426
427 /* Let's count a session now */
Willy Tarreaub36b4242010-06-04 20:59:39 +0200428 proxy_inc_fe_sess_ctr(l, p);
Willy Tarreau91c43d72010-06-20 11:19:22 +0200429
Willy Tarreaub4c84932013-07-23 19:15:30 +0200430 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200431 void *ptr;
432
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100433 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100434 continue;
435
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100436 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_CNT);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200437 if (ptr)
438 stktable_data_cast(ptr, sess_cnt)++;
439
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100440 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_SESS_RATE);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200441 if (ptr)
442 update_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100443 s->stkctr[i].table->data_arg[STKTABLE_DT_SESS_RATE].u, 1);
Willy Tarreau9e9879a2010-08-06 15:25:22 +0200444 }
445
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200446 /* this part should be common with other protocols */
447 si_reset(&s->si[0], t);
448 si_set_state(&s->si[0], SI_ST_EST);
449
Willy Tarreaub4f98092014-05-08 21:06:11 +0200450 /* attach the incoming connection to the stream interface now.
451 * We must do that *before* clearing ->target because we need
452 * to keep a pointer to the connection in case we have to call
453 * kill_mini_session().
454 */
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200455 si_attach_conn(&s->si[0], conn);
456
457 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
458 s->si[0].flags |= SI_FL_INDEP_STR;
459
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200460 /* pre-initialize the other side's stream interface to an INIT state. The
461 * callbacks will be initialized before attempting to connect.
462 */
Willy Tarreau3ed35ef2013-10-24 11:51:38 +0200463 si_reset(&s->si[1], t);
Willy Tarreau2a6e8802013-10-24 15:50:53 +0200464 si_detach(&s->si[1]);
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200465
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200466 if (likely(s->fe->options2 & PR_O2_INDEPSTR))
467 s->si[1].flags |= SI_FL_INDEP_STR;
468
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200469 session_init_srv_conn(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100470 s->target = NULL;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200471 s->pend_pos = NULL;
472
473 /* init store persistence */
474 s->store_count = 0;
475
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200476 if (unlikely((s->req = pool_alloc2(pool2_channel)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200477 goto out_free_task; /* no memory */
478
Willy Tarreau9b28e032012-10-12 23:49:43 +0200479 if (unlikely((s->req->buf = pool_alloc2(pool2_buffer)) == NULL))
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200480 goto out_free_req; /* no memory */
481
Willy Tarreau9b28e032012-10-12 23:49:43 +0200482 if (unlikely((s->rep = pool_alloc2(pool2_channel)) == NULL))
483 goto out_free_req_buf; /* no memory */
484
485 if (unlikely((s->rep->buf = pool_alloc2(pool2_buffer)) == NULL))
486 goto out_free_rep; /* no memory */
487
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200488 /* initialize the request buffer */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200489 s->req->buf->size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200490 channel_init(s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200491 s->req->prod = &s->si[0];
492 s->req->cons = &s->si[1];
493 s->si[0].ib = s->si[1].ob = s->req;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200494 s->req->flags |= CF_READ_ATTACHED; /* the producer is already connected */
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200495
496 /* activate default analysers enabled for this listener */
497 s->req->analysers = l->analysers;
498
499 s->req->wto = TICK_ETERNITY;
500 s->req->rto = TICK_ETERNITY;
501 s->req->rex = TICK_ETERNITY;
502 s->req->wex = TICK_ETERNITY;
503 s->req->analyse_exp = TICK_ETERNITY;
504
505 /* initialize response buffer */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200506 s->rep->buf->size = global.tune.bufsize;
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200507 channel_init(s->rep);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200508 s->rep->prod = &s->si[1];
509 s->rep->cons = &s->si[0];
510 s->si[0].ob = s->si[1].ib = s->rep;
511 s->rep->analysers = 0;
512
Willy Tarreau96e31212011-05-30 18:10:30 +0200513 if (s->fe->options2 & PR_O2_NODELAY) {
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200514 s->req->flags |= CF_NEVER_WAIT;
515 s->rep->flags |= CF_NEVER_WAIT;
Willy Tarreau96e31212011-05-30 18:10:30 +0200516 }
517
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200518 s->rep->rto = TICK_ETERNITY;
519 s->rep->wto = TICK_ETERNITY;
520 s->rep->rex = TICK_ETERNITY;
521 s->rep->wex = TICK_ETERNITY;
522 s->rep->analyse_exp = TICK_ETERNITY;
523
Willy Tarreau62f791e2012-03-09 11:32:30 +0100524 txn = &s->txn;
525 /* Those variables will be checked and freed if non-NULL in
526 * session.c:session_free(). It is important that they are
527 * properly initialized.
528 */
529 txn->sessid = NULL;
530 txn->srv_cookie = NULL;
531 txn->cli_cookie = NULL;
532 txn->uri = NULL;
533 txn->req.cap = NULL;
534 txn->rsp.cap = NULL;
535 txn->hdr_idx.v = NULL;
536 txn->hdr_idx.size = txn->hdr_idx.used = 0;
Willy Tarreau068621e2013-12-23 15:11:25 +0100537 txn->flags = 0;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100538 txn->req.flags = 0;
539 txn->rsp.flags = 0;
540 /* the HTTP messages need to know what buffer they're associated with */
Willy Tarreau394db372012-10-12 22:40:39 +0200541 txn->req.chn = s->req;
542 txn->rsp.chn = s->rep;
Willy Tarreau62f791e2012-03-09 11:32:30 +0100543
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200544 /* finish initialization of the accepted file descriptor */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200545 conn_data_want_recv(conn);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200546
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100547 if (p->accept && (ret = p->accept(s)) <= 0) {
548 /* Either we had an unrecoverable error (<0) or work is
549 * finished (=0, eg: monitoring), in both situations,
550 * we can release everything and close.
551 */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200552 goto out_free_rep_buf;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200553 }
554
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200555 /* if logs require transport layer information, note it on the connection */
556 if (s->logs.logwait & LW_XPRT)
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200557 conn->flags |= CO_FL_XPRT_TRACKED;
Willy Tarreau93dbc2b2012-10-12 18:01:49 +0200558
Willy Tarreau2542b532012-08-31 16:01:23 +0200559 /* we want the connection handler to notify the stream interface about updates. */
Willy Tarreauf8a49ea2013-10-14 21:32:07 +0200560 conn->flags |= CO_FL_WAKE_DATA;
Willy Tarreau2542b532012-08-31 16:01:23 +0200561
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200562 /* it is important not to call the wakeup function directly but to
563 * pass through task_wakeup(), because this one knows how to apply
564 * priorities to tasks.
565 */
566 task_wakeup(t, TASK_WOKEN_INIT);
567 return 1;
568
569 /* Error unrolling */
Willy Tarreau9b28e032012-10-12 23:49:43 +0200570 out_free_rep_buf:
571 pool_free2(pool2_buffer, s->rep->buf);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200572 out_free_rep:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200573 pool_free2(pool2_channel, s->rep);
Willy Tarreau9b28e032012-10-12 23:49:43 +0200574 out_free_req_buf:
575 pool_free2(pool2_buffer, s->req->buf);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200576 out_free_req:
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200577 pool_free2(pool2_channel, s->req);
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200578 out_free_task:
Willy Tarreaub4f98092014-05-08 21:06:11 +0200579 /* and restore the connection pointer in case we destroyed it,
580 * because kill_mini_session() will need it.
581 */
Willy Tarreau8a0b93b2014-11-25 17:10:33 +0100582 LIST_DEL(&s->list);
Willy Tarreaub4f98092014-05-08 21:06:11 +0200583 s->target = &conn->obj_type;
Willy Tarreauabe8ea52010-11-11 10:56:04 +0100584 return ret;
Willy Tarreau81f9aa32010-06-01 17:45:26 +0200585}
586
Willy Tarreaubaaee002006-06-26 02:48:02 +0200587/*
588 * frees the context associated to a session. It must have been removed first.
589 */
Simon Hormandec5be42011-06-08 09:19:07 +0900590static void session_free(struct session *s)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200591{
Willy Tarreau4dbc4a22007-03-03 16:23:22 +0100592 struct http_txn *txn = &s->txn;
Willy Tarreau632f5a72007-07-11 10:42:35 +0200593 struct proxy *fe = s->fe;
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100594 struct bref *bref, *back;
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200595 struct connection *cli_conn = objt_conn(s->si[0].end);
Willy Tarreaua4cda672010-06-06 18:28:49 +0200596 int i;
Willy Tarreau0f7562b2007-01-07 15:46:13 +0100597
Willy Tarreaubaaee002006-06-26 02:48:02 +0200598 if (s->pend_pos)
599 pendconn_free(s->pend_pos);
Willy Tarreau922a8062008-12-04 09:33:58 +0100600
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100601 if (objt_server(s->target)) { /* there may be requests left pending in queue */
Willy Tarreau1e62de62008-11-11 20:20:02 +0100602 if (s->flags & SN_CURR_SESS) {
603 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100604 objt_server(s->target)->cur_sess--;
Willy Tarreau1e62de62008-11-11 20:20:02 +0100605 }
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100606 if (may_dequeue_tasks(objt_server(s->target), s->be))
607 process_srv_queue(objt_server(s->target));
Willy Tarreau1e62de62008-11-11 20:20:02 +0100608 }
Willy Tarreau922a8062008-12-04 09:33:58 +0100609
Willy Tarreau7c669d72008-06-20 15:04:11 +0200610 if (unlikely(s->srv_conn)) {
611 /* the session still has a reserved slot on a server, but
612 * it should normally be only the same as the one above,
613 * so this should not happen in fact.
614 */
615 sess_change_server(s, NULL);
616 }
617
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100618 if (s->req->pipe)
619 put_pipe(s->req->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100620
Willy Tarreau3eba98a2009-01-25 13:56:13 +0100621 if (s->rep->pipe)
622 put_pipe(s->rep->pipe);
Willy Tarreau259de1b2009-01-18 21:56:21 +0100623
Willy Tarreau9b28e032012-10-12 23:49:43 +0200624 pool_free2(pool2_buffer, s->req->buf);
625 pool_free2(pool2_buffer, s->rep->buf);
626
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200627 pool_free2(pool2_channel, s->req);
628 pool_free2(pool2_channel, s->rep);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200629
Willy Tarreau46023632010-01-07 22:51:47 +0100630 http_end_txn(s);
631
Willy Tarreau1e954912012-10-12 17:50:05 +0200632 /* ensure the client-side transport layer is destroyed */
Willy Tarreauf79c8172013-10-21 16:30:56 +0200633 if (cli_conn)
634 conn_force_close(cli_conn);
Willy Tarreau1e954912012-10-12 17:50:05 +0200635
Willy Tarreaua4cda672010-06-06 18:28:49 +0200636 for (i = 0; i < s->store_count; i++) {
637 if (!s->store[i].ts)
638 continue;
639 stksess_free(s->store[i].table, s->store[i].ts);
640 s->store[i].ts = NULL;
641 }
642
Willy Tarreau34eb6712011-10-24 18:15:04 +0200643 pool_free2(pool2_hdr_idx, txn->hdr_idx.v);
Willy Tarreau92fb9832007-10-16 17:34:28 +0200644 if (fe) {
Willy Tarreau46023632010-01-07 22:51:47 +0100645 pool_free2(fe->rsp_cap_pool, txn->rsp.cap);
646 pool_free2(fe->req_cap_pool, txn->req.cap);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200647 }
Willy Tarreau0937bc42009-12-22 15:03:09 +0100648
Willy Tarreau7af7d592013-07-01 18:07:03 +0200649 session_store_counters(s);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200650
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100651 list_for_each_entry_safe(bref, back, &s->back_refs, users) {
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100652 /* we have to unlink all watchers. We must not relink them if
653 * this session was the last one in the list.
654 */
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100655 LIST_DEL(&bref->users);
Willy Tarreaufd3828e2009-02-22 15:17:24 +0100656 LIST_INIT(&bref->users);
657 if (s->list.n != &sessions)
658 LIST_ADDQ(&LIST_ELEM(s->list.n, struct session *, list)->back_refs, &bref->users);
Willy Tarreau62e4f1d2008-12-07 20:16:23 +0100659 bref->ref = s->list.n;
660 }
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100661 LIST_DEL(&s->list);
Willy Tarreau32e3c6a2013-10-11 19:34:20 +0200662 si_release_endpoint(&s->si[1]);
663 si_release_endpoint(&s->si[0]);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200664 pool_free2(pool2_session, s);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200665
666 /* We may want to free the maximum amount of pools if the proxy is stopping */
Willy Tarreau92fb9832007-10-16 17:34:28 +0200667 if (fe && unlikely(fe->state == PR_STSTOPPED)) {
Willy Tarreau9b28e032012-10-12 23:49:43 +0200668 pool_flush2(pool2_buffer);
Willy Tarreau8263d2b2012-08-28 00:06:31 +0200669 pool_flush2(pool2_channel);
Willy Tarreau34eb6712011-10-24 18:15:04 +0200670 pool_flush2(pool2_hdr_idx);
Willy Tarreau48d63db2008-08-03 17:41:33 +0200671 pool_flush2(pool2_requri);
672 pool_flush2(pool2_capture);
673 pool_flush2(pool2_session);
674 pool_flush2(fe->req_cap_pool);
675 pool_flush2(fe->rsp_cap_pool);
Willy Tarreau632f5a72007-07-11 10:42:35 +0200676 }
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200677}
678
679
680/* perform minimal intializations, report 0 in case of error, 1 if OK. */
681int init_session()
682{
Willy Tarreauf54f8bd2008-11-23 19:53:55 +0100683 LIST_INIT(&sessions);
Willy Tarreauc6ca1a02007-05-13 19:43:47 +0200684 pool2_session = create_pool("session", sizeof(struct session), MEM_F_SHARED);
685 return pool2_session != NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200686}
687
Willy Tarreau30e71012007-11-26 20:15:35 +0100688void session_process_counters(struct session *s)
689{
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100690 unsigned long long bytes;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100691 void *ptr;
692 int i;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100693
Willy Tarreau30e71012007-11-26 20:15:35 +0100694 if (s->req) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100695 bytes = s->req->total - s->logs.bytes_in;
Willy Tarreau30e71012007-11-26 20:15:35 +0100696 s->logs.bytes_in = s->req->total;
697 if (bytes) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100698 s->fe->fe_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100699
Willy Tarreau20d46a52012-12-09 15:55:40 +0100700 s->be->be_counters.bytes_in += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100701
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100702 if (objt_server(s->target))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100703 objt_server(s->target)->counters.bytes_in += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200704
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +0100705 if (s->listener && s->listener->counters)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100706 s->listener->counters->bytes_in += bytes;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200707
Willy Tarreaub4c84932013-07-23 19:15:30 +0200708 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100709 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100710 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200711
Willy Tarreau20d46a52012-12-09 15:55:40 +0100712 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100713 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100714 STKTABLE_DT_BYTES_IN_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200715 if (ptr)
716 stktable_data_cast(ptr, bytes_in_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200717
Willy Tarreau20d46a52012-12-09 15:55:40 +0100718 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100719 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100720 STKTABLE_DT_BYTES_IN_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200721 if (ptr)
722 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100723 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200724 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100725 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100726 }
727
Willy Tarreau30e71012007-11-26 20:15:35 +0100728 if (s->rep) {
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100729 bytes = s->rep->total - s->logs.bytes_out;
Willy Tarreau30e71012007-11-26 20:15:35 +0100730 s->logs.bytes_out = s->rep->total;
731 if (bytes) {
Willy Tarreau20d46a52012-12-09 15:55:40 +0100732 s->fe->fe_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100733
Willy Tarreau20d46a52012-12-09 15:55:40 +0100734 s->be->be_counters.bytes_out += bytes;
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100735
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100736 if (objt_server(s->target))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100737 objt_server(s->target)->counters.bytes_out += bytes;
Krzysztof Piotr Oledzkiaeebf9b2009-10-04 15:43:17 +0200738
Thierry FOURNIERa47a94f2014-03-20 15:42:53 +0100739 if (s->listener && s->listener->counters)
Willy Tarreau20d46a52012-12-09 15:55:40 +0100740 s->listener->counters->bytes_out += bytes;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200741
Willy Tarreaub4c84932013-07-23 19:15:30 +0200742 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100743 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100744 continue;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200745
Willy Tarreau20d46a52012-12-09 15:55:40 +0100746 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100747 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100748 STKTABLE_DT_BYTES_OUT_CNT);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200749 if (ptr)
750 stktable_data_cast(ptr, bytes_out_cnt) += bytes;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200751
Willy Tarreau20d46a52012-12-09 15:55:40 +0100752 ptr = stktable_data_ptr(s->stkctr[i].table,
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100753 stkctr_entry(&s->stkctr[i]),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100754 STKTABLE_DT_BYTES_OUT_RATE);
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200755 if (ptr)
756 update_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100757 s->stkctr[i].table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u, bytes);
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200758 }
Willy Tarreau30e71012007-11-26 20:15:35 +0100759 }
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100760 }
761}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200762
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100763/* This function is called with (si->state == SI_ST_CON) meaning that a
764 * connection was attempted and that the file descriptor is already allocated.
765 * We must check for establishment, error and abort. Possible output states
766 * are SI_ST_EST (established), SI_ST_CER (error), SI_ST_DIS (abort), and
767 * SI_ST_CON (no change). The function returns 0 if it switches to SI_ST_CER,
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200768 * otherwise 1. This only works with connection-based sessions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100769 */
Simon Hormandec5be42011-06-08 09:19:07 +0900770static int sess_update_st_con_tcp(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100771{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200772 struct channel *req = si->ob;
773 struct channel *rep = si->ib;
Willy Tarreau7f3f29e2016-11-17 12:05:13 +0100774 struct connection *srv_conn = __objt_conn(si->end);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100775
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100776 /* If we got an error, or if nothing happened and the connection timed
777 * out, we must give up. The CER state handler will take care of retry
778 * attempts and error reports.
779 */
780 if (unlikely(si->flags & (SI_FL_EXP|SI_FL_ERR))) {
Willy Tarreaue3224e82012-10-29 22:41:31 +0100781 if (unlikely(si->ob->flags & CF_WRITE_PARTIAL)) {
782 /* Some data were sent past the connection establishment,
783 * so we need to pretend we're established to log correctly
784 * and let later states handle the failure.
785 */
Willy Tarreaue3224e82012-10-29 22:41:31 +0100786 si->state = SI_ST_EST;
787 si->err_type = SI_ET_DATA_ERR;
788 si->ib->flags |= CF_READ_ERROR | CF_WRITE_ERROR;
Willy Tarreaue3224e82012-10-29 22:41:31 +0100789 return 1;
790 }
Willy Tarreau127334e2009-03-28 10:47:26 +0100791 si->exp = TICK_ETERNITY;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100792 si->state = SI_ST_CER;
Willy Tarreau0ede5a32012-12-08 08:44:02 +0100793
Willy Tarreau7f3f29e2016-11-17 12:05:13 +0100794 conn_force_close(srv_conn);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100795
796 if (si->err_type)
797 return 0;
798
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100799 if (si->flags & SI_FL_ERR)
800 si->err_type = SI_ET_CONN_ERR;
801 else
802 si->err_type = SI_ET_CONN_TO;
803 return 0;
804 }
805
806 /* OK, maybe we want to abort */
Willy Tarreaua7a7ebc2012-12-30 00:50:35 +0100807 if (!(req->flags & CF_WRITE_PARTIAL) &&
808 unlikely((rep->flags & CF_SHUTW) ||
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200809 ((req->flags & CF_SHUTW_NOW) && /* FIXME: this should not prevent a connection from establishing */
810 ((!(req->flags & CF_WRITE_ACTIVITY) && channel_is_empty(req)) ||
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100811 s->be->options & PR_O_ABRT_CLOSE)))) {
812 /* give up */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200813 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100814 si->err_type |= SI_ET_CONN_ABRT;
Willy Tarreau84455332009-03-15 22:34:05 +0100815 if (s->srv_error)
816 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100817 return 1;
818 }
819
820 /* we need to wait a bit more if there was no activity either */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200821 if (!(req->flags & CF_WRITE_ACTIVITY))
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100822 return 1;
823
824 /* OK, this means that a connection succeeded. The caller will be
825 * responsible for handling the transition from CON to EST.
826 */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100827 si->state = SI_ST_EST;
828 si->err_type = SI_ET_NONE;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100829 return 1;
830}
831
832/* This function is called with (si->state == SI_ST_CER) meaning that a
833 * previous connection attempt has failed and that the file descriptor
834 * has already been released. Possible causes include asynchronous error
835 * notification and time out. Possible output states are SI_ST_CLO when
836 * retries are exhausted, SI_ST_TAR when a delay is wanted before a new
837 * connection attempt, SI_ST_ASS when it's wise to retry on the same server,
838 * and SI_ST_REQ when an immediate redispatch is wanted. The buffers are
839 * marked as in error state. It returns 0.
840 */
Simon Hormandec5be42011-06-08 09:19:07 +0900841static int sess_update_st_cer(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100842{
843 /* we probably have to release last session from the server */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100844 if (objt_server(s->target)) {
845 health_adjust(objt_server(s->target), HANA_STATUS_L4_ERR);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100846
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100847 if (s->flags & SN_CURR_SESS) {
848 s->flags &= ~SN_CURR_SESS;
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100849 objt_server(s->target)->cur_sess--;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100850 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100851 }
852
853 /* ensure that we have enough retries left */
Willy Tarreauee28de02010-06-01 09:51:00 +0200854 si->conn_retries--;
855 if (si->conn_retries < 0) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100856 if (!si->err_type) {
857 si->err_type = SI_ET_CONN_ERR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100858 }
859
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100860 if (objt_server(s->target))
861 objt_server(s->target)->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100862 s->be->be_counters.failed_conns++;
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100863 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100864 if (may_dequeue_tasks(objt_server(s->target), s->be))
865 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100866
867 /* shutw is enough so stop a connecting socket */
Willy Tarreau73b013b2012-05-21 16:31:45 +0200868 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200869 si->ob->flags |= CF_WRITE_ERROR;
870 si->ib->flags |= CF_READ_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100871
872 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +0100873 if (s->srv_error)
874 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100875 return 0;
876 }
877
878 /* If the "redispatch" option is set on the backend, we are allowed to
879 * retry on another server for the last retry. In order to achieve this,
880 * we must mark the session unassigned, and eventually clear the DIRECT
881 * bit to ignore any persistence cookie. We won't count a retry nor a
882 * redispatch yet, because this will depend on what server is selected.
Willy Tarreau33a14e52014-06-13 17:49:40 +0200883 * If the connection is not persistent, the balancing algorithm is not
884 * determinist (round robin) and there is more than one active server,
885 * we accept to perform an immediate redispatch without waiting since
886 * we don't care about this particular server.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100887 */
Willy Tarreau33a14e52014-06-13 17:49:40 +0200888 if (objt_server(s->target) &&
889 (si->conn_retries == 0 ||
Willy Tarreau8db2a602016-01-13 07:58:44 +0100890 (__objt_server(s->target)->state < SRV_ST_RUNNING) ||
Willy Tarreau33a14e52014-06-13 17:49:40 +0200891 (!(s->flags & SN_DIRECT) && s->be->srv_act > 1 &&
892 ((s->be->lbprm.algo & BE_LB_KIND) == BE_LB_KIND_RR))) &&
Willy Tarreau4de91492010-01-22 19:10:05 +0100893 s->be->options & PR_O_REDISP && !(s->flags & SN_FORCE_PRST)) {
Willy Tarreaub89cfca2010-12-29 14:32:28 +0100894 sess_change_server(s, NULL);
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100895 if (may_dequeue_tasks(objt_server(s->target), s->be))
896 process_srv_queue(objt_server(s->target));
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100897
898 s->flags &= ~(SN_DIRECT | SN_ASSIGNED | SN_ADDR_SET);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100899 si->state = SI_ST_REQ;
900 } else {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100901 if (objt_server(s->target))
902 objt_server(s->target)->counters.retries++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +0100903 s->be->be_counters.retries++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100904 si->state = SI_ST_ASS;
905 }
906
907 if (si->flags & SI_FL_ERR) {
908 /* The error was an asynchronous connection error, and we will
909 * likely have to retry connecting to the same server, most
910 * likely leading to the same result. To avoid this, we wait
Willy Tarreaub0290662014-06-13 17:04:44 +0200911 * MIN(one second, connect timeout) before retrying.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100912 */
913
Willy Tarreaub0290662014-06-13 17:04:44 +0200914 int delay = 1000;
915
916 if (s->be->timeout.connect && s->be->timeout.connect < delay)
917 delay = s->be->timeout.connect;
918
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100919 if (!si->err_type)
920 si->err_type = SI_ET_CONN_ERR;
921
Willy Tarreaudb6d0122014-06-13 17:40:15 +0200922 /* only wait when we're retrying on the same server */
923 if (si->state == SI_ST_ASS ||
924 (s->be->lbprm.algo & BE_LB_KIND) != BE_LB_KIND_RR ||
925 (s->be->srv_act <= 1)) {
926 si->state = SI_ST_TAR;
927 si->exp = tick_add(now_ms, MS_TO_TICKS(delay));
928 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100929 return 0;
930 }
931 return 0;
932}
933
934/*
935 * This function handles the transition between the SI_ST_CON state and the
Willy Tarreau85e7d002010-05-31 11:57:51 +0200936 * SI_ST_EST state. It must only be called after switching from SI_ST_CON (or
Willy Tarreau26d8c592012-05-07 18:12:14 +0200937 * SI_ST_INI) to SI_ST_EST, but only when a ->proto is defined.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100938 */
Simon Hormandec5be42011-06-08 09:19:07 +0900939static void sess_establish(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100940{
Willy Tarreau7421efb2012-07-02 15:11:27 +0200941 struct channel *req = si->ob;
942 struct channel *rep = si->ib;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100943
Willy Tarreau0e37f1c2013-12-31 23:06:46 +0100944 /* First, centralize the timers information */
945 s->logs.t_connect = tv_ms_elapsed(&s->logs.tv_accept, &now);
946 si->exp = TICK_ETERNITY;
947
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100948 if (objt_server(s->target))
949 health_adjust(objt_server(s->target), HANA_STATUS_L4_OK);
Krzysztof Piotr Oledzki97f07b82009-12-15 22:31:24 +0100950
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100951 if (s->be->mode == PR_MODE_TCP) { /* let's allow immediate data connection in this case */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100952 /* if the user wants to log as soon as possible, without counting
953 * bytes from the server, then this is the right moment. */
Willy Tarreaud79a3b22012-12-28 09:40:16 +0100954 if (!LIST_ISEMPTY(&s->fe->logformat) && !(s->logs.logwait & LW_BYTES)) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100955 s->logs.t_close = s->logs.t_connect; /* to get a valid end date */
Willy Tarreaua5555ec2008-11-30 19:02:32 +0100956 s->do_log(s);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100957 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100958 }
959 else {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100960 s->txn.rsp.msg_state = HTTP_MSG_RPBEFORE;
Willy Tarreaud81ca042013-12-31 22:33:13 +0100961 rep->flags |= CF_READ_DONTWAIT; /* a single read is enough to get response headers */
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100962 }
963
Willy Tarreau4e5b8282009-08-16 22:57:50 +0200964 rep->analysers |= s->fe->fe_rsp_ana | s->be->be_rsp_ana;
Willy Tarreau03cdb7c2012-08-27 23:14:58 +0200965 rep->flags |= CF_READ_ATTACHED; /* producer is now attached */
Willy Tarreau644c1012014-04-30 18:11:11 +0200966 if (req->flags & CF_WAKE_CONNECT) {
967 req->flags |= CF_WAKE_ONCE;
968 req->flags &= ~CF_WAKE_CONNECT;
969 }
Willy Tarreaub363a1f2013-10-01 10:45:07 +0200970 if (objt_conn(si->end)) {
Willy Tarreaud04e8582010-05-31 12:31:35 +0200971 /* real connections have timeouts */
972 req->wto = s->be->timeout.server;
973 rep->rto = s->be->timeout.server;
974 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100975 req->wex = TICK_ETERNITY;
976}
977
978/* Update stream interface status for input states SI_ST_ASS, SI_ST_QUE, SI_ST_TAR.
979 * Other input states are simply ignored.
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +0100980 * Possible output states are SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ, SI_ST_CON
981 * and SI_ST_EST. Flags must have previously been updated for timeouts and other
982 * conditions.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100983 */
Simon Hormandec5be42011-06-08 09:19:07 +0900984static void sess_update_stream_int(struct session *s, struct stream_interface *si)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100985{
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100986 struct server *srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +0100987
Willy Tarreau02d6cfc2012-03-01 18:19:58 +0100988 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100989 now_ms, __FUNCTION__,
990 s,
991 s->req, s->rep,
992 s->req->rex, s->rep->wex,
993 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +0200994 s->req->buf->i, s->req->buf->o, s->rep->buf->i, s->rep->buf->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +0100995
996 if (si->state == SI_ST_ASS) {
997 /* Server assigned to connection request, we have to try to connect now */
998 int conn_err;
999
1000 conn_err = connect_server(s);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001001 srv = objt_server(s->target);
Willy Tarreau827aee92011-03-10 16:55:02 +01001002
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001003 if (conn_err == SN_ERR_NONE) {
Willy Tarreau9e5a3aa2013-12-31 23:32:12 +01001004 /* state = SI_ST_CON or SI_ST_EST now */
Willy Tarreau827aee92011-03-10 16:55:02 +01001005 if (srv)
1006 srv_inc_sess_ctr(srv);
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001007 if (srv)
1008 srv_set_sess_last(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001009 return;
1010 }
1011
1012 /* We have received a synchronous error. We might have to
1013 * abort, retry immediately or redispatch.
1014 */
1015 if (conn_err == SN_ERR_INTERNAL) {
1016 if (!si->err_type) {
1017 si->err_type = SI_ET_CONN_OTHER;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001018 }
1019
Willy Tarreau827aee92011-03-10 16:55:02 +01001020 if (srv)
1021 srv_inc_sess_ctr(srv);
1022 if (srv)
Bhaskar Maddalaa20cb852014-02-03 16:26:46 -05001023 srv_set_sess_last(srv);
1024 if (srv)
Willy Tarreau827aee92011-03-10 16:55:02 +01001025 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001026 s->be->be_counters.failed_conns++;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001027
1028 /* release other sessions waiting for this server */
Willy Tarreaub89cfca2010-12-29 14:32:28 +01001029 sess_change_server(s, NULL);
Willy Tarreau827aee92011-03-10 16:55:02 +01001030 if (may_dequeue_tasks(srv, s->be))
1031 process_srv_queue(srv);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001032
1033 /* Failed and not retryable. */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001034 si_shutr(si);
1035 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001036 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001037
1038 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1039
1040 /* no session was ever accounted for this server */
1041 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001042 if (s->srv_error)
1043 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001044 return;
1045 }
1046
1047 /* We are facing a retryable error, but we don't want to run a
1048 * turn-around now, as the problem is likely a source port
1049 * allocation problem, so we want to retry now.
1050 */
1051 si->state = SI_ST_CER;
1052 si->flags &= ~SI_FL_ERR;
1053 sess_update_st_cer(s, si);
1054 /* now si->state is one of SI_ST_CLO, SI_ST_TAR, SI_ST_ASS, SI_ST_REQ */
1055 return;
1056 }
1057 else if (si->state == SI_ST_QUE) {
1058 /* connection request was queued, check for any update */
1059 if (!s->pend_pos) {
1060 /* The connection is not in the queue anymore. Either
1061 * we have a server connection slot available and we
1062 * go directly to the assigned state, or we need to
1063 * load-balance first and go to the INI state.
1064 */
1065 si->exp = TICK_ETERNITY;
1066 if (unlikely(!(s->flags & SN_ASSIGNED)))
1067 si->state = SI_ST_REQ;
1068 else {
1069 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1070 si->state = SI_ST_ASS;
1071 }
1072 return;
1073 }
1074
1075 /* Connection request still in queue... */
1076 if (si->flags & SI_FL_EXP) {
1077 /* ... and timeout expired */
1078 si->exp = TICK_ETERNITY;
1079 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau827aee92011-03-10 16:55:02 +01001080 if (srv)
1081 srv->counters.failed_conns++;
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001082 s->be->be_counters.failed_conns++;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001083 si_shutr(si);
1084 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001085 si->ob->flags |= CF_WRITE_TIMEOUT;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001086 if (!si->err_type)
1087 si->err_type = SI_ET_QUEUE_TO;
1088 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001089 if (s->srv_error)
1090 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001091 return;
1092 }
1093
1094 /* Connection remains in queue, check if we have to abort it */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001095 if ((si->ob->flags & (CF_READ_ERROR)) ||
1096 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02001097 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001098 /* give up */
1099 si->exp = TICK_ETERNITY;
1100 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreau73b013b2012-05-21 16:31:45 +02001101 si_shutr(si);
1102 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001103 si->err_type |= SI_ET_QUEUE_ABRT;
1104 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001105 if (s->srv_error)
1106 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001107 return;
1108 }
1109
1110 /* Nothing changed */
1111 return;
1112 }
1113 else if (si->state == SI_ST_TAR) {
1114 /* Connection request might be aborted */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001115 if ((si->ob->flags & (CF_READ_ERROR)) ||
1116 ((si->ob->flags & CF_SHUTW_NOW) && /* empty and client aborted */
Willy Tarreau8e21bb92012-08-24 22:40:29 +02001117 (channel_is_empty(si->ob) || s->be->options & PR_O_ABRT_CLOSE))) {
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001118 /* give up */
1119 si->exp = TICK_ETERNITY;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001120 si_shutr(si);
1121 si_shutw(si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001122 si->err_type |= SI_ET_CONN_ABRT;
1123 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001124 if (s->srv_error)
1125 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001126 return;
1127 }
1128
1129 if (!(si->flags & SI_FL_EXP))
1130 return; /* still in turn-around */
1131
1132 si->exp = TICK_ETERNITY;
1133
1134 /* we keep trying on the same server as long as the session is
1135 * marked "assigned".
1136 * FIXME: Should we force a redispatch attempt when the server is down ?
1137 */
1138 if (s->flags & SN_ASSIGNED)
1139 si->state = SI_ST_ASS;
1140 else
1141 si->state = SI_ST_REQ;
1142 return;
1143 }
1144}
1145
Simon Hormandec5be42011-06-08 09:19:07 +09001146/* Set correct session termination flags in case no analyser has done it. It
1147 * also counts a failed request if the server state has not reached the request
1148 * stage.
1149 */
1150static void sess_set_term_flags(struct session *s)
1151{
1152 if (!(s->flags & SN_FINST_MASK)) {
1153 if (s->si[1].state < SI_ST_REQ) {
1154
1155 s->fe->fe_counters.failed_req++;
1156 if (s->listener->counters)
1157 s->listener->counters->failed_req++;
1158
1159 s->flags |= SN_FINST_R;
1160 }
1161 else if (s->si[1].state == SI_ST_QUE)
1162 s->flags |= SN_FINST_Q;
1163 else if (s->si[1].state < SI_ST_EST)
1164 s->flags |= SN_FINST_C;
1165 else if (s->si[1].state == SI_ST_EST || s->si[1].prev_state == SI_ST_EST)
1166 s->flags |= SN_FINST_D;
1167 else
1168 s->flags |= SN_FINST_L;
1169 }
1170}
1171
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001172/* This function initiates a server connection request on a stream interface
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001173 * already in SI_ST_REQ state. Upon success, the state goes to SI_ST_ASS for
1174 * a real connection to a server, indicating that a server has been assigned,
1175 * or SI_ST_EST for a successful connection to an applet. It may also return
1176 * SI_ST_QUE, or SI_ST_CLO upon error.
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001177 */
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001178static void sess_prepare_conn_req(struct session *s, struct stream_interface *si)
1179{
1180 DPRINTF(stderr,"[%u] %s: sess=%p rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001181 now_ms, __FUNCTION__,
1182 s,
1183 s->req, s->rep,
1184 s->req->rex, s->rep->wex,
1185 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001186 s->req->buf->i, s->req->buf->o, s->rep->buf->i, s->rep->buf->o, s->rep->cons->state, s->req->cons->state);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001187
1188 if (si->state != SI_ST_REQ)
1189 return;
1190
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001191 if (unlikely(obj_type(s->target) == OBJ_TYPE_APPLET)) {
1192 /* the applet directly goes to the EST state */
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001193 struct appctx *appctx = objt_appctx(si->end);
1194
1195 if (!appctx || appctx->applet != __objt_applet(s->target))
1196 appctx = stream_int_register_handler(si, objt_applet(s->target));
1197
1198 if (!appctx) {
1199 /* No more memory, let's immediately abort. Force the
1200 * error code to ignore the ERR_LOCAL which is not a
1201 * real error.
1202 */
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001203 s->flags &= ~(SN_ERR_MASK | SN_FINST_MASK);
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001204
1205 si_shutr(si);
1206 si_shutw(si);
1207 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau6bbb2f62013-12-09 17:14:23 +01001208 si->err_type = SI_ET_CONN_RES;
Willy Tarreau4384ddf2013-12-01 12:25:52 +01001209 si->state = SI_ST_CLO;
1210 if (s->srv_error)
1211 s->srv_error(s, si);
1212 return;
1213 }
1214
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001215 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001216 si->state = SI_ST_EST;
1217 si->err_type = SI_ET_NONE;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001218 be_set_sess_last(s->be);
Willy Tarreaufac4bd12013-11-30 09:21:49 +01001219 /* let sess_establish() finish the job */
Willy Tarreaud84fb5e2013-11-30 09:06:53 +01001220 return;
1221 }
1222
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001223 /* Try to assign a server */
1224 if (srv_redispatch_connect(s) != 0) {
1225 /* We did not get a server. Either we queued the
1226 * connection request, or we encountered an error.
1227 */
1228 if (si->state == SI_ST_QUE)
1229 return;
1230
1231 /* we did not get any server, let's check the cause */
Willy Tarreau73b013b2012-05-21 16:31:45 +02001232 si_shutr(si);
1233 si_shutw(si);
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001234 si->ob->flags |= CF_WRITE_ERROR;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001235 if (!si->err_type)
1236 si->err_type = SI_ET_CONN_OTHER;
1237 si->state = SI_ST_CLO;
Willy Tarreau0cac36f2008-11-30 20:44:17 +01001238 if (s->srv_error)
1239 s->srv_error(s, si);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001240 return;
1241 }
1242
1243 /* The server is assigned */
1244 s->logs.t_queue = tv_ms_elapsed(&s->logs.tv_accept, &now);
1245 si->state = SI_ST_ASS;
Willy Tarreaub9a551e2014-04-23 00:35:17 +02001246 be_set_sess_last(s->be);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001247}
1248
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001249/* This stream analyser checks the switching rules and changes the backend
Willy Tarreau4de91492010-01-22 19:10:05 +01001250 * if appropriate. The default_backend rule is also considered, then the
1251 * target backend's forced persistence rules are also evaluated last if any.
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001252 * It returns 1 if the processing can continue on next analysers, or zero if it
1253 * either needs more data or wants to immediately abort the request.
1254 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001255static int process_switching_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001256{
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001257 struct persist_rule *prst_rule;
Willy Tarreau4de91492010-01-22 19:10:05 +01001258
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001259 req->analysers &= ~an_bit;
1260 req->analyse_exp = TICK_ETERNITY;
1261
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001262 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001263 now_ms, __FUNCTION__,
1264 s,
1265 req,
1266 req->rex, req->wex,
1267 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001268 req->buf->i,
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001269 req->analysers);
1270
1271 /* now check whether we have some switching rules for this request */
1272 if (!(s->flags & SN_BE_ASSIGNED)) {
1273 struct switching_rule *rule;
1274
1275 list_for_each_entry(rule, &s->fe->switching_rules, list) {
Willy Tarreauf51658d2014-04-23 01:21:56 +02001276 int ret = 1;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001277
Willy Tarreauf51658d2014-04-23 01:21:56 +02001278 if (rule->cond) {
1279 ret = acl_exec_cond(rule->cond, s->fe, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
1280 ret = acl_pass(ret);
1281 if (rule->cond->pol == ACL_COND_UNLESS)
1282 ret = !ret;
1283 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001284
1285 if (ret) {
Bertrand Jacquin702d44f2013-11-19 11:43:06 +01001286 /* If the backend name is dynamic, try to resolve the name.
1287 * If we can't resolve the name, or if any error occurs, break
1288 * the loop and fallback to the default backend.
1289 */
1290 struct proxy *backend;
1291
1292 if (rule->dynamic) {
1293 struct chunk *tmp = get_trash_chunk();
1294 if (!build_logline(s, tmp->str, tmp->size, &rule->be.expr))
1295 break;
1296 backend = findproxy(tmp->str, PR_CAP_BE);
1297 if (!backend)
1298 break;
1299 }
1300 else
1301 backend = rule->be.backend;
1302
1303 if (!session_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 */
1314 if (!(s->flags & SN_BE_ASSIGNED))
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001315 if (!session_set_backend(s, s->fe->defbe.be ? s->fe->defbe.be : s->be))
1316 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 */
1320 if (s->fe == s->be) {
1321 s->req->analysers &= ~AN_REQ_INSPECT_BE;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001322 s->req->analysers &= ~AN_REQ_HTTP_PROCESS_BE;
Willy Tarreaufb356202010-08-03 14:02:05 +02001323 }
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001324
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001325 /* as soon as we know the backend, we must check if we have a matching forced or ignored
Willy Tarreau4de91492010-01-22 19:10:05 +01001326 * persistence rule, and report that in the session.
1327 */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001328 list_for_each_entry(prst_rule, &s->be->persist_rules, list) {
Willy Tarreau4de91492010-01-22 19:10:05 +01001329 int ret = 1;
1330
1331 if (prst_rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001332 ret = acl_exec_cond(prst_rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4de91492010-01-22 19:10:05 +01001333 ret = acl_pass(ret);
1334 if (prst_rule->cond->pol == ACL_COND_UNLESS)
1335 ret = !ret;
1336 }
1337
1338 if (ret) {
1339 /* no rule, or the rule matches */
Cyril Bonté47fdd8e2010-04-25 00:00:51 +02001340 if (prst_rule->type == PERSIST_TYPE_FORCE) {
1341 s->flags |= SN_FORCE_PRST;
1342 } else {
1343 s->flags |= SN_IGNORE_PRST;
1344 }
Willy Tarreau4de91492010-01-22 19:10:05 +01001345 break;
1346 }
1347 }
1348
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001349 return 1;
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001350
1351 sw_failed:
1352 /* immediately abort this request in case of allocation failure */
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001353 channel_abort(s->req);
1354 channel_abort(s->rep);
Willy Tarreaubedb9ba2009-07-12 08:27:39 +02001355
1356 if (!(s->flags & SN_ERR_MASK))
1357 s->flags |= SN_ERR_RESOURCE;
1358 if (!(s->flags & SN_FINST_MASK))
1359 s->flags |= SN_FINST_R;
1360
1361 s->txn.status = 500;
1362 s->req->analysers = 0;
1363 s->req->analyse_exp = TICK_ETERNITY;
1364 return 0;
Willy Tarreau1d0dfb12009-07-07 15:10:31 +02001365}
1366
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001367/* This stream analyser works on a request. It applies all use-server rules on
1368 * it then returns 1. The data must already be present in the buffer otherwise
1369 * they won't match. It always returns 1.
1370 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001371static int process_server_rules(struct session *s, struct channel *req, int an_bit)
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001372{
1373 struct proxy *px = s->be;
1374 struct server_rule *rule;
1375
1376 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bl=%d analysers=%02x\n",
1377 now_ms, __FUNCTION__,
1378 s,
1379 req,
1380 req->rex, req->wex,
1381 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001382 req->buf->i + req->buf->o,
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001383 req->analysers);
1384
1385 if (!(s->flags & SN_ASSIGNED)) {
1386 list_for_each_entry(rule, &px->server_rules, list) {
1387 int ret;
1388
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001389 ret = acl_exec_cond(rule->cond, s->be, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001390 ret = acl_pass(ret);
1391 if (rule->cond->pol == ACL_COND_UNLESS)
1392 ret = !ret;
1393
1394 if (ret) {
1395 struct server *srv = rule->srv.ptr;
1396
Willy Tarreau892337c2014-05-13 23:41:20 +02001397 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001398 (px->options & PR_O_PERSIST) ||
1399 (s->flags & SN_FORCE_PRST)) {
1400 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001401 s->target = &srv->obj_type;
Willy Tarreau4a5cade2012-04-05 21:09:48 +02001402 break;
1403 }
1404 /* if the server is not UP, let's go on with next rules
1405 * just in case another one is suited.
1406 */
1407 }
1408 }
1409 }
1410
1411 req->analysers &= ~an_bit;
1412 req->analyse_exp = TICK_ETERNITY;
1413 return 1;
1414}
1415
Emeric Brun1d33b292010-01-04 15:47:17 +01001416/* This stream analyser works on a request. It applies all sticking rules on
1417 * it then returns 1. The data must already be present in the buffer otherwise
1418 * they won't match. It always returns 1.
1419 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001420static int process_sticking_rules(struct session *s, struct channel *req, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001421{
1422 struct proxy *px = s->be;
1423 struct sticking_rule *rule;
1424
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001425 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001426 now_ms, __FUNCTION__,
1427 s,
1428 req,
1429 req->rex, req->wex,
1430 req->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001431 req->buf->i,
Emeric Brun1d33b292010-01-04 15:47:17 +01001432 req->analysers);
1433
1434 list_for_each_entry(rule, &px->sticking_rules, list) {
1435 int ret = 1 ;
1436 int i;
1437
Willy Tarreau9667a802013-12-09 12:52:13 +01001438 /* Only the first stick store-request of each table is applied
1439 * and other ones are ignored. The purpose is to allow complex
1440 * configurations which look for multiple entries by decreasing
1441 * order of precision and to stop at the first which matches.
1442 * An example could be a store of the IP address from an HTTP
1443 * header first, then from the source if not found.
1444 */
Emeric Brun1d33b292010-01-04 15:47:17 +01001445 for (i = 0; i < s->store_count; i++) {
1446 if (rule->table.t == s->store[i].table)
1447 break;
1448 }
1449
1450 if (i != s->store_count)
1451 continue;
1452
1453 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001454 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001455 ret = acl_pass(ret);
1456 if (rule->cond->pol == ACL_COND_UNLESS)
1457 ret = !ret;
1458 }
1459
1460 if (ret) {
1461 struct stktable_key *key;
1462
Willy Tarreaud0083942014-06-25 16:20:53 +02001463 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_REQ|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001464 if (!key)
1465 continue;
1466
1467 if (rule->flags & STK_IS_MATCH) {
1468 struct stksess *ts;
1469
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001470 if ((ts = stktable_lookup_key(rule->table.t, key)) != NULL) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001471 if (!(s->flags & SN_ASSIGNED)) {
1472 struct eb32_node *node;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001473 void *ptr;
Emeric Brun1d33b292010-01-04 15:47:17 +01001474
1475 /* srv found in table */
Willy Tarreau13c29de2010-06-06 16:40:39 +02001476 ptr = stktable_data_ptr(rule->table.t, ts, STKTABLE_DT_SERVER_ID);
1477 node = eb32_lookup(&px->conf.used_server_id, stktable_data_cast(ptr, server_id));
Emeric Brun1d33b292010-01-04 15:47:17 +01001478 if (node) {
1479 struct server *srv;
1480
1481 srv = container_of(node, struct server, conf.id);
Willy Tarreau892337c2014-05-13 23:41:20 +02001482 if ((srv->state != SRV_ST_STOPPED) ||
Willy Tarreau4de91492010-01-22 19:10:05 +01001483 (px->options & PR_O_PERSIST) ||
1484 (s->flags & SN_FORCE_PRST)) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001485 s->flags |= SN_DIRECT | SN_ASSIGNED;
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001486 s->target = &srv->obj_type;
Emeric Brun1d33b292010-01-04 15:47:17 +01001487 }
1488 }
1489 }
Emeric Brun85e77c72010-09-23 18:16:52 +02001490 stktable_touch(rule->table.t, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001491 }
1492 }
1493 if (rule->flags & STK_IS_STORE) {
1494 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
1495 struct stksess *ts;
1496
1497 ts = stksess_new(rule->table.t, key);
1498 if (ts) {
1499 s->store[s->store_count].table = rule->table.t;
1500 s->store[s->store_count++].ts = ts;
1501 }
1502 }
1503 }
1504 }
1505 }
1506
1507 req->analysers &= ~an_bit;
1508 req->analyse_exp = TICK_ETERNITY;
1509 return 1;
1510}
1511
1512/* This stream analyser works on a response. It applies all store rules on it
1513 * then returns 1. The data must already be present in the buffer otherwise
1514 * they won't match. It always returns 1.
1515 */
Willy Tarreau7421efb2012-07-02 15:11:27 +02001516static int process_store_rules(struct session *s, struct channel *rep, int an_bit)
Emeric Brun1d33b292010-01-04 15:47:17 +01001517{
1518 struct proxy *px = s->be;
1519 struct sticking_rule *rule;
1520 int i;
Willy Tarreau9667a802013-12-09 12:52:13 +01001521 int nbreq = s->store_count;
Emeric Brun1d33b292010-01-04 15:47:17 +01001522
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001523 DPRINTF(stderr,"[%u] %s: session=%p b=%p, exp(r,w)=%u,%u bf=%08x bh=%d analysers=%02x\n",
Emeric Brun1d33b292010-01-04 15:47:17 +01001524 now_ms, __FUNCTION__,
1525 s,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001526 rep,
1527 rep->rex, rep->wex,
1528 rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001529 rep->buf->i,
Willy Tarreau2e2b3eb2010-02-09 20:55:44 +01001530 rep->analysers);
Emeric Brun1d33b292010-01-04 15:47:17 +01001531
1532 list_for_each_entry(rule, &px->storersp_rules, list) {
1533 int ret = 1 ;
Emeric Brun1d33b292010-01-04 15:47:17 +01001534
Willy Tarreau9667a802013-12-09 12:52:13 +01001535 /* Only the first stick store-response of each table is applied
1536 * and other ones are ignored. The purpose is to allow complex
1537 * configurations which look for multiple entries by decreasing
1538 * order of precision and to stop at the first which matches.
1539 * An example could be a store of a set-cookie value, with a
1540 * fallback to a parameter found in a 302 redirect.
1541 *
1542 * The store-response rules are not allowed to override the
1543 * store-request rules for the same table, but they may coexist.
1544 * Thus we can have up to one store-request entry and one store-
1545 * response entry for the same table at any time.
1546 */
1547 for (i = nbreq; i < s->store_count; i++) {
1548 if (rule->table.t == s->store[i].table)
1549 break;
1550 }
1551
1552 /* skip existing entries for this table */
1553 if (i < s->store_count)
1554 continue;
1555
Emeric Brun1d33b292010-01-04 15:47:17 +01001556 if (rule->cond) {
Willy Tarreau32a6f2e2012-04-25 10:13:36 +02001557 ret = acl_exec_cond(rule->cond, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001558 ret = acl_pass(ret);
1559 if (rule->cond->pol == ACL_COND_UNLESS)
1560 ret = !ret;
1561 }
1562
1563 if (ret) {
1564 struct stktable_key *key;
1565
Willy Tarreaud0083942014-06-25 16:20:53 +02001566 key = stktable_fetch_key(rule->table.t, px, s, &s->txn, SMP_OPT_DIR_RES|SMP_OPT_FINAL, rule->expr, NULL);
Emeric Brun1d33b292010-01-04 15:47:17 +01001567 if (!key)
1568 continue;
1569
Willy Tarreau37e340c2013-12-06 23:05:21 +01001570 if (s->store_count < (sizeof(s->store) / sizeof(s->store[0]))) {
Emeric Brun1d33b292010-01-04 15:47:17 +01001571 struct stksess *ts;
1572
1573 ts = stksess_new(rule->table.t, key);
1574 if (ts) {
1575 s->store[s->store_count].table = rule->table.t;
Emeric Brun1d33b292010-01-04 15:47:17 +01001576 s->store[s->store_count++].ts = ts;
1577 }
1578 }
1579 }
1580 }
1581
1582 /* process store request and store response */
1583 for (i = 0; i < s->store_count; i++) {
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001584 struct stksess *ts;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001585 void *ptr;
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001586
Willy Tarreauc93cd162014-05-13 15:54:22 +02001587 if (objt_server(s->target) && objt_server(s->target)->flags & SRV_F_NON_STICK) {
Simon Hormanfa461682011-06-25 09:39:49 +09001588 stksess_free(s->store[i].table, s->store[i].ts);
1589 s->store[i].ts = NULL;
1590 continue;
1591 }
1592
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001593 ts = stktable_lookup(s->store[i].table, s->store[i].ts);
1594 if (ts) {
1595 /* the entry already existed, we can free ours */
Emeric Brun85e77c72010-09-23 18:16:52 +02001596 stktable_touch(s->store[i].table, ts, 1);
Emeric Brun1d33b292010-01-04 15:47:17 +01001597 stksess_free(s->store[i].table, s->store[i].ts);
Emeric Brun1d33b292010-01-04 15:47:17 +01001598 }
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001599 else
Emeric Brun85e77c72010-09-23 18:16:52 +02001600 ts = stktable_store(s->store[i].table, s->store[i].ts, 1);
Willy Tarreauf16d2b82010-06-06 15:38:59 +02001601
1602 s->store[i].ts = NULL;
Willy Tarreau13c29de2010-06-06 16:40:39 +02001603 ptr = stktable_data_ptr(s->store[i].table, ts, STKTABLE_DT_SERVER_ID);
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001604 stktable_data_cast(ptr, server_id) = objt_server(s->target)->puid;
Emeric Brun1d33b292010-01-04 15:47:17 +01001605 }
Willy Tarreau2a164ee2010-06-18 09:57:45 +02001606 s->store_count = 0; /* everything is stored */
Emeric Brun1d33b292010-01-04 15:47:17 +01001607
1608 rep->analysers &= ~an_bit;
1609 rep->analyse_exp = TICK_ETERNITY;
1610 return 1;
1611}
1612
Willy Tarreau1e0bbaf2010-01-06 23:53:24 +01001613/* This macro is very specific to the function below. See the comments in
1614 * process_session() below to understand the logic and the tests.
1615 */
1616#define UPDATE_ANALYSERS(real, list, back, flag) { \
1617 list = (((list) & ~(flag)) | ~(back)) & (real); \
1618 back = real; \
1619 if (!(list)) \
1620 break; \
1621 if (((list) ^ ((list) & ((list) - 1))) < (flag)) \
1622 continue; \
1623}
1624
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001625/* Processes the client, server, request and response jobs of a session task,
1626 * then puts it back to the wait queue in a clean state, or cleans up its
1627 * resources if it must be deleted. Returns in <next> the date the task wants
1628 * to be woken up, or TICK_ETERNITY. In order not to call all functions for
1629 * nothing too many times, the request and response buffers flags are monitored
1630 * and each function is called only if at least another function has changed at
1631 * least one flag it is interested in.
1632 */
Willy Tarreau26c25062009-03-08 09:38:41 +01001633struct task *process_session(struct task *t)
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001634{
Willy Tarreau827aee92011-03-10 16:55:02 +01001635 struct server *srv;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001636 struct session *s = t->context;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001637 unsigned int rqf_last, rpf_last;
Willy Tarreau815a9b22010-07-27 17:15:12 +02001638 unsigned int rq_prod_last, rq_cons_last;
1639 unsigned int rp_cons_last, rp_prod_last;
Willy Tarreau576507f2010-01-07 00:09:04 +01001640 unsigned int req_ana_back;
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001641
1642 //DPRINTF(stderr, "%s:%d: cs=%d ss=%d(%d) rqf=0x%08x rpf=0x%08x\n", __FUNCTION__, __LINE__,
1643 // s->si[0].state, s->si[1].state, s->si[1].err_type, s->req->flags, s->rep->flags);
1644
Krzysztof Piotr Oledzkif9423ae2010-01-29 19:26:18 +01001645 /* this data may be no longer valid, clear it */
1646 memset(&s->txn.auth, 0, sizeof(s->txn.auth));
1647
Willy Tarreau2a4f5112014-06-23 15:22:31 +02001648 /* This flag must explicitly be set every time */
1649 s->req->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
1650 s->rep->flags &= ~(CF_READ_NOEXP|CF_WAKE_WRITE);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001651
1652 /* Keep a copy of req/rep flags so that we can detect shutdowns */
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001653 rqf_last = s->req->flags & ~CF_MASK_ANALYSER;
1654 rpf_last = s->rep->flags & ~CF_MASK_ANALYSER;
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001655
Willy Tarreau89f7ef22009-09-05 20:57:35 +02001656 /* we don't want the stream interface functions to recursively wake us up */
1657 if (s->req->prod->owner == t)
1658 s->req->prod->flags |= SI_FL_DONT_WAKE;
1659 if (s->req->cons->owner == t)
1660 s->req->cons->flags |= SI_FL_DONT_WAKE;
1661
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001662 /* 1a: Check for low level timeouts if needed. We just set a flag on
1663 * stream interfaces when their timeouts have expired.
1664 */
1665 if (unlikely(t->state & TASK_WOKEN_TIMER)) {
1666 stream_int_check_timeouts(&s->si[0]);
1667 stream_int_check_timeouts(&s->si[1]);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001668
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001669 /* check channel timeouts, and close the corresponding stream interfaces
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001670 * for future reads or writes. Note: this will also concern upper layers
1671 * but we do not touch any other flag. We must be careful and correctly
1672 * detect state changes when calling them.
1673 */
1674
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001675 channel_check_timeouts(s->req);
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001676
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001677 if (unlikely((s->req->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001678 s->req->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001679 si_shutw(s->req->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001680 }
1681
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001682 if (unlikely((s->req->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001683 if (s->req->prod->flags & SI_FL_NOHALF)
1684 s->req->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001685 si_shutr(s->req->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001686 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001687
Willy Tarreau8263d2b2012-08-28 00:06:31 +02001688 channel_check_timeouts(s->rep);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001689
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001690 if (unlikely((s->rep->flags & (CF_SHUTW|CF_WRITE_TIMEOUT)) == CF_WRITE_TIMEOUT)) {
Willy Tarreau14641402009-12-29 14:49:56 +01001691 s->rep->cons->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001692 si_shutw(s->rep->cons);
Willy Tarreau14641402009-12-29 14:49:56 +01001693 }
1694
Willy Tarreau03cdb7c2012-08-27 23:14:58 +02001695 if (unlikely((s->rep->flags & (CF_SHUTR|CF_READ_TIMEOUT)) == CF_READ_TIMEOUT)) {
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001696 if (s->rep->prod->flags & SI_FL_NOHALF)
1697 s->rep->prod->flags |= SI_FL_NOLINGER;
Willy Tarreau73b013b2012-05-21 16:31:45 +02001698 si_shutr(s->rep->prod);
Willy Tarreau7bb68ab2012-05-13 14:48:59 +02001699 }
Willy Tarreau798f4322012-11-08 14:49:17 +01001700
1701 /* Once in a while we're woken up because the task expires. But
1702 * this does not necessarily mean that a timeout has been reached.
1703 * So let's not run a whole session processing if only an expiration
1704 * timeout needs to be refreshed.
1705 */
1706 if (!((s->req->flags | s->rep->flags) &
1707 (CF_SHUTR|CF_READ_ACTIVITY|CF_READ_TIMEOUT|CF_SHUTW|
1708 CF_WRITE_ACTIVITY|CF_WRITE_TIMEOUT|CF_ANA_TIMEOUT)) &&
1709 !((s->si[0].flags | s->si[1].flags) & (SI_FL_EXP|SI_FL_ERR)) &&
Willy Tarreau09ae6c92016-05-04 10:18:37 +02001710 ((t->state & TASK_WOKEN_ANY) == TASK_WOKEN_TIMER)) {
1711 s->si[0].flags &= ~SI_FL_DONT_WAKE;
1712 s->si[1].flags &= ~SI_FL_DONT_WAKE;
Willy Tarreau798f4322012-11-08 14:49:17 +01001713 goto update_exp_and_leave;
Willy Tarreau09ae6c92016-05-04 10:18:37 +02001714 }
Willy Tarreaub67a9b82009-06-21 22:03:51 +02001715 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001716
1717 /* 1b: check for low-level errors reported at the stream interface.
1718 * First we check if it's a retryable error (in which case we don't
1719 * want to tell the buffer). Otherwise we report the error one level
1720 * upper by setting flags into the buffers. Note that the side towards
1721 * the client cannot have connect (hence retryable) errors. Also, the
1722 * connection setup code must be able to deal with any type of abort.
1723 */
Willy Tarreau3fdb3662012-11-12 00:42:33 +01001724 srv = objt_server(s->target);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001725 if (unlikely(s->si[0].flags & SI_FL_ERR)) {
1726 if (s->si[0].state == SI_ST_EST || s->si[0].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001727 si_shutr(&s->si[0]);
1728 si_shutw(&s->si[0]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001729 stream_int_report_error(&s->si[0]);
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001730 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001731 s->be->be_counters.cli_aborts++;
1732 s->fe->fe_counters.cli_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001733 if (srv)
1734 srv->counters.cli_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001735 if (!(s->flags & SN_ERR_MASK))
1736 s->flags |= SN_ERR_CLICL;
1737 if (!(s->flags & SN_FINST_MASK))
1738 s->flags |= SN_FINST_D;
1739 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001740 }
1741 }
1742
1743 if (unlikely(s->si[1].flags & SI_FL_ERR)) {
1744 if (s->si[1].state == SI_ST_EST || s->si[1].state == SI_ST_DIS) {
Willy Tarreau73b013b2012-05-21 16:31:45 +02001745 si_shutr(&s->si[1]);
1746 si_shutw(&s->si[1]);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001747 stream_int_report_error(&s->si[1]);
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001748 s->be->be_counters.failed_resp++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001749 if (srv)
1750 srv->counters.failed_resp++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001751 if (!(s->req->analysers) && !(s->rep->analysers)) {
Willy Tarreau7d0aaf32011-03-10 23:25:56 +01001752 s->be->be_counters.srv_aborts++;
1753 s->fe->fe_counters.srv_aborts++;
Willy Tarreau827aee92011-03-10 16:55:02 +01001754 if (srv)
1755 srv->counters.srv_aborts++;
Willy Tarreau05cb29b2008-12-14 11:44:04 +01001756 if (!(s->flags & SN_ERR_MASK))
1757 s->flags |= SN_ERR_SRVCL;
1758 if (!(s->flags & SN_FINST_MASK))
1759 s->flags |= SN_FINST_D;
1760 }
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001761 }
1762 /* note: maybe we should process connection errors here ? */
1763 }
1764
1765 if (s->si[1].state == SI_ST_CON) {
1766 /* we were trying to establish a connection on the server side,
1767 * maybe it succeeded, maybe it failed, maybe we timed out, ...
1768 */
1769 if (unlikely(!sess_update_st_con_tcp(s, &s->si[1])))
1770 sess_update_st_cer(s, &s->si[1]);
1771 else if (s->si[1].state == SI_ST_EST)
1772 sess_establish(s, &s->si[1]);
1773
1774 /* state is now one of SI_ST_CON (still in progress), SI_ST_EST
1775 * (established), SI_ST_DIS (abort), SI_ST_CLO (last error),
1776 * SI_ST_ASS/SI_ST_TAR/SI_ST_REQ for retryable errors.
1777 */
1778 }
1779
Willy Tarreau815a9b22010-07-27 17:15:12 +02001780 rq_prod_last = s->si[0].state;
1781 rq_cons_last = s->si[1].state;
1782 rp_cons_last = s->si[0].state;
1783 rp_prod_last = s->si[1].state;
1784
1785 resync_stream_interface:
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001786 /* Check for connection closure */
1787
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001788 DPRINTF(stderr,
Willy Tarreau02d6cfc2012-03-01 18:19:58 +01001789 "[%u] %s:%d: task=%p s=%p, sfl=0x%08x, rq=%p, rp=%p, exp(r,w)=%u,%u rqf=%08x rpf=%08x rqh=%d rqt=%d rph=%d rpt=%d cs=%d ss=%d, cet=0x%x set=0x%x retr=%d\n",
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001790 now_ms, __FUNCTION__, __LINE__,
1791 t,
1792 s, s->flags,
1793 s->req, s->rep,
1794 s->req->rex, s->rep->wex,
1795 s->req->flags, s->rep->flags,
Willy Tarreau9b28e032012-10-12 23:49:43 +02001796 s->req->buf->i, s->req->buf->o, s->rep->buf->i, s->rep->buf->o, s->rep->cons->state, s->req->cons->state,
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001797 s->rep->cons->err_type, s->req->cons->err_type,
Willy Tarreauee28de02010-06-01 09:51:00 +02001798 s->req->cons->conn_retries);
Willy Tarreau55a8d0e2008-11-30 18:47:21 +01001799
1800 /* nothing special to be done on client side */
1801 if