Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/types/stick_table.h |
| 3 | * Macros, variables and structures for stick tables management. |
| 4 | * |
| 5 | * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 6 | * Copyright (C) 2010 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 7 | * |
| 8 | * This library is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public |
| 10 | * License as published by the Free Software Foundation, version 2.1 |
| 11 | * exclusively. |
| 12 | * |
| 13 | * This library is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public |
| 19 | * License along with this library; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 21 | */ |
| 22 | |
| 23 | #ifndef _TYPES_STICK_TABLE_H |
| 24 | #define _TYPES_STICK_TABLE_H |
| 25 | |
| 26 | #include <sys/socket.h> |
| 27 | #include <netinet/in.h> |
| 28 | |
| 29 | #include <ebtree.h> |
| 30 | #include <ebmbtree.h> |
| 31 | #include <eb32tree.h> |
| 32 | #include <common/memory.h> |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 33 | #include <types/freq_ctr.h> |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 34 | #include <types/sample.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 35 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 36 | /* The types of extra data we can store in a stick table */ |
| 37 | enum { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 38 | STKTABLE_DT_SERVER_ID, /* the server ID to use with this stream if > 0 */ |
Thierry FOURNIER | 3cf1111 | 2015-07-28 08:57:05 +0200 | [diff] [blame] | 39 | STKTABLE_DT_GPT0, /* General Purpose Flag 0. */ |
Willy Tarreau | c3bd972 | 2010-06-20 12:47:25 +0200 | [diff] [blame] | 40 | STKTABLE_DT_GPC0, /* General Purpose Counter 0 (unsigned 32-bit integer) */ |
Willy Tarreau | ba2ffd1 | 2013-05-29 15:54:14 +0200 | [diff] [blame] | 41 | STKTABLE_DT_GPC0_RATE, /* General Purpose Counter 0's event rate */ |
Willy Tarreau | 8fb12c4 | 2010-06-18 20:16:39 +0200 | [diff] [blame] | 42 | STKTABLE_DT_CONN_CNT, /* cumulated number of connections */ |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 43 | STKTABLE_DT_CONN_RATE, /* incoming connection rate */ |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 44 | STKTABLE_DT_CONN_CUR, /* concurrent number of connections */ |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 45 | STKTABLE_DT_SESS_CNT, /* cumulated number of sessions (accepted connections) */ |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 46 | STKTABLE_DT_SESS_RATE, /* accepted sessions rate */ |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 47 | STKTABLE_DT_HTTP_REQ_CNT, /* cumulated number of incoming HTTP requests */ |
| 48 | STKTABLE_DT_HTTP_REQ_RATE,/* incoming HTTP request rate */ |
| 49 | STKTABLE_DT_HTTP_ERR_CNT, /* cumulated number of HTTP requests errors (4xx) */ |
| 50 | STKTABLE_DT_HTTP_ERR_RATE,/* HTTP request error rate */ |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 51 | STKTABLE_DT_BYTES_IN_CNT, /* cumulated bytes count from client to servers */ |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 52 | STKTABLE_DT_BYTES_IN_RATE,/* bytes rate from client to servers */ |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 53 | STKTABLE_DT_BYTES_OUT_CNT,/* cumulated bytes count from servers to client */ |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 54 | STKTABLE_DT_BYTES_OUT_RATE,/* bytes rate from servers to client */ |
Frédéric Lécaille | 6778b27 | 2018-01-29 15:22:53 +0100 | [diff] [blame] | 55 | STKTABLE_DT_GPC1, /* General Purpose Counter 1 (unsigned 32-bit integer) */ |
| 56 | STKTABLE_DT_GPC1_RATE, /* General Purpose Counter 1's event rate */ |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 57 | STKTABLE_STATIC_DATA_TYPES,/* number of types above */ |
| 58 | /* up to STKTABLE_EXTRA_DATA_TYPES types may be registered here, always |
| 59 | * followed by the number of data types, must always be last. |
| 60 | */ |
| 61 | STKTABLE_DATA_TYPES = STKTABLE_STATIC_DATA_TYPES + STKTABLE_EXTRA_DATA_TYPES |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 64 | /* The equivalent standard types of the stored data */ |
| 65 | enum { |
| 66 | STD_T_SINT = 0, /* data is of type signed int */ |
| 67 | STD_T_UINT, /* data is of type unsigned int */ |
| 68 | STD_T_ULL, /* data is of type unsigned long long */ |
| 69 | STD_T_FRQP, /* data is of type freq_ctr_period */ |
| 70 | }; |
| 71 | |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 72 | /* The types of optional arguments to stored data */ |
| 73 | enum { |
| 74 | ARG_T_NONE = 0, /* data type takes no argument (default) */ |
| 75 | ARG_T_INT, /* signed integer */ |
| 76 | ARG_T_DELAY, /* a delay which supports time units */ |
| 77 | }; |
| 78 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 79 | /* stick_table extra data. This is mainly used for casting or size computation */ |
| 80 | union stktable_data { |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 81 | /* standard types for easy casting */ |
| 82 | int std_t_sint; |
| 83 | unsigned int std_t_uint; |
| 84 | unsigned long long std_t_ull; |
| 85 | struct freq_ctr_period std_t_frqp; |
| 86 | |
| 87 | /* types of each storable data */ |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 88 | int server_id; |
Thierry FOURNIER | 3cf1111 | 2015-07-28 08:57:05 +0200 | [diff] [blame] | 89 | unsigned int gpt0; |
Willy Tarreau | c3bd972 | 2010-06-20 12:47:25 +0200 | [diff] [blame] | 90 | unsigned int gpc0; |
Willy Tarreau | ba2ffd1 | 2013-05-29 15:54:14 +0200 | [diff] [blame] | 91 | struct freq_ctr_period gpc0_rate; |
Frédéric Lécaille | 6778b27 | 2018-01-29 15:22:53 +0100 | [diff] [blame] | 92 | unsigned int gpc1; |
| 93 | struct freq_ctr_period gpc1_rate; |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 94 | unsigned int conn_cnt; |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 95 | struct freq_ctr_period conn_rate; |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 96 | unsigned int conn_cur; |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 97 | unsigned int sess_cnt; |
Willy Tarreau | 91c43d7 | 2010-06-20 11:19:22 +0200 | [diff] [blame] | 98 | struct freq_ctr_period sess_rate; |
Willy Tarreau | da7ff64 | 2010-06-23 11:44:09 +0200 | [diff] [blame] | 99 | unsigned int http_req_cnt; |
| 100 | struct freq_ctr_period http_req_rate; |
| 101 | unsigned int http_err_cnt; |
| 102 | struct freq_ctr_period http_err_rate; |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 103 | unsigned long long bytes_in_cnt; |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 104 | struct freq_ctr_period bytes_in_rate; |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 105 | unsigned long long bytes_out_cnt; |
Willy Tarreau | 6c59e0a | 2010-06-20 11:56:30 +0200 | [diff] [blame] | 106 | struct freq_ctr_period bytes_out_rate; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 107 | }; |
| 108 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 109 | /* known data types */ |
| 110 | struct stktable_data_type { |
| 111 | const char *name; /* name of the data type */ |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 112 | int std_type; /* standard type we can use for this data, STD_T_* */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 113 | int arg_type; /* type of optional argument, ARG_T_* */ |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 116 | /* stick table key type flags */ |
| 117 | #define STK_F_CUSTOM_KEYSIZE 0x00000001 /* this table's key size is configurable */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 118 | |
| 119 | /* stick table keyword type */ |
| 120 | struct stktable_type { |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 121 | const char *kw; /* keyword string */ |
| 122 | int flags; /* type flags */ |
| 123 | size_t default_size; /* default key size */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 124 | }; |
| 125 | |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 126 | extern struct stktable_type stktable_types[]; |
| 127 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 128 | /* Sticky session. |
| 129 | * Any additional data related to the stuck session is installed *before* |
| 130 | * stksess (with negative offsets). This allows us to run variable-sized |
| 131 | * keys and variable-sized data without making use of intermediate pointers. |
| 132 | */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 133 | struct stksess { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 134 | unsigned int expire; /* session expiration date */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 135 | unsigned int ref_cnt; /* reference count, can only purge when zero */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 136 | __decl_hathreads(HA_RWLOCK_T lock); /* lock related to the table entry */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 137 | struct eb32_node exp; /* ebtree node used to hold the session in expiration tree */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 138 | struct eb32_node upd; /* ebtree node used to hold the update sequence tree */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 139 | struct ebmb_node key; /* ebtree node used to hold the session in table */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 140 | /* WARNING! do not put anything after <keys>, it's used by the key */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 143 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 144 | /* stick table */ |
| 145 | struct stktable { |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 146 | char *id; /* table id name */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 147 | struct eb_root keys; /* head of sticky session tree */ |
| 148 | struct eb_root exps; /* head of sticky session expiration tree */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 149 | struct eb_root updates; /* head of sticky updates sequence tree */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 150 | struct pool_head *pool; /* pool used to allocate sticky sessions */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 151 | __decl_hathreads(HA_SPINLOCK_T lock); /* spin lock related to the table */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 152 | struct task *exp_task; /* expiration task */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 153 | struct task *sync_task; /* sync task */ |
| 154 | unsigned int update; |
| 155 | unsigned int localupdate; |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 156 | unsigned int commitupdate;/* used to identify the latest local updates |
| 157 | pending for sync */ |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 158 | unsigned int syncing; /* number of sync tasks watching this table now */ |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 159 | union { |
| 160 | struct peers *p; /* sync peers */ |
| 161 | char *name; |
| 162 | } peers; |
| 163 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 164 | unsigned long type; /* type of table (determines key format) */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 165 | size_t key_size; /* size of a key, maximum size in case of string */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 166 | unsigned int size; /* maximum number of sticky sessions in table */ |
| 167 | unsigned int current; /* number of sticky sessions currently in table */ |
| 168 | int nopurge; /* if non-zero, don't purge sticky sessions when full */ |
| 169 | int exp_next; /* next expiration date (ticks) */ |
| 170 | int expire; /* time to live for sticky sessions (milliseconds) */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 171 | int data_size; /* the size of the data that is prepended *before* stksess */ |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 172 | int data_ofs[STKTABLE_DATA_TYPES]; /* negative offsets of present data types, or 0 if absent */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 173 | union { |
| 174 | int i; |
| 175 | unsigned int u; |
| 176 | void *p; |
| 177 | } data_arg[STKTABLE_DATA_TYPES]; /* optional argument of each data type */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 178 | }; |
| 179 | |
Willy Tarreau | 795e4a9 | 2010-07-18 07:38:17 +0200 | [diff] [blame] | 180 | extern struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES]; |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 181 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 182 | /* stick table key */ |
| 183 | struct stktable_key { |
| 184 | void *key; /* pointer on key buffer */ |
| 185 | size_t key_len; /* data len to read in buff in case of null terminated string */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 186 | }; |
| 187 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 188 | /* WARNING: if new fields are added, they must be initialized in stream_accept() |
| 189 | * and freed in stream_free() ! |
Ruoshan Huang | e4edc6b | 2016-07-14 15:07:45 +0800 | [diff] [blame] | 190 | * |
| 191 | * What's the purpose of there two macro: |
| 192 | * - STKCTR_TRACK_BACKEND indicates that a tracking pointer was set from the backend |
| 193 | * and thus that when a keep-alive request goes to another backend, the track |
| 194 | * must cease. |
| 195 | * |
| 196 | * - STKCTR_TRACK_CONTENT indicates that the tracking pointer was set in a |
| 197 | * content-aware rule (tcp-request content or http-request) and that the |
| 198 | * tracking has to be performed in the stream and not in the session, and |
| 199 | * will cease for a new keep-alive request over the same connection. |
Willy Tarreau | 5ed1bbf | 2014-06-25 18:30:01 +0200 | [diff] [blame] | 200 | */ |
| 201 | #define STKCTR_TRACK_BACKEND 1 |
| 202 | #define STKCTR_TRACK_CONTENT 2 |
| 203 | |
| 204 | /* stick counter. The <entry> member is a composite address (caddr) made of a |
| 205 | * pointer to an stksess struct, and two flags among STKCTR_TRACK_* above. |
| 206 | */ |
| 207 | struct stkctr { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 208 | unsigned long entry; /* entry containing counters currently being tracked by this stream */ |
Willy Tarreau | 5ed1bbf | 2014-06-25 18:30:01 +0200 | [diff] [blame] | 209 | struct stktable *table; /* table the counters above belong to (undefined if counters are null) */ |
| 210 | }; |
| 211 | |
| 212 | /* parameters to configure tracked counters */ |
| 213 | struct track_ctr_prm { |
| 214 | struct sample_expr *expr; /* expression used as the key */ |
| 215 | union { |
| 216 | struct stktable *t; /* a pointer to the table */ |
| 217 | char *n; /* or its name during parsing. */ |
| 218 | } table; |
| 219 | }; |
| 220 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 221 | #endif /* _TYPES_STICK_TABLE_H */ |