blob: a43c1101367aff3f56f693adae827ec50a35a143 [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>
33
34/* stick table key types */
Willy Tarreauaea940e2010-06-06 11:56:36 +020035enum {
36 STKTABLE_TYPE_IP = 0, /* table key is ipv4 */
37 STKTABLE_TYPE_INTEGER, /* table key is unsigned 32bit integer */
38 STKTABLE_TYPE_STRING, /* table key is a null terminated string */
39 STKTABLE_TYPES /* Number of types, must always be last */
40};
Emeric Brun3bd697e2010-01-04 15:23:48 +010041
Willy Tarreau08d5f982010-06-06 13:34:54 +020042/* The types of extra data we can store in a stick table */
43enum {
Willy Tarreau69b870f2010-06-06 14:30:13 +020044 STKTABLE_DT_CONN_CUM, /* cumulated number of connections */
Willy Tarreau08d5f982010-06-06 13:34:54 +020045 STKTABLE_DATA_TYPES /* Number of data types, must always be last */
46};
47
48/* stick_table extra data. This is mainly used for casting or size computation */
49union stktable_data {
Willy Tarreau69b870f2010-06-06 14:30:13 +020050 unsigned int conn_cum;
Willy Tarreau08d5f982010-06-06 13:34:54 +020051};
52
53#define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type))
54#define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type
55
56/* known data types */
57struct stktable_data_type {
58 const char *name; /* name of the data type */
59 int data_length; /* length of this type, or 0 if variable (eg: string) */
60};
61
Willy Tarreauaea940e2010-06-06 11:56:36 +020062/* stick table key type flags */
63#define STK_F_CUSTOM_KEYSIZE 0x00000001 /* this table's key size is configurable */
Emeric Brun3bd697e2010-01-04 15:23:48 +010064
65/* stick table keyword type */
66struct stktable_type {
Willy Tarreauaea940e2010-06-06 11:56:36 +020067 const char *kw; /* keyword string */
68 int flags; /* type flags */
69 size_t default_size; /* default key size */
Emeric Brun3bd697e2010-01-04 15:23:48 +010070};
71
Willy Tarreau393379c2010-06-06 12:11:37 +020072/* Sticky session.
73 * Any additional data related to the stuck session is installed *before*
74 * stksess (with negative offsets). This allows us to run variable-sized
75 * keys and variable-sized data without making use of intermediate pointers.
76 */
Emeric Brun3bd697e2010-01-04 15:23:48 +010077struct stksess {
Willy Tarreauaea940e2010-06-06 11:56:36 +020078 int sid; /* id of server to use for this session */
Emeric Brun3bd697e2010-01-04 15:23:48 +010079 unsigned int expire; /* session expiration date */
Willy Tarreau86257dc2010-06-06 12:57:10 +020080 struct eb32_node exp; /* ebtree node used to hold the session in expiration tree */
81 struct ebmb_node key; /* ebtree node used to hold the session in table */
Willy Tarreauaea940e2010-06-06 11:56:36 +020082 /* WARNING! do not put anything after <keys>, it's used by the key */
Emeric Brun3bd697e2010-01-04 15:23:48 +010083};
84
Emeric Brun3bd697e2010-01-04 15:23:48 +010085/* stick table */
86struct stktable {
Willy Tarreauaea940e2010-06-06 11:56:36 +020087 struct eb_root keys; /* head of sticky session tree */
88 struct eb_root exps; /* head of sticky session expiration tree */
89 struct pool_head *pool; /* pool used to allocate sticky sessions */
Emeric Brun3bd697e2010-01-04 15:23:48 +010090 struct task *exp_task; /* expiration task */
Willy Tarreauaea940e2010-06-06 11:56:36 +020091 unsigned long type; /* type of table (determines key format) */
Emeric Brun3bd697e2010-01-04 15:23:48 +010092 size_t key_size; /* size of a key, maximum size in case of string */
Willy Tarreauaea940e2010-06-06 11:56:36 +020093 unsigned int size; /* maximum number of sticky sessions in table */
94 unsigned int current; /* number of sticky sessions currently in table */
95 int nopurge; /* if non-zero, don't purge sticky sessions when full */
96 int exp_next; /* next expiration date (ticks) */
97 int expire; /* time to live for sticky sessions (milliseconds) */
Willy Tarreau393379c2010-06-06 12:11:37 +020098 int data_size; /* the size of the data that is prepended *before* stksess */
Willy Tarreau08d5f982010-06-06 13:34:54 +020099 int data_ofs[STKTABLE_DATA_TYPES]; /* negative offsets of present data types, or 0 if absent */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100100};
101
Willy Tarreau08d5f982010-06-06 13:34:54 +0200102struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES];
Willy Tarreauaea940e2010-06-06 11:56:36 +0200103
Emeric Brun3bd697e2010-01-04 15:23:48 +0100104/* stick table key data */
105union stktable_key_data {
106 struct in_addr ip; /* used to store an ip key */
107 uint32_t integer; /* used to store an integer key */
108 char buf[BUFSIZE]; /* used to store a null terminated string key */
109};
110
111/* stick table key */
112struct stktable_key {
113 void *key; /* pointer on key buffer */
114 size_t key_len; /* data len to read in buff in case of null terminated string */
115 union stktable_key_data data; /* data */
116};
117
118#endif /* _TYPES_STICK_TABLE_H */
119