blob: 3b6e1fcbe1a0482fa54ba2aeac35fdc053de0727 [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;
Willy Tarreau20d46a52012-12-09 15:55:40 +010060 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +020061
Willy Tarreau20d46a52012-12-09 15:55:40 +010062 for (i = 0; i < sizeof(s->stkctr) / sizeof(s->stkctr[0]); i++) {
63 if (!s->stkctr[i].entry)
64 continue;
65 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_CONN_CUR);
Willy Tarreauf059a0f2010-08-03 16:29:52 +020066 if (ptr)
67 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau20d46a52012-12-09 15:55:40 +010068 s->stkctr[i].entry->ref_cnt--;
69 stksess_kill_if_expired(s->stkctr[i].table, s->stkctr[i].entry);
70 s->stkctr[i].entry = NULL;
Willy Tarreau38285c12010-06-18 16:35:43 +020071 }
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020072}
73
Willy Tarreauf059a0f2010-08-03 16:29:52 +020074/* Remove the refcount from the session counters tracked only by the backend if
75 * any, and clear the pointer to ensure this is only performed once. The caller
76 * is responsible for ensuring that the pointer is valid first.
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020077 */
Willy Tarreauf059a0f2010-08-03 16:29:52 +020078static inline void session_stop_backend_counters(struct session *s)
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020079{
Willy Tarreauf059a0f2010-08-03 16:29:52 +020080 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +010081 int i;
Willy Tarreaue3487932010-06-18 21:03:20 +020082
Willy Tarreau0a4838c2010-08-06 20:11:05 +020083 if (!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2)))
Willy Tarreauf059a0f2010-08-03 16:29:52 +020084 return;
Willy Tarreaue3487932010-06-18 21:03:20 +020085
Willy Tarreau20d46a52012-12-09 15:55:40 +010086 for (i = 0; i < sizeof(s->stkctr) / sizeof(s->stkctr[0]); i++) {
87 if (!s->stkctr[i].entry)
88 continue;
Willy Tarreau0a4838c2010-08-06 20:11:05 +020089
Willy Tarreau20d46a52012-12-09 15:55:40 +010090 if ((i == 0) && !(s->flags & SN_BE_TRACK_SC1))
91 continue;
92
93 if ((i == 1) && !(s->flags & SN_BE_TRACK_SC2))
94 continue;
95
96 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_CONN_CUR);
Willy Tarreau0a4838c2010-08-06 20:11:05 +020097 if (ptr)
98 stktable_data_cast(ptr, conn_cur)--;
Willy Tarreau20d46a52012-12-09 15:55:40 +010099 s->stkctr[i].entry->ref_cnt--;
100 stksess_kill_if_expired(s->stkctr[i].table, s->stkctr[i].entry);
101 s->stkctr[i].entry = NULL;
Willy Tarreau0a4838c2010-08-06 20:11:05 +0200102 }
103 s->flags &= ~(SN_BE_TRACK_SC1|SN_BE_TRACK_SC2);
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200104}
Willy Tarreaue3487932010-06-18 21:03:20 +0200105
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200106/* Increase total and concurrent connection count for stick entry <ts> of table
107 * <t>. The caller is responsible for ensuring that <t> and <ts> are valid
108 * pointers, and for calling this only once per connection.
109 */
110static inline void session_start_counters(struct stktable *t, struct stksess *ts)
111{
112 void *ptr;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200113
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200114 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
115 if (ptr)
116 stktable_data_cast(ptr, conn_cur)++;
117
118 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
119 if (ptr)
120 stktable_data_cast(ptr, conn_cnt)++;
121
122 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
123 if (ptr)
124 update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
125 t->data_arg[STKTABLE_DT_CONN_RATE].u, 1);
126 if (tick_isset(t->expire))
127 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
128}
129
Willy Tarreau20d46a52012-12-09 15:55:40 +0100130/* Enable tracking of session counters as <stkctr> on stksess <ts>. The caller is
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200131 * responsible for ensuring that <t> and <ts> are valid pointers. Some controls
132 * are performed to ensure the state can still change.
133 */
Willy Tarreau20d46a52012-12-09 15:55:40 +0100134static inline void session_track_stkctr(struct stkctr *ctr, struct stktable *t, struct stksess *ts)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200135{
Willy Tarreau20d46a52012-12-09 15:55:40 +0100136 if (ctr->entry)
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200137 return;
138
139 ts->ref_cnt++;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100140 ctr->table = t;
141 ctr->entry = ts;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200142 session_start_counters(t, ts);
143}
144
Willy Tarreauda7ff642010-06-23 11:44:09 +0200145/* Increase the number of cumulated HTTP requests in the tracked counters */
146static void inline session_inc_http_req_ctr(struct session *s)
147{
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200148 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100149 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200150
Willy Tarreau20d46a52012-12-09 15:55:40 +0100151 for (i = 0; i < sizeof(s->stkctr) / sizeof(s->stkctr[0]); i++) {
152 if (!s->stkctr[i].entry)
153 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200154
Willy Tarreau20d46a52012-12-09 15:55:40 +0100155 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200156 if (ptr)
157 stktable_data_cast(ptr, http_req_cnt)++;
158
Willy Tarreau20d46a52012-12-09 15:55:40 +0100159 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200160 if (ptr)
161 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100162 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200163 }
164}
165
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100166/* Increase the number of cumulated HTTP requests in the backend's tracked counters */
167static void inline session_inc_be_http_req_ctr(struct session *s)
168{
169 void *ptr;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100170 int i;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100171
172 if (likely(!(s->flags & (SN_BE_TRACK_SC1|SN_BE_TRACK_SC2))))
173 return;
174
Willy Tarreau20d46a52012-12-09 15:55:40 +0100175 for (i = 0; i < sizeof(s->stkctr) / sizeof(s->stkctr[0]); i++) {
176 if (!s->stkctr[i].entry)
177 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100178
Willy Tarreau20d46a52012-12-09 15:55:40 +0100179 if ((i == 0) && !(s->flags & SN_BE_TRACK_SC1))
180 continue;
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100181
Willy Tarreau20d46a52012-12-09 15:55:40 +0100182 if ((i == 1) && !(s->flags & SN_BE_TRACK_SC2))
183 continue;
184
185 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_CNT);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100186 if (ptr)
187 stktable_data_cast(ptr, http_req_cnt)++;
188
Willy Tarreau20d46a52012-12-09 15:55:40 +0100189 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_REQ_RATE);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100190 if (ptr)
191 update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100192 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1);
Willy Tarreau5d5b5d82012-12-09 12:00:04 +0100193 }
194}
195
Willy Tarreauda7ff642010-06-23 11:44:09 +0200196/* 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;
Willy Tarreau20d46a52012-12-09 15:55:40 +0100205 int i;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200206
Willy Tarreau20d46a52012-12-09 15:55:40 +0100207 for (i = 0; i < sizeof(s->stkctr) / sizeof(s->stkctr[0]); i++) {
208 if (!s->stkctr[i].entry)
209 continue;
Willy Tarreauf059a0f2010-08-03 16:29:52 +0200210
Willy Tarreau20d46a52012-12-09 15:55:40 +0100211 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_ERR_CNT);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200212 if (ptr)
213 stktable_data_cast(ptr, http_err_cnt)++;
214
Willy Tarreau20d46a52012-12-09 15:55:40 +0100215 ptr = stktable_data_ptr(s->stkctr[i].table, s->stkctr[i].entry, STKTABLE_DT_HTTP_ERR_RATE);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200216 if (ptr)
217 update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreau20d46a52012-12-09 15:55:40 +0100218 s->stkctr[i].table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1);
Willy Tarreauda7ff642010-06-23 11:44:09 +0200219 }
220}
221
Simon Hormanaf514952011-06-21 14:34:57 +0900222static void inline session_add_srv_conn(struct session *sess, struct server *srv)
223{
224 sess->srv_conn = srv;
225 LIST_ADD(&srv->actconns, &sess->by_srv);
226}
227
228static void inline session_del_srv_conn(struct session *sess)
229{
230 if (!sess->srv_conn)
231 return;
232
233 sess->srv_conn = NULL;
234 LIST_DEL(&sess->by_srv);
235}
236
Willy Tarreau9bd0d742011-07-20 00:17:39 +0200237static void inline session_init_srv_conn(struct session *sess)
238{
239 sess->srv_conn = NULL;
240 LIST_INIT(&sess->by_srv);
241}
242
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243#endif /* _PROTO_SESSION_H */
Willy TARREAU3dc06442006-06-15 21:48:13 +0200244
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245/*
246 * Local variables:
247 * c-indent-level: 8
248 * c-basic-offset: 8
249 * End:
250 */