blob: 15f3fe744a2efc349538bda0c33801a641288385 [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 +020035void session_free(struct session *s);
36
Willy Tarreauc6ca1a02007-05-13 19:43:47 +020037/* perform minimal intializations, report 0 in case of error, 1 if OK. */
38int init_session();
Willy Tarreaubaaee002006-06-26 02:48:02 +020039
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 sess_set_term_flags(struct session *s);
44void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020045int parse_track_counters(char **args, int *arg,
46 int section_type, struct proxy *curpx,
47 struct track_ctr_prm *prm,
48 struct proxy *defpx, char *err, int errlen);
49
50/* Remove the refcount from the session to the tracked counters, and clear the
51 * pointer to ensure this is only performed once. The caller is responsible for
52 * ensuring that the pointer is valid first.
53 */
54static inline void session_store_counters(struct session *s)
55{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020056 void *ptr;
57
Willy Tarreau56123282010-08-06 19:06:56 +020058 if (s->stkctr2_entry) {
59 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020060 if (ptr)
61 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020062 s->stkctr2_entry->ref_cnt--;
63 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
64 s->stkctr2_entry = NULL;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020065 }
66
Willy Tarreau56123282010-08-06 19:06:56 +020067 if (s->stkctr1_entry) {
68 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreau38285c12010-06-18 16:35:43 +020069 if (ptr)
70 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020071 s->stkctr1_entry->ref_cnt--;
72 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
73 s->stkctr1_entry = NULL;
Willy Tarreau38285c12010-06-18 16:35:43 +020074 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020075}
76
Willy Tarreauf059a0f2010-08-03 16:29:52 +020077/* Remove the refcount from the session counters tracked only by the backend if
78 * any, and clear the pointer to ensure this is only performed once. The caller
79 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020080 */
Willy Tarreauf059a0f2010-08-03 16:29:52 +020081static inline void session_stop_backend_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020082{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020083 void *ptr;
Willy Tarreaue3487932010-06-18 21:03:20 +020084
Willy Tarreau0a4838c2010-08-06 20:11:05 +020085 if (!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2)))
Willy Tarreauf059a0f2010-08-03 16:29:52 +020086 return;
Willy Tarreaue3487932010-06-18 21:03:20 +020087
Willy Tarreau0a4838c2010-08-06 20:11:05 +020088 if ((s->flags & SN_BE_TRACK_SC1) && s->stkctr1_entry) {
89 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
90 if (ptr)
91 stktable_data_cast(ptr, conn_cur)--;
92 s->stkctr1_entry->ref_cnt--;
93 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
94 s->stkctr1_entry = NULL;
95 }
96
97 if ((s->flags & SN_BE_TRACK_SC2) && s->stkctr2_entry) {
98 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
99 if (ptr)
100 stktable_data_cast(ptr, conn_cur)--;
101 s->stkctr2_entry->ref_cnt--;
102 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
103 s->stkctr2_entry = NULL;
104 }
105 s->flags &= ~(SN_BE_TRACK_SC1|SN_BE_TRACK_SC2);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200106}
Willy Tarreaue3487932010-06-18 21:03:20 +0200107
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200108/* Increase total and concurrent connection count for stick entry <ts> of table
109 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
110 * pointers, and for calling this only once per connection.
111 */
112static inline void session_start_counters(struct stktable *t, struct stksess *ts)
113{
114 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200115
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200116 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
117 if (ptr)
118 stktable_data_cast(ptr, conn_cur)++;
119
120 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
121 if (ptr)
122 stktable_data_cast(ptr, conn_cnt)++;
123
124 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
125 if (ptr)
126 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
127 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
128 if (tick_isset(t->expire))
129 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
130}
131
Willy Tarreau56123282010-08-06 19:06:56 +0200132/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200133 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
134 * are performed to ensure the state can still change.
135 */
Willy Tarreau56123282010-08-06 19:06:56 +0200136static inline void session_track_stkctr1(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200137{
Willy Tarreau56123282010-08-06 19:06:56 +0200138 if (s->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200139 return;
140
141 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200142 s->stkctr1_table = t;
143 s->stkctr1_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200144 session_start_counters(t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200145}
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100146
Willy Tarreau56123282010-08-06 19:06:56 +0200147/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200148 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
149 * are performed to ensure the state can still change.
150 */
Willy Tarreau56123282010-08-06 19:06:56 +0200151static inline void session_track_stkctr2(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200152{
Willy Tarreau56123282010-08-06 19:06:56 +0200153 if (s->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200154 return;
155
156 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200157 s->stkctr2_table = t;
158 s->stkctr2_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200159 session_start_counters(t, ts);
160}
161
Willy Tarreauf8533202008-08-16 14:55:08 +0200162static void inline trace_term(struct session *s, unsigned int code)
163{
164 s->term_trace <<= TT_BIT_SHIFT;
165 s->term_trace |= code;
166}
167
Willy Tarreauda7ff642010-06-23 11:44:09 +0200168/* Increase the number of cumulated HTTP requests in the tracked counters */
169static void inline session_inc_http_req_ctr(struct session *s)
170{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200171 void *ptr;
172
Willy Tarreau56123282010-08-06 19:06:56 +0200173 if (s->stkctr2_entry) {
174 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200175 if (ptr)
176 stktable_data_cast(ptr, http_req_cnt)++;
177
Willy Tarreau56123282010-08-06 19:06:56 +0200178 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200179 if (ptr)
180 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200181 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200182 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200183
Willy Tarreau56123282010-08-06 19:06:56 +0200184 if (s->stkctr1_entry) {
185 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200186 if (ptr)
187 stktable_data_cast(ptr, http_req_cnt)++;
188
Willy Tarreau56123282010-08-06 19:06:56 +0200189 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200190 if (ptr)
191 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200192 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200193 }
194}
195
196/* Increase the number of cumulated failed HTTP requests in the tracked
197 * counters. Only 4xx requests should be counted here so that we can
198 * distinguish between errors caused by client behaviour and other ones.
199 * Note that even 404 are interesting because they're generally caused by
200 * vulnerability scans.
201 */
202static void inline session_inc_http_err_ctr(struct session *s)
203{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200204 void *ptr;
205
Willy Tarreau56123282010-08-06 19:06:56 +0200206 if (s->stkctr2_entry) {
207 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200208 if (ptr)
209 stktable_data_cast(ptr, http_err_cnt)++;
210
Willy Tarreau56123282010-08-06 19:06:56 +0200211 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200212 if (ptr)
213 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200214 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200215 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200216
Willy Tarreau56123282010-08-06 19:06:56 +0200217 if (s->stkctr1_entry) {
218 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200219 if (ptr)
220 stktable_data_cast(ptr, http_err_cnt)++;
221
Willy Tarreau56123282010-08-06 19:06:56 +0200222 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200223 if (ptr)
224 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200225 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200226 }
227}
228
Willy Tarreaubaaee002006-06-26 02:48:02 +0200229#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200230
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231/*
232 * Local variables:
233 * c-indent-level: 8
234 * c-basic-offset: 8
235 * End:
236 */