blob: 463b2e4b02fdbba093fec940ff0229789a38cf92 [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 Tarreauc6ca1a02007-05-13 19:43:47 +020033
Willy Tarreaubc174aa2012-11-19 16:10:32 +010034extern struct data_cb sess_conn_cb;
35
Willy Tarreau81f9aa32010-06-01 17:45:26 +020036int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +020037
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020038/* perform minimal intializations, report 0 in case of error, 1 if OK. */
39int init_session();
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreaua2a64e92011-09-07 23:01:56 +020041/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
42void session_shutdown(struct session *session, int why);
Simon Hormandec5be42011-06-08 09:19:07 +090043
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010044void session_process_counters(struct session *s);
Willy Tarreau7c669d72008-06-20 15:04:11 +020045void sess_change_server(struct session *sess, struct server *newsrv);
Willy Tarreau26c25062009-03-08 09:38:41 +010046struct task *process_session(struct task *t);
Willy Tarreau84455332009-03-15 22:34:05 +010047void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020048int parse_track_counters(char **args, int *arg,
49 int section_type, struct proxy *curpx,
50 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020051 struct proxy *defpx, char **err);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020052
53/* Remove the refcount from the session to the tracked counters, and clear the
54 * pointer to ensure this is only performed once. The caller is responsible for
55 * ensuring that the pointer is valid first.
56 */
57static inline void session_store_counters(struct session *s)
58{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020059 void *ptr;
60
Willy Tarreau56123282010-08-06 19:06:56 +020061 if (s->stkctr2_entry) {
62 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020063 if (ptr)
64 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020065 s->stkctr2_entry->ref_cnt--;
66 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
67 s->stkctr2_entry = NULL;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020068 }
69
Willy Tarreau56123282010-08-06 19:06:56 +020070 if (s->stkctr1_entry) {
71 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreau38285c12010-06-18 16:35:43 +020072 if (ptr)
73 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020074 s->stkctr1_entry->ref_cnt--;
75 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
76 s->stkctr1_entry = NULL;
Willy Tarreau38285c12010-06-18 16:35:43 +020077 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020078}
79
Willy Tarreauf059a0f2010-08-03 16:29:52 +020080/* Remove the refcount from the session counters tracked only by the backend if
81 * any, and clear the pointer to ensure this is only performed once. The caller
82 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020083 */
Willy Tarreauf059a0f2010-08-03 16:29:52 +020084static inline void session_stop_backend_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020085{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020086 void *ptr;
Willy Tarreaue3487932010-06-18 21:03:20 +020087
Willy Tarreau0a4838c2010-08-06 20:11:05 +020088 if (!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2)))
Willy Tarreauf059a0f2010-08-03 16:29:52 +020089 return;
Willy Tarreaue3487932010-06-18 21:03:20 +020090
Willy Tarreau0a4838c2010-08-06 20:11:05 +020091 if ((s->flags & SN_BE_TRACK_SC1) && s->stkctr1_entry) {
92 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
93 if (ptr)
94 stktable_data_cast(ptr, conn_cur)--;
95 s->stkctr1_entry->ref_cnt--;
96 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
97 s->stkctr1_entry = NULL;
98 }
99
100 if ((s->flags & SN_BE_TRACK_SC2) && s->stkctr2_entry) {
101 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
102 if (ptr)
103 stktable_data_cast(ptr, conn_cur)--;
104 s->stkctr2_entry->ref_cnt--;
105 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
106 s->stkctr2_entry = NULL;
107 }
108 s->flags &= ~(SN_BE_TRACK_SC1|SN_BE_TRACK_SC2);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200109}
Willy Tarreaue3487932010-06-18 21:03:20 +0200110
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200111/* Increase total and concurrent connection count for stick entry <ts> of table
112 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
113 * pointers, and for calling this only once per connection.
114 */
115static inline void session_start_counters(struct stktable *t, struct stksess *ts)
116{
117 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200118
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200119 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
120 if (ptr)
121 stktable_data_cast(ptr, conn_cur)++;
122
123 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
124 if (ptr)
125 stktable_data_cast(ptr, conn_cnt)++;
126
127 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
128 if (ptr)
129 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
130 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
131 if (tick_isset(t->expire))
132 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
133}
134
Willy Tarreau56123282010-08-06 19:06:56 +0200135/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200136 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
137 * are performed to ensure the state can still change.
138 */
Willy Tarreau56123282010-08-06 19:06:56 +0200139static inline void session_track_stkctr1(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200140{
Willy Tarreau56123282010-08-06 19:06:56 +0200141 if (s->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200142 return;
143
144 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200145 s->stkctr1_table = t;
146 s->stkctr1_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200147 session_start_counters(t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200148}
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100149
Willy Tarreau56123282010-08-06 19:06:56 +0200150/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200151 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
152 * are performed to ensure the state can still change.
153 */
Willy Tarreau56123282010-08-06 19:06:56 +0200154static inline void session_track_stkctr2(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200155{
Willy Tarreau56123282010-08-06 19:06:56 +0200156 if (s->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200157 return;
158
159 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200160 s->stkctr2_table = t;
161 s->stkctr2_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200162 session_start_counters(t, ts);
163}
164
Willy Tarreauda7ff642010-06-23 11:44:09 +0200165/* Increase the number of cumulated HTTP requests in the tracked counters */
166static void inline session_inc_http_req_ctr(struct session *s)
167{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200168 void *ptr;
169
Willy Tarreau56123282010-08-06 19:06:56 +0200170 if (s->stkctr2_entry) {
171 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200172 if (ptr)
173 stktable_data_cast(ptr, http_req_cnt)++;
174
Willy Tarreau56123282010-08-06 19:06:56 +0200175 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200176 if (ptr)
177 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200178 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200179 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200180
Willy Tarreau56123282010-08-06 19:06:56 +0200181 if (s->stkctr1_entry) {
182 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200183 if (ptr)
184 stktable_data_cast(ptr, http_req_cnt)++;
185
Willy Tarreau56123282010-08-06 19:06:56 +0200186 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200187 if (ptr)
188 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200189 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200190 }
191}
192
193/* Increase the number of cumulated failed HTTP requests in the tracked
194 * counters. Only 4xx requests should be counted here so that we can
195 * distinguish between errors caused by client behaviour and other ones.
196 * Note that even 404 are interesting because they're generally caused by
197 * vulnerability scans.
198 */
199static void inline session_inc_http_err_ctr(struct session *s)
200{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200201 void *ptr;
202
Willy Tarreau56123282010-08-06 19:06:56 +0200203 if (s->stkctr2_entry) {
204 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200205 if (ptr)
206 stktable_data_cast(ptr, http_err_cnt)++;
207
Willy Tarreau56123282010-08-06 19:06:56 +0200208 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200209 if (ptr)
210 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200211 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200212 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200213
Willy Tarreau56123282010-08-06 19:06:56 +0200214 if (s->stkctr1_entry) {
215 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200216 if (ptr)
217 stktable_data_cast(ptr, http_err_cnt)++;
218
Willy Tarreau56123282010-08-06 19:06:56 +0200219 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200220 if (ptr)
221 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200222 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200223 }
224}
225
Simon Hormanaf514952011-06-21 14:34:57 +0900226static void inline session_add_srv_conn(struct session *sess, struct server *srv)
227{
228 sess->srv_conn = srv;
229 LIST_ADD(&srv->actconns, &sess->by_srv);
230}
231
232static void inline session_del_srv_conn(struct session *sess)
233{
234 if (!sess->srv_conn)
235 return;
236
237 sess->srv_conn = NULL;
238 LIST_DEL(&sess->by_srv);
239}
240
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200241static void inline session_init_srv_conn(struct session *sess)
242{
243 sess->srv_conn = NULL;
244 LIST_INIT(&sess->by_srv);
245}
246
Willy Tarreaubaaee002006-06-26 02:48:02 +0200247#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200248
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249/*
250 * Local variables:
251 * c-indent-level: 8
252 * c-basic-offset: 8
253 * End:
254 */