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> |
| 30 | |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 31 | #define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type)) |
| 32 | #define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type |
| 33 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 34 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key); |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 35 | 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] | 36 | void stksess_free(struct stktable *t, struct stksess *ts); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 37 | void stksess_kill(struct stktable *t, struct stksess *ts); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 38 | |
| 39 | int stktable_init(struct stktable *t); |
| 40 | int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 41 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 42 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local); |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 43 | struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts, |
| 44 | int local, int expire); |
| 45 | struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts, |
| 46 | int local, int expire); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 47 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 48 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts); |
| 49 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 50 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key); |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 51 | struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 52 | 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] | 53 | struct stream *strm, unsigned int opt, |
| 54 | struct sample_expr *expr, struct sample *smp); |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 55 | struct stkctr *smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw); |
| 56 | struct stkctr *smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw); |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 57 | int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type); |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 58 | 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] | 59 | int stktable_get_data_type(char *name); |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 60 | int stktable_trash_oldest(struct stktable *t, int to_batch); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 61 | |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 62 | /* return allocation size for standard data type <type> */ |
| 63 | static inline int stktable_type_size(int type) |
| 64 | { |
| 65 | switch(type) { |
| 66 | case STD_T_SINT: |
| 67 | case STD_T_UINT: |
| 68 | return sizeof(int); |
| 69 | case STD_T_ULL: |
| 70 | return sizeof(unsigned long long); |
| 71 | case STD_T_FRQP: |
| 72 | return sizeof(struct freq_ctr_period); |
| 73 | } |
| 74 | return 0; |
| 75 | } |
| 76 | |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 77 | /* reserve some space for data type <type>, and associate argument at <sa> if |
| 78 | * not NULL. Returns PE_NONE (0) if OK or an error code among : |
| 79 | * - PE_ENUM_OOR if <type> does not exist |
| 80 | * - PE_EXIST if <type> is already registered |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 81 | * - PE_ARG_NOT_USE if <sa> was provided but not expected |
| 82 | * - PE_ARG_MISSING if <sa> was expected but not provided |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 83 | */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 84 | 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] | 85 | { |
| 86 | if (type >= STKTABLE_DATA_TYPES) |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 87 | return PE_ENUM_OOR; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 88 | |
| 89 | if (t->data_ofs[type]) |
| 90 | /* already allocated */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 91 | return PE_EXIST; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 92 | |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 93 | switch (stktable_data_types[type].arg_type) { |
| 94 | case ARG_T_NONE: |
| 95 | if (sa) |
| 96 | return PE_ARG_NOT_USED; |
| 97 | break; |
| 98 | case ARG_T_INT: |
| 99 | if (!sa) |
| 100 | return PE_ARG_MISSING; |
| 101 | t->data_arg[type].i = atoi(sa); |
| 102 | break; |
| 103 | case ARG_T_DELAY: |
| 104 | if (!sa) |
| 105 | return PE_ARG_MISSING; |
| 106 | sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS); |
| 107 | if (sa) |
| 108 | return PE_ARG_INVC; /* invalid char */ |
| 109 | break; |
| 110 | } |
| 111 | |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 112 | t->data_size += stktable_type_size(stktable_data_types[type].std_type); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 113 | t->data_ofs[type] = -t->data_size; |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 114 | return PE_NONE; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 115 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 116 | |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 117 | /* return pointer for data type <type> in sticky session <ts> of table <t>, or |
| 118 | * NULL if either <ts> is NULL or the type is not stored. |
| 119 | */ |
| 120 | static inline void *stktable_data_ptr(struct stktable *t, struct stksess *ts, int type) |
| 121 | { |
| 122 | if (type >= STKTABLE_DATA_TYPES) |
| 123 | return NULL; |
| 124 | |
| 125 | if (!t->data_ofs[type]) /* type not stored */ |
| 126 | return NULL; |
| 127 | |
| 128 | if (!ts) |
| 129 | return NULL; |
| 130 | |
| 131 | return (void *)ts + t->data_ofs[type]; |
| 132 | } |
| 133 | |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 134 | /* kill an entry if it's expired and its ref_cnt is zero */ |
| 135 | static inline void stksess_kill_if_expired(struct stktable *t, struct stksess *ts) |
| 136 | { |
Emeric Brun | c89a572 | 2010-09-23 18:11:05 +0200 | [diff] [blame] | 137 | if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms)) |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 138 | stksess_kill(t, ts); |
| 139 | } |
| 140 | |
Willy Tarreau | 7698c90 | 2015-04-04 16:24:42 +0200 | [diff] [blame] | 141 | /* sets the stick counter's entry pointer */ |
| 142 | static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry) |
| 143 | { |
| 144 | stkctr->entry = caddr_from_ptr(entry, 0); |
| 145 | } |
| 146 | |
| 147 | /* returns the entry pointer from a stick counter */ |
| 148 | static inline struct stksess *stkctr_entry(struct stkctr *stkctr) |
| 149 | { |
| 150 | return caddr_to_ptr(stkctr->entry); |
| 151 | } |
| 152 | |
| 153 | /* returns the two flags from a stick counter */ |
| 154 | static inline unsigned int stkctr_flags(struct stkctr *stkctr) |
| 155 | { |
| 156 | return caddr_to_data(stkctr->entry); |
| 157 | } |
| 158 | |
| 159 | /* sets up to two flags at a time on a composite address */ |
| 160 | static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags) |
| 161 | { |
| 162 | stkctr->entry = caddr_set_flags(stkctr->entry, flags); |
| 163 | } |
| 164 | |
| 165 | /* returns the two flags from a stick counter */ |
| 166 | static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags) |
| 167 | { |
| 168 | stkctr->entry = caddr_clr_flags(stkctr->entry, flags); |
| 169 | } |
| 170 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 171 | #endif /* _PROTO_STICK_TABLE_H */ |