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 | |
Willy Tarreau | 41883e2 | 2010-06-06 17:39:30 +0200 | [diff] [blame] | 34 | extern struct stktable_key static_table_key; |
| 35 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 36 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key); |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 37 | 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] | 38 | void stksess_free(struct stktable *t, struct stksess *ts); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 39 | void stksess_kill(struct stktable *t, struct stksess *ts); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 40 | |
| 41 | int stktable_init(struct stktable *t); |
| 42 | 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] | 43 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 44 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local); |
| 45 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 46 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts); |
| 47 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 48 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 49 | struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, |
| 50 | struct session *l4, void *l7, int dir, |
| 51 | struct pattern_expr *expr); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 52 | int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 53 | int stktable_get_data_type(char *name); |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 54 | struct proxy *find_stktable(const char *name); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 55 | |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 56 | /* return allocation size for standard data type <type> */ |
| 57 | static inline int stktable_type_size(int type) |
| 58 | { |
| 59 | switch(type) { |
| 60 | case STD_T_SINT: |
| 61 | case STD_T_UINT: |
| 62 | return sizeof(int); |
| 63 | case STD_T_ULL: |
| 64 | return sizeof(unsigned long long); |
| 65 | case STD_T_FRQP: |
| 66 | return sizeof(struct freq_ctr_period); |
| 67 | } |
| 68 | return 0; |
| 69 | } |
| 70 | |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 71 | /* reserve some space for data type <type>, and associate argument at <sa> if |
| 72 | * not NULL. Returns PE_NONE (0) if OK or an error code among : |
| 73 | * - PE_ENUM_OOR if <type> does not exist |
| 74 | * - PE_EXIST if <type> is already registered |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 75 | * - PE_ARG_NOT_USE if <sa> was provided but not expected |
| 76 | * - PE_ARG_MISSING if <sa> was expected but not provided |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 77 | */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 78 | 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] | 79 | { |
| 80 | if (type >= STKTABLE_DATA_TYPES) |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 81 | return PE_ENUM_OOR; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 82 | |
| 83 | if (t->data_ofs[type]) |
| 84 | /* already allocated */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 85 | return PE_EXIST; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 86 | |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 87 | switch (stktable_data_types[type].arg_type) { |
| 88 | case ARG_T_NONE: |
| 89 | if (sa) |
| 90 | return PE_ARG_NOT_USED; |
| 91 | break; |
| 92 | case ARG_T_INT: |
| 93 | if (!sa) |
| 94 | return PE_ARG_MISSING; |
| 95 | t->data_arg[type].i = atoi(sa); |
| 96 | break; |
| 97 | case ARG_T_DELAY: |
| 98 | if (!sa) |
| 99 | return PE_ARG_MISSING; |
| 100 | sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS); |
| 101 | if (sa) |
| 102 | return PE_ARG_INVC; /* invalid char */ |
| 103 | break; |
| 104 | } |
| 105 | |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 106 | t->data_size += stktable_type_size(stktable_data_types[type].std_type); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 107 | t->data_ofs[type] = -t->data_size; |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 108 | return PE_NONE; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 109 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 110 | |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 111 | /* return pointer for data type <type> in sticky session <ts> of table <t>, or |
| 112 | * NULL if either <ts> is NULL or the type is not stored. |
| 113 | */ |
| 114 | static inline void *stktable_data_ptr(struct stktable *t, struct stksess *ts, int type) |
| 115 | { |
| 116 | if (type >= STKTABLE_DATA_TYPES) |
| 117 | return NULL; |
| 118 | |
| 119 | if (!t->data_ofs[type]) /* type not stored */ |
| 120 | return NULL; |
| 121 | |
| 122 | if (!ts) |
| 123 | return NULL; |
| 124 | |
| 125 | return (void *)ts + t->data_ofs[type]; |
| 126 | } |
| 127 | |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 128 | /* kill an entry if it's expired and its ref_cnt is zero */ |
| 129 | static inline void stksess_kill_if_expired(struct stktable *t, struct stksess *ts) |
| 130 | { |
Emeric Brun | c89a572 | 2010-09-23 18:11:05 +0200 | [diff] [blame] | 131 | if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms)) |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 132 | stksess_kill(t, ts); |
| 133 | } |
| 134 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 135 | #endif /* _PROTO_STICK_TABLE_H */ |