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> |
| 33 | |
| 34 | /* stick table key types */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 35 | enum { |
| 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 Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 41 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 42 | /* The types of extra data we can store in a stick table */ |
| 43 | enum { |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 44 | STKTABLE_DT_SERVER_ID, /* the server ID to use with this session if > 0 */ |
Willy Tarreau | 8fb12c4 | 2010-06-18 20:16:39 +0200 | [diff] [blame] | 45 | STKTABLE_DT_CONN_CNT, /* cumulated number of connections */ |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 46 | STKTABLE_DT_CONN_CUR, /* concurrent number of connections */ |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 47 | STKTABLE_DT_SESS_CNT, /* cumulated number of sessions (accepted connections) */ |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 48 | STKTABLE_DT_BYTES_IN_CNT, /* cumulated bytes count from client to servers */ |
| 49 | STKTABLE_DT_BYTES_OUT_CNT,/* cumulated bytes count from servers to client */ |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 50 | STKTABLE_DATA_TYPES /* Number of data types, must always be last */ |
| 51 | }; |
| 52 | |
| 53 | /* stick_table extra data. This is mainly used for casting or size computation */ |
| 54 | union stktable_data { |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame] | 55 | int server_id; |
Willy Tarreau | 38285c1 | 2010-06-18 16:35:43 +0200 | [diff] [blame] | 56 | unsigned int conn_cnt; |
| 57 | unsigned int conn_cur; |
Willy Tarreau | f4d17d9 | 2010-06-18 22:10:12 +0200 | [diff] [blame] | 58 | unsigned int sess_cnt; |
Willy Tarreau | 855e4bb | 2010-06-18 18:33:32 +0200 | [diff] [blame] | 59 | unsigned long long bytes_in_cnt; |
| 60 | unsigned long long bytes_out_cnt; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 61 | }; |
| 62 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 63 | /* known data types */ |
| 64 | struct stktable_data_type { |
| 65 | const char *name; /* name of the data type */ |
| 66 | int data_length; /* length of this type, or 0 if variable (eg: string) */ |
| 67 | }; |
| 68 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 69 | /* stick table key type flags */ |
| 70 | #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] | 71 | |
| 72 | /* stick table keyword type */ |
| 73 | struct stktable_type { |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 74 | const char *kw; /* keyword string */ |
| 75 | int flags; /* type flags */ |
| 76 | size_t default_size; /* default key size */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 77 | }; |
| 78 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 79 | /* Sticky session. |
| 80 | * Any additional data related to the stuck session is installed *before* |
| 81 | * stksess (with negative offsets). This allows us to run variable-sized |
| 82 | * keys and variable-sized data without making use of intermediate pointers. |
| 83 | */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 84 | struct stksess { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 85 | unsigned int expire; /* session expiration date */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 86 | unsigned int ref_cnt; /* reference count, can only purge when zero */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 87 | struct eb32_node exp; /* ebtree node used to hold the session in expiration tree */ |
| 88 | 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] | 89 | /* 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] | 90 | }; |
| 91 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 92 | /* stick table */ |
| 93 | struct stktable { |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 94 | struct eb_root keys; /* head of sticky session tree */ |
| 95 | struct eb_root exps; /* head of sticky session expiration tree */ |
| 96 | struct pool_head *pool; /* pool used to allocate sticky sessions */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 97 | struct task *exp_task; /* expiration task */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 98 | unsigned long type; /* type of table (determines key format) */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 99 | 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] | 100 | unsigned int size; /* maximum number of sticky sessions in table */ |
| 101 | unsigned int current; /* number of sticky sessions currently in table */ |
| 102 | int nopurge; /* if non-zero, don't purge sticky sessions when full */ |
| 103 | int exp_next; /* next expiration date (ticks) */ |
| 104 | int expire; /* time to live for sticky sessions (milliseconds) */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 105 | 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] | 106 | int data_ofs[STKTABLE_DATA_TYPES]; /* negative offsets of present data types, or 0 if absent */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 107 | }; |
| 108 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 109 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES]; |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 110 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 111 | /* stick table key data */ |
| 112 | union stktable_key_data { |
| 113 | struct in_addr ip; /* used to store an ip key */ |
| 114 | uint32_t integer; /* used to store an integer key */ |
| 115 | char buf[BUFSIZE]; /* used to store a null terminated string key */ |
| 116 | }; |
| 117 | |
| 118 | /* stick table key */ |
| 119 | struct stktable_key { |
| 120 | void *key; /* pointer on key buffer */ |
| 121 | size_t key_len; /* data len to read in buff in case of null terminated string */ |
| 122 | union stktable_key_data data; /* data */ |
| 123 | }; |
| 124 | |
| 125 | #endif /* _TYPES_STICK_TABLE_H */ |
| 126 | |