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> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 27 | #include <types/stick_table.h> |
| 28 | |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 29 | #define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type)) |
| 30 | #define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type |
| 31 | |
Willy Tarreau | 41883e2 | 2010-06-06 17:39:30 +0200 | [diff] [blame] | 32 | extern struct stktable_key static_table_key; |
| 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); |
| 37 | |
| 38 | int stktable_init(struct stktable *t); |
| 39 | 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] | 40 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 41 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 42 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 43 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts); |
| 44 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 45 | struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, |
| 46 | void *l7, int dir, struct pattern_expr *expr, |
| 47 | unsigned long table_type); |
| 48 | int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 49 | int stktable_get_data_type(char *name); |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 50 | struct proxy *find_stktable(const char *name); |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 51 | |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 52 | /* reserve some space for data type <type>, and associate argument at <sa> if |
| 53 | * not NULL. Returns PE_NONE (0) if OK or an error code among : |
| 54 | * - PE_ENUM_OOR if <type> does not exist |
| 55 | * - PE_EXIST if <type> is already registered |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 56 | * - PE_ARG_NOT_USE if <sa> was provided but not expected |
| 57 | * - PE_ARG_MISSING if <sa> was expected but not provided |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 58 | */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 59 | 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] | 60 | { |
| 61 | if (type >= STKTABLE_DATA_TYPES) |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 62 | return PE_ENUM_OOR; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 63 | |
| 64 | if (t->data_ofs[type]) |
| 65 | /* already allocated */ |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 66 | return PE_EXIST; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 67 | |
Willy Tarreau | ac78288 | 2010-06-20 10:41:54 +0200 | [diff] [blame] | 68 | switch (stktable_data_types[type].arg_type) { |
| 69 | case ARG_T_NONE: |
| 70 | if (sa) |
| 71 | return PE_ARG_NOT_USED; |
| 72 | break; |
| 73 | case ARG_T_INT: |
| 74 | if (!sa) |
| 75 | return PE_ARG_MISSING; |
| 76 | t->data_arg[type].i = atoi(sa); |
| 77 | break; |
| 78 | case ARG_T_DELAY: |
| 79 | if (!sa) |
| 80 | return PE_ARG_MISSING; |
| 81 | sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS); |
| 82 | if (sa) |
| 83 | return PE_ARG_INVC; /* invalid char */ |
| 84 | break; |
| 85 | } |
| 86 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 87 | t->data_size += stktable_data_types[type].data_length; |
| 88 | t->data_ofs[type] = -t->data_size; |
Willy Tarreau | 888617d | 2010-06-20 09:11:39 +0200 | [diff] [blame] | 89 | return PE_NONE; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 90 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 91 | |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 92 | /* return pointer for data type <type> in sticky session <ts> of table <t>, or |
| 93 | * NULL if either <ts> is NULL or the type is not stored. |
| 94 | */ |
| 95 | static inline void *stktable_data_ptr(struct stktable *t, struct stksess *ts, int type) |
| 96 | { |
| 97 | if (type >= STKTABLE_DATA_TYPES) |
| 98 | return NULL; |
| 99 | |
| 100 | if (!t->data_ofs[type]) /* type not stored */ |
| 101 | return NULL; |
| 102 | |
| 103 | if (!ts) |
| 104 | return NULL; |
| 105 | |
| 106 | return (void *)ts + t->data_ofs[type]; |
| 107 | } |
| 108 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 109 | #endif /* _PROTO_STICK_TABLE_H */ |