blob: 5e26edc76b08eed80e9d0dc54976811e0f7e90e3 [file] [log] [blame]
Willy TARREAU3dc06442006-06-15 21:48:13 +02001/*
Willy Tarreau81f9aa32010-06-01 17:45:26 +02002 * include/proto/session.h
3 * This file defines everything related to sessions.
4 *
5 * Copyright (C) 2000-2010 Willy Tarreau - w@1wt.eu
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation, version 2.1
10 * exclusively.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
Willy TARREAU3dc06442006-06-15 21:48:13 +020021
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#ifndef _PROTO_SESSION_H
23#define _PROTO_SESSION_H
24
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020026#include <common/memory.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027#include <types/session.h>
Willy Tarreau91c43d72010-06-20 11:19:22 +020028#include <proto/freq_ctr.h>
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020029#include <proto/stick_table.h>
Willy TARREAU3dc06442006-06-15 21:48:13 +020030
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020031extern struct pool_head *pool2_session;
Willy Tarreauf54f8bd2008-11-23 19:53:55 +010032extern struct list sessions;
Willy Tarreaubf883e02014-11-25 21:10:35 +010033extern struct list buffer_wq;
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020034
Willy Tarreaubc174aa2012-11-19 16:10:32 +010035extern struct data_cb sess_conn_cb;
36
Willy Tarreau81f9aa32010-06-01 17:45:26 +020037int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020039/* perform minimal intializations, report 0 in case of error, 1 if OK. */
40int init_session();
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreaua2a64e92011-09-07 23:01:56 +020042/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
43void session_shutdown(struct session *session, int why);
Simon Hormandec5be42011-06-08 09:19:07 +090044
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010045void session_process_counters(struct session *s);
Willy Tarreau7c669d72008-06-20 15:04:11 +020046void sess_change_server(struct session *sess, struct server *newsrv);
Willy Tarreau26c25062009-03-08 09:38:41 +010047struct task *process_session(struct task *t);
Willy Tarreau84455332009-03-15 22:34:05 +010048void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreaue12704b2014-07-15 19:06:18 +020049struct stkctr *smp_fetch_sc_stkctr(struct session *l4, const struct arg *args, const char *kw);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020050int parse_track_counters(char **args, int *arg,
51 int section_type, struct proxy *curpx,
52 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020053 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020054
Willy Tarreau4bfc5802014-06-17 12:19:18 +020055/* Update the session's backend and server time stats */
56void session_update_time_stats(struct session *s);
Willy Tarreaubf883e02014-11-25 21:10:35 +010057void session_offer_buffers(int count);
Willy Tarreau656859d2014-11-25 19:46:36 +010058int session_alloc_buffers(struct session *s);
59void session_release_buffers(struct session *s);
60int session_alloc_recv_buffer(struct session *s, struct buffer **buf);
Willy Tarreau4bfc5802014-06-17 12:19:18 +020061
Willy Tarreau787add22013-11-23 23:37:04 +010062/* returns the session from a void *owner */
63static inline struct session *session_from_task(struct task *t)
64{
65 return (struct session *)t->context;
66}
67
Willy Tarreaucc08d2c2014-01-28 23:18:23 +010068/* sets the stick counter's entry pointer */
69static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry)
70{
71 stkctr->entry = caddr_from_ptr(entry, 0);
72}
73
74/* returns the entry pointer from a stick counter */
75static inline struct stksess *stkctr_entry(struct stkctr *stkctr)
76{
77 return caddr_to_ptr(stkctr->entry);
78}
79
80/* returns the two flags from a stick counter */
81static inline unsigned int stkctr_flags(struct stkctr *stkctr)
82{
83 return caddr_to_data(stkctr->entry);
84}
85
86/* sets up to two flags at a time on a composite address */
87static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags)
88{
89 stkctr->entry = caddr_set_flags(stkctr->entry, flags);
90}
91
92/* returns the two flags from a stick counter */
93static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
94{
95 stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
96}
97
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020098/* Remove the refcount from the session to the tracked counters, and clear the
99 * pointer to ensure this is only performed once. The caller is responsible for
100 * ensuring that the pointer is valid first.
101 */
102static inline void session_store_counters(struct session *s)
103{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200104 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100105 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200106
Willy Tarreaub4c84932013-07-23 19:15:30 +0200107 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100108 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100109 continue;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100110 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200111 if (ptr)
112 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100113 stkctr_entry(&s->stkctr[i])->ref_cnt--;
114 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
115 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau38285c12010-06-18 16:35:43 +0200116 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200117}
118
Willy Tarreauf3338342014-01-28 21:40:28 +0100119/* Remove the refcount from the session counters tracked at the content level if
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200120 * any, and clear the pointer to ensure this is only performed once. The caller
121 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200122 */
Willy Tarreauf3338342014-01-28 21:40:28 +0100123static inline void session_stop_content_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200124{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200125 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100126 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +0200127
Willy Tarreaub4c84932013-07-23 19:15:30 +0200128 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100129 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100130 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200131
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100132 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_CONTENT))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100133 continue;
134
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100135 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_CONN_CUR);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200136 if (ptr)
137 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100138 stkctr_entry(&s->stkctr[i])->ref_cnt--;
139 stksess_kill_if_expired(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]));
140 stkctr_set_entry(&s->stkctr[i], NULL);
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200141 }
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200142}
Willy Tarreaue3487932010-06-18 21:03:20 +0200143
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200144/* Increase total and concurrent connection count for stick entry <ts> of table
145 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
146 * pointers, and for calling this only once per connection.
147 */
148static inline void session_start_counters(struct stktable *t, struct stksess *ts)
149{
150 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200151
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200152 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
153 if (ptr)
154 stktable_data_cast(ptr, conn_cur)++;
155
156 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
157 if (ptr)
158 stktable_data_cast(ptr, conn_cnt)++;
159
160 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
161 if (ptr)
162 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
163 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
164 if (tick_isset(t->expire))
165 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
166}
167
Willy Tarreau20d46a52012-12-09 15:55:40 +0100168/* Enable tracking of session counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200169 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
170 * are performed to ensure the state can still change.
171 */
Willy Tarreau20d46a52012-12-09 15:55:40 +0100172static inline void session_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200173{
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100174 if (stkctr_entry(ctr))
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200175 return;
176
177 ts->ref_cnt++;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100178 ctr->table = t;
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100179 stkctr_set_entry(ctr, ts);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200180 session_start_counters(t, ts);
181}
182
Willy Tarreauda7ff642010-06-23 11:44:09 +0200183/* Increase the number of cumulated HTTP requests in the tracked counters */
184static void inline session_inc_http_req_ctr(struct session *s)
185{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200186 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100187 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200188
Willy Tarreaub4c84932013-07-23 19:15:30 +0200189 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100190 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100191 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200192
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100193 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200194 if (ptr)
195 stktable_data_cast(ptr, http_req_cnt)++;
196
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100197 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200198 if (ptr)
199 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100200 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200201 }
202}
203
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100204/* Increase the number of cumulated HTTP requests in the backend's tracked counters */
205static void inline session_inc_be_http_req_ctr(struct session *s)
206{
207 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100208 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100209
Willy Tarreaub4c84932013-07-23 19:15:30 +0200210 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100211 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100212 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100213
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100214 if (!(stkctr_flags(&s->stkctr[i]) & STKCTR_TRACK_BACKEND))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100215 continue;
216
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100217 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100218 if (ptr)
219 stktable_data_cast(ptr, http_req_cnt)++;
220
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100221 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100222 if (ptr)
223 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100224 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100225 }
226}
227
Willy Tarreauda7ff642010-06-23 11:44:09 +0200228/* Increase the number of cumulated failed HTTP requests in the tracked
229 * counters. Only 4xx requests should be counted here so that we can
230 * distinguish between errors caused by client behaviour and other ones.
231 * Note that even 404 are interesting because they're generally caused by
232 * vulnerability scans.
233 */
234static void inline session_inc_http_err_ctr(struct session *s)
235{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200236 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100237 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200238
Willy Tarreaub4c84932013-07-23 19:15:30 +0200239 for (i = 0; i < MAX_SESS_STKCTR; i++) {
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100240 if (!stkctr_entry(&s->stkctr[i]))
Willy Tarreau20d46a52012-12-09 15:55:40 +0100241 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200242
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100243 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200244 if (ptr)
245 stktable_data_cast(ptr, http_err_cnt)++;
246
Willy Tarreaucc08d2c2014-01-28 23:18:23 +0100247 ptr = stktable_data_ptr(s->stkctr[i].table, stkctr_entry(&s->stkctr[i]), STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200248 if (ptr)
249 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100250 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200251 }
252}
253
Simon Hormanaf514952011-06-21 14:34:57 +0900254static void inline session_add_srv_conn(struct session *sess, struct server *srv)
255{
256 sess->srv_conn = srv;
257 LIST_ADD(&srv->actconns, &sess->by_srv);
258}
259
260static void inline session_del_srv_conn(struct session *sess)
261{
262 if (!sess->srv_conn)
263 return;
264
265 sess->srv_conn = NULL;
266 LIST_DEL(&sess->by_srv);
267}
268
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200269static void inline session_init_srv_conn(struct session *sess)
270{
271 sess->srv_conn = NULL;
272 LIST_INIT(&sess->by_srv);
273}
274
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200276
Willy Tarreaubaaee002006-06-26 02:48:02 +0200277/*
278 * Local variables:
279 * c-indent-level: 8
280 * c-basic-offset: 8
281 * End:
282 */