blob: 384a0a853aa3fee264aad04fa8083ef51d567b29 [file] [log] [blame]
Emeric Brun3bd697e2010-01-04 15:23:48 +01001/*
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 Tarreau08d5f982010-06-06 13:34:54 +02006 * Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
Emeric Brun3bd697e2010-01-04 15:23:48 +01007 *
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 Tarreau91c43d72010-06-20 11:19:22 +020033#include <types/freq_ctr.h>
Frédéric Lécaillef92da382019-03-15 11:24:53 +010034#include <types/peers.h>
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +020035#include <types/sample.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010036
Willy Tarreau08d5f982010-06-06 13:34:54 +020037/* The types of extra data we can store in a stick table */
38enum {
Willy Tarreau87b09662015-04-03 00:22:06 +020039 STKTABLE_DT_SERVER_ID, /* the server ID to use with this stream if > 0 */
Thierry FOURNIER3cf11112015-07-28 08:57:05 +020040 STKTABLE_DT_GPT0, /* General Purpose Flag 0. */
Willy Tarreauc3bd9722010-06-20 12:47:25 +020041 STKTABLE_DT_GPC0, /* General Purpose Counter 0 (unsigned 32-bit integer) */
Willy Tarreauba2ffd12013-05-29 15:54:14 +020042 STKTABLE_DT_GPC0_RATE, /* General Purpose Counter 0's event rate */
Willy Tarreau8fb12c42010-06-18 20:16:39 +020043 STKTABLE_DT_CONN_CNT, /* cumulated number of connections */
Willy Tarreau91c43d72010-06-20 11:19:22 +020044 STKTABLE_DT_CONN_RATE, /* incoming connection rate */
Willy Tarreau38285c12010-06-18 16:35:43 +020045 STKTABLE_DT_CONN_CUR, /* concurrent number of connections */
Willy Tarreauf4d17d92010-06-18 22:10:12 +020046 STKTABLE_DT_SESS_CNT, /* cumulated number of sessions (accepted connections) */
Willy Tarreau91c43d72010-06-20 11:19:22 +020047 STKTABLE_DT_SESS_RATE, /* accepted sessions rate */
Willy Tarreauda7ff642010-06-23 11:44:09 +020048 STKTABLE_DT_HTTP_REQ_CNT, /* cumulated number of incoming HTTP requests */
49 STKTABLE_DT_HTTP_REQ_RATE,/* incoming HTTP request rate */
50 STKTABLE_DT_HTTP_ERR_CNT, /* cumulated number of HTTP requests errors (4xx) */
51 STKTABLE_DT_HTTP_ERR_RATE,/* HTTP request error rate */
Willy Tarreau855e4bb2010-06-18 18:33:32 +020052 STKTABLE_DT_BYTES_IN_CNT, /* cumulated bytes count from client to servers */
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020053 STKTABLE_DT_BYTES_IN_RATE,/* bytes rate from client to servers */
Willy Tarreau855e4bb2010-06-18 18:33:32 +020054 STKTABLE_DT_BYTES_OUT_CNT,/* cumulated bytes count from servers to client */
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020055 STKTABLE_DT_BYTES_OUT_RATE,/* bytes rate from servers to client */
Frédéric Lécaille6778b272018-01-29 15:22:53 +010056 STKTABLE_DT_GPC1, /* General Purpose Counter 1 (unsigned 32-bit integer) */
57 STKTABLE_DT_GPC1_RATE, /* General Purpose Counter 1's event rate */
Willy Tarreauedee1d62014-07-15 16:44:27 +020058 STKTABLE_STATIC_DATA_TYPES,/* number of types above */
59 /* up to STKTABLE_EXTRA_DATA_TYPES types may be registered here, always
60 * followed by the number of data types, must always be last.
61 */
62 STKTABLE_DATA_TYPES = STKTABLE_STATIC_DATA_TYPES + STKTABLE_EXTRA_DATA_TYPES
Willy Tarreau08d5f982010-06-06 13:34:54 +020063};
64
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020065/* The equivalent standard types of the stored data */
66enum {
67 STD_T_SINT = 0, /* data is of type signed int */
68 STD_T_UINT, /* data is of type unsigned int */
69 STD_T_ULL, /* data is of type unsigned long long */
70 STD_T_FRQP, /* data is of type freq_ctr_period */
71};
72
Willy Tarreau888617d2010-06-20 09:11:39 +020073/* The types of optional arguments to stored data */
74enum {
75 ARG_T_NONE = 0, /* data type takes no argument (default) */
76 ARG_T_INT, /* signed integer */
77 ARG_T_DELAY, /* a delay which supports time units */
78};
79
Willy Tarreau08d5f982010-06-06 13:34:54 +020080/* stick_table extra data. This is mainly used for casting or size computation */
81union stktable_data {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020082 /* standard types for easy casting */
83 int std_t_sint;
84 unsigned int std_t_uint;
85 unsigned long long std_t_ull;
86 struct freq_ctr_period std_t_frqp;
87
88 /* types of each storable data */
Willy Tarreau13c29de2010-06-06 16:40:39 +020089 int server_id;
Thierry FOURNIER3cf11112015-07-28 08:57:05 +020090 unsigned int gpt0;
Willy Tarreauc3bd9722010-06-20 12:47:25 +020091 unsigned int gpc0;
Willy Tarreauba2ffd12013-05-29 15:54:14 +020092 struct freq_ctr_period gpc0_rate;
Frédéric Lécaille6778b272018-01-29 15:22:53 +010093 unsigned int gpc1;
94 struct freq_ctr_period gpc1_rate;
Willy Tarreau38285c12010-06-18 16:35:43 +020095 unsigned int conn_cnt;
Willy Tarreau91c43d72010-06-20 11:19:22 +020096 struct freq_ctr_period conn_rate;
Willy Tarreau38285c12010-06-18 16:35:43 +020097 unsigned int conn_cur;
Willy Tarreauf4d17d92010-06-18 22:10:12 +020098 unsigned int sess_cnt;
Willy Tarreau91c43d72010-06-20 11:19:22 +020099 struct freq_ctr_period sess_rate;
Willy Tarreauda7ff642010-06-23 11:44:09 +0200100 unsigned int http_req_cnt;
101 struct freq_ctr_period http_req_rate;
102 unsigned int http_err_cnt;
103 struct freq_ctr_period http_err_rate;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200104 unsigned long long bytes_in_cnt;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200105 struct freq_ctr_period bytes_in_rate;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200106 unsigned long long bytes_out_cnt;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200107 struct freq_ctr_period bytes_out_rate;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200108};
109
Willy Tarreau08d5f982010-06-06 13:34:54 +0200110/* known data types */
111struct stktable_data_type {
112 const char *name; /* name of the data type */
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200113 int std_type; /* standard type we can use for this data, STD_T_* */
Willy Tarreau888617d2010-06-20 09:11:39 +0200114 int arg_type; /* type of optional argument, ARG_T_* */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200115};
116
Willy Tarreauaea940e2010-06-06 11:56:36 +0200117/* stick table key type flags */
118#define STK_F_CUSTOM_KEYSIZE 0x00000001 /* this table's key size is configurable */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100119
120/* stick table keyword type */
121struct stktable_type {
Willy Tarreauaea940e2010-06-06 11:56:36 +0200122 const char *kw; /* keyword string */
123 int flags; /* type flags */
124 size_t default_size; /* default key size */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100125};
126
Emeric Brun485479d2010-09-23 18:02:19 +0200127extern struct stktable_type stktable_types[];
128
Willy Tarreau393379c2010-06-06 12:11:37 +0200129/* Sticky session.
130 * Any additional data related to the stuck session is installed *before*
131 * stksess (with negative offsets). This allows us to run variable-sized
132 * keys and variable-sized data without making use of intermediate pointers.
133 */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100134struct stksess {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100135 unsigned int expire; /* session expiration date */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200136 unsigned int ref_cnt; /* reference count, can only purge when zero */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100137 __decl_hathreads(HA_RWLOCK_T lock); /* lock related to the table entry */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200138 struct eb32_node exp; /* ebtree node used to hold the session in expiration tree */
Emeric Brun85e77c72010-09-23 18:16:52 +0200139 struct eb32_node upd; /* ebtree node used to hold the update sequence tree */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200140 struct ebmb_node key; /* ebtree node used to hold the session in table */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200141 /* WARNING! do not put anything after <keys>, it's used by the key */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100142};
143
Emeric Brun85e77c72010-09-23 18:16:52 +0200144
Emeric Brun3bd697e2010-01-04 15:23:48 +0100145/* stick table */
146struct stktable {
Frédéric Lécaillec02766a2019-03-20 15:06:55 +0100147 char *id; /* local table id name. */
148 char *nid; /* table id name sent over the network with peers protocol. */
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100149 struct stktable *next; /* The stick-table may be linked when belonging to
150 * the same configuration section.
151 */
152 struct {
153 const char *file; /* The file where the stick-table is declared. */
154 int line; /* The line in this <file> the stick-table is declared. */
155 } conf;
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +0100156 struct ebpt_node name; /* Stick-table are lookup by name here. */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200157 struct eb_root keys; /* head of sticky session tree */
158 struct eb_root exps; /* head of sticky session expiration tree */
Emeric Brun85e77c72010-09-23 18:16:52 +0200159 struct eb_root updates; /* head of sticky updates sequence tree */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200160 struct pool_head *pool; /* pool used to allocate sticky sessions */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100161 __decl_hathreads(HA_SPINLOCK_T lock); /* spin lock related to the table */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100162 struct task *exp_task; /* expiration task */
Emeric Brun85e77c72010-09-23 18:16:52 +0200163 struct task *sync_task; /* sync task */
164 unsigned int update;
165 unsigned int localupdate;
Emeric Brunaaf58602015-06-15 17:23:30 +0200166 unsigned int commitupdate;/* used to identify the latest local updates
167 pending for sync */
Willy Tarreau3a925c12013-09-04 17:54:01 +0200168 unsigned int syncing; /* number of sync tasks watching this table now */
Emeric Brun32da3c42010-09-23 18:39:19 +0200169 union {
170 struct peers *p; /* sync peers */
171 char *name;
172 } peers;
173
Willy Tarreauaea940e2010-06-06 11:56:36 +0200174 unsigned long type; /* type of table (determines key format) */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100175 size_t key_size; /* size of a key, maximum size in case of string */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200176 unsigned int size; /* maximum number of sticky sessions in table */
177 unsigned int current; /* number of sticky sessions currently in table */
178 int nopurge; /* if non-zero, don't purge sticky sessions when full */
179 int exp_next; /* next expiration date (ticks) */
180 int expire; /* time to live for sticky sessions (milliseconds) */
Willy Tarreau393379c2010-06-06 12:11:37 +0200181 int data_size; /* the size of the data that is prepended *before* stksess */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200182 int data_ofs[STKTABLE_DATA_TYPES]; /* negative offsets of present data types, or 0 if absent */
Willy Tarreau888617d2010-06-20 09:11:39 +0200183 union {
184 int i;
185 unsigned int u;
186 void *p;
187 } data_arg[STKTABLE_DATA_TYPES]; /* optional argument of each data type */
Frédéric Lécailled456aa42019-03-08 14:47:00 +0100188 struct proxy *proxy; /* The proxy this stick-table is attached to, if any.*/
Frédéric Lécaille015e4d72019-03-19 14:55:01 +0100189 struct proxy *proxies_list; /* The list of proxies which reference this stick-table. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100190};
191
Willy Tarreau795e4a92010-07-18 07:38:17 +0200192extern struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES];
Willy Tarreauaea940e2010-06-06 11:56:36 +0200193
Emeric Brun3bd697e2010-01-04 15:23:48 +0100194/* stick table key */
195struct stktable_key {
196 void *key; /* pointer on key buffer */
197 size_t key_len; /* data len to read in buff in case of null terminated string */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100198};
199
Willy Tarreau87b09662015-04-03 00:22:06 +0200200/* WARNING: if new fields are added, they must be initialized in stream_accept()
201 * and freed in stream_free() !
Ruoshan Huange4edc6b2016-07-14 15:07:45 +0800202 *
203 * What's the purpose of there two macro:
204 * - STKCTR_TRACK_BACKEND indicates that a tracking pointer was set from the backend
205 * and thus that when a keep-alive request goes to another backend, the track
206 * must cease.
207 *
208 * - STKCTR_TRACK_CONTENT indicates that the tracking pointer was set in a
209 * content-aware rule (tcp-request content or http-request) and that the
210 * tracking has to be performed in the stream and not in the session, and
211 * will cease for a new keep-alive request over the same connection.
Willy Tarreau5ed1bbf2014-06-25 18:30:01 +0200212 */
213#define STKCTR_TRACK_BACKEND 1
214#define STKCTR_TRACK_CONTENT 2
215
216/* stick counter. The <entry> member is a composite address (caddr) made of a
217 * pointer to an stksess struct, and two flags among STKCTR_TRACK_* above.
218 */
219struct stkctr {
Willy Tarreau87b09662015-04-03 00:22:06 +0200220 unsigned long entry; /* entry containing counters currently being tracked by this stream */
Willy Tarreau5ed1bbf2014-06-25 18:30:01 +0200221 struct stktable *table; /* table the counters above belong to (undefined if counters are null) */
222};
223
224/* parameters to configure tracked counters */
225struct track_ctr_prm {
226 struct sample_expr *expr; /* expression used as the key */
227 union {
228 struct stktable *t; /* a pointer to the table */
229 char *n; /* or its name during parsing. */
230 } table;
231};
232
Emeric Brun3bd697e2010-01-04 15:23:48 +0100233#endif /* _TYPES_STICK_TABLE_H */