blob: b9eb4477173c7c53efd93e552c9fa36229a14d25 [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
Willy Tarreaua2a64e92011-09-07 23:01:56 +020039/* kill a session and set the termination flags to <why> (one of SN_ERR_*) */
40void session_shutdown(struct session *session, int why);
Simon Hormandec5be42011-06-08 09:19:07 +090041
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +010042void session_process_counters(struct session *s);
Willy Tarreau7c669d72008-06-20 15:04:11 +020043void sess_change_server(struct session *sess, struct server *newsrv);
Willy Tarreau26c25062009-03-08 09:38:41 +010044struct task *process_session(struct task *t);
Willy Tarreau84455332009-03-15 22:34:05 +010045void default_srv_error(struct session *s, struct stream_interface *si);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020046int parse_track_counters(char **args, int *arg,
47 int section_type, struct proxy *curpx,
48 struct track_ctr_prm *prm,
Willy Tarreau0a3dd742012-05-08 19:47:01 +020049 struct proxy *defpx, char **err);
Willy Tarreau22cda212012-08-31 17:43:29 +020050int conn_session_complete(struct connection *conn, int flag);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020051
52/* Remove the refcount from the session to the tracked counters, and clear the
53 * pointer to ensure this is only performed once. The caller is responsible for
54 * ensuring that the pointer is valid first.
55 */
56static inline void session_store_counters(struct session *s)
57{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020058 void *ptr;
59
Willy Tarreau56123282010-08-06 19:06:56 +020060 if (s->stkctr2_entry) {
61 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020062 if (ptr)
63 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020064 s->stkctr2_entry->ref_cnt--;
65 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
66 s->stkctr2_entry = NULL;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020067 }
68
Willy Tarreau56123282010-08-06 19:06:56 +020069 if (s->stkctr1_entry) {
70 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
Willy Tarreau38285c12010-06-18 16:35:43 +020071 if (ptr)
72 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau56123282010-08-06 19:06:56 +020073 s->stkctr1_entry->ref_cnt--;
74 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
75 s->stkctr1_entry = NULL;
Willy Tarreau38285c12010-06-18 16:35:43 +020076 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020077}
78
Willy Tarreauf059a0f2010-08-03 16:29:52 +020079/* Remove the refcount from the session counters tracked only by the backend if
80 * any, and clear the pointer to ensure this is only performed once. The caller
81 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020082 */
Willy Tarreauf059a0f2010-08-03 16:29:52 +020083static inline void session_stop_backend_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020084{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020085 void *ptr;
Willy Tarreaue3487932010-06-18 21:03:20 +020086
Willy Tarreau0a4838c2010-08-06 20:11:05 +020087 if (!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2)))
Willy Tarreauf059a0f2010-08-03 16:29:52 +020088 return;
Willy Tarreaue3487932010-06-18 21:03:20 +020089
Willy Tarreau0a4838c2010-08-06 20:11:05 +020090 if ((s->flags & SN_BE_TRACK_SC1) && s->stkctr1_entry) {
91 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_CONN_CUR);
92 if (ptr)
93 stktable_data_cast(ptr, conn_cur)--;
94 s->stkctr1_entry->ref_cnt--;
95 stksess_kill_if_expired(s->stkctr1_table, s->stkctr1_entry);
96 s->stkctr1_entry = NULL;
97 }
98
99 if ((s->flags & SN_BE_TRACK_SC2) && s->stkctr2_entry) {
100 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_CONN_CUR);
101 if (ptr)
102 stktable_data_cast(ptr, conn_cur)--;
103 s->stkctr2_entry->ref_cnt--;
104 stksess_kill_if_expired(s->stkctr2_table, s->stkctr2_entry);
105 s->stkctr2_entry = NULL;
106 }
107 s->flags &= ~(SN_BE_TRACK_SC1|SN_BE_TRACK_SC2);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200108}
Willy Tarreaue3487932010-06-18 21:03:20 +0200109
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200110/* Increase total and concurrent connection count for stick entry <ts> of table
111 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
112 * pointers, and for calling this only once per connection.
113 */
114static inline void session_start_counters(struct stktable *t, struct stksess *ts)
115{
116 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200117
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200118 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
119 if (ptr)
120 stktable_data_cast(ptr, conn_cur)++;
121
122 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
123 if (ptr)
124 stktable_data_cast(ptr, conn_cnt)++;
125
126 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
127 if (ptr)
128 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
129 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
130 if (tick_isset(t->expire))
131 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
132}
133
Willy Tarreau56123282010-08-06 19:06:56 +0200134/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200135 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
136 * are performed to ensure the state can still change.
137 */
Willy Tarreau56123282010-08-06 19:06:56 +0200138static inline void session_track_stkctr1(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200139{
Willy Tarreau56123282010-08-06 19:06:56 +0200140 if (s->stkctr1_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200141 return;
142
143 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200144 s->stkctr1_table = t;
145 s->stkctr1_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200146 session_start_counters(t, ts);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200147}
Krzysztof Piotr Oledzki583bc962007-11-24 22:12:47 +0100148
Willy Tarreau56123282010-08-06 19:06:56 +0200149/* Enable tracking of session counters as stkctr1 on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200150 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
151 * are performed to ensure the state can still change.
152 */
Willy Tarreau56123282010-08-06 19:06:56 +0200153static inline void session_track_stkctr2(struct session *s, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200154{
Willy Tarreau56123282010-08-06 19:06:56 +0200155 if (s->stkctr2_entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200156 return;
157
158 ts->ref_cnt++;
Willy Tarreau56123282010-08-06 19:06:56 +0200159 s->stkctr2_table = t;
160 s->stkctr2_entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200161 session_start_counters(t, ts);
162}
163
Willy Tarreauf8533202008-08-16 14:55:08 +0200164static void inline trace_term(struct session *s, unsigned int code)
165{
166 s->term_trace <<= TT_BIT_SHIFT;
167 s->term_trace |= code;
168}
169
Willy Tarreauda7ff642010-06-23 11:44:09 +0200170/* Increase the number of cumulated HTTP requests in the tracked counters */
171static void inline session_inc_http_req_ctr(struct session *s)
172{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200173 void *ptr;
174
Willy Tarreau56123282010-08-06 19:06:56 +0200175 if (s->stkctr2_entry) {
176 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200177 if (ptr)
178 stktable_data_cast(ptr, http_req_cnt)++;
179
Willy Tarreau56123282010-08-06 19:06:56 +0200180 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200181 if (ptr)
182 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200183 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200184 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200185
Willy Tarreau56123282010-08-06 19:06:56 +0200186 if (s->stkctr1_entry) {
187 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200188 if (ptr)
189 stktable_data_cast(ptr, http_req_cnt)++;
190
Willy Tarreau56123282010-08-06 19:06:56 +0200191 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200192 if (ptr)
193 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200194 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200195 }
196}
197
198/* Increase the number of cumulated failed HTTP requests in the tracked
199 * counters. Only 4xx requests should be counted here so that we can
200 * distinguish between errors caused by client behaviour and other ones.
201 * Note that even 404 are interesting because they're generally caused by
202 * vulnerability scans.
203 */
204static void inline session_inc_http_err_ctr(struct session *s)
205{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200206 void *ptr;
207
Willy Tarreau56123282010-08-06 19:06:56 +0200208 if (s->stkctr2_entry) {
209 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200210 if (ptr)
211 stktable_data_cast(ptr, http_err_cnt)++;
212
Willy Tarreau56123282010-08-06 19:06:56 +0200213 ptr = stktable_data_ptr(s->stkctr2_table, s->stkctr2_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200214 if (ptr)
215 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200216 s->stkctr2_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200217 }
Willy Tarreauda7ff642010-06-23 11:44:09 +0200218
Willy Tarreau56123282010-08-06 19:06:56 +0200219 if (s->stkctr1_entry) {
220 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200221 if (ptr)
222 stktable_data_cast(ptr, http_err_cnt)++;
223
Willy Tarreau56123282010-08-06 19:06:56 +0200224 ptr = stktable_data_ptr(s->stkctr1_table, s->stkctr1_entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200225 if (ptr)
226 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau56123282010-08-06 19:06:56 +0200227 s->stkctr1_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200228 }
229}
230
Simon Hormanaf514952011-06-21 14:34:57 +0900231static void inline session_add_srv_conn(struct session *sess, struct server *srv)
232{
233 sess->srv_conn = srv;
234 LIST_ADD(&srv->actconns, &sess->by_srv);
235}
236
237static void inline session_del_srv_conn(struct session *sess)
238{
239 if (!sess->srv_conn)
240 return;
241
242 sess->srv_conn = NULL;
243 LIST_DEL(&sess->by_srv);
244}
245
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200246static void inline session_init_srv_conn(struct session *sess)
247{
248 sess->srv_conn = NULL;
249 LIST_INIT(&sess->by_srv);
250}
251
Willy Tarreaubaaee002006-06-26 02:48:02 +0200252#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200253
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254/*
255 * Local variables:
256 * c-indent-level: 8
257 * c-basic-offset: 8
258 * End:
259 */