Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Stick tables management functions. |
| 3 | * |
| 4 | * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 5 | * Copyright (C) 2010 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <string.h> |
| 15 | |
| 16 | #include <common/config.h> |
| 17 | #include <common/memory.h> |
| 18 | #include <common/mini-clist.h> |
| 19 | #include <common/standard.h> |
| 20 | #include <common/time.h> |
| 21 | |
| 22 | #include <ebmbtree.h> |
| 23 | #include <ebsttree.h> |
| 24 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 25 | #include <proto/pattern.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 26 | #include <proto/proxy.h> |
| 27 | #include <proto/session.h> |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 28 | #include <proto/stick_table.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 29 | #include <proto/task.h> |
| 30 | |
| 31 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 32 | /* static structure used to return a table key built from a pattern */ |
| 33 | static struct stktable_key static_table_key; |
| 34 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 35 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 36 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 37 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 38 | */ |
| 39 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 40 | { |
| 41 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 42 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 46 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 47 | * from the value present in <key>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 48 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 49 | 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] | 50 | { |
| 51 | if (t->type != STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 52 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 53 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 54 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 55 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | |
| 59 | |
| 60 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 61 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 62 | * is returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 63 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 64 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 65 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 66 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 67 | ts->key.node.leaf_p = NULL; |
| 68 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 69 | return ts; |
| 70 | } |
| 71 | |
| 72 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 73 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 74 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 75 | */ |
| 76 | static int stktable_trash_oldest(struct stktable *t, int to_batch) |
| 77 | { |
| 78 | struct stksess *ts; |
| 79 | struct eb32_node *eb; |
| 80 | int batched = 0; |
| 81 | |
| 82 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 83 | |
| 84 | while (batched < to_batch) { |
| 85 | |
| 86 | if (unlikely(!eb)) { |
| 87 | /* we might have reached the end of the tree, typically because |
| 88 | * <now_ms> is in the first half and we're first scanning the last |
| 89 | * half. Let's loop back to the beginning of the tree now. |
| 90 | */ |
| 91 | eb = eb32_first(&t->exps); |
| 92 | if (likely(!eb)) |
| 93 | break; |
| 94 | } |
| 95 | |
| 96 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 97 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 98 | eb = eb32_next(eb); |
| 99 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 100 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 101 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 102 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 103 | if (!tick_isset(ts->expire)) |
| 104 | continue; |
| 105 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 106 | ts->exp.key = ts->expire; |
| 107 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 108 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 109 | if (!eb || eb->key > ts->exp.key) |
| 110 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 111 | |
| 112 | continue; |
| 113 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 114 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 115 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 116 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 117 | stksess_free(t, ts); |
| 118 | batched++; |
| 119 | } |
| 120 | |
| 121 | return batched; |
| 122 | } |
| 123 | |
| 124 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 125 | * Allocate and initialise a new sticky session. |
| 126 | * The new sticky session is returned or NULL in case of lack of memory. |
| 127 | * Sticky sessions should only be allocated this way, and must be freed using |
| 128 | * stksess_free(). Increase table <t> sticky session counter. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 129 | */ |
| 130 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 131 | { |
| 132 | struct stksess *ts; |
| 133 | |
| 134 | if (unlikely(t->current == t->size)) { |
| 135 | if ( t->nopurge ) |
| 136 | return NULL; |
| 137 | |
| 138 | if (!stktable_trash_oldest(t, t->size >> 8)) |
| 139 | return NULL; |
| 140 | } |
| 141 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 142 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 143 | if (ts) { |
| 144 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 145 | stksess_init(t, ts); |
| 146 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | return ts; |
| 150 | } |
| 151 | |
| 152 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 153 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 154 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 155 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 156 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 157 | { |
| 158 | struct ebmb_node *eb; |
| 159 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 160 | if (t->type == STKTABLE_TYPE_STRING) |
| 161 | eb = ebst_lookup_len(&t->keys, key->key, key->key_len); |
| 162 | else |
| 163 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 164 | |
| 165 | if (unlikely(!eb)) { |
| 166 | /* no session found */ |
| 167 | return NULL; |
| 168 | } |
| 169 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 170 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 171 | } |
| 172 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 173 | /* |
| 174 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 175 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 176 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 177 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 178 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 179 | struct ebmb_node *eb; |
| 180 | |
| 181 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 182 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 183 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 184 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 185 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 186 | if (unlikely(!eb)) |
| 187 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 188 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 189 | return ebmb_entry(eb, struct stksess, key); |
| 190 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 191 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 192 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 193 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 194 | * is set. <ts> is returned. |
| 195 | */ |
| 196 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts) |
| 197 | { |
| 198 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 199 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 200 | ts->exp.key = ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 201 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 202 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 203 | if (t->expire) { |
| 204 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 205 | task_queue(t->exp_task); |
| 206 | } |
| 207 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 211 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 212 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 213 | */ |
| 214 | static int stktable_trash_expired(struct stktable *t) |
| 215 | { |
| 216 | struct stksess *ts; |
| 217 | struct eb32_node *eb; |
| 218 | |
| 219 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 220 | |
| 221 | while (1) { |
| 222 | if (unlikely(!eb)) { |
| 223 | /* we might have reached the end of the tree, typically because |
| 224 | * <now_ms> is in the first half and we're first scanning the last |
| 225 | * half. Let's loop back to the beginning of the tree now. |
| 226 | */ |
| 227 | eb = eb32_first(&t->exps); |
| 228 | if (likely(!eb)) |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 233 | /* timer not expired yet, revisit it later */ |
| 234 | t->exp_next = eb->key; |
| 235 | return t->exp_next; |
| 236 | } |
| 237 | |
| 238 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 239 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 240 | eb = eb32_next(eb); |
| 241 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 242 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 243 | |
| 244 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 245 | if (!tick_isset(ts->expire)) |
| 246 | continue; |
| 247 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 248 | ts->exp.key = ts->expire; |
| 249 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 250 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 251 | if (!eb || eb->key > ts->exp.key) |
| 252 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 253 | continue; |
| 254 | } |
| 255 | |
| 256 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 257 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 258 | stksess_free(t, ts); |
| 259 | } |
| 260 | |
| 261 | /* We have found no task to expire in any tree */ |
| 262 | t->exp_next = TICK_ETERNITY; |
| 263 | return t->exp_next; |
| 264 | } |
| 265 | |
| 266 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 267 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 268 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 269 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 270 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 271 | { |
| 272 | struct stktable *t = (struct stktable *)task->context; |
| 273 | |
| 274 | task->expire = stktable_trash_expired(t); |
| 275 | return task; |
| 276 | } |
| 277 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 278 | /* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */ |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 279 | int stktable_init(struct stktable *t) |
| 280 | { |
| 281 | if (t->size) { |
| 282 | memset(&t->keys, 0, sizeof(t->keys)); |
| 283 | memset(&t->exps, 0, sizeof(t->exps)); |
| 284 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 285 | t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 286 | |
| 287 | t->exp_next = TICK_ETERNITY; |
| 288 | if ( t->expire ) { |
| 289 | t->exp_task = task_new(); |
| 290 | t->exp_task->process = process_table_expire; |
| 291 | t->exp_task->expire = TICK_ETERNITY; |
| 292 | t->exp_task->context = (void *)t; |
| 293 | } |
| 294 | return t->pool != NULL; |
| 295 | } |
| 296 | return 1; |
| 297 | } |
| 298 | |
| 299 | /* |
| 300 | * Configuration keywords of known table types |
| 301 | */ |
| 302 | struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } , |
| 303 | { "integer", 0, 4 }, |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 304 | { "string", STK_F_CUSTOM_KEYSIZE, 32 } }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 305 | |
| 306 | |
| 307 | /* |
| 308 | * Parse table type configuration. |
| 309 | * Returns 0 on successful parsing, else 1. |
| 310 | * <myidx> is set at next configuration <args> index. |
| 311 | */ |
| 312 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 313 | { |
| 314 | for (*type = 0; *type < STKTABLE_TYPES; (*type)++) { |
| 315 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 316 | continue; |
| 317 | |
| 318 | *key_size = stktable_types[*type].default_size; |
| 319 | (*myidx)++; |
| 320 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 321 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 322 | if (strcmp("len", args[*myidx]) == 0) { |
| 323 | (*myidx)++; |
| 324 | *key_size = atol(args[*myidx]); |
| 325 | if ( !*key_size ) |
| 326 | break; |
| 327 | /* null terminated string needs +1 for '\0'. */ |
| 328 | (*key_size)++; |
| 329 | (*myidx)++; |
| 330 | } |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | return 1; |
| 335 | } |
| 336 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 337 | /*****************************************************************/ |
| 338 | /* typed pattern to typed table key functions */ |
| 339 | /*****************************************************************/ |
| 340 | |
| 341 | static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 342 | { |
| 343 | return (void *)&pdata->integer; |
| 344 | } |
| 345 | |
| 346 | static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 347 | { |
| 348 | return (void *)&pdata->ip.s_addr; |
| 349 | } |
| 350 | |
| 351 | static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 352 | { |
| 353 | kdata->integer = ntohl(pdata->ip.s_addr); |
| 354 | return (void *)&kdata->integer; |
| 355 | } |
| 356 | |
| 357 | static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 358 | { |
| 359 | kdata->ip.s_addr = htonl(pdata->integer); |
| 360 | return (void *)&kdata->ip.s_addr; |
| 361 | } |
| 362 | |
| 363 | static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 364 | { |
| 365 | *len = pdata->str.len; |
| 366 | return (void *)pdata->str.str; |
| 367 | } |
| 368 | |
| 369 | static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 370 | { |
| 371 | if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf))) |
| 372 | return NULL; |
| 373 | |
| 374 | *len = strlen((const char *)kdata->buf); |
| 375 | return (void *)kdata->buf; |
| 376 | } |
| 377 | |
| 378 | static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 379 | { |
| 380 | void *key; |
| 381 | |
| 382 | key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf)); |
| 383 | if (!key) |
| 384 | return NULL; |
| 385 | |
| 386 | *len = strlen((const char *)key); |
| 387 | return key; |
| 388 | } |
| 389 | |
| 390 | static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 391 | { |
| 392 | if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip)) |
| 393 | return NULL; |
| 394 | |
| 395 | return (void *)&kdata->ip.s_addr; |
| 396 | } |
| 397 | |
| 398 | |
| 399 | static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 400 | { |
| 401 | int i; |
| 402 | |
| 403 | kdata->integer = 0; |
| 404 | for (i = 0; i < pdata->str.len; i++) { |
| 405 | uint32_t val = pdata->str.str[i] - '0'; |
| 406 | |
| 407 | if (val > 9) |
| 408 | break; |
| 409 | |
| 410 | kdata->integer = kdata->integer * 10 + val; |
| 411 | } |
| 412 | return (void *)&kdata->integer; |
| 413 | } |
| 414 | |
| 415 | /*****************************************************************/ |
| 416 | /* typed pattern to typed table key matrix: */ |
| 417 | /* pattern_to_key[from pattern type][to table key type] */ |
| 418 | /* NULL pointer used for impossible pattern casts */ |
| 419 | /*****************************************************************/ |
| 420 | |
| 421 | typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len); |
| 422 | static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = { |
| 423 | { k_ip2ip, k_ip2int, k_ip2str }, |
| 424 | { k_int2ip, k_int2int, k_int2str }, |
| 425 | { k_str2ip, k_str2int, k_str2str }, |
| 426 | }; |
| 427 | |
| 428 | |
| 429 | /* |
| 430 | * Process a fetch + format conversion as defined by the pattern expression <expr> |
| 431 | * on request or response considering the <dir> parameter. Returns either NULL if |
| 432 | * no key could be extracted, or a pointer to the converted result stored in |
| 433 | * static_table_key in format <table_type>. |
| 434 | */ |
| 435 | struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir, |
| 436 | struct pattern_expr *expr, unsigned long table_type) |
| 437 | { |
| 438 | struct pattern *ptrn; |
| 439 | |
| 440 | ptrn = pattern_process(px, l4, l7, dir, expr, NULL); |
| 441 | if (!ptrn) |
| 442 | return NULL; |
| 443 | |
| 444 | static_table_key.key_len = (size_t)-1; |
| 445 | static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len); |
| 446 | |
| 447 | if (!static_table_key.key) |
| 448 | return NULL; |
| 449 | |
| 450 | return &static_table_key; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Returns 1 if pattern expression <expr> result can be converted to table key of |
| 455 | * type <table_type>, otherwise zero. Used in configuration check. |
| 456 | */ |
| 457 | int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type) |
| 458 | { |
| 459 | if (table_type >= STKTABLE_TYPES) |
| 460 | return 0; |
| 461 | |
| 462 | if (LIST_ISEMPTY(&expr->conv_exprs)) { |
| 463 | if (!pattern_to_key[expr->fetch->out_type][table_type]) |
| 464 | return 0; |
| 465 | } else { |
| 466 | struct pattern_conv_expr *conv_expr; |
| 467 | conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list); |
| 468 | |
| 469 | if (!pattern_to_key[conv_expr->conv->out_type][table_type]) |
| 470 | return 0; |
| 471 | } |
| 472 | return 1; |
| 473 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 474 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 475 | /* Extra data types processing */ |
| 476 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 69b870f | 2010-06-06 14:30:13 +0200 | [diff] [blame] | 477 | [STKTABLE_DT_CONN_CUM] = { .name = "conn_cum", .data_length = stktable_data_size(conn_cum) }, |
Willy Tarreau | 13c29de | 2010-06-06 16:40:39 +0200 | [diff] [blame^] | 478 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .data_length = stktable_data_size(server_id) }, |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 479 | }; |
| 480 | |
| 481 | /* |
| 482 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 483 | * or <0 if not found. |
| 484 | */ |
| 485 | int stktable_get_data_type(char *name) |
| 486 | { |
| 487 | int type; |
| 488 | |
| 489 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
| 490 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 491 | return type; |
| 492 | } |
| 493 | return -1; |
| 494 | } |
| 495 | |