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