blob: 373ded4dd0831ae00b10a3dfdd0f60c0eb18b3ea [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>
Emeric Brun3bd697e2010-01-04 15:23:48 +010034
35/* stick table key types */
Willy Tarreauaea940e2010-06-06 11:56:36 +020036enum {
37 STKTABLE_TYPE_IP = 0, /* table key is ipv4 */
David du Colombier4f92d322011-03-24 11:09:31 +010038 STKTABLE_TYPE_IPV6, /* table key is ipv6 */
Willy Tarreauaea940e2010-06-06 11:56:36 +020039 STKTABLE_TYPE_INTEGER, /* table key is unsigned 32bit integer */
40 STKTABLE_TYPE_STRING, /* table key is a null terminated string */
Emeric Brun485479d2010-09-23 18:02:19 +020041 STKTABLE_TYPE_BINARY, /* table key is a buffer of data */
Willy Tarreauaea940e2010-06-06 11:56:36 +020042 STKTABLE_TYPES /* Number of types, must always be last */
43};
Emeric Brun3bd697e2010-01-04 15:23:48 +010044
Willy Tarreau08d5f982010-06-06 13:34:54 +020045/* The types of extra data we can store in a stick table */
46enum {
Willy Tarreau87b09662015-04-03 00:22:06 +020047 STKTABLE_DT_SERVER_ID, /* the server ID to use with this stream if > 0 */
Willy Tarreauc3bd9722010-06-20 12:47:25 +020048 STKTABLE_DT_GPC0, /* General Purpose Counter 0 (unsigned 32-bit integer) */
Willy Tarreauba2ffd12013-05-29 15:54:14 +020049 STKTABLE_DT_GPC0_RATE, /* General Purpose Counter 0's event rate */
Willy Tarreau8fb12c42010-06-18 20:16:39 +020050 STKTABLE_DT_CONN_CNT, /* cumulated number of connections */
Willy Tarreau91c43d72010-06-20 11:19:22 +020051 STKTABLE_DT_CONN_RATE, /* incoming connection rate */
Willy Tarreau38285c12010-06-18 16:35:43 +020052 STKTABLE_DT_CONN_CUR, /* concurrent number of connections */
Willy Tarreauf4d17d92010-06-18 22:10:12 +020053 STKTABLE_DT_SESS_CNT, /* cumulated number of sessions (accepted connections) */
Willy Tarreau91c43d72010-06-20 11:19:22 +020054 STKTABLE_DT_SESS_RATE, /* accepted sessions rate */
Willy Tarreauda7ff642010-06-23 11:44:09 +020055 STKTABLE_DT_HTTP_REQ_CNT, /* cumulated number of incoming HTTP requests */
56 STKTABLE_DT_HTTP_REQ_RATE,/* incoming HTTP request rate */
57 STKTABLE_DT_HTTP_ERR_CNT, /* cumulated number of HTTP requests errors (4xx) */
58 STKTABLE_DT_HTTP_ERR_RATE,/* HTTP request error rate */
Willy Tarreau855e4bb2010-06-18 18:33:32 +020059 STKTABLE_DT_BYTES_IN_CNT, /* cumulated bytes count from client to servers */
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020060 STKTABLE_DT_BYTES_IN_RATE,/* bytes rate from client to servers */
Willy Tarreau855e4bb2010-06-18 18:33:32 +020061 STKTABLE_DT_BYTES_OUT_CNT,/* cumulated bytes count from servers to client */
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020062 STKTABLE_DT_BYTES_OUT_RATE,/* bytes rate from servers to client */
Willy Tarreauedee1d62014-07-15 16:44:27 +020063 STKTABLE_STATIC_DATA_TYPES,/* number of types above */
64 /* up to STKTABLE_EXTRA_DATA_TYPES types may be registered here, always
65 * followed by the number of data types, must always be last.
66 */
67 STKTABLE_DATA_TYPES = STKTABLE_STATIC_DATA_TYPES + STKTABLE_EXTRA_DATA_TYPES
Willy Tarreau08d5f982010-06-06 13:34:54 +020068};
69
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020070/* The equivalent standard types of the stored data */
71enum {
72 STD_T_SINT = 0, /* data is of type signed int */
73 STD_T_UINT, /* data is of type unsigned int */
74 STD_T_ULL, /* data is of type unsigned long long */
75 STD_T_FRQP, /* data is of type freq_ctr_period */
76};
77
Willy Tarreau888617d2010-06-20 09:11:39 +020078/* The types of optional arguments to stored data */
79enum {
80 ARG_T_NONE = 0, /* data type takes no argument (default) */
81 ARG_T_INT, /* signed integer */
82 ARG_T_DELAY, /* a delay which supports time units */
83};
84
Willy Tarreau08d5f982010-06-06 13:34:54 +020085/* stick_table extra data. This is mainly used for casting or size computation */
86union stktable_data {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020087 /* standard types for easy casting */
88 int std_t_sint;
89 unsigned int std_t_uint;
90 unsigned long long std_t_ull;
91 struct freq_ctr_period std_t_frqp;
92
93 /* types of each storable data */
Willy Tarreau13c29de2010-06-06 16:40:39 +020094 int server_id;
Willy Tarreauc3bd9722010-06-20 12:47:25 +020095 unsigned int gpc0;
Willy Tarreauba2ffd12013-05-29 15:54:14 +020096 struct freq_ctr_period gpc0_rate;
Willy Tarreau38285c12010-06-18 16:35:43 +020097 unsigned int conn_cnt;
Willy Tarreau91c43d72010-06-20 11:19:22 +020098 struct freq_ctr_period conn_rate;
Willy Tarreau38285c12010-06-18 16:35:43 +020099 unsigned int conn_cur;
Willy Tarreauf4d17d92010-06-18 22:10:12 +0200100 unsigned int sess_cnt;
Willy Tarreau91c43d72010-06-20 11:19:22 +0200101 struct freq_ctr_period sess_rate;
Willy Tarreauda7ff642010-06-23 11:44:09 +0200102 unsigned int http_req_cnt;
103 struct freq_ctr_period http_req_rate;
104 unsigned int http_err_cnt;
105 struct freq_ctr_period http_err_rate;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200106 unsigned long long bytes_in_cnt;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200107 struct freq_ctr_period bytes_in_rate;
Willy Tarreau855e4bb2010-06-18 18:33:32 +0200108 unsigned long long bytes_out_cnt;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +0200109 struct freq_ctr_period bytes_out_rate;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200110};
111
Willy Tarreau08d5f982010-06-06 13:34:54 +0200112/* known data types */
113struct stktable_data_type {
114 const char *name; /* name of the data type */
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200115 int std_type; /* standard type we can use for this data, STD_T_* */
Willy Tarreau888617d2010-06-20 09:11:39 +0200116 int arg_type; /* type of optional argument, ARG_T_* */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200117};
118
Willy Tarreauaea940e2010-06-06 11:56:36 +0200119/* stick table key type flags */
120#define STK_F_CUSTOM_KEYSIZE 0x00000001 /* this table's key size is configurable */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100121
122/* stick table keyword type */
123struct stktable_type {
Willy Tarreauaea940e2010-06-06 11:56:36 +0200124 const char *kw; /* keyword string */
125 int flags; /* type flags */
126 size_t default_size; /* default key size */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100127};
128
Emeric Brun485479d2010-09-23 18:02:19 +0200129extern struct stktable_type stktable_types[];
130
Willy Tarreau393379c2010-06-06 12:11:37 +0200131/* Sticky session.
132 * Any additional data related to the stuck session is installed *before*
133 * stksess (with negative offsets). This allows us to run variable-sized
134 * keys and variable-sized data without making use of intermediate pointers.
135 */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100136struct stksess {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137 unsigned int expire; /* session expiration date */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200138 unsigned int ref_cnt; /* reference count, can only purge when zero */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200139 struct eb32_node exp; /* ebtree node used to hold the session in expiration tree */
Emeric Brun85e77c72010-09-23 18:16:52 +0200140 struct eb32_node upd; /* ebtree node used to hold the update sequence tree */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200141 struct ebmb_node key; /* ebtree node used to hold the session in table */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200142 /* WARNING! do not put anything after <keys>, it's used by the key */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143};
144
Emeric Brun85e77c72010-09-23 18:16:52 +0200145
Emeric Brun3bd697e2010-01-04 15:23:48 +0100146/* stick table */
147struct stktable {
Emeric Brun85e77c72010-09-23 18:16:52 +0200148 char *id; /* table id name */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200149 struct eb_root keys; /* head of sticky session tree */
150 struct eb_root exps; /* head of sticky session expiration tree */
Emeric Brun85e77c72010-09-23 18:16:52 +0200151 struct eb_root updates; /* head of sticky updates sequence tree */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200152 struct pool_head *pool; /* pool used to allocate sticky sessions */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100153 struct task *exp_task; /* expiration task */
Emeric Brun85e77c72010-09-23 18:16:52 +0200154 struct task *sync_task; /* sync task */
155 unsigned int update;
156 unsigned int localupdate;
Emeric Brunaaf58602015-06-15 17:23:30 +0200157 unsigned int commitupdate;/* used to identify the latest local updates
158 pending for sync */
Willy Tarreau3a925c12013-09-04 17:54:01 +0200159 unsigned int syncing; /* number of sync tasks watching this table now */
Emeric Brun32da3c42010-09-23 18:39:19 +0200160 union {
161 struct peers *p; /* sync peers */
162 char *name;
163 } peers;
164
Willy Tarreauaea940e2010-06-06 11:56:36 +0200165 unsigned long type; /* type of table (determines key format) */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100166 size_t key_size; /* size of a key, maximum size in case of string */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200167 unsigned int size; /* maximum number of sticky sessions in table */
168 unsigned int current; /* number of sticky sessions currently in table */
169 int nopurge; /* if non-zero, don't purge sticky sessions when full */
170 int exp_next; /* next expiration date (ticks) */
171 int expire; /* time to live for sticky sessions (milliseconds) */
Willy Tarreau393379c2010-06-06 12:11:37 +0200172 int data_size; /* the size of the data that is prepended *before* stksess */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200173 int data_ofs[STKTABLE_DATA_TYPES]; /* negative offsets of present data types, or 0 if absent */
Willy Tarreau888617d2010-06-20 09:11:39 +0200174 union {
175 int i;
176 unsigned int u;
177 void *p;
178 } data_arg[STKTABLE_DATA_TYPES]; /* optional argument of each data type */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100179};
180
Willy Tarreau795e4a92010-07-18 07:38:17 +0200181extern struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES];
Willy Tarreauaea940e2010-06-06 11:56:36 +0200182
Emeric Brun3bd697e2010-01-04 15:23:48 +0100183/* stick table key data */
184union stktable_key_data {
David du Colombier4f92d322011-03-24 11:09:31 +0100185 struct in_addr ip; /* used to store an ipv4 key */
186 struct in6_addr ipv6; /* used to store an ipv6 key */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 uint32_t integer; /* used to store an integer key */
Willy Tarreau07115412012-10-29 21:56:59 +0100188 char buf[0]; /* dynamically allocated, used to store a null terminated string key or a buffer of data */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189};
190
191/* stick table key */
192struct stktable_key {
193 void *key; /* pointer on key buffer */
194 size_t key_len; /* data len to read in buff in case of null terminated string */
Willy Tarreau07115412012-10-29 21:56:59 +0100195 union stktable_key_data data; /* data, must always be last */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100196};
197
Willy Tarreau87b09662015-04-03 00:22:06 +0200198/* WARNING: if new fields are added, they must be initialized in stream_accept()
199 * and freed in stream_free() !
Willy Tarreau5ed1bbf2014-06-25 18:30:01 +0200200 */
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 */
207struct stkctr {
Willy Tarreau87b09662015-04-03 00:22:06 +0200208 unsigned long entry; /* entry containing counters currently being tracked by this stream */
Willy Tarreau5ed1bbf2014-06-25 18:30:01 +0200209 struct stktable *table; /* table the counters above belong to (undefined if counters are null) */
210};
211
212/* parameters to configure tracked counters */
213struct 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 Brun3bd697e2010-01-04 15:23:48 +0100221#endif /* _TYPES_STICK_TABLE_H */