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 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 25 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 26 | #include <proto/sample.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 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> |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 30 | #include <proto/peers.h> |
| 31 | #include <types/global.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 32 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 33 | /* structure used to return a table key built from a sample */ |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 34 | struct stktable_key *static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 35 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 36 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 37 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 38 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 39 | */ |
| 40 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 41 | { |
| 42 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 43 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | /* |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 47 | * Kill an stksess (only if its ref_cnt is zero). |
| 48 | */ |
| 49 | void stksess_kill(struct stktable *t, struct stksess *ts) |
| 50 | { |
| 51 | if (ts->ref_cnt) |
| 52 | return; |
| 53 | |
| 54 | eb32_delete(&ts->exp); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 55 | eb32_delete(&ts->upd); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 56 | ebmb_delete(&ts->key); |
| 57 | stksess_free(t, ts); |
| 58 | } |
| 59 | |
| 60 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 61 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 62 | * from the value present in <key>. |
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 | 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] | 65 | { |
| 66 | if (t->type != STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 67 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 68 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 69 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 70 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 76 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 77 | * is returned. |
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 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 80 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 81 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 82 | ts->ref_cnt = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 83 | ts->key.node.leaf_p = NULL; |
| 84 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 85 | ts->upd.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 86 | return ts; |
| 87 | } |
| 88 | |
| 89 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 90 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 91 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 92 | */ |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 93 | int stktable_trash_oldest(struct stktable *t, int to_batch) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 94 | { |
| 95 | struct stksess *ts; |
| 96 | struct eb32_node *eb; |
| 97 | int batched = 0; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 98 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 99 | |
| 100 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 101 | |
| 102 | while (batched < to_batch) { |
| 103 | |
| 104 | if (unlikely(!eb)) { |
| 105 | /* we might have reached the end of the tree, typically because |
| 106 | * <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] | 107 | * half. Let's loop back to the beginning of the tree now if we |
| 108 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 109 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 110 | if (looped) |
| 111 | break; |
| 112 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 113 | eb = eb32_first(&t->exps); |
| 114 | if (likely(!eb)) |
| 115 | break; |
| 116 | } |
| 117 | |
| 118 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 119 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 120 | eb = eb32_next(eb); |
| 121 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 122 | /* don't delete an entry which is currently referenced */ |
| 123 | if (ts->ref_cnt) |
| 124 | continue; |
| 125 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 126 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 127 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 128 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 129 | if (!tick_isset(ts->expire)) |
| 130 | continue; |
| 131 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 132 | ts->exp.key = ts->expire; |
| 133 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 134 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 135 | if (!eb || eb->key > ts->exp.key) |
| 136 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 137 | |
| 138 | continue; |
| 139 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 140 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 141 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 142 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 143 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 144 | stksess_free(t, ts); |
| 145 | batched++; |
| 146 | } |
| 147 | |
| 148 | return batched; |
| 149 | } |
| 150 | |
| 151 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 152 | * Allocate and initialise a new sticky session. |
| 153 | * The new sticky session is returned or NULL in case of lack of memory. |
| 154 | * Sticky sessions should only be allocated this way, and must be freed using |
Willy Tarreau | a7b46b5 | 2013-04-11 16:55:37 +0200 | [diff] [blame] | 155 | * stksess_free(). Table <t>'s sticky session counter is increased. If <key> |
| 156 | * is not NULL, it is assigned to the new session. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 157 | */ |
| 158 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 159 | { |
| 160 | struct stksess *ts; |
| 161 | |
| 162 | if (unlikely(t->current == t->size)) { |
| 163 | if ( t->nopurge ) |
| 164 | return NULL; |
| 165 | |
Emeric Brun | fbce6d0 | 2010-09-23 18:10:00 +0200 | [diff] [blame] | 166 | if (!stktable_trash_oldest(t, (t->size >> 8) + 1)) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 167 | return NULL; |
| 168 | } |
| 169 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 170 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 171 | if (ts) { |
| 172 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 173 | stksess_init(t, ts); |
Willy Tarreau | a7b46b5 | 2013-04-11 16:55:37 +0200 | [diff] [blame] | 174 | if (key) |
| 175 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return ts; |
| 179 | } |
| 180 | |
| 181 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 182 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 183 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 184 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 185 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 186 | { |
| 187 | struct ebmb_node *eb; |
| 188 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 189 | if (t->type == STKTABLE_TYPE_STRING) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 190 | 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] | 191 | else |
| 192 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 193 | |
| 194 | if (unlikely(!eb)) { |
| 195 | /* no session found */ |
| 196 | return NULL; |
| 197 | } |
| 198 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 199 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 202 | /* Lookup and touch <key> in <table>, or create the entry if it does not exist. |
| 203 | * This is mainly used for situations where we want to refresh a key's usage so |
| 204 | * that it does not expire, and we want to have it created if it was not there. |
| 205 | * The stksess is returned, or NULL if it could not be created. |
| 206 | */ |
| 207 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key) |
| 208 | { |
| 209 | struct stksess *ts; |
| 210 | |
| 211 | ts = stktable_lookup_key(table, key); |
| 212 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 213 | return stktable_touch(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 214 | |
| 215 | /* entry does not exist, initialize a new one */ |
| 216 | ts = stksess_new(table, key); |
| 217 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 218 | stktable_store(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 219 | return ts; |
| 220 | } |
| 221 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 222 | /* |
| 223 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 224 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 225 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 226 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 227 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 228 | struct ebmb_node *eb; |
| 229 | |
| 230 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 231 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 232 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 233 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
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 | if (unlikely(!eb)) |
| 236 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 237 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 238 | return ebmb_entry(eb, struct stksess, key); |
| 239 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 240 | |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 241 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 242 | * The table's expiration timer is updated if set. |
| 243 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 244 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 245 | { |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 246 | struct eb32_node * eb; |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 247 | ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 248 | if (t->expire) { |
| 249 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 250 | task_queue(t->exp_task); |
| 251 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 252 | |
| 253 | if (t->sync_task && local) { |
| 254 | ts->upd.key = ++t->update; |
| 255 | t->localupdate = t->update; |
| 256 | eb32_delete(&ts->upd); |
| 257 | eb = eb32_insert(&t->updates, &ts->upd); |
| 258 | if (eb != &ts->upd) { |
| 259 | eb32_delete(eb); |
| 260 | eb32_insert(&t->updates, &ts->upd); |
| 261 | } |
| 262 | task_wakeup(t->sync_task, TASK_WOKEN_MSG); |
| 263 | } |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 264 | return ts; |
| 265 | } |
| 266 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 267 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 268 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 269 | * is set. <ts> is returned. |
| 270 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 271 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 272 | { |
| 273 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 274 | stktable_touch(t, ts, local); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 275 | ts->exp.key = ts->expire; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 276 | eb32_insert(&t->exps, &ts->exp); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 277 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 280 | /* Returns a valid or initialized stksess for the specified stktable_key in the |
| 281 | * specified table, or NULL if the key was NULL, or if no entry was found nor |
| 282 | * could be created. The entry's expiration is updated. |
| 283 | */ |
| 284 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key) |
| 285 | { |
| 286 | struct stksess *ts; |
| 287 | |
| 288 | if (!key) |
| 289 | return NULL; |
| 290 | |
| 291 | ts = stktable_lookup_key(table, key); |
| 292 | if (ts == NULL) { |
| 293 | /* entry does not exist, initialize a new one */ |
| 294 | ts = stksess_new(table, key); |
| 295 | if (!ts) |
| 296 | return NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 297 | stktable_store(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 298 | } |
| 299 | else |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 300 | stktable_touch(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 301 | return ts; |
| 302 | } |
| 303 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 304 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 305 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 306 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 307 | */ |
| 308 | static int stktable_trash_expired(struct stktable *t) |
| 309 | { |
| 310 | struct stksess *ts; |
| 311 | struct eb32_node *eb; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 312 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 313 | |
| 314 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 315 | |
| 316 | while (1) { |
| 317 | if (unlikely(!eb)) { |
| 318 | /* we might have reached the end of the tree, typically because |
| 319 | * <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] | 320 | * half. Let's loop back to the beginning of the tree now if we |
| 321 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 322 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 323 | if (looped) |
| 324 | break; |
| 325 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 326 | eb = eb32_first(&t->exps); |
| 327 | if (likely(!eb)) |
| 328 | break; |
| 329 | } |
| 330 | |
| 331 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 332 | /* timer not expired yet, revisit it later */ |
| 333 | t->exp_next = eb->key; |
| 334 | return t->exp_next; |
| 335 | } |
| 336 | |
| 337 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 338 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 339 | eb = eb32_next(eb); |
| 340 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 341 | /* don't delete an entry which is currently referenced */ |
| 342 | if (ts->ref_cnt) |
| 343 | continue; |
| 344 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 345 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 346 | |
| 347 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 348 | if (!tick_isset(ts->expire)) |
| 349 | continue; |
| 350 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 351 | ts->exp.key = ts->expire; |
| 352 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 353 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 354 | if (!eb || eb->key > ts->exp.key) |
| 355 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 356 | continue; |
| 357 | } |
| 358 | |
| 359 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 360 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 361 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 362 | stksess_free(t, ts); |
| 363 | } |
| 364 | |
| 365 | /* We have found no task to expire in any tree */ |
| 366 | t->exp_next = TICK_ETERNITY; |
| 367 | return t->exp_next; |
| 368 | } |
| 369 | |
| 370 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 371 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 372 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 373 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 374 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 375 | { |
| 376 | struct stktable *t = (struct stktable *)task->context; |
| 377 | |
| 378 | task->expire = stktable_trash_expired(t); |
| 379 | return task; |
| 380 | } |
| 381 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 382 | /* 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] | 383 | int stktable_init(struct stktable *t) |
| 384 | { |
| 385 | if (t->size) { |
| 386 | memset(&t->keys, 0, sizeof(t->keys)); |
| 387 | memset(&t->exps, 0, sizeof(t->exps)); |
Emeric Brun | a320fd1 | 2015-12-16 15:28:12 +0100 | [diff] [blame] | 388 | t->updates = EB_ROOT_UNIQUE; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 389 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 390 | 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] | 391 | |
| 392 | t->exp_next = TICK_ETERNITY; |
| 393 | if ( t->expire ) { |
| 394 | t->exp_task = task_new(); |
| 395 | t->exp_task->process = process_table_expire; |
| 396 | t->exp_task->expire = TICK_ETERNITY; |
| 397 | t->exp_task->context = (void *)t; |
| 398 | } |
Willy Tarreau | f8a9d2e | 2015-05-01 18:29:57 +0200 | [diff] [blame] | 399 | if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) { |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 400 | peers_register_table(t->peers.p, t); |
| 401 | } |
| 402 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 403 | return t->pool != NULL; |
| 404 | } |
| 405 | return 1; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * Configuration keywords of known table types |
| 410 | */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 411 | struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 412 | { "ipv6", 0, 16 }, |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 413 | { "integer", 0, 4 }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 414 | { "string", STK_F_CUSTOM_KEYSIZE, 32 }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 415 | { "binary", STK_F_CUSTOM_KEYSIZE, 32 } }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 416 | |
| 417 | |
| 418 | /* |
| 419 | * Parse table type configuration. |
| 420 | * Returns 0 on successful parsing, else 1. |
| 421 | * <myidx> is set at next configuration <args> index. |
| 422 | */ |
| 423 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 424 | { |
| 425 | for (*type = 0; *type < STKTABLE_TYPES; (*type)++) { |
| 426 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 427 | continue; |
| 428 | |
| 429 | *key_size = stktable_types[*type].default_size; |
| 430 | (*myidx)++; |
| 431 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 432 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 433 | if (strcmp("len", args[*myidx]) == 0) { |
| 434 | (*myidx)++; |
| 435 | *key_size = atol(args[*myidx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 436 | if (!*key_size) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 437 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 438 | if (*type == STKTABLE_TYPE_STRING) { |
| 439 | /* null terminated string needs +1 for '\0'. */ |
| 440 | (*key_size)++; |
| 441 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 442 | (*myidx)++; |
| 443 | } |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | return 1; |
| 448 | } |
| 449 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 450 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 451 | /* typed sample to typed table key functions */ |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 452 | /*****************************************************************/ |
| 453 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 454 | static void *k_int2int(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 455 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 456 | return (void *)&smp->data.uint; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 457 | } |
| 458 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 459 | static void *k_ip2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 460 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 461 | if (smp->type == SMP_T_IPV6) { |
| 462 | v6tov4(&kdata->ip, &smp->data.ipv6); |
| 463 | return (void *)&kdata->ip.s_addr; |
| 464 | } |
| 465 | else { |
| 466 | return (void *)&smp->data.ipv4.s_addr; |
| 467 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 468 | } |
| 469 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 470 | static void *k_ip2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 471 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 472 | if (smp->type == SMP_T_IPV6) { |
| 473 | return (void *)&smp->data.ipv6.s6_addr; |
| 474 | } |
| 475 | else { |
| 476 | v4tov6(&kdata->ipv6, &smp->data.ipv4); |
| 477 | return (void *)&kdata->ipv6.s6_addr; |
| 478 | } |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 479 | } |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 480 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 481 | static void *k_ip2int(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 482 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 483 | if (smp->type == SMP_T_IPV6) { |
| 484 | if (!v6tov4(&kdata->ip, &smp->data.ipv6)) |
| 485 | return NULL; |
| 486 | kdata->integer = ntohl(kdata->ip.s_addr); |
| 487 | } |
| 488 | else { |
| 489 | kdata->integer = ntohl(smp->data.ipv4.s_addr); |
| 490 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 491 | return (void *)&kdata->integer; |
| 492 | } |
| 493 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 494 | static void *k_int2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 495 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 496 | kdata->ip.s_addr = htonl(smp->data.uint); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 497 | return (void *)&kdata->ip.s_addr; |
| 498 | } |
| 499 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 500 | static void *k_str2str(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 501 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 502 | *len = smp->data.str.len; |
| 503 | return (void *)smp->data.str.str; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 504 | } |
| 505 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 506 | static void *k_ip2str(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 507 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 508 | if (smp->type == SMP_T_IPV6) { |
| 509 | if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, *len)) |
| 510 | return NULL; |
| 511 | } |
| 512 | else { |
| 513 | if (!inet_ntop(AF_INET, &smp->data.ipv4, kdata->buf, *len)) |
| 514 | return NULL; |
| 515 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 516 | |
| 517 | *len = strlen((const char *)kdata->buf); |
| 518 | return (void *)kdata->buf; |
| 519 | } |
| 520 | |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 521 | static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
| 522 | { |
| 523 | unsigned char c; |
| 524 | int ptr = 0; |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 525 | int max = *len; |
| 526 | int size = 0; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 527 | |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 528 | while (ptr < smp->data.str.len && size <= max - 2) { |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 529 | c = smp->data.str.str[ptr++]; |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 530 | kdata->buf[size++] = hextab[(c >> 4) & 0xF]; |
| 531 | kdata->buf[size++] = hextab[c & 0xF]; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 532 | } |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 533 | *len = size; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 534 | return (void *)kdata->buf; |
| 535 | } |
| 536 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 537 | static void *k_int2str(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 538 | { |
| 539 | void *key; |
| 540 | |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 541 | key = (void *)ultoa_r(smp->data.uint, kdata->buf, *len); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 542 | if (!key) |
| 543 | return NULL; |
| 544 | |
| 545 | *len = strlen((const char *)key); |
| 546 | return key; |
| 547 | } |
| 548 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 549 | static void *k_str2ip(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 550 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 551 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &kdata->ip)) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 552 | return NULL; |
| 553 | |
| 554 | return (void *)&kdata->ip.s_addr; |
| 555 | } |
| 556 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 557 | static void *k_str2ipv6(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 558 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 559 | if (!inet_pton(AF_INET6, smp->data.str.str, &kdata->ipv6)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 560 | return NULL; |
| 561 | |
| 562 | return (void *)&kdata->ipv6.s6_addr; |
| 563 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 564 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 565 | static void *k_str2int(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 566 | { |
| 567 | int i; |
| 568 | |
| 569 | kdata->integer = 0; |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 570 | for (i = 0; i < smp->data.str.len; i++) { |
| 571 | uint32_t val = smp->data.str.str[i] - '0'; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 572 | |
| 573 | if (val > 9) |
| 574 | break; |
| 575 | |
| 576 | kdata->integer = kdata->integer * 10 + val; |
| 577 | } |
| 578 | return (void *)&kdata->integer; |
| 579 | } |
| 580 | |
| 581 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 582 | /* typed sample to typed table key matrix: */ |
| 583 | /* sample_to_key[from sample type][to table key type] */ |
| 584 | /* NULL pointer used for impossible sample casts */ |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 585 | /*****************************************************************/ |
| 586 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 587 | typedef void *(*sample_to_key_fct)(struct sample *smp, union stktable_key_data *kdata, size_t *len); |
| 588 | static sample_to_key_fct sample_to_key[SMP_TYPES][STKTABLE_TYPES] = { |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 589 | /* table type: IP IPV6 INTEGER STRING BINARY */ |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 590 | /* patt. type: BOOL */ { NULL, NULL, k_int2int, k_int2str, NULL }, |
| 591 | /* UINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL }, |
| 592 | /* SINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL }, |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 593 | /* ADDR */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL }, |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 594 | /* IPV4 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL }, |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 595 | /* IPV6 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL }, |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 596 | /* STR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str }, |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 597 | /* BIN */ { NULL, NULL, NULL, k_bin2str, k_str2str }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 598 | }; |
| 599 | |
| 600 | |
| 601 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 602 | * Process a fetch + format conversion as defined by the sample expression <expr> |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 603 | * on request or response considering the <opt> parameter. Returns either NULL if |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 604 | * no key could be extracted, or a pointer to the converted result stored in |
Willy Tarreau | d008394 | 2014-06-25 16:20:53 +0200 | [diff] [blame] | 605 | * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset |
| 606 | * and its flags will be initialized so that the caller gets a copy of the input |
Willy Tarreau | f94735e | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 607 | * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present |
| 608 | * without SMP_OPT_FINAL). The output will be usable like this : |
| 609 | * |
| 610 | * return MAY_CHANGE FINAL Meaning for the sample |
| 611 | * NULL 0 * Not present and will never be (eg: header) |
| 612 | * NULL 1 0 Not present or unstable, could change (eg: req_len) |
| 613 | * NULL 1 1 Not present, will not change anymore |
| 614 | * smp 0 * Present and will not change (eg: header) |
| 615 | * smp 1 0 not possible |
| 616 | * smp 1 1 Present, last known value (eg: request length) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 617 | */ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 618 | struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *l4, void *l7, |
Willy Tarreau | d008394 | 2014-06-25 16:20:53 +0200 | [diff] [blame] | 619 | unsigned int opt, struct sample_expr *expr, struct sample *smp) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 620 | { |
Willy Tarreau | d008394 | 2014-06-25 16:20:53 +0200 | [diff] [blame] | 621 | if (smp) |
| 622 | memset(smp, 0, sizeof(*smp)); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 623 | |
Willy Tarreau | d008394 | 2014-06-25 16:20:53 +0200 | [diff] [blame] | 624 | smp = sample_process(px, l4, l7, opt, expr, smp); |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 625 | if (!smp) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 626 | return NULL; |
| 627 | |
Willy Tarreau | 67ff7e0 | 2013-12-05 02:19:58 +0100 | [diff] [blame] | 628 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 629 | return NULL; /* we can only use stable samples */ |
| 630 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 631 | if (!sample_to_key[smp->type][t->type]) |
Willy Tarreau | 12e5011 | 2012-04-25 17:21:49 +0200 | [diff] [blame] | 632 | return NULL; |
| 633 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 634 | static_table_key->key_len = t->key_size; |
| 635 | static_table_key->key = sample_to_key[smp->type][t->type](smp, &static_table_key->data, &static_table_key->key_len); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 636 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 637 | if (!static_table_key->key) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 638 | return NULL; |
| 639 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 640 | if (static_table_key->key_len == 0) |
Willy Tarreau | 7fc1c6e | 2012-04-26 11:03:17 +0200 | [diff] [blame] | 641 | return NULL; |
| 642 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 643 | if ((static_table_key->key_len < t->key_size) && (t->type != STKTABLE_TYPE_STRING)) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 644 | /* need padding with null */ |
| 645 | |
| 646 | /* assume static_table_key.key_len is less than sizeof(static_table_key.data.buf) |
| 647 | cause t->key_size is necessary less than sizeof(static_table_key.data) */ |
| 648 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 649 | if ((char *)static_table_key->key > (char *)&static_table_key->data && |
| 650 | (char *)static_table_key->key < (char *)&static_table_key->data + global.tune.bufsize) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 651 | /* key buffer is part of the static_table_key private data buffer, but is not aligned */ |
| 652 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 653 | if (global.tune.bufsize - ((char *)static_table_key->key - (char *)&static_table_key->data) < t->key_size) { |
| 654 | /* if not remain enough place for padding , process a realign */ |
| 655 | memmove(static_table_key->data.buf, static_table_key->key, static_table_key->key_len); |
| 656 | static_table_key->key = static_table_key->data.buf; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 657 | } |
| 658 | } |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 659 | else if (static_table_key->key != static_table_key->data.buf) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 660 | /* key definitly not part of the static_table_key private data buffer */ |
| 661 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 662 | memcpy(static_table_key->data.buf, static_table_key->key, static_table_key->key_len); |
| 663 | static_table_key->key = static_table_key->data.buf; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 664 | } |
| 665 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 666 | memset(static_table_key->key + static_table_key->key_len, 0, t->key_size - static_table_key->key_len); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 667 | } |
| 668 | |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 669 | return static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 673 | * Returns 1 if sample expression <expr> result can be converted to table key of |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 674 | * type <table_type>, otherwise zero. Used in configuration check. |
| 675 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 676 | int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 677 | { |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 678 | int out_type; |
| 679 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 680 | if (table_type >= STKTABLE_TYPES) |
| 681 | return 0; |
| 682 | |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 683 | out_type = smp_expr_output_type(expr); |
| 684 | if (!sample_to_key[out_type][table_type]) |
| 685 | return 0; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 686 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 687 | return 1; |
| 688 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 689 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 690 | /* Extra data types processing */ |
| 691 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 692 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT }, |
| 693 | [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT }, |
Willy Tarreau | ba2ffd1 | 2013-05-29 15:54:14 +0200 | [diff] [blame] | 694 | [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 695 | [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT }, |
| 696 | [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 697 | [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT }, |
| 698 | [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT }, |
| 699 | [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 700 | [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT }, |
| 701 | [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 702 | [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT }, |
| 703 | [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 704 | [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL }, |
| 705 | [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 706 | [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL }, |
| 707 | [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] | 708 | }; |
| 709 | |
| 710 | /* |
| 711 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 712 | * or <0 if not found. |
| 713 | */ |
| 714 | int stktable_get_data_type(char *name) |
| 715 | { |
| 716 | int type; |
| 717 | |
| 718 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
| 719 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 720 | return type; |
| 721 | } |
| 722 | return -1; |
| 723 | } |
| 724 | |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 725 | /* Returns pointer to proxy containing table <name> or NULL if not found */ |
| 726 | struct proxy *find_stktable(const char *name) |
| 727 | { |
| 728 | struct proxy *px; |
Willy Tarreau | 991610d | 2014-03-15 08:03:57 +0100 | [diff] [blame] | 729 | struct ebpt_node *node; |
| 730 | |
| 731 | for (node = ebis_lookup(&proxy_by_name, name); node; node = ebpt_next(node)) { |
| 732 | px = container_of(node, struct proxy, conf.by_name); |
| 733 | |
| 734 | if (strcmp(px->id, name) != 0) |
| 735 | break; |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 736 | |
Willy Tarreau | 991610d | 2014-03-15 08:03:57 +0100 | [diff] [blame] | 737 | if (px->table.size) |
Willy Tarreau | 4a0347a | 2010-06-18 17:26:50 +0200 | [diff] [blame] | 738 | return px; |
| 739 | } |
| 740 | return NULL; |
| 741 | } |