blob: fba6e24673ec0acde293cca6e54fef9b08e84e54 [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 */
38 STKTABLE_TYPE_INTEGER, /* table key is unsigned 32bit integer */
39 STKTABLE_TYPE_STRING, /* table key is a null terminated string */
40 STKTABLE_TYPES /* Number of types, must always be last */
41};
Emeric Brun3bd697e2010-01-04 15:23:48 +010042
Willy Tarreau08d5f982010-06-06 13:34:54 +020043/* The types of extra data we can store in a stick table */
44enum {
Willy Tarreau13c29de2010-06-06 16:40:39 +020045 STKTABLE_DT_SERVER_ID, /* the server ID to use with this session if > 0 */
Willy Tarreau8fb12c42010-06-18 20:16:39 +020046 STKTABLE_DT_CONN_CNT, /* cumulated number of connections */
Willy Tarreau91c43d72010-06-20 11:19:22 +020047 STKTABLE_DT_CONN_RATE, /* incoming connection rate */
Willy Tarreau38285c12010-06-18 16:35:43 +020048 STKTABLE_DT_CONN_CUR, /* concurrent number of connections */
Willy Tarreauf4d17d92010-06-18 22:10:12 +020049 STKTABLE_DT_SESS_CNT, /* cumulated number of sessions (accepted connections) */
Willy Tarreau91c43d72010-06-20 11:19:22 +020050 STKTABLE_DT_SESS_RATE, /* accepted sessions rate */
Willy Tarreau855e4bb2010-06-18 18:33:32 +020051 STKTABLE_DT_BYTES_IN_CNT, /* cumulated bytes count from client to servers */
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020052 STKTABLE_DT_BYTES_IN_RATE,/* bytes rate from client to servers */
Willy Tarreau855e4bb2010-06-18 18:33:32 +020053 STKTABLE_DT_BYTES_OUT_CNT,/* cumulated bytes count from servers to client */
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020054 STKTABLE_DT_BYTES_OUT_RATE,/* bytes rate from servers to client */
Willy Tarreau08d5f982010-06-06 13:34:54 +020055 STKTABLE_DATA_TYPES /* Number of data types, must always be last */
56};
57
Willy Tarreau888617d2010-06-20 09:11:39 +020058/* The types of optional arguments to stored data */
59enum {
60 ARG_T_NONE = 0, /* data type takes no argument (default) */
61 ARG_T_INT, /* signed integer */
62 ARG_T_DELAY, /* a delay which supports time units */
63};
64
Willy Tarreau08d5f982010-06-06 13:34:54 +020065/* stick_table extra data. This is mainly used for casting or size computation */
66union stktable_data {
Willy Tarreau13c29de2010-06-06 16:40:39 +020067 int server_id;
Willy Tarreau38285c12010-06-18 16:35:43 +020068 unsigned int conn_cnt;
Willy Tarreau91c43d72010-06-20 11:19:22 +020069 struct freq_ctr_period conn_rate;
Willy Tarreau38285c12010-06-18 16:35:43 +020070 unsigned int conn_cur;
Willy Tarreauf4d17d92010-06-18 22:10:12 +020071 unsigned int sess_cnt;
Willy Tarreau91c43d72010-06-20 11:19:22 +020072 struct freq_ctr_period sess_rate;
Willy Tarreau855e4bb2010-06-18 18:33:32 +020073 unsigned long long bytes_in_cnt;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020074 struct freq_ctr_period bytes_in_rate;
Willy Tarreau855e4bb2010-06-18 18:33:32 +020075 unsigned long long bytes_out_cnt;
Willy Tarreau6c59e0a2010-06-20 11:56:30 +020076 struct freq_ctr_period bytes_out_rate;
Willy Tarreau08d5f982010-06-06 13:34:54 +020077};
78
Willy Tarreau08d5f982010-06-06 13:34:54 +020079/* known data types */
80struct stktable_data_type {
81 const char *name; /* name of the data type */
82 int data_length; /* length of this type, or 0 if variable (eg: string) */
Willy Tarreau888617d2010-06-20 09:11:39 +020083 int arg_type; /* type of optional argument, ARG_T_* */
Willy Tarreau08d5f982010-06-06 13:34:54 +020084};
85
Willy Tarreauaea940e2010-06-06 11:56:36 +020086/* stick table key type flags */
87#define STK_F_CUSTOM_KEYSIZE 0x00000001 /* this table's key size is configurable */
Emeric Brun3bd697e2010-01-04 15:23:48 +010088
89/* stick table keyword type */
90struct stktable_type {
Willy Tarreauaea940e2010-06-06 11:56:36 +020091 const char *kw; /* keyword string */
92 int flags; /* type flags */
93 size_t default_size; /* default key size */
Emeric Brun3bd697e2010-01-04 15:23:48 +010094};
95
Willy Tarreau393379c2010-06-06 12:11:37 +020096/* Sticky session.
97 * Any additional data related to the stuck session is installed *before*
98 * stksess (with negative offsets). This allows us to run variable-sized
99 * keys and variable-sized data without making use of intermediate pointers.
100 */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100101struct stksess {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100102 unsigned int expire; /* session expiration date */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200103 unsigned int ref_cnt; /* reference count, can only purge when zero */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200104 struct eb32_node exp; /* ebtree node used to hold the session in expiration tree */
105 struct ebmb_node key; /* ebtree node used to hold the session in table */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200106 /* WARNING! do not put anything after <keys>, it's used by the key */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100107};
108
Emeric Brun3bd697e2010-01-04 15:23:48 +0100109/* stick table */
110struct stktable {
Willy Tarreauaea940e2010-06-06 11:56:36 +0200111 struct eb_root keys; /* head of sticky session tree */
112 struct eb_root exps; /* head of sticky session expiration tree */
113 struct pool_head *pool; /* pool used to allocate sticky sessions */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100114 struct task *exp_task; /* expiration task */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200115 unsigned long type; /* type of table (determines key format) */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100116 size_t key_size; /* size of a key, maximum size in case of string */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200117 unsigned int size; /* maximum number of sticky sessions in table */
118 unsigned int current; /* number of sticky sessions currently in table */
119 int nopurge; /* if non-zero, don't purge sticky sessions when full */
120 int exp_next; /* next expiration date (ticks) */
121 int expire; /* time to live for sticky sessions (milliseconds) */
Willy Tarreau393379c2010-06-06 12:11:37 +0200122 int data_size; /* the size of the data that is prepended *before* stksess */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200123 int data_ofs[STKTABLE_DATA_TYPES]; /* negative offsets of present data types, or 0 if absent */
Willy Tarreau888617d2010-06-20 09:11:39 +0200124 union {
125 int i;
126 unsigned int u;
127 void *p;
128 } data_arg[STKTABLE_DATA_TYPES]; /* optional argument of each data type */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100129};
130
Willy Tarreau08d5f982010-06-06 13:34:54 +0200131struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES];
Willy Tarreauaea940e2010-06-06 11:56:36 +0200132
Emeric Brun3bd697e2010-01-04 15:23:48 +0100133/* stick table key data */
134union stktable_key_data {
135 struct in_addr ip; /* used to store an ip key */
136 uint32_t integer; /* used to store an integer key */
137 char buf[BUFSIZE]; /* used to store a null terminated string key */
138};
139
140/* stick table key */
141struct stktable_key {
142 void *key; /* pointer on key buffer */
143 size_t key_len; /* data len to read in buff in case of null terminated string */
144 union stktable_key_data data; /* data */
145};
146
147#endif /* _TYPES_STICK_TABLE_H */
148