blob: bf390cacab6e64b86252c0cdd063df82a6d781fd [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
58 if (s->be_tracked_counters) {
59 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_CONN_CUR);
60 if (ptr)
61 stktable_data_cast(ptr, conn_cur)--;
62 s->be_tracked_counters->ref_cnt--;
Willy Tarreauf6efda12010-08-03 20:34:06 +020063 stksess_kill_if_expired(s->be_tracked_table, s->be_tracked_counters);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020064 s->be_tracked_counters = NULL;
65 }
66
67 if (s->fe_tracked_counters) {
68 ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_CONN_CUR);
Willy Tarreau38285c12010-06-18 16:35:43 +020069 if (ptr)
70 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020071 s->fe_tracked_counters->ref_cnt--;
Willy Tarreauf6efda12010-08-03 20:34:06 +020072 stksess_kill_if_expired(s->fe_tracked_table, s->fe_tracked_counters);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020073 s->fe_tracked_counters = 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 Tarreauf059a0f2010-08-03 16:29:52 +020085 if (!s->be_tracked_counters)
86 return;
Willy Tarreaue3487932010-06-18 21:03:20 +020087
Willy Tarreauf059a0f2010-08-03 16:29:52 +020088 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_CONN_CUR);
89 if (ptr)
90 stktable_data_cast(ptr, conn_cur)--;
91 s->be_tracked_counters->ref_cnt--;
Willy Tarreauf6efda12010-08-03 20:34:06 +020092 stksess_kill_if_expired(s->be_tracked_table, s->be_tracked_counters);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020093 s->be_tracked_counters = NULL;
94}
Willy Tarreaue3487932010-06-18 21:03:20 +020095
Willy Tarreauf059a0f2010-08-03 16:29:52 +020096/* Increase total and concurrent connection count for stick entry <ts> of table
97 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
98 * pointers, and for calling this only once per connection.
99 */
100static inline void session_start_counters(struct stktable *t, struct stksess *ts)
101{
102 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200103
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200104 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
105 if (ptr)
106 stktable_data_cast(ptr, conn_cur)++;
107
108 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
109 if (ptr)
110 stktable_data_cast(ptr, conn_cnt)++;
111
112 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
113 if (ptr)
114 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
115 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
116 if (tick_isset(t->expire))
117 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
118}
119
120/* Enable frontend tracking of session counters on stksess <ts>. The caller is
121 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
122 * are performed to ensure the state can still change.
123 */
124static inline void session_track_fe_counters(struct session *s, struct stktable *t, struct stksess *ts)
125{
126 if (s->fe_tracked_counters)
127 return;
128
129 ts->ref_cnt++;
130 s->fe_tracked_table = t;
131 s->fe_tracked_counters = ts;
132 session_start_counters(t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200133}
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100134
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200135/* Enable backend tracking of session counters on stksess <ts>. The caller is
136 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
137 * are performed to ensure the state can still change.
138 */
139static inline void session_track_be_counters(struct session *s, struct stktable *t, struct stksess *ts)
140{
141 if (s->be_tracked_counters)
142 return;
143
144 ts->ref_cnt++;
145 s->be_tracked_table = t;
146 s->be_tracked_counters = ts;
147 session_start_counters(t, ts);
148}
149
Willy Tarreauf8533202008-08-16 14:55:08 +0200150static void inline trace_term(struct session *s, unsigned int code)
151{
152 s->term_trace <<= TT_BIT_SHIFT;
153 s->term_trace |= code;
154}
155
Willy Tarreauda7ff642010-06-23 11:44:09 +0200156/* Increase the number of cumulated HTTP requests in the tracked counters */
157static void inline session_inc_http_req_ctr(struct session *s)
158{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200159 void *ptr;
160
161 if (s->be_tracked_counters) {
162 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_REQ_CNT);
163 if (ptr)
164 stktable_data_cast(ptr, http_req_cnt)++;
165
166 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_REQ_RATE);
167 if (ptr)
168 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
169 s->be_tracked_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
170 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200171
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200172 if (s->fe_tracked_counters) {
173 ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200174 if (ptr)
175 stktable_data_cast(ptr, http_req_cnt)++;
176
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200177 ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200178 if (ptr)
179 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200180 s->fe_tracked_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200181 }
182}
183
184/* Increase the number of cumulated failed HTTP requests in the tracked
185 * counters. Only 4xx requests should be counted here so that we can
186 * distinguish between errors caused by client behaviour and other ones.
187 * Note that even 404 are interesting because they're generally caused by
188 * vulnerability scans.
189 */
190static void inline session_inc_http_err_ctr(struct session *s)
191{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200192 void *ptr;
193
194 if (s->be_tracked_counters) {
195 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_ERR_CNT);
196 if (ptr)
197 stktable_data_cast(ptr, http_err_cnt)++;
198
199 ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_ERR_RATE);
200 if (ptr)
201 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
202 s->be_tracked_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
203 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200204
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200205 if (s->fe_tracked_counters) {
206 ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200207 if (ptr)
208 stktable_data_cast(ptr, http_err_cnt)++;
209
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200210 ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200211 if (ptr)
212 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200213 s->fe_tracked_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200214 }
215}
216
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200218
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219/*
220 * Local variables:
221 * c-indent-level: 8
222 * c-basic-offset: 8
223 * End:
224 */