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