Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * include/proto/stick_table.h |
| 3 | * Functions 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 _PROTO_STICK_TABLE_H |
| 24 | #define _PROTO_STICK_TABLE_H |
| 25 | |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 26 | #include <common/errors.h> |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 27 | #include <common/ticks.h> |
| 28 | #include <common/time.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 29 | #include <types/stick_table.h> |
Frédéric Lécaille | 5ad57ea | 2019-05-17 10:08:29 +0200 | [diff] [blame] | 30 | #include <types/dict.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 31 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 32 | extern struct stktable *stktables_list; |
| 33 | |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 34 | #define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type)) |
| 35 | #define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type |
| 36 | |
Frédéric Lécaille | 1b8e68e | 2019-03-14 07:07:41 +0100 | [diff] [blame] | 37 | void stktable_store_name(struct stktable *t); |
| 38 | struct stktable *stktable_find_by_name(const char *name); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 39 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key); |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 40 | void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 41 | void stksess_free(struct stktable *t, struct stksess *ts); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 42 | int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcount); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 43 | |
| 44 | int stktable_init(struct stktable *t); |
| 45 | int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size); |
Frédéric Lécaille | d456aa4 | 2019-03-08 14:47:00 +0100 | [diff] [blame] | 46 | int parse_stick_table(const char *file, int linenum, char **args, |
Frédéric Lécaille | c02766a | 2019-03-20 15:06:55 +0100 | [diff] [blame] | 47 | struct stktable *t, char *id, char *nid, struct peers *peers); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 48 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 49 | struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts); |
| 50 | void stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int decrefcount, int expire); |
| 51 | void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt); |
| 52 | void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefccount); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 53 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts); |
| 54 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 55 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key); |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 56 | struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 57 | struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, |
Willy Tarreau | 15e91e1 | 2015-04-04 00:52:09 +0200 | [diff] [blame] | 58 | struct stream *strm, unsigned int opt, |
| 59 | struct sample_expr *expr, struct sample *smp); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 60 | struct stkctr *smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw, struct stkctr *stkctr); |
| 61 | struct stkctr *smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw, struct stkctr *stkctr); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 62 | int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type); |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 63 | int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 64 | int stktable_get_data_type(char *name); |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 65 | int stktable_trash_oldest(struct stktable *t, int to_batch); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 66 | int __stksess_kill(struct stktable *t, struct stksess *ts); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 68 | /* return allocation size for standard data type <type> */ |
| 69 | static inline int stktable_type_size(int type) |
| 70 | { |
| 71 | switch(type) { |
| 72 | case STD_T_SINT: |
| 73 | case STD_T_UINT: |
| 74 | return sizeof(int); |
| 75 | case STD_T_ULL: |
| 76 | return sizeof(unsigned long long); |
| 77 | case STD_T_FRQP: |
| 78 | return sizeof(struct freq_ctr_period); |
Frédéric Lécaille | 5ad57ea | 2019-05-17 10:08:29 +0200 | [diff] [blame] | 79 | case STD_T_DICT: |
| 80 | return sizeof(struct dict_entry *); |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 81 | } |
| 82 | return 0; |
| 83 | } |
| 84 | |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 85 | /* reserve some space for data type <type>, and associate argument at <sa> if |
| 86 | * not NULL. Returns PE_NONE (0) if OK or an error code among : |
| 87 | * - PE_ENUM_OOR if <type> does not exist |
| 88 | * - PE_EXIST if <type> is already registered |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 89 | * - PE_ARG_NOT_USE if <sa> was provided but not expected |
| 90 | * - PE_ARG_MISSING if <sa> was expected but not provided |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 91 | */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 92 | static inline int stktable_alloc_data_type(struct stktable *t, int type, const char *sa) |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 93 | { |
| 94 | if (type >= STKTABLE_DATA_TYPES) |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 95 | return PE_ENUM_OOR; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 96 | |
| 97 | if (t->data_ofs[type]) |
| 98 | /* already allocated */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 99 | return PE_EXIST; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 100 | |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 101 | switch (stktable_data_types[type].arg_type) { |
| 102 | case ARG_T_NONE: |
| 103 | if (sa) |
| 104 | return PE_ARG_NOT_USED; |
| 105 | break; |
| 106 | case ARG_T_INT: |
| 107 | if (!sa) |
| 108 | return PE_ARG_MISSING; |
| 109 | t->data_arg[type].i = atoi(sa); |
| 110 | break; |
| 111 | case ARG_T_DELAY: |
| 112 | if (!sa) |
| 113 | return PE_ARG_MISSING; |
| 114 | sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS); |
| 115 | if (sa) |
| 116 | return PE_ARG_INVC; /* invalid char */ |
| 117 | break; |
| 118 | } |
| 119 | |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 120 | t->data_size += stktable_type_size(stktable_data_types[type].std_type); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 121 | t->data_ofs[type] = -t->data_size; |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 122 | return PE_NONE; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 123 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | babc15e | 2018-09-20 11:06:33 +0200 | [diff] [blame] | 125 | /* return pointer for data type <type> in sticky session <ts> of table <t>, all |
| 126 | * of which must exist (otherwise use stktable_data_ptr() if unsure). |
| 127 | */ |
| 128 | static inline void *__stktable_data_ptr(struct stktable *t, struct stksess *ts, int type) |
| 129 | { |
| 130 | return (void *)ts + t->data_ofs[type]; |
| 131 | } |
| 132 | |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 133 | /* return pointer for data type <type> in sticky session <ts> of table <t>, or |
| 134 | * NULL if either <ts> is NULL or the type is not stored. |
| 135 | */ |
| 136 | static inline void *stktable_data_ptr(struct stktable *t, struct stksess *ts, int type) |
| 137 | { |
| 138 | if (type >= STKTABLE_DATA_TYPES) |
| 139 | return NULL; |
| 140 | |
| 141 | if (!t->data_ofs[type]) /* type not stored */ |
| 142 | return NULL; |
| 143 | |
| 144 | if (!ts) |
| 145 | return NULL; |
| 146 | |
Willy Tarreau | babc15e | 2018-09-20 11:06:33 +0200 | [diff] [blame] | 147 | return __stktable_data_ptr(t, ts, type); |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 148 | } |
| 149 | |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 150 | /* kill an entry if it's expired and its ref_cnt is zero */ |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 151 | static inline int __stksess_kill_if_expired(struct stktable *t, struct stksess *ts) |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 152 | { |
Emeric Brun | c89a572 | 2010-09-23 18:11:05 +0200 | [diff] [blame] | 153 | if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms)) |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 154 | return __stksess_kill(t, ts); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Willy Tarreau | a5e0590 | 2017-10-31 15:45:42 +0100 | [diff] [blame] | 159 | static inline void stksess_kill_if_expired(struct stktable *t, struct stksess *ts, int decrefcnt) |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 160 | { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 161 | HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 162 | |
| 163 | if (decrefcnt) |
| 164 | ts->ref_cnt--; |
| 165 | |
| 166 | if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms)) |
Willy Tarreau | a5e0590 | 2017-10-31 15:45:42 +0100 | [diff] [blame] | 167 | __stksess_kill_if_expired(t, ts); |
Emeric Brun | 819fc6f | 2017-06-13 19:37:32 +0200 | [diff] [blame] | 168 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 169 | HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 170 | } |
| 171 | |
Willy Tarreau | 7698c90 | 2015-04-04 16:24:42 +0200 | [diff] [blame] | 172 | /* sets the stick counter's entry pointer */ |
| 173 | static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry) |
| 174 | { |
| 175 | stkctr->entry = caddr_from_ptr(entry, 0); |
| 176 | } |
| 177 | |
| 178 | /* returns the entry pointer from a stick counter */ |
| 179 | static inline struct stksess *stkctr_entry(struct stkctr *stkctr) |
| 180 | { |
| 181 | return caddr_to_ptr(stkctr->entry); |
| 182 | } |
| 183 | |
| 184 | /* returns the two flags from a stick counter */ |
| 185 | static inline unsigned int stkctr_flags(struct stkctr *stkctr) |
| 186 | { |
| 187 | return caddr_to_data(stkctr->entry); |
| 188 | } |
| 189 | |
| 190 | /* sets up to two flags at a time on a composite address */ |
| 191 | static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags) |
| 192 | { |
| 193 | stkctr->entry = caddr_set_flags(stkctr->entry, flags); |
| 194 | } |
| 195 | |
| 196 | /* returns the two flags from a stick counter */ |
| 197 | static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags) |
| 198 | { |
| 199 | stkctr->entry = caddr_clr_flags(stkctr->entry, flags); |
| 200 | } |
| 201 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 202 | #endif /* _PROTO_STICK_TABLE_H */ |