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 | 41883e2 | 2010-06-06 17:39:30 +0200 | [diff] [blame] | 32 | /* structure used to return a table key built from a pattern */ |
| 33 | struct stktable_key static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 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 | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 46 | * Kill an stksess (only if its ref_cnt is zero). |
| 47 | */ |
| 48 | void stksess_kill(struct stktable *t, struct stksess *ts) |
| 49 | { |
| 50 | if (ts->ref_cnt) |
| 51 | return; |
| 52 | |
| 53 | eb32_delete(&ts->exp); |
| 54 | ebmb_delete(&ts->key); |
| 55 | stksess_free(t, ts); |
| 56 | } |
| 57 | |
| 58 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 59 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 60 | * from the value present in <key>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 61 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 62 | 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] | 63 | { |
| 64 | if (t->type != STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 65 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 66 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 67 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 68 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 74 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 75 | * is returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 76 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 77 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 78 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 79 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 80 | ts->ref_cnt = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 81 | ts->key.node.leaf_p = NULL; |
| 82 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 83 | return ts; |
| 84 | } |
| 85 | |
| 86 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 87 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 88 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 89 | */ |
| 90 | static int stktable_trash_oldest(struct stktable *t, int to_batch) |
| 91 | { |
| 92 | struct stksess *ts; |
| 93 | struct eb32_node *eb; |
| 94 | int batched = 0; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 95 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 96 | |
| 97 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 98 | |
| 99 | while (batched < to_batch) { |
| 100 | |
| 101 | if (unlikely(!eb)) { |
| 102 | /* we might have reached the end of the tree, typically because |
| 103 | * <now_ms> is in the first half and we're first scanning the last |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 104 | * half. Let's loop back to the beginning of the tree now if we |
| 105 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 106 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 107 | if (looped) |
| 108 | break; |
| 109 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 110 | eb = eb32_first(&t->exps); |
| 111 | if (likely(!eb)) |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 116 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 117 | eb = eb32_next(eb); |
| 118 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 119 | /* don't delete an entry which is currently referenced */ |
| 120 | if (ts->ref_cnt) |
| 121 | continue; |
| 122 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 123 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 124 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 125 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 126 | if (!tick_isset(ts->expire)) |
| 127 | continue; |
| 128 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 129 | ts->exp.key = ts->expire; |
| 130 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 131 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 132 | if (!eb || eb->key > ts->exp.key) |
| 133 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 134 | |
| 135 | continue; |
| 136 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 137 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 138 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 139 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 140 | stksess_free(t, ts); |
| 141 | batched++; |
| 142 | } |
| 143 | |
| 144 | return batched; |
| 145 | } |
| 146 | |
| 147 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 148 | * Allocate and initialise a new sticky session. |
| 149 | * The new sticky session is returned or NULL in case of lack of memory. |
| 150 | * Sticky sessions should only be allocated this way, and must be freed using |
| 151 | * stksess_free(). Increase table <t> sticky session counter. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 152 | */ |
| 153 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 154 | { |
| 155 | struct stksess *ts; |
| 156 | |
| 157 | if (unlikely(t->current == t->size)) { |
| 158 | if ( t->nopurge ) |
| 159 | return NULL; |
| 160 | |
| 161 | if (!stktable_trash_oldest(t, t->size >> 8)) |
| 162 | return NULL; |
| 163 | } |
| 164 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 165 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 166 | if (ts) { |
| 167 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 168 | stksess_init(t, ts); |
| 169 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | return ts; |
| 173 | } |
| 174 | |
| 175 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 176 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 177 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 178 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 179 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 180 | { |
| 181 | struct ebmb_node *eb; |
| 182 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 183 | if (t->type == STKTABLE_TYPE_STRING) |
| 184 | eb = ebst_lookup_len(&t->keys, key->key, key->key_len); |
| 185 | else |
| 186 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 187 | |
| 188 | if (unlikely(!eb)) { |
| 189 | /* no session found */ |
| 190 | return NULL; |
| 191 | } |
| 192 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 193 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 196 | /* Lookup and touch <key> in <table>, or create the entry if it does not exist. |
| 197 | * This is mainly used for situations where we want to refresh a key's usage so |
| 198 | * that it does not expire, and we want to have it created if it was not there. |
| 199 | * The stksess is returned, or NULL if it could not be created. |
| 200 | */ |
| 201 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key) |
| 202 | { |
| 203 | struct stksess *ts; |
| 204 | |
| 205 | ts = stktable_lookup_key(table, key); |
| 206 | if (likely(ts)) |
| 207 | return stktable_touch(table, ts); |
| 208 | |
| 209 | /* entry does not exist, initialize a new one */ |
| 210 | ts = stksess_new(table, key); |
| 211 | if (likely(ts)) |
| 212 | stktable_store(table, ts); |
| 213 | return ts; |
| 214 | } |
| 215 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 216 | /* |
| 217 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 218 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 219 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 220 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 221 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 222 | struct ebmb_node *eb; |
| 223 | |
| 224 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 225 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 226 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 227 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 228 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 229 | if (unlikely(!eb)) |
| 230 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 231 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 232 | return ebmb_entry(eb, struct stksess, key); |
| 233 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 234 | |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 235 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 236 | * The table's expiration timer is updated if set. |
| 237 | */ |
| 238 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts) |
| 239 | { |
| 240 | ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 241 | if (t->expire) { |
| 242 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 243 | task_queue(t->exp_task); |
| 244 | } |
| 245 | return ts; |
| 246 | } |
| 247 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 248 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 249 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 250 | * is set. <ts> is returned. |
| 251 | */ |
| 252 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts) |
| 253 | { |
| 254 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 255 | stktable_touch(t, ts); |
| 256 | ts->exp.key = ts->expire; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 257 | eb32_insert(&t->exps, &ts->exp); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 258 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 259 | } |
| 260 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 261 | /* Returns a valid or initialized stksess for the specified stktable_key in the |
| 262 | * specified table, or NULL if the key was NULL, or if no entry was found nor |
| 263 | * could be created. The entry's expiration is updated. |
| 264 | */ |
| 265 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key) |
| 266 | { |
| 267 | struct stksess *ts; |
| 268 | |
| 269 | if (!key) |
| 270 | return NULL; |
| 271 | |
| 272 | ts = stktable_lookup_key(table, key); |
| 273 | if (ts == NULL) { |
| 274 | /* entry does not exist, initialize a new one */ |
| 275 | ts = stksess_new(table, key); |
| 276 | if (!ts) |
| 277 | return NULL; |
| 278 | stktable_store(table, ts); |
| 279 | } |
| 280 | else |
| 281 | stktable_touch(table, ts); |
| 282 | return ts; |
| 283 | } |
| 284 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 285 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 286 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 287 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 288 | */ |
| 289 | static int stktable_trash_expired(struct stktable *t) |
| 290 | { |
| 291 | struct stksess *ts; |
| 292 | struct eb32_node *eb; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 293 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 294 | |
| 295 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 296 | |
| 297 | while (1) { |
| 298 | if (unlikely(!eb)) { |
| 299 | /* we might have reached the end of the tree, typically because |
| 300 | * <now_ms> is in the first half and we're first scanning the last |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 301 | * half. Let's loop back to the beginning of the tree now if we |
| 302 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 303 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 304 | if (looped) |
| 305 | break; |
| 306 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 307 | eb = eb32_first(&t->exps); |
| 308 | if (likely(!eb)) |
| 309 | break; |
| 310 | } |
| 311 | |
| 312 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 313 | /* timer not expired yet, revisit it later */ |
| 314 | t->exp_next = eb->key; |
| 315 | return t->exp_next; |
| 316 | } |
| 317 | |
| 318 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 319 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 320 | eb = eb32_next(eb); |
| 321 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 322 | /* don't delete an entry which is currently referenced */ |
| 323 | if (ts->ref_cnt) |
| 324 | continue; |
| 325 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 326 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 327 | |
| 328 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 329 | if (!tick_isset(ts->expire)) |
| 330 | continue; |
| 331 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 332 | ts->exp.key = ts->expire; |
| 333 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 334 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 335 | if (!eb || eb->key > ts->exp.key) |
| 336 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 337 | continue; |
| 338 | } |
| 339 | |
| 340 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 341 | ebmb_delete(&ts->key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 342 | stksess_free(t, ts); |
| 343 | } |
| 344 | |
| 345 | /* We have found no task to expire in any tree */ |
| 346 | t->exp_next = TICK_ETERNITY; |
| 347 | return t->exp_next; |
| 348 | } |
| 349 | |
| 350 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 351 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 352 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 353 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 354 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 355 | { |
| 356 | struct stktable *t = (struct stktable *)task->context; |
| 357 | |
| 358 | task->expire = stktable_trash_expired(t); |
| 359 | return task; |
| 360 | } |
| 361 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 362 | /* 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] | 363 | int stktable_init(struct stktable *t) |
| 364 | { |
| 365 | if (t->size) { |
| 366 | memset(&t->keys, 0, sizeof(t->keys)); |
| 367 | memset(&t->exps, 0, sizeof(t->exps)); |
| 368 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 369 | 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] | 370 | |
| 371 | t->exp_next = TICK_ETERNITY; |
| 372 | if ( t->expire ) { |
| 373 | t->exp_task = task_new(); |
| 374 | t->exp_task->process = process_table_expire; |
| 375 | t->exp_task->expire = TICK_ETERNITY; |
| 376 | t->exp_task->context = (void *)t; |
| 377 | } |
| 378 | return t->pool != NULL; |
| 379 | } |
| 380 | return 1; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Configuration keywords of known table types |
| 385 | */ |
| 386 | struct stktable_type stktable_types[STKTABLE_TYPES] = { { "ip", 0, 4 } , |
| 387 | { "integer", 0, 4 }, |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 388 | { "string", STK_F_CUSTOM_KEYSIZE, 32 } }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 389 | |
| 390 | |
| 391 | /* |
| 392 | * Parse table type configuration. |
| 393 | * Returns 0 on successful parsing, else 1. |
| 394 | * <myidx> is set at next configuration <args> index. |
| 395 | */ |
| 396 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 397 | { |
| 398 | for (*type = 0; *type < STKTABLE_TYPES; (*type)++) { |
| 399 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 400 | continue; |
| 401 | |
| 402 | *key_size = stktable_types[*type].default_size; |
| 403 | (*myidx)++; |
| 404 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 405 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 406 | if (strcmp("len", args[*myidx]) == 0) { |
| 407 | (*myidx)++; |
| 408 | *key_size = atol(args[*myidx]); |
| 409 | if ( !*key_size ) |
| 410 | break; |
| 411 | /* null terminated string needs +1 for '\0'. */ |
| 412 | (*key_size)++; |
| 413 | (*myidx)++; |
| 414 | } |
| 415 | } |
| 416 | return 0; |
| 417 | } |
| 418 | return 1; |
| 419 | } |
| 420 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 421 | /*****************************************************************/ |
| 422 | /* typed pattern to typed table key functions */ |
| 423 | /*****************************************************************/ |
| 424 | |
| 425 | static void *k_int2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 426 | { |
| 427 | return (void *)&pdata->integer; |
| 428 | } |
| 429 | |
| 430 | static void *k_ip2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 431 | { |
| 432 | return (void *)&pdata->ip.s_addr; |
| 433 | } |
| 434 | |
| 435 | static void *k_ip2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 436 | { |
| 437 | kdata->integer = ntohl(pdata->ip.s_addr); |
| 438 | return (void *)&kdata->integer; |
| 439 | } |
| 440 | |
| 441 | static void *k_int2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 442 | { |
| 443 | kdata->ip.s_addr = htonl(pdata->integer); |
| 444 | return (void *)&kdata->ip.s_addr; |
| 445 | } |
| 446 | |
| 447 | static void *k_str2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 448 | { |
| 449 | *len = pdata->str.len; |
| 450 | return (void *)pdata->str.str; |
| 451 | } |
| 452 | |
| 453 | static void *k_ip2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 454 | { |
| 455 | if (!inet_ntop(AF_INET, &pdata->ip, kdata->buf, sizeof(kdata->buf))) |
| 456 | return NULL; |
| 457 | |
| 458 | *len = strlen((const char *)kdata->buf); |
| 459 | return (void *)kdata->buf; |
| 460 | } |
| 461 | |
| 462 | static void *k_int2str(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 463 | { |
| 464 | void *key; |
| 465 | |
| 466 | key = (void *)ultoa_r(pdata->integer, kdata->buf, sizeof(kdata->buf)); |
| 467 | if (!key) |
| 468 | return NULL; |
| 469 | |
| 470 | *len = strlen((const char *)key); |
| 471 | return key; |
| 472 | } |
| 473 | |
| 474 | static void *k_str2ip(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 475 | { |
| 476 | if (!buf2ip(pdata->str.str, pdata->str.len, &kdata->ip)) |
| 477 | return NULL; |
| 478 | |
| 479 | return (void *)&kdata->ip.s_addr; |
| 480 | } |
| 481 | |
| 482 | |
| 483 | static void *k_str2int(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len) |
| 484 | { |
| 485 | int i; |
| 486 | |
| 487 | kdata->integer = 0; |
| 488 | for (i = 0; i < pdata->str.len; i++) { |
| 489 | uint32_t val = pdata->str.str[i] - '0'; |
| 490 | |
| 491 | if (val > 9) |
| 492 | break; |
| 493 | |
| 494 | kdata->integer = kdata->integer * 10 + val; |
| 495 | } |
| 496 | return (void *)&kdata->integer; |
| 497 | } |
| 498 | |
| 499 | /*****************************************************************/ |
| 500 | /* typed pattern to typed table key matrix: */ |
| 501 | /* pattern_to_key[from pattern type][to table key type] */ |
| 502 | /* NULL pointer used for impossible pattern casts */ |
| 503 | /*****************************************************************/ |
| 504 | |
| 505 | typedef void *(*pattern_to_key_fct)(union pattern_data *pdata, union stktable_key_data *kdata, size_t *len); |
| 506 | static pattern_to_key_fct pattern_to_key[PATTERN_TYPES][STKTABLE_TYPES] = { |
| 507 | { k_ip2ip, k_ip2int, k_ip2str }, |
| 508 | { k_int2ip, k_int2int, k_int2str }, |
| 509 | { k_str2ip, k_str2int, k_str2str }, |
| 510 | }; |
| 511 | |
| 512 | |
| 513 | /* |
| 514 | * Process a fetch + format conversion as defined by the pattern expression <expr> |
| 515 | * on request or response considering the <dir> parameter. Returns either NULL if |
| 516 | * no key could be extracted, or a pointer to the converted result stored in |
| 517 | * static_table_key in format <table_type>. |
| 518 | */ |
| 519 | struct stktable_key *stktable_fetch_key(struct proxy *px, struct session *l4, void *l7, int dir, |
| 520 | struct pattern_expr *expr, unsigned long table_type) |
| 521 | { |
| 522 | struct pattern *ptrn; |
| 523 | |
| 524 | ptrn = pattern_process(px, l4, l7, dir, expr, NULL); |
| 525 | if (!ptrn) |
| 526 | return NULL; |
| 527 | |
| 528 | static_table_key.key_len = (size_t)-1; |
| 529 | static_table_key.key = pattern_to_key[ptrn->type][table_type](&ptrn->data, &static_table_key.data, &static_table_key.key_len); |
| 530 | |
| 531 | if (!static_table_key.key) |
| 532 | return NULL; |
| 533 | |
| 534 | return &static_table_key; |
| 535 | } |
| 536 | |
| 537 | /* |
| 538 | * Returns 1 if pattern expression <expr> result can be converted to table key of |
| 539 | * type <table_type>, otherwise zero. Used in configuration check. |
| 540 | */ |
| 541 | int stktable_compatible_pattern(struct pattern_expr *expr, unsigned long table_type) |
| 542 | { |
| 543 | if (table_type >= STKTABLE_TYPES) |
| 544 | return 0; |
| 545 | |
| 546 | if (LIST_ISEMPTY(&expr->conv_exprs)) { |
| 547 | if (!pattern_to_key[expr->fetch->out_type][table_type]) |
| 548 | return 0; |
| 549 | } else { |
| 550 | struct pattern_conv_expr *conv_expr; |
| 551 | conv_expr = LIST_PREV(&expr->conv_exprs, typeof(conv_expr), list); |
| 552 | |
| 553 | if (!pattern_to_key[conv_expr->conv->out_type][table_type]) |
| 554 | return 0; |
| 555 | } |
| 556 | return 1; |
| 557 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 558 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 559 | /* Extra data types processing */ |
| 560 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 561 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT }, |
| 562 | [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT }, |
| 563 | [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT }, |
| 564 | [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 565 | [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT }, |
| 566 | [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT }, |
| 567 | [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 568 | [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT }, |
| 569 | [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 570 | [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT }, |
| 571 | [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 572 | [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL }, |
| 573 | [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 574 | [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL }, |
| 575 | [STKTABLE_DT_BYTES_OUT_RATE]= { .name = "bytes_out_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 576 | }; |
| 577 | |
| 578 | /* |
| 579 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 580 | * or <0 if not found. |
| 581 | */ |
| 582 | int stktable_get_data_type(char *name) |
| 583 | { |
| 584 | int type; |
| 585 | |
| 586 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
| 587 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 588 | return type; |
| 589 | } |
| 590 | return -1; |
| 591 | } |
| 592 | |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 593 | /* Returns pointer to proxy containing table <name> or NULL if not found */ |
| 594 | struct proxy *find_stktable(const char *name) |
| 595 | { |
| 596 | struct proxy *px; |
| 597 | |
| 598 | for (px = proxy; px; px = px->next) { |
| 599 | if (px->table.size && strcmp(px->id, name) == 0) |
| 600 | return px; |
| 601 | } |
| 602 | return NULL; |
| 603 | } |