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 | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 25 | #include <proto/arg.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 26 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 27 | #include <proto/sample.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 28 | #include <proto/stream.h> |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 29 | #include <proto/stick_table.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 30 | #include <proto/task.h> |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 31 | #include <proto/peers.h> |
| 32 | #include <types/global.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 33 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 34 | /* structure used to return a table key built from a sample */ |
Willy Tarreau | 0711541 | 2012-10-29 21:56:59 +0100 | [diff] [blame] | 35 | struct stktable_key *static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 36 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 37 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 38 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 39 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 40 | */ |
| 41 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 42 | { |
| 43 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 44 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | /* |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 48 | * Kill an stksess (only if its ref_cnt is zero). |
| 49 | */ |
| 50 | void stksess_kill(struct stktable *t, struct stksess *ts) |
| 51 | { |
| 52 | if (ts->ref_cnt) |
| 53 | return; |
| 54 | |
| 55 | eb32_delete(&ts->exp); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 56 | eb32_delete(&ts->upd); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 57 | ebmb_delete(&ts->key); |
| 58 | stksess_free(t, ts); |
| 59 | } |
| 60 | |
| 61 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 62 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 63 | * from the value present in <key>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 64 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 65 | 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] | 66 | { |
| 67 | if (t->type != STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 68 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 69 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 70 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 71 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 77 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 78 | * is returned. |
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 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 81 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 82 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 83 | ts->ref_cnt = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 84 | ts->key.node.leaf_p = NULL; |
| 85 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 86 | ts->upd.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 87 | return ts; |
| 88 | } |
| 89 | |
| 90 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 91 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 92 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 93 | */ |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 94 | int stktable_trash_oldest(struct stktable *t, int to_batch) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 95 | { |
| 96 | struct stksess *ts; |
| 97 | struct eb32_node *eb; |
| 98 | int batched = 0; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 99 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 100 | |
| 101 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 102 | |
| 103 | while (batched < to_batch) { |
| 104 | |
| 105 | if (unlikely(!eb)) { |
| 106 | /* we might have reached the end of the tree, typically because |
| 107 | * <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] | 108 | * half. Let's loop back to the beginning of the tree now if we |
| 109 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 110 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 111 | if (looped) |
| 112 | break; |
| 113 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 114 | eb = eb32_first(&t->exps); |
| 115 | if (likely(!eb)) |
| 116 | break; |
| 117 | } |
| 118 | |
| 119 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 120 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 121 | eb = eb32_next(eb); |
| 122 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 123 | /* don't delete an entry which is currently referenced */ |
| 124 | if (ts->ref_cnt) |
| 125 | continue; |
| 126 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 127 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 128 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 129 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 130 | if (!tick_isset(ts->expire)) |
| 131 | continue; |
| 132 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 133 | ts->exp.key = ts->expire; |
| 134 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 135 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 136 | if (!eb || eb->key > ts->exp.key) |
| 137 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 138 | |
| 139 | continue; |
| 140 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 141 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 142 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 143 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 144 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 145 | stksess_free(t, ts); |
| 146 | batched++; |
| 147 | } |
| 148 | |
| 149 | return batched; |
| 150 | } |
| 151 | |
| 152 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 153 | * Allocate and initialise a new sticky session. |
| 154 | * The new sticky session is returned or NULL in case of lack of memory. |
| 155 | * 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] | 156 | * stksess_free(). Table <t>'s sticky session counter is increased. If <key> |
| 157 | * is not NULL, it is assigned to the new session. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 158 | */ |
| 159 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 160 | { |
| 161 | struct stksess *ts; |
| 162 | |
| 163 | if (unlikely(t->current == t->size)) { |
| 164 | if ( t->nopurge ) |
| 165 | return NULL; |
| 166 | |
Emeric Brun | fbce6d0 | 2010-09-23 18:10:00 +0200 | [diff] [blame] | 167 | if (!stktable_trash_oldest(t, (t->size >> 8) + 1)) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 168 | return NULL; |
| 169 | } |
| 170 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 171 | ts = pool_alloc2(t->pool) + t->data_size; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 172 | if (ts) { |
| 173 | t->current++; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 174 | stksess_init(t, ts); |
Willy Tarreau | a7b46b5 | 2013-04-11 16:55:37 +0200 | [diff] [blame] | 175 | if (key) |
| 176 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | return ts; |
| 180 | } |
| 181 | |
| 182 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 183 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 184 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 185 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 186 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 187 | { |
| 188 | struct ebmb_node *eb; |
| 189 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 190 | if (t->type == STKTABLE_TYPE_STRING) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 191 | 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] | 192 | else |
| 193 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 194 | |
| 195 | if (unlikely(!eb)) { |
| 196 | /* no session found */ |
| 197 | return NULL; |
| 198 | } |
| 199 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 200 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 203 | /* Lookup and touch <key> in <table>, or create the entry if it does not exist. |
| 204 | * This is mainly used for situations where we want to refresh a key's usage so |
| 205 | * that it does not expire, and we want to have it created if it was not there. |
| 206 | * The stksess is returned, or NULL if it could not be created. |
| 207 | */ |
| 208 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key) |
| 209 | { |
| 210 | struct stksess *ts; |
| 211 | |
| 212 | ts = stktable_lookup_key(table, key); |
| 213 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 214 | return stktable_touch(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 215 | |
| 216 | /* entry does not exist, initialize a new one */ |
| 217 | ts = stksess_new(table, key); |
| 218 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 219 | stktable_store(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 220 | return ts; |
| 221 | } |
| 222 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 223 | /* |
| 224 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 225 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 226 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 227 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 228 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 229 | struct ebmb_node *eb; |
| 230 | |
| 231 | if (t->type == STKTABLE_TYPE_STRING) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 232 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 233 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 234 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 235 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 236 | if (unlikely(!eb)) |
| 237 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 238 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 239 | return ebmb_entry(eb, struct stksess, key); |
| 240 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 241 | |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 242 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 243 | * The table's expiration timer is updated if set. |
| 244 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 245 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 246 | { |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 247 | struct eb32_node * eb; |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 248 | ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 249 | if (t->expire) { |
| 250 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 251 | task_queue(t->exp_task); |
| 252 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 253 | |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 254 | /* If sync is enabled and update is local */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 255 | if (t->sync_task && local) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 256 | /* If this entry was already pushed to a peer |
| 257 | We want to push it again */ |
| 258 | if ((int)(ts->upd.key - t->commitupdate) <= 0) { |
| 259 | ts->upd.key = ++t->update; |
| 260 | t->localupdate = t->update; |
| 261 | eb32_delete(&ts->upd); |
| 262 | eb = eb32_insert(&t->updates, &ts->upd); |
| 263 | if (eb != &ts->upd) { |
| 264 | eb32_delete(eb); |
| 265 | eb32_insert(&t->updates, &ts->upd); |
| 266 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 267 | } |
| 268 | task_wakeup(t->sync_task, TASK_WOKEN_MSG); |
| 269 | } |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 270 | return ts; |
| 271 | } |
| 272 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 273 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 274 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 275 | * is set. <ts> is returned. |
| 276 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 277 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 278 | { |
| 279 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 280 | stktable_touch(t, ts, local); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 281 | ts->exp.key = ts->expire; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 282 | eb32_insert(&t->exps, &ts->exp); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 283 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 284 | } |
| 285 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 286 | /* Returns a valid or initialized stksess for the specified stktable_key in the |
| 287 | * specified table, or NULL if the key was NULL, or if no entry was found nor |
| 288 | * could be created. The entry's expiration is updated. |
| 289 | */ |
| 290 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key) |
| 291 | { |
| 292 | struct stksess *ts; |
| 293 | |
| 294 | if (!key) |
| 295 | return NULL; |
| 296 | |
| 297 | ts = stktable_lookup_key(table, key); |
| 298 | if (ts == NULL) { |
| 299 | /* entry does not exist, initialize a new one */ |
| 300 | ts = stksess_new(table, key); |
| 301 | if (!ts) |
| 302 | return NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 303 | stktable_store(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 304 | } |
| 305 | else |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 306 | stktable_touch(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 307 | return ts; |
| 308 | } |
| 309 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 310 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 311 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 312 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 313 | */ |
| 314 | static int stktable_trash_expired(struct stktable *t) |
| 315 | { |
| 316 | struct stksess *ts; |
| 317 | struct eb32_node *eb; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 318 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 319 | |
| 320 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 321 | |
| 322 | while (1) { |
| 323 | if (unlikely(!eb)) { |
| 324 | /* we might have reached the end of the tree, typically because |
| 325 | * <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] | 326 | * half. Let's loop back to the beginning of the tree now if we |
| 327 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 328 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 329 | if (looped) |
| 330 | break; |
| 331 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 332 | eb = eb32_first(&t->exps); |
| 333 | if (likely(!eb)) |
| 334 | break; |
| 335 | } |
| 336 | |
| 337 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 338 | /* timer not expired yet, revisit it later */ |
| 339 | t->exp_next = eb->key; |
| 340 | return t->exp_next; |
| 341 | } |
| 342 | |
| 343 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 344 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 345 | eb = eb32_next(eb); |
| 346 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 347 | /* don't delete an entry which is currently referenced */ |
| 348 | if (ts->ref_cnt) |
| 349 | continue; |
| 350 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 351 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 352 | |
| 353 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 354 | if (!tick_isset(ts->expire)) |
| 355 | continue; |
| 356 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 357 | ts->exp.key = ts->expire; |
| 358 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 359 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 360 | if (!eb || eb->key > ts->exp.key) |
| 361 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 362 | continue; |
| 363 | } |
| 364 | |
| 365 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 366 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 367 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 368 | stksess_free(t, ts); |
| 369 | } |
| 370 | |
| 371 | /* We have found no task to expire in any tree */ |
| 372 | t->exp_next = TICK_ETERNITY; |
| 373 | return t->exp_next; |
| 374 | } |
| 375 | |
| 376 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 377 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 378 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 379 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 380 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 381 | { |
| 382 | struct stktable *t = (struct stktable *)task->context; |
| 383 | |
| 384 | task->expire = stktable_trash_expired(t); |
| 385 | return task; |
| 386 | } |
| 387 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 388 | /* 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] | 389 | int stktable_init(struct stktable *t) |
| 390 | { |
| 391 | if (t->size) { |
| 392 | memset(&t->keys, 0, sizeof(t->keys)); |
| 393 | memset(&t->exps, 0, sizeof(t->exps)); |
| 394 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 395 | 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] | 396 | |
| 397 | t->exp_next = TICK_ETERNITY; |
| 398 | if ( t->expire ) { |
| 399 | t->exp_task = task_new(); |
| 400 | t->exp_task->process = process_table_expire; |
| 401 | t->exp_task->expire = TICK_ETERNITY; |
| 402 | t->exp_task->context = (void *)t; |
| 403 | } |
Willy Tarreau | c8b6791 | 2015-05-01 18:29:57 +0200 | [diff] [blame] | 404 | 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] | 405 | peers_register_table(t->peers.p, t); |
| 406 | } |
| 407 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 408 | return t->pool != NULL; |
| 409 | } |
| 410 | return 1; |
| 411 | } |
| 412 | |
| 413 | /* |
| 414 | * Configuration keywords of known table types |
| 415 | */ |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 416 | struct stktable_type stktable_types[STKTABLE_TYPES] = {{ "ip", 0, 4 }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 417 | { "ipv6", 0, 16 }, |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 418 | { "integer", 0, 4 }, |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 419 | { "string", STK_F_CUSTOM_KEYSIZE, 32 }, |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 420 | { "binary", STK_F_CUSTOM_KEYSIZE, 32 } }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 421 | |
| 422 | |
| 423 | /* |
| 424 | * Parse table type configuration. |
| 425 | * Returns 0 on successful parsing, else 1. |
| 426 | * <myidx> is set at next configuration <args> index. |
| 427 | */ |
| 428 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 429 | { |
| 430 | for (*type = 0; *type < STKTABLE_TYPES; (*type)++) { |
| 431 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 432 | continue; |
| 433 | |
| 434 | *key_size = stktable_types[*type].default_size; |
| 435 | (*myidx)++; |
| 436 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 437 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 438 | if (strcmp("len", args[*myidx]) == 0) { |
| 439 | (*myidx)++; |
| 440 | *key_size = atol(args[*myidx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 441 | if (!*key_size) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 442 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 443 | if (*type == STKTABLE_TYPE_STRING) { |
| 444 | /* null terminated string needs +1 for '\0'. */ |
| 445 | (*key_size)++; |
| 446 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 447 | (*myidx)++; |
| 448 | } |
| 449 | } |
| 450 | return 0; |
| 451 | } |
| 452 | return 1; |
| 453 | } |
| 454 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 455 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 456 | /* typed sample to typed table key functions */ |
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_int2int(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 460 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 461 | kdata->integer = smp->data.sint; |
| 462 | return (void *)&kdata->integer; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 463 | } |
| 464 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 465 | 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] | 466 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 467 | if (smp->type == SMP_T_IPV6) { |
| 468 | v6tov4(&kdata->ip, &smp->data.ipv6); |
| 469 | return (void *)&kdata->ip.s_addr; |
| 470 | } |
| 471 | else { |
| 472 | return (void *)&smp->data.ipv4.s_addr; |
| 473 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 476 | 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] | 477 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 478 | if (smp->type == SMP_T_IPV6) { |
| 479 | return (void *)&smp->data.ipv6.s6_addr; |
| 480 | } |
| 481 | else { |
| 482 | v4tov6(&kdata->ipv6, &smp->data.ipv4); |
| 483 | return (void *)&kdata->ipv6.s6_addr; |
| 484 | } |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 485 | } |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 486 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 487 | 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] | 488 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 489 | if (smp->type == SMP_T_IPV6) { |
| 490 | if (!v6tov4(&kdata->ip, &smp->data.ipv6)) |
| 491 | return NULL; |
| 492 | kdata->integer = ntohl(kdata->ip.s_addr); |
| 493 | } |
| 494 | else { |
| 495 | kdata->integer = ntohl(smp->data.ipv4.s_addr); |
| 496 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 497 | return (void *)&kdata->integer; |
| 498 | } |
| 499 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 500 | 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] | 501 | { |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 502 | kdata->ip.s_addr = htonl((unsigned int)smp->data.sint); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 503 | return (void *)&kdata->ip.s_addr; |
| 504 | } |
| 505 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 506 | 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] | 507 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 508 | *len = smp->data.str.len; |
| 509 | return (void *)smp->data.str.str; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 510 | } |
| 511 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 512 | 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] | 513 | { |
Willy Tarreau | 803685f | 2013-12-02 23:17:27 +0100 | [diff] [blame] | 514 | if (smp->type == SMP_T_IPV6) { |
| 515 | if (!inet_ntop(AF_INET6, &smp->data.ipv6, kdata->buf, *len)) |
| 516 | return NULL; |
| 517 | } |
| 518 | else { |
| 519 | if (!inet_ntop(AF_INET, &smp->data.ipv4, kdata->buf, *len)) |
| 520 | return NULL; |
| 521 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 522 | |
| 523 | *len = strlen((const char *)kdata->buf); |
| 524 | return (void *)kdata->buf; |
| 525 | } |
| 526 | |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 527 | static void *k_ip2bin(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
| 528 | { |
| 529 | if (smp->type == SMP_T_IPV4) { |
| 530 | if (*len > 4) |
| 531 | *len = 4; |
| 532 | memcpy(kdata->buf, &smp->data.ipv4, *len); |
| 533 | } |
| 534 | else if (smp->type == SMP_T_IPV6) { |
| 535 | if (*len > 16) |
| 536 | *len = 16; |
| 537 | memcpy(kdata->buf, &smp->data.ipv6, *len); |
| 538 | } |
| 539 | else |
| 540 | *len = 0; |
| 541 | return (void *)kdata->buf; |
| 542 | } |
| 543 | |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 544 | static void *k_bin2str(struct sample *smp, union stktable_key_data *kdata, size_t *len) |
| 545 | { |
| 546 | unsigned char c; |
| 547 | int ptr = 0; |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 548 | int max = *len; |
| 549 | int size = 0; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 550 | |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 551 | while (ptr < smp->data.str.len && size <= max - 2) { |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 552 | c = smp->data.str.str[ptr++]; |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 553 | kdata->buf[size++] = hextab[(c >> 4) & 0xF]; |
| 554 | kdata->buf[size++] = hextab[c & 0xF]; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 555 | } |
Willy Tarreau | f22180f | 2012-12-09 11:08:14 +0100 | [diff] [blame] | 556 | *len = size; |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 557 | return (void *)kdata->buf; |
| 558 | } |
| 559 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 560 | 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] | 561 | { |
| 562 | void *key; |
| 563 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 564 | key = (void *)lltoa_r(smp->data.sint, kdata->buf, *len); |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 565 | if (!key) |
| 566 | return NULL; |
| 567 | |
| 568 | *len = strlen((const char *)key); |
| 569 | return key; |
| 570 | } |
| 571 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 572 | 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] | 573 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 574 | if (!buf2ip(smp->data.str.str, smp->data.str.len, &kdata->ip)) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 575 | return NULL; |
| 576 | |
| 577 | return (void *)&kdata->ip.s_addr; |
| 578 | } |
| 579 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 580 | 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] | 581 | { |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 582 | if (!inet_pton(AF_INET6, smp->data.str.str, &kdata->ipv6)) |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 583 | return NULL; |
| 584 | |
| 585 | return (void *)&kdata->ipv6.s6_addr; |
| 586 | } |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 587 | |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 588 | 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] | 589 | { |
| 590 | int i; |
| 591 | |
| 592 | kdata->integer = 0; |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 593 | for (i = 0; i < smp->data.str.len; i++) { |
| 594 | uint32_t val = smp->data.str.str[i] - '0'; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 595 | |
| 596 | if (val > 9) |
| 597 | break; |
| 598 | |
| 599 | kdata->integer = kdata->integer * 10 + val; |
| 600 | } |
| 601 | return (void *)&kdata->integer; |
| 602 | } |
| 603 | |
| 604 | /*****************************************************************/ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 605 | /* typed sample to typed table key matrix: */ |
| 606 | /* sample_to_key[from sample type][to table key type] */ |
| 607 | /* NULL pointer used for impossible sample casts */ |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 608 | /*****************************************************************/ |
| 609 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 610 | typedef void *(*sample_to_key_fct)(struct sample *smp, union stktable_key_data *kdata, size_t *len); |
| 611 | 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] | 612 | /* table type: IP IPV6 INTEGER STRING BINARY */ |
Emeric Brun | 31c5653 | 2015-06-16 18:26:17 +0200 | [diff] [blame] | 613 | /* patt. type: ANY */ { k_ip2ip, k_ip2ipv6, k_int2int, k_str2str, k_str2str }, |
| 614 | /* BOOL */ { NULL, NULL, k_int2int, k_int2str, NULL }, |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 615 | /* SINT */ { k_int2ip, NULL, k_int2int, k_int2str, NULL }, |
Thierry FOURNIER | b805f71 | 2013-11-26 20:47:54 +0100 | [diff] [blame] | 616 | /* ADDR */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, NULL }, |
Willy Tarreau | 9700e5c | 2014-07-15 21:03:26 +0200 | [diff] [blame] | 617 | /* IPV4 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, k_ip2bin }, |
| 618 | /* IPV6 */ { k_ip2ip, k_ip2ipv6, k_ip2int, k_ip2str, k_ip2bin }, |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 619 | /* STR */ { k_str2ip, k_str2ipv6, k_str2int, k_str2str, k_str2str }, |
Emeric Brun | 8ac33d9 | 2012-10-17 13:36:06 +0200 | [diff] [blame] | 620 | /* BIN */ { NULL, NULL, NULL, k_bin2str, k_str2str }, |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 621 | }; |
| 622 | |
| 623 | |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 624 | /* Prepares a stktable_key from a sample <smp> to search into table <t>. |
| 625 | * Returns NULL if the sample could not be converted (eg: no matching type), |
| 626 | * otherwise a pointer to the static stktable_key filled with what is needed |
| 627 | * for the lookup. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 628 | */ |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 629 | struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 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 | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 673 | * Process a fetch + format conversion as defined by the sample expression <expr> |
| 674 | * on request or response considering the <opt> parameter. Returns either NULL if |
| 675 | * no key could be extracted, or a pointer to the converted result stored in |
| 676 | * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset |
| 677 | * and its flags will be initialized so that the caller gets a copy of the input |
Willy Tarreau | 6bcb0a8 | 2014-07-30 08:56:35 +0200 | [diff] [blame] | 678 | * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present |
| 679 | * without SMP_OPT_FINAL). The output will be usable like this : |
| 680 | * |
| 681 | * return MAY_CHANGE FINAL Meaning for the sample |
| 682 | * NULL 0 * Not present and will never be (eg: header) |
| 683 | * NULL 1 0 Not present or unstable, could change (eg: req_len) |
| 684 | * NULL 1 1 Not present, will not change anymore |
| 685 | * smp 0 * Present and will not change (eg: header) |
| 686 | * smp 1 0 not possible |
| 687 | * smp 1 1 Present, last known value (eg: request length) |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 688 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 689 | struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm, |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 690 | unsigned int opt, struct sample_expr *expr, struct sample *smp) |
| 691 | { |
| 692 | if (smp) |
| 693 | memset(smp, 0, sizeof(*smp)); |
| 694 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 695 | smp = sample_process(px, sess, strm, opt, expr, smp); |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 696 | if (!smp) |
| 697 | return NULL; |
| 698 | |
| 699 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 700 | return NULL; /* we can only use stable samples */ |
| 701 | |
| 702 | return smp_to_stkey(smp, t); |
| 703 | } |
| 704 | |
| 705 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 706 | * 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] | 707 | * type <table_type>, otherwise zero. Used in configuration check. |
| 708 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 709 | int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 710 | { |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 711 | int out_type; |
| 712 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 713 | if (table_type >= STKTABLE_TYPES) |
| 714 | return 0; |
| 715 | |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 716 | out_type = smp_expr_output_type(expr); |
| 717 | if (!sample_to_key[out_type][table_type]) |
| 718 | return 0; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 719 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 720 | return 1; |
| 721 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 722 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 723 | /* Extra data types processing : after the last one, some room may remain |
| 724 | * before STKTABLE_DATA_TYPES that may be used to register extra data types |
| 725 | * at run time. |
| 726 | */ |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 727 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 728 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT }, |
| 729 | [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT }, |
Willy Tarreau | ba2ffd1 | 2013-05-29 15:54:14 +0200 | [diff] [blame] | 730 | [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] | 731 | [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT }, |
| 732 | [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 733 | [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT }, |
| 734 | [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT }, |
| 735 | [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 736 | [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT }, |
| 737 | [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 738 | [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT }, |
| 739 | [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 740 | [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL }, |
| 741 | [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 742 | [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL }, |
| 743 | [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] | 744 | }; |
| 745 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 746 | /* Registers stick-table extra data type with index <idx>, name <name>, type |
| 747 | * <std_type> and arg type <arg_type>. If the index is negative, the next free |
| 748 | * index is automatically allocated. The allocated index is returned, or -1 if |
| 749 | * no free index was found or <name> was already registered. The <name> is used |
| 750 | * directly as a pointer, so if it's not stable, the caller must allocate it. |
| 751 | */ |
| 752 | int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type) |
| 753 | { |
| 754 | if (idx < 0) { |
| 755 | for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) { |
| 756 | if (!stktable_data_types[idx].name) |
| 757 | break; |
| 758 | |
| 759 | if (strcmp(stktable_data_types[idx].name, name) == 0) |
| 760 | return -1; |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | if (idx >= STKTABLE_DATA_TYPES) |
| 765 | return -1; |
| 766 | |
| 767 | if (stktable_data_types[idx].name != NULL) |
| 768 | return -1; |
| 769 | |
| 770 | stktable_data_types[idx].name = name; |
| 771 | stktable_data_types[idx].std_type = std_type; |
| 772 | stktable_data_types[idx].arg_type = arg_type; |
| 773 | return idx; |
| 774 | } |
| 775 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 776 | /* |
| 777 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 778 | * or <0 if not found. |
| 779 | */ |
| 780 | int stktable_get_data_type(char *name) |
| 781 | { |
| 782 | int type; |
| 783 | |
| 784 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 785 | if (!stktable_data_types[type].name) |
| 786 | continue; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 787 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 788 | return type; |
| 789 | } |
| 790 | return -1; |
| 791 | } |
| 792 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 793 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 794 | * it up into this table. Returns true if found, false otherwise. The input |
| 795 | * type is STR so that input samples are converted to string (since all types |
| 796 | * can be converted to strings), then the function casts the string again into |
| 797 | * the table's type. This is a double conversion, but in the future we might |
| 798 | * support automatic input types to perform the cast on the fly. |
| 799 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 800 | static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 801 | { |
| 802 | struct stktable *t; |
| 803 | struct stktable_key *key; |
| 804 | struct stksess *ts; |
| 805 | |
| 806 | t = &arg_p[0].data.prx->table; |
| 807 | |
| 808 | key = smp_to_stkey(smp, t); |
| 809 | if (!key) |
| 810 | return 0; |
| 811 | |
| 812 | ts = stktable_lookup_key(t, key); |
| 813 | |
| 814 | smp->type = SMP_T_BOOL; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 815 | smp->data.sint = !!ts; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 816 | smp->flags = SMP_F_VOL_TEST; |
| 817 | return 1; |
| 818 | } |
| 819 | |
| 820 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 821 | * it up into this table. Returns the data rate received from clients in bytes/s |
| 822 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 823 | * be easily performed. If the inspected parameter is not stored in the table, |
| 824 | * <not found> is returned. |
| 825 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 826 | static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 827 | { |
| 828 | struct stktable *t; |
| 829 | struct stktable_key *key; |
| 830 | struct stksess *ts; |
| 831 | void *ptr; |
| 832 | |
| 833 | t = &arg_p[0].data.prx->table; |
| 834 | |
| 835 | key = smp_to_stkey(smp, t); |
| 836 | if (!key) |
| 837 | return 0; |
| 838 | |
| 839 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 840 | smp->type = SMP_T_SINT; |
| 841 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 842 | |
| 843 | ts = stktable_lookup_key(t, key); |
| 844 | if (!ts) /* key not present */ |
| 845 | return 1; |
| 846 | |
| 847 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE); |
| 848 | if (!ptr) |
| 849 | return 0; /* parameter not stored */ |
| 850 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 851 | smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 852 | t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u); |
| 853 | return 1; |
| 854 | } |
| 855 | |
| 856 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 857 | * it up into this table. Returns the cumulated number of connections for the key |
| 858 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 859 | * be easily performed. If the inspected parameter is not stored in the table, |
| 860 | * <not found> is returned. |
| 861 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 862 | static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 863 | { |
| 864 | struct stktable *t; |
| 865 | struct stktable_key *key; |
| 866 | struct stksess *ts; |
| 867 | void *ptr; |
| 868 | |
| 869 | t = &arg_p[0].data.prx->table; |
| 870 | |
| 871 | key = smp_to_stkey(smp, t); |
| 872 | if (!key) |
| 873 | return 0; |
| 874 | |
| 875 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 876 | smp->type = SMP_T_SINT; |
| 877 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 878 | |
| 879 | ts = stktable_lookup_key(t, key); |
| 880 | if (!ts) /* key not present */ |
| 881 | return 1; |
| 882 | |
| 883 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT); |
| 884 | if (!ptr) |
| 885 | return 0; /* parameter not stored */ |
| 886 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 887 | smp->data.sint = stktable_data_cast(ptr, conn_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 888 | return 1; |
| 889 | } |
| 890 | |
| 891 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 892 | * it up into this table. Returns the number of concurrent connections for the |
| 893 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 894 | * can be easily performed. If the inspected parameter is not stored in the |
| 895 | * table, <not found> is returned. |
| 896 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 897 | static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 898 | { |
| 899 | struct stktable *t; |
| 900 | struct stktable_key *key; |
| 901 | struct stksess *ts; |
| 902 | void *ptr; |
| 903 | |
| 904 | t = &arg_p[0].data.prx->table; |
| 905 | |
| 906 | key = smp_to_stkey(smp, t); |
| 907 | if (!key) |
| 908 | return 0; |
| 909 | |
| 910 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 911 | smp->type = SMP_T_SINT; |
| 912 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 913 | |
| 914 | ts = stktable_lookup_key(t, key); |
| 915 | if (!ts) /* key not present */ |
| 916 | return 1; |
| 917 | |
| 918 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR); |
| 919 | if (!ptr) |
| 920 | return 0; /* parameter not stored */ |
| 921 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 922 | smp->data.sint = stktable_data_cast(ptr, conn_cur); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 923 | return 1; |
| 924 | } |
| 925 | |
| 926 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 927 | * it up into this table. Returns the rate of incoming connections from the key |
| 928 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 929 | * be easily performed. If the inspected parameter is not stored in the table, |
| 930 | * <not found> is returned. |
| 931 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 932 | static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 933 | { |
| 934 | struct stktable *t; |
| 935 | struct stktable_key *key; |
| 936 | struct stksess *ts; |
| 937 | void *ptr; |
| 938 | |
| 939 | t = &arg_p[0].data.prx->table; |
| 940 | |
| 941 | key = smp_to_stkey(smp, t); |
| 942 | if (!key) |
| 943 | return 0; |
| 944 | |
| 945 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 946 | smp->type = SMP_T_SINT; |
| 947 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 948 | |
| 949 | ts = stktable_lookup_key(t, key); |
| 950 | if (!ts) /* key not present */ |
| 951 | return 1; |
| 952 | |
| 953 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE); |
| 954 | if (!ptr) |
| 955 | return 0; /* parameter not stored */ |
| 956 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 957 | smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 958 | t->data_arg[STKTABLE_DT_CONN_RATE].u); |
| 959 | return 1; |
| 960 | } |
| 961 | |
| 962 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 963 | * it up into this table. Returns the data rate sent to clients in bytes/s |
| 964 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 965 | * be easily performed. If the inspected parameter is not stored in the table, |
| 966 | * <not found> is returned. |
| 967 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 968 | static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 969 | { |
| 970 | struct stktable *t; |
| 971 | struct stktable_key *key; |
| 972 | struct stksess *ts; |
| 973 | void *ptr; |
| 974 | |
| 975 | t = &arg_p[0].data.prx->table; |
| 976 | |
| 977 | key = smp_to_stkey(smp, t); |
| 978 | if (!key) |
| 979 | return 0; |
| 980 | |
| 981 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 982 | smp->type = SMP_T_SINT; |
| 983 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 984 | |
| 985 | ts = stktable_lookup_key(t, key); |
| 986 | if (!ts) /* key not present */ |
| 987 | return 1; |
| 988 | |
| 989 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE); |
| 990 | if (!ptr) |
| 991 | return 0; /* parameter not stored */ |
| 992 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 993 | smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 994 | t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u); |
| 995 | return 1; |
| 996 | } |
| 997 | |
| 998 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 999 | * it up into this table. Returns the value of the GPC0 counter for the key |
| 1000 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1001 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1002 | * <not found> is returned. |
| 1003 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1004 | static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1005 | { |
| 1006 | struct stktable *t; |
| 1007 | struct stktable_key *key; |
| 1008 | struct stksess *ts; |
| 1009 | void *ptr; |
| 1010 | |
| 1011 | t = &arg_p[0].data.prx->table; |
| 1012 | |
| 1013 | key = smp_to_stkey(smp, t); |
| 1014 | if (!key) |
| 1015 | return 0; |
| 1016 | |
| 1017 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1018 | smp->type = SMP_T_SINT; |
| 1019 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1020 | |
| 1021 | ts = stktable_lookup_key(t, key); |
| 1022 | if (!ts) /* key not present */ |
| 1023 | return 1; |
| 1024 | |
| 1025 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0); |
| 1026 | if (!ptr) |
| 1027 | return 0; /* parameter not stored */ |
| 1028 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1029 | smp->data.sint = stktable_data_cast(ptr, gpc0); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1030 | return 1; |
| 1031 | } |
| 1032 | |
| 1033 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1034 | * it up into this table. Returns the event rate of the GPC0 counter for the key |
| 1035 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1036 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1037 | * <not found> is returned. |
| 1038 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1039 | static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1040 | { |
| 1041 | struct stktable *t; |
| 1042 | struct stktable_key *key; |
| 1043 | struct stksess *ts; |
| 1044 | void *ptr; |
| 1045 | |
| 1046 | t = &arg_p[0].data.prx->table; |
| 1047 | |
| 1048 | key = smp_to_stkey(smp, t); |
| 1049 | if (!key) |
| 1050 | return 0; |
| 1051 | |
| 1052 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1053 | smp->type = SMP_T_SINT; |
| 1054 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1055 | |
| 1056 | ts = stktable_lookup_key(t, key); |
| 1057 | if (!ts) /* key not present */ |
| 1058 | return 1; |
| 1059 | |
| 1060 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE); |
| 1061 | if (!ptr) |
| 1062 | return 0; /* parameter not stored */ |
| 1063 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1064 | smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1065 | t->data_arg[STKTABLE_DT_GPC0_RATE].u); |
| 1066 | return 1; |
| 1067 | } |
| 1068 | |
| 1069 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1070 | * it up into this table. Returns the cumulated number of HTTP request errors |
| 1071 | * for the key if the key is present in the table, otherwise zero, so that |
| 1072 | * comparisons can be easily performed. If the inspected parameter is not stored |
| 1073 | * in the table, <not found> is returned. |
| 1074 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1075 | static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1076 | { |
| 1077 | struct stktable *t; |
| 1078 | struct stktable_key *key; |
| 1079 | struct stksess *ts; |
| 1080 | void *ptr; |
| 1081 | |
| 1082 | t = &arg_p[0].data.prx->table; |
| 1083 | |
| 1084 | key = smp_to_stkey(smp, t); |
| 1085 | if (!key) |
| 1086 | return 0; |
| 1087 | |
| 1088 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1089 | smp->type = SMP_T_SINT; |
| 1090 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1091 | |
| 1092 | ts = stktable_lookup_key(t, key); |
| 1093 | if (!ts) /* key not present */ |
| 1094 | return 1; |
| 1095 | |
| 1096 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 1097 | if (!ptr) |
| 1098 | return 0; /* parameter not stored */ |
| 1099 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1100 | smp->data.sint = stktable_data_cast(ptr, http_err_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1101 | return 1; |
| 1102 | } |
| 1103 | |
| 1104 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1105 | * it up into this table. Returns the HTTP request error rate the key |
| 1106 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1107 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1108 | * <not found> is returned. |
| 1109 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1110 | static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1111 | { |
| 1112 | struct stktable *t; |
| 1113 | struct stktable_key *key; |
| 1114 | struct stksess *ts; |
| 1115 | void *ptr; |
| 1116 | |
| 1117 | t = &arg_p[0].data.prx->table; |
| 1118 | |
| 1119 | key = smp_to_stkey(smp, t); |
| 1120 | if (!key) |
| 1121 | return 0; |
| 1122 | |
| 1123 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1124 | smp->type = SMP_T_SINT; |
| 1125 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1126 | |
| 1127 | ts = stktable_lookup_key(t, key); |
| 1128 | if (!ts) /* key not present */ |
| 1129 | return 1; |
| 1130 | |
| 1131 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 1132 | if (!ptr) |
| 1133 | return 0; /* parameter not stored */ |
| 1134 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1135 | smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1136 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u); |
| 1137 | return 1; |
| 1138 | } |
| 1139 | |
| 1140 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1141 | * it up into this table. Returns the cumulated number of HTTP request for the |
| 1142 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1143 | * can be easily performed. If the inspected parameter is not stored in the |
| 1144 | * table, <not found> is returned. |
| 1145 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1146 | static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1147 | { |
| 1148 | struct stktable *t; |
| 1149 | struct stktable_key *key; |
| 1150 | struct stksess *ts; |
| 1151 | void *ptr; |
| 1152 | |
| 1153 | t = &arg_p[0].data.prx->table; |
| 1154 | |
| 1155 | key = smp_to_stkey(smp, t); |
| 1156 | if (!key) |
| 1157 | return 0; |
| 1158 | |
| 1159 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1160 | smp->type = SMP_T_SINT; |
| 1161 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1162 | |
| 1163 | ts = stktable_lookup_key(t, key); |
| 1164 | if (!ts) /* key not present */ |
| 1165 | return 1; |
| 1166 | |
| 1167 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 1168 | if (!ptr) |
| 1169 | return 0; /* parameter not stored */ |
| 1170 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1171 | smp->data.sint = stktable_data_cast(ptr, http_req_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1172 | return 1; |
| 1173 | } |
| 1174 | |
| 1175 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1176 | * it up into this table. Returns the HTTP request rate the key if the key is |
| 1177 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1178 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1179 | * is returned. |
| 1180 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1181 | static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1182 | { |
| 1183 | struct stktable *t; |
| 1184 | struct stktable_key *key; |
| 1185 | struct stksess *ts; |
| 1186 | void *ptr; |
| 1187 | |
| 1188 | t = &arg_p[0].data.prx->table; |
| 1189 | |
| 1190 | key = smp_to_stkey(smp, t); |
| 1191 | if (!key) |
| 1192 | return 0; |
| 1193 | |
| 1194 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1195 | smp->type = SMP_T_SINT; |
| 1196 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1197 | |
| 1198 | ts = stktable_lookup_key(t, key); |
| 1199 | if (!ts) /* key not present */ |
| 1200 | return 1; |
| 1201 | |
| 1202 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 1203 | if (!ptr) |
| 1204 | return 0; /* parameter not stored */ |
| 1205 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1206 | smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1207 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u); |
| 1208 | return 1; |
| 1209 | } |
| 1210 | |
| 1211 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1212 | * it up into this table. Returns the volume of datareceived from clients in kbytes |
| 1213 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1214 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1215 | * <not found> is returned. |
| 1216 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1217 | static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1218 | { |
| 1219 | struct stktable *t; |
| 1220 | struct stktable_key *key; |
| 1221 | struct stksess *ts; |
| 1222 | void *ptr; |
| 1223 | |
| 1224 | t = &arg_p[0].data.prx->table; |
| 1225 | |
| 1226 | key = smp_to_stkey(smp, t); |
| 1227 | if (!key) |
| 1228 | return 0; |
| 1229 | |
| 1230 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1231 | smp->type = SMP_T_SINT; |
| 1232 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1233 | |
| 1234 | ts = stktable_lookup_key(t, key); |
| 1235 | if (!ts) /* key not present */ |
| 1236 | return 1; |
| 1237 | |
| 1238 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT); |
| 1239 | if (!ptr) |
| 1240 | return 0; /* parameter not stored */ |
| 1241 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1242 | smp->data.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1243 | return 1; |
| 1244 | } |
| 1245 | |
| 1246 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1247 | * it up into this table. Returns the volume of data sent to clients in kbytes |
| 1248 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1249 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1250 | * <not found> is returned. |
| 1251 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1252 | static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1253 | { |
| 1254 | struct stktable *t; |
| 1255 | struct stktable_key *key; |
| 1256 | struct stksess *ts; |
| 1257 | void *ptr; |
| 1258 | |
| 1259 | t = &arg_p[0].data.prx->table; |
| 1260 | |
| 1261 | key = smp_to_stkey(smp, t); |
| 1262 | if (!key) |
| 1263 | return 0; |
| 1264 | |
| 1265 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1266 | smp->type = SMP_T_SINT; |
| 1267 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1268 | |
| 1269 | ts = stktable_lookup_key(t, key); |
| 1270 | if (!ts) /* key not present */ |
| 1271 | return 1; |
| 1272 | |
| 1273 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT); |
| 1274 | if (!ptr) |
| 1275 | return 0; /* parameter not stored */ |
| 1276 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1277 | smp->data.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1278 | return 1; |
| 1279 | } |
| 1280 | |
| 1281 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1282 | * it up into this table. Returns the server ID associated with the key if the |
| 1283 | * key is present in the table, otherwise zero, so that comparisons can be |
| 1284 | * easily performed. If the inspected parameter is not stored in the table, |
| 1285 | * <not found> is returned. |
| 1286 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1287 | static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1288 | { |
| 1289 | struct stktable *t; |
| 1290 | struct stktable_key *key; |
| 1291 | struct stksess *ts; |
| 1292 | void *ptr; |
| 1293 | |
| 1294 | t = &arg_p[0].data.prx->table; |
| 1295 | |
| 1296 | key = smp_to_stkey(smp, t); |
| 1297 | if (!key) |
| 1298 | return 0; |
| 1299 | |
| 1300 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1301 | smp->type = SMP_T_SINT; |
| 1302 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1303 | |
| 1304 | ts = stktable_lookup_key(t, key); |
| 1305 | if (!ts) /* key not present */ |
| 1306 | return 1; |
| 1307 | |
| 1308 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID); |
| 1309 | if (!ptr) |
| 1310 | return 0; /* parameter not stored */ |
| 1311 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1312 | smp->data.sint = stktable_data_cast(ptr, server_id); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1313 | return 1; |
| 1314 | } |
| 1315 | |
| 1316 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1317 | * it up into this table. Returns the cumulated number of sessions for the |
| 1318 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1319 | * can be easily performed. If the inspected parameter is not stored in the |
| 1320 | * table, <not found> is returned. |
| 1321 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1322 | static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1323 | { |
| 1324 | struct stktable *t; |
| 1325 | struct stktable_key *key; |
| 1326 | struct stksess *ts; |
| 1327 | void *ptr; |
| 1328 | |
| 1329 | t = &arg_p[0].data.prx->table; |
| 1330 | |
| 1331 | key = smp_to_stkey(smp, t); |
| 1332 | if (!key) |
| 1333 | return 0; |
| 1334 | |
| 1335 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1336 | smp->type = SMP_T_SINT; |
| 1337 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1338 | |
| 1339 | ts = stktable_lookup_key(t, key); |
| 1340 | if (!ts) /* key not present */ |
| 1341 | return 1; |
| 1342 | |
| 1343 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT); |
| 1344 | if (!ptr) |
| 1345 | return 0; /* parameter not stored */ |
| 1346 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1347 | smp->data.sint = stktable_data_cast(ptr, sess_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1348 | return 1; |
| 1349 | } |
| 1350 | |
| 1351 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1352 | * it up into this table. Returns the session rate the key if the key is |
| 1353 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1354 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1355 | * is returned. |
| 1356 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1357 | static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1358 | { |
| 1359 | struct stktable *t; |
| 1360 | struct stktable_key *key; |
| 1361 | struct stksess *ts; |
| 1362 | void *ptr; |
| 1363 | |
| 1364 | t = &arg_p[0].data.prx->table; |
| 1365 | |
| 1366 | key = smp_to_stkey(smp, t); |
| 1367 | if (!key) |
| 1368 | return 0; |
| 1369 | |
| 1370 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1371 | smp->type = SMP_T_SINT; |
| 1372 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1373 | |
| 1374 | ts = stktable_lookup_key(t, key); |
| 1375 | if (!ts) /* key not present */ |
| 1376 | return 1; |
| 1377 | |
| 1378 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE); |
| 1379 | if (!ptr) |
| 1380 | return 0; /* parameter not stored */ |
| 1381 | |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1382 | smp->data.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1383 | t->data_arg[STKTABLE_DT_SESS_RATE].u); |
| 1384 | return 1; |
| 1385 | } |
| 1386 | |
| 1387 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1388 | * it up into this table. Returns the amount of concurrent connections tracking |
| 1389 | * the same key if the key is present in the table, otherwise zero, so that |
| 1390 | * comparisons can be easily performed. If the inspected parameter is not |
| 1391 | * stored in the table, <not found> is returned. |
| 1392 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1393 | static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private) |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1394 | { |
| 1395 | struct stktable *t; |
| 1396 | struct stktable_key *key; |
| 1397 | struct stksess *ts; |
| 1398 | |
| 1399 | t = &arg_p[0].data.prx->table; |
| 1400 | |
| 1401 | key = smp_to_stkey(smp, t); |
| 1402 | if (!key) |
| 1403 | return 0; |
| 1404 | |
| 1405 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1406 | smp->type = SMP_T_SINT; |
| 1407 | smp->data.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1408 | |
| 1409 | ts = stktable_lookup_key(t, key); |
| 1410 | if (ts) |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1411 | smp->data.sint = ts->ref_cnt; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1412 | |
| 1413 | return 1; |
| 1414 | } |
| 1415 | |
| 1416 | |
| 1417 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 1418 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
| 1419 | { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_BOOL }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1420 | { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1421 | { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1422 | { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1423 | { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1424 | { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1425 | { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1426 | { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1427 | { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1428 | { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1429 | { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1430 | { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1431 | { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1432 | { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1433 | { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1434 | { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1435 | { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
| 1436 | { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT }, |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1437 | { /* END */ }, |
| 1438 | }}; |
| 1439 | |
| 1440 | __attribute__((constructor)) |
| 1441 | static void __stick_table_init(void) |
| 1442 | { |
| 1443 | /* register sample fetch and format conversion keywords */ |
| 1444 | sample_register_convs(&sample_conv_kws); |
| 1445 | } |