blob: 78a22226b5185199d96c59b51597cca3fd32bdf9 [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 Tarreau81f9aa32010-06-01 17:45:26 +020034int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr);
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020036/* perform minimal intializations, report 0 in case of error, 1 if OK. */
37int init_session();
Willy Tarreaubaaee002006-06-26 02:48:02 +020038
Simon Hormandec5be42011-06-08 09:19:07 +090039
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010040void session_process_counters(struct session *s);
Willy Tarreau7c669d72008-06-20 15:04:11 +020041void sess_change_server(struct session *sess, struct server *newsrv);
Willy Tarreau26c25062009-03-08 09:38:41 +010042struct task *process_session(struct task *t);
Willy Tarreau84455332009-03-15 22:34:05 +010043void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020044int parse_track_counters(char **args, int *arg,
45 int section_type, struct proxy *curpx,
46 struct track_ctr_prm *prm,
47 struct proxy *defpx, char *err, int errlen);
48
49/* Remove the refcount from the session to the tracked counters, and clear the
50 * pointer to ensure this is only performed once. The caller is responsible for
51 * ensuring that the pointer is valid first.
52 */
53static inline void session_store_counters(struct session *s)
54{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020055 void *ptr;
56
Willy Tarreau56123282010-08-06 19:06:56 +020057 if (s->stkctr2_entry) {
58 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020059 if (ptr)
60 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020061 s->stkctr2_entry->ref_cnt--;
62 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
63 s->stkctr2_entry = NULL;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020064 }
65
Willy Tarreau56123282010-08-06 19:06:56 +020066 if (s->stkctr1_entry) {
67 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreau38285c12010-06-18 16:35:43 +020068 if (ptr)
69 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020070 s->stkctr1_entry->ref_cnt--;
71 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
72 s->stkctr1_entry = NULL;
Willy Tarreau38285c12010-06-18 16:35:43 +020073 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020074}
75
Willy Tarreauf059a0f2010-08-03 16:29:52 +020076/* Remove the refcount from the session counters tracked only by the backend if
77 * any, and clear the pointer to ensure this is only performed once. The caller
78 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020079 */
Willy Tarreauf059a0f2010-08-03 16:29:52 +020080static inline void session_stop_backend_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020081{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020082 void *ptr;
Willy Tarreaue3487932010-06-18 21:03:20 +020083
Willy Tarreau0a4838c2010-08-06 20:11:05 +020084 if (!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2)))
Willy Tarreauf059a0f2010-08-03 16:29:52 +020085 return;
Willy Tarreaue3487932010-06-18 21:03:20 +020086
Willy Tarreau0a4838c2010-08-06 20:11:05 +020087 if ((s->flags & SN_BE_TRACK_SC1) && s->stkctr1_entry) {
88 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
89 if (ptr)
90 stktable_data_cast(ptr, conn_cur)--;
91 s->stkctr1_entry->ref_cnt--;
92 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
93 s->stkctr1_entry = NULL;
94 }
95
96 if ((s->flags & SN_BE_TRACK_SC2) && s->stkctr2_entry) {
97 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
98 if (ptr)
99 stktable_data_cast(ptr, conn_cur)--;
100 s->stkctr2_entry->ref_cnt--;
101 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
102 s->stkctr2_entry = NULL;
103 }
104 s->flags &= ~(SN_BE_TRACK_SC1|SN_BE_TRACK_SC2);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200105}
Willy Tarreaue3487932010-06-18 21:03:20 +0200106
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200107/* Increase total and concurrent connection count for stick entry <ts> of table
108 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
109 * pointers, and for calling this only once per connection.
110 */
111static inline void session_start_counters(struct stktable *t, struct stksess *ts)
112{
113 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200114
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200115 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
116 if (ptr)
117 stktable_data_cast(ptr, conn_cur)++;
118
119 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
120 if (ptr)
121 stktable_data_cast(ptr, conn_cnt)++;
122
123 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
124 if (ptr)
125 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
126 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
127 if (tick_isset(t->expire))
128 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
129}
130
Willy Tarreau56123282010-08-06 19:06:56 +0200131/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200132 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
133 * are performed to ensure the state can still change.
134 */
Willy Tarreau56123282010-08-06 19:06:56 +0200135static inline void session_track_stkctr1(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200136{
Willy Tarreau56123282010-08-06 19:06:56 +0200137 if (s->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200138 return;
139
140 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200141 s->stkctr1_table = t;
142 s->stkctr1_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200143 session_start_counters(t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200144}
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100145
Willy Tarreau56123282010-08-06 19:06:56 +0200146/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200147 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
148 * are performed to ensure the state can still change.
149 */
Willy Tarreau56123282010-08-06 19:06:56 +0200150static inline void session_track_stkctr2(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200151{
Willy Tarreau56123282010-08-06 19:06:56 +0200152 if (s->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200153 return;
154
155 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200156 s->stkctr2_table = t;
157 s->stkctr2_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200158 session_start_counters(t, ts);
159}
160
Willy Tarreauf8533202008-08-16 14:55:08 +0200161static void inline trace_term(struct session *s, unsigned int code)
162{
163 s->term_trace <<= TT_BIT_SHIFT;
164 s->term_trace |= code;
165}
166
Willy Tarreauda7ff642010-06-23 11:44:09 +0200167/* Increase the number of cumulated HTTP requests in the tracked counters */
168static void inline session_inc_http_req_ctr(struct session *s)
169{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200170 void *ptr;
171
Willy Tarreau56123282010-08-06 19:06:56 +0200172 if (s->stkctr2_entry) {
173 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200174 if (ptr)
175 stktable_data_cast(ptr, http_req_cnt)++;
176
Willy Tarreau56123282010-08-06 19:06:56 +0200177 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200178 if (ptr)
179 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200180 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200181 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200182
Willy Tarreau56123282010-08-06 19:06:56 +0200183 if (s->stkctr1_entry) {
184 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200185 if (ptr)
186 stktable_data_cast(ptr, http_req_cnt)++;
187
Willy Tarreau56123282010-08-06 19:06:56 +0200188 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200189 if (ptr)
190 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200191 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200192 }
193}
194
195/* Increase the number of cumulated failed HTTP requests in the tracked
196 * counters. Only 4xx requests should be counted here so that we can
197 * distinguish between errors caused by client behaviour and other ones.
198 * Note that even 404 are interesting because they're generally caused by
199 * vulnerability scans.
200 */
201static void inline session_inc_http_err_ctr(struct session *s)
202{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200203 void *ptr;
204
Willy Tarreau56123282010-08-06 19:06:56 +0200205 if (s->stkctr2_entry) {
206 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200207 if (ptr)
208 stktable_data_cast(ptr, http_err_cnt)++;
209
Willy Tarreau56123282010-08-06 19:06:56 +0200210 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200211 if (ptr)
212 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200213 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200214 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200215
Willy Tarreau56123282010-08-06 19:06:56 +0200216 if (s->stkctr1_entry) {
217 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200218 if (ptr)
219 stktable_data_cast(ptr, http_err_cnt)++;
220
Willy Tarreau56123282010-08-06 19:06:56 +0200221 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200222 if (ptr)
223 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200224 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200225 }
226}
227
Simon Hormanaf514952011-06-21 14:34:57 +0900228static void inline session_add_srv_conn(struct session *sess, struct server *srv)
229{
230 sess->srv_conn = srv;
231 LIST_ADD(&srv->actconns, &sess->by_srv);
232}
233
234static void inline session_del_srv_conn(struct session *sess)
235{
236 if (!sess->srv_conn)
237 return;
238
239 sess->srv_conn = NULL;
240 LIST_DEL(&sess->by_srv);
241}
242
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200243static void inline session_init_srv_conn(struct session *sess)
244{
245 sess->srv_conn = NULL;
246 LIST_INIT(&sess->by_srv);
247}
248
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200250
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251/*
252 * Local variables:
253 * c-indent-level: 8
254 * c-basic-offset: 8
255 * End:
256 */