Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 2 | * 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 TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 21 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 22 | #ifndef _PROTO_SESSION_H |
| 23 | #define _PROTO_SESSION_H |
| 24 | |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 26 | #include <common/memory.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | #include <types/session.h> |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 28 | #include <proto/freq_ctr.h> |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 29 | #include <proto/stick_table.h> |
Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 30 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 31 | extern struct pool_head *pool2_session; |
Willy Tarreau | f54f8bd | 2008-11-23 19:53:55 +0100 | [diff] [blame] | 32 | extern struct list sessions; |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | 81f9aa3 | 2010-06-01 17:45:26 +0200 | [diff] [blame] | 34 | int session_accept(struct listener *l, int cfd, struct sockaddr_storage *addr); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | void session_free(struct session *s); |
| 36 | |
Willy Tarreau | c6ca1a0 | 2007-05-13 19:43:47 +0200 | [diff] [blame] | 37 | /* perform minimal intializations, report 0 in case of error, 1 if OK. */ |
| 38 | int init_session(); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 39 | |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 40 | void session_process_counters(struct session *s); |
Willy Tarreau | 7c669d7 | 2008-06-20 15:04:11 +0200 | [diff] [blame] | 41 | void sess_change_server(struct session *sess, struct server *newsrv); |
Willy Tarreau | 26c2506 | 2009-03-08 09:38:41 +0100 | [diff] [blame] | 42 | struct task *process_session(struct task *t); |
Willy Tarreau | 8445533 | 2009-03-15 22:34:05 +0100 | [diff] [blame] | 43 | void sess_set_term_flags(struct session *s); |
| 44 | void default_srv_error(struct session *s, struct stream_interface *si); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 45 | int 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 | */ |
| 54 | static inline void session_store_counters(struct session *s) |
| 55 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 56 | 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--; |
| 63 | s->be_tracked_counters = NULL; |
| 64 | } |
| 65 | |
| 66 | if (s->fe_tracked_counters) { |
| 67 | ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_CONN_CUR); |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 68 | if (ptr) |
| 69 | stktable_data_cast(ptr, conn_cur)--; |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 70 | s->fe_tracked_counters->ref_cnt--; |
| 71 | s->fe_tracked_counters = NULL; |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 72 | } |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 73 | } |
| 74 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 75 | /* Remove the refcount from the session counters tracked only by the backend if |
| 76 | * any, and clear the pointer to ensure this is only performed once. The caller |
| 77 | * is responsible for ensuring that the pointer is valid first. |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 78 | */ |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 79 | static inline void session_stop_backend_counters(struct session *s) |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 80 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 81 | void *ptr; |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 83 | if (!s->be_tracked_counters) |
| 84 | return; |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 85 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 86 | ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_CONN_CUR); |
| 87 | if (ptr) |
| 88 | stktable_data_cast(ptr, conn_cur)--; |
| 89 | s->be_tracked_counters->ref_cnt--; |
| 90 | s->be_tracked_counters = NULL; |
| 91 | } |
Willy Tarreau | e348793 | 2010-06-18 21:03:20 +0200 | [diff] [blame] | 92 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 93 | /* Increase total and concurrent connection count for stick entry <ts> of table |
| 94 | * <t>. The caller is responsible for ensuring that <t> and <ts> are valid |
| 95 | * pointers, and for calling this only once per connection. |
| 96 | */ |
| 97 | static inline void session_start_counters(struct stktable *t, struct stksess *ts) |
| 98 | { |
| 99 | void *ptr; |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 100 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 101 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR); |
| 102 | if (ptr) |
| 103 | stktable_data_cast(ptr, conn_cur)++; |
| 104 | |
| 105 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT); |
| 106 | if (ptr) |
| 107 | stktable_data_cast(ptr, conn_cnt)++; |
| 108 | |
| 109 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE); |
| 110 | if (ptr) |
| 111 | update_freq_ctr_period(&stktable_data_cast(ptr, conn_rate), |
| 112 | t->data_arg[STKTABLE_DT_CONN_RATE].u, 1); |
| 113 | if (tick_isset(t->expire)) |
| 114 | ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 115 | } |
| 116 | |
| 117 | /* Enable frontend tracking of session counters on stksess <ts>. The caller is |
| 118 | * responsible for ensuring that <t> and <ts> are valid pointers. Some controls |
| 119 | * are performed to ensure the state can still change. |
| 120 | */ |
| 121 | static inline void session_track_fe_counters(struct session *s, struct stktable *t, struct stksess *ts) |
| 122 | { |
| 123 | if (s->fe_tracked_counters) |
| 124 | return; |
| 125 | |
| 126 | ts->ref_cnt++; |
| 127 | s->fe_tracked_table = t; |
| 128 | s->fe_tracked_counters = ts; |
| 129 | session_start_counters(t, ts); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 130 | } |
Krzysztof Piotr Oledzki | 583bc96 | 2007-11-24 22:12:47 +0100 | [diff] [blame] | 131 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 132 | /* Enable backend tracking of session counters on stksess <ts>. The caller is |
| 133 | * responsible for ensuring that <t> and <ts> are valid pointers. Some controls |
| 134 | * are performed to ensure the state can still change. |
| 135 | */ |
| 136 | static inline void session_track_be_counters(struct session *s, struct stktable *t, struct stksess *ts) |
| 137 | { |
| 138 | if (s->be_tracked_counters) |
| 139 | return; |
| 140 | |
| 141 | ts->ref_cnt++; |
| 142 | s->be_tracked_table = t; |
| 143 | s->be_tracked_counters = ts; |
| 144 | session_start_counters(t, ts); |
| 145 | } |
| 146 | |
Willy Tarreau | f853320 | 2008-08-16 14:55:08 +0200 | [diff] [blame] | 147 | static void inline trace_term(struct session *s, unsigned int code) |
| 148 | { |
| 149 | s->term_trace <<= TT_BIT_SHIFT; |
| 150 | s->term_trace |= code; |
| 151 | } |
| 152 | |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 153 | /* Increase the number of cumulated HTTP requests in the tracked counters */ |
| 154 | static void inline session_inc_http_req_ctr(struct session *s) |
| 155 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 156 | void *ptr; |
| 157 | |
| 158 | if (s->be_tracked_counters) { |
| 159 | ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_REQ_CNT); |
| 160 | if (ptr) |
| 161 | stktable_data_cast(ptr, http_req_cnt)++; |
| 162 | |
| 163 | ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_REQ_RATE); |
| 164 | if (ptr) |
| 165 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 166 | s->be_tracked_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
| 167 | } |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 168 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 169 | if (s->fe_tracked_counters) { |
| 170 | ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_REQ_CNT); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 171 | if (ptr) |
| 172 | stktable_data_cast(ptr, http_req_cnt)++; |
| 173 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 174 | ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_REQ_RATE); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 175 | if (ptr) |
| 176 | update_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 177 | s->fe_tracked_table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u, 1); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
| 181 | /* Increase the number of cumulated failed HTTP requests in the tracked |
| 182 | * counters. Only 4xx requests should be counted here so that we can |
| 183 | * distinguish between errors caused by client behaviour and other ones. |
| 184 | * Note that even 404 are interesting because they're generally caused by |
| 185 | * vulnerability scans. |
| 186 | */ |
| 187 | static void inline session_inc_http_err_ctr(struct session *s) |
| 188 | { |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 189 | void *ptr; |
| 190 | |
| 191 | if (s->be_tracked_counters) { |
| 192 | ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_ERR_CNT); |
| 193 | if (ptr) |
| 194 | stktable_data_cast(ptr, http_err_cnt)++; |
| 195 | |
| 196 | ptr = stktable_data_ptr(s->be_tracked_table, s->be_tracked_counters, STKTABLE_DT_HTTP_ERR_RATE); |
| 197 | if (ptr) |
| 198 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 199 | s->be_tracked_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
| 200 | } |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 201 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 202 | if (s->fe_tracked_counters) { |
| 203 | ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_ERR_CNT); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 204 | if (ptr) |
| 205 | stktable_data_cast(ptr, http_err_cnt)++; |
| 206 | |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 207 | ptr = stktable_data_ptr(s->fe_tracked_table, s->fe_tracked_counters, STKTABLE_DT_HTTP_ERR_RATE); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 208 | if (ptr) |
| 209 | update_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
Willy Tarreau | f059a0f | 2010-08-03 16:29:52 +0200 | [diff] [blame] | 210 | s->fe_tracked_table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u, 1); |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 214 | #endif /* _PROTO_SESSION_H */ |
Willy TARREAU | 3dc0644 | 2006-06-15 21:48:13 +0200 | [diff] [blame] | 215 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 216 | /* |
| 217 | * Local variables: |
| 218 | * c-indent-level: 8 |
| 219 | * c-basic-offset: 8 |
| 220 | * End: |
| 221 | */ |