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> |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 15 | #include <errno.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 16 | |
| 17 | #include <common/config.h> |
| 18 | #include <common/memory.h> |
| 19 | #include <common/mini-clist.h> |
| 20 | #include <common/standard.h> |
| 21 | #include <common/time.h> |
| 22 | |
| 23 | #include <ebmbtree.h> |
| 24 | #include <ebsttree.h> |
| 25 | |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 26 | #include <types/cli.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 27 | #include <types/global.h> |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 28 | #include <types/stats.h> |
| 29 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 30 | #include <proto/arg.h> |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 31 | #include <proto/cli.h> |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 32 | #include <proto/log.h> |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 33 | #include <proto/proto_http.h> |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 34 | #include <proto/proto_tcp.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 35 | #include <proto/proxy.h> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 36 | #include <proto/sample.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 37 | #include <proto/stream.h> |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 38 | #include <proto/stream_interface.h> |
Willy Tarreau | 68129b9 | 2010-06-06 16:06:52 +0200 | [diff] [blame] | 39 | #include <proto/stick_table.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 40 | #include <proto/task.h> |
Emeric Brun | 32da3c4 | 2010-09-23 18:39:19 +0200 | [diff] [blame] | 41 | #include <proto/peers.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 42 | #include <proto/tcp_rules.h> |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 43 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 44 | /* structure used to return a table key built from a sample */ |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 45 | static struct stktable_key static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 46 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 47 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 48 | * Free an allocated sticky session <ts>, and decrease sticky sessions counter |
| 49 | * in table <t>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 50 | */ |
| 51 | void stksess_free(struct stktable *t, struct stksess *ts) |
| 52 | { |
| 53 | t->current--; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 54 | pool_free2(t->pool, (void *)ts - t->data_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 58 | * Kill an stksess (only if its ref_cnt is zero). |
| 59 | */ |
| 60 | void stksess_kill(struct stktable *t, struct stksess *ts) |
| 61 | { |
| 62 | if (ts->ref_cnt) |
| 63 | return; |
| 64 | |
| 65 | eb32_delete(&ts->exp); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 66 | eb32_delete(&ts->upd); |
Willy Tarreau | f6efda1 | 2010-08-03 20:34:06 +0200 | [diff] [blame] | 67 | ebmb_delete(&ts->key); |
| 68 | stksess_free(t, ts); |
| 69 | } |
| 70 | |
| 71 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 72 | * Initialize or update the key in the sticky session <ts> present in table <t> |
| 73 | * from the value present in <key>. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 74 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 75 | 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] | 76 | { |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 77 | if (t->type != SMP_T_STR) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 78 | memcpy(ts->key.key, key->key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 79 | else { |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 80 | memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len)); |
| 81 | ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | /* |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 87 | * Init sticky session <ts> of table <t>. The data parts are cleared and <ts> |
| 88 | * is returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 89 | */ |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 90 | static struct stksess *stksess_init(struct stktable *t, struct stksess * ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 91 | { |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 92 | memset((void *)ts - t->data_size, 0, t->data_size); |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 93 | ts->ref_cnt = 0; |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 94 | ts->key.node.leaf_p = NULL; |
| 95 | ts->exp.node.leaf_p = NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 96 | ts->upd.node.leaf_p = NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 97 | return ts; |
| 98 | } |
| 99 | |
| 100 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 101 | * Trash oldest <to_batch> sticky sessions from table <t> |
| 102 | * Returns number of trashed sticky sessions. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 103 | */ |
Willy Tarreau | 3a925c1 | 2013-09-04 17:54:01 +0200 | [diff] [blame] | 104 | int stktable_trash_oldest(struct stktable *t, int to_batch) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 105 | { |
| 106 | struct stksess *ts; |
| 107 | struct eb32_node *eb; |
| 108 | int batched = 0; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 109 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 110 | |
| 111 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 112 | |
| 113 | while (batched < to_batch) { |
| 114 | |
| 115 | if (unlikely(!eb)) { |
| 116 | /* we might have reached the end of the tree, typically because |
| 117 | * <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] | 118 | * half. Let's loop back to the beginning of the tree now if we |
| 119 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 120 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 121 | if (looped) |
| 122 | break; |
| 123 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 124 | eb = eb32_first(&t->exps); |
| 125 | if (likely(!eb)) |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 130 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 131 | eb = eb32_next(eb); |
| 132 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 133 | /* don't delete an entry which is currently referenced */ |
| 134 | if (ts->ref_cnt) |
| 135 | continue; |
| 136 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 137 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 138 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 139 | if (ts->expire != ts->exp.key) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 140 | if (!tick_isset(ts->expire)) |
| 141 | continue; |
| 142 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 143 | ts->exp.key = ts->expire; |
| 144 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 145 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 146 | if (!eb || eb->key > ts->exp.key) |
| 147 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 148 | |
| 149 | continue; |
| 150 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 151 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 152 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 153 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 154 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 155 | stksess_free(t, ts); |
| 156 | batched++; |
| 157 | } |
| 158 | |
| 159 | return batched; |
| 160 | } |
| 161 | |
| 162 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 163 | * Allocate and initialise a new sticky session. |
| 164 | * The new sticky session is returned or NULL in case of lack of memory. |
| 165 | * 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] | 166 | * stksess_free(). Table <t>'s sticky session counter is increased. If <key> |
| 167 | * is not NULL, it is assigned to the new session. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 168 | */ |
| 169 | struct stksess *stksess_new(struct stktable *t, struct stktable_key *key) |
| 170 | { |
| 171 | struct stksess *ts; |
| 172 | |
| 173 | if (unlikely(t->current == t->size)) { |
| 174 | if ( t->nopurge ) |
| 175 | return NULL; |
| 176 | |
Emeric Brun | fbce6d0 | 2010-09-23 18:10:00 +0200 | [diff] [blame] | 177 | if (!stktable_trash_oldest(t, (t->size >> 8) + 1)) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 178 | return NULL; |
| 179 | } |
| 180 | |
Vincent Bernat | ef8f4fe | 2016-11-17 15:42:40 +0100 | [diff] [blame] | 181 | ts = pool_alloc2(t->pool); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 182 | if (ts) { |
| 183 | t->current++; |
Willy Tarreau | 5179146 | 2016-11-18 18:21:39 +0100 | [diff] [blame] | 184 | ts = (void *)ts + t->data_size; |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 185 | stksess_init(t, ts); |
Willy Tarreau | a7b46b5 | 2013-04-11 16:55:37 +0200 | [diff] [blame] | 186 | if (key) |
| 187 | stksess_setkey(t, ts, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | return ts; |
| 191 | } |
| 192 | |
| 193 | /* |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 194 | * Looks in table <t> for a sticky session matching key <key>. |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 195 | * Returns pointer on requested sticky session or NULL if none was found. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 196 | */ |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 197 | struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 198 | { |
| 199 | struct ebmb_node *eb; |
| 200 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 201 | if (t->type == SMP_T_STR) |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 202 | 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] | 203 | else |
| 204 | eb = ebmb_lookup(&t->keys, key->key, t->key_size); |
| 205 | |
| 206 | if (unlikely(!eb)) { |
| 207 | /* no session found */ |
| 208 | return NULL; |
| 209 | } |
| 210 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 211 | return ebmb_entry(eb, struct stksess, key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 214 | /* Lookup and touch <key> in <table>, or create the entry if it does not exist. |
| 215 | * This is mainly used for situations where we want to refresh a key's usage so |
| 216 | * that it does not expire, and we want to have it created if it was not there. |
| 217 | * The stksess is returned, or NULL if it could not be created. |
| 218 | */ |
| 219 | struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key) |
| 220 | { |
| 221 | struct stksess *ts; |
| 222 | |
| 223 | ts = stktable_lookup_key(table, key); |
| 224 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 225 | return stktable_touch(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 226 | |
| 227 | /* entry does not exist, initialize a new one */ |
| 228 | ts = stksess_new(table, key); |
| 229 | if (likely(ts)) |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 230 | stktable_store(table, ts, 1); |
Willy Tarreau | 1f7e925 | 2010-06-20 12:27:21 +0200 | [diff] [blame] | 231 | return ts; |
| 232 | } |
| 233 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 234 | /* |
| 235 | * Looks in table <t> for a sticky session with same key as <ts>. |
| 236 | * Returns pointer on requested sticky session or NULL if none was found. |
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 | struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 239 | { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 240 | struct ebmb_node *eb; |
| 241 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 242 | if (t->type == SMP_T_STR) |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 243 | eb = ebst_lookup(&(t->keys), (char *)ts->key.key); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 244 | else |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 245 | eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 246 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 247 | if (unlikely(!eb)) |
| 248 | return NULL; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 249 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 250 | return ebmb_entry(eb, struct stksess, key); |
| 251 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 252 | |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 253 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 254 | * The table's expiration timer is updated if set. |
| 255 | */ |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 256 | struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts, |
| 257 | int local, int expire) |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 258 | { |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 259 | struct eb32_node * eb; |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 260 | ts->expire = expire; |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 261 | if (t->expire) { |
| 262 | t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next); |
| 263 | task_queue(t->exp_task); |
| 264 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 265 | |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 266 | /* If sync is enabled and update is local */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 267 | if (t->sync_task && local) { |
Emeric Brun | c703a9d | 2015-09-22 15:05:06 +0200 | [diff] [blame] | 268 | /* If this entry is not in the tree |
| 269 | or not scheduled for at least one peer */ |
| 270 | if (!ts->upd.node.leaf_p |
| 271 | || (int)(t->commitupdate - ts->upd.key) >= 0 |
| 272 | || (int)(ts->upd.key - t->localupdate) >= 0) { |
Emeric Brun | aaf5860 | 2015-06-15 17:23:30 +0200 | [diff] [blame] | 273 | ts->upd.key = ++t->update; |
| 274 | t->localupdate = t->update; |
| 275 | eb32_delete(&ts->upd); |
| 276 | eb = eb32_insert(&t->updates, &ts->upd); |
| 277 | if (eb != &ts->upd) { |
| 278 | eb32_delete(eb); |
| 279 | eb32_insert(&t->updates, &ts->upd); |
| 280 | } |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 281 | } |
| 282 | task_wakeup(t->sync_task, TASK_WOKEN_MSG); |
| 283 | } |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 284 | return ts; |
| 285 | } |
| 286 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 287 | /* Update the expiration timer for <ts> but do not touch its expiration node. |
| 288 | * The table's expiration timer is updated if set. The date of expiration coming from |
| 289 | * <t> stick-table configuration. |
| 290 | */ |
| 291 | struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local) |
| 292 | { |
| 293 | int expire = tick_add(now_ms, MS_TO_TICKS(t->expire)); |
| 294 | |
| 295 | return stktable_touch_with_exp(t, ts, local, expire); |
| 296 | } |
| 297 | |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 298 | /* Insert new sticky session <ts> in the table. It is assumed that it does not |
| 299 | * yet exist (the caller must check this). The table's timeout is updated if it |
| 300 | * is set. <ts> is returned. |
| 301 | */ |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 302 | struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local) |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 303 | { |
| 304 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 305 | stktable_touch(t, ts, local); |
Willy Tarreau | cb18364 | 2010-06-06 17:58:34 +0200 | [diff] [blame] | 306 | ts->exp.key = ts->expire; |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 307 | eb32_insert(&t->exps, &ts->exp); |
Willy Tarreau | f16d2b8 | 2010-06-06 15:38:59 +0200 | [diff] [blame] | 308 | return ts; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Frédéric Lécaille | 523cc9e | 2016-10-12 17:30:30 +0200 | [diff] [blame] | 311 | /* Same function as stktable_store(), but with <expire> as supplementary argument |
| 312 | * to set the date of expiration of <ts> new sticky session thanks to |
| 313 | * stktable_touch_with_exp(). |
| 314 | */ |
| 315 | struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts, |
| 316 | int local, int expire) |
| 317 | { |
| 318 | ebmb_insert(&t->keys, &ts->key, t->key_size); |
| 319 | stktable_touch_with_exp(t, ts, local, expire); |
| 320 | ts->exp.key = ts->expire; |
| 321 | eb32_insert(&t->exps, &ts->exp); |
| 322 | return ts; |
| 323 | } |
| 324 | |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 325 | /* Returns a valid or initialized stksess for the specified stktable_key in the |
| 326 | * specified table, or NULL if the key was NULL, or if no entry was found nor |
| 327 | * could be created. The entry's expiration is updated. |
| 328 | */ |
| 329 | struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key) |
| 330 | { |
| 331 | struct stksess *ts; |
| 332 | |
| 333 | if (!key) |
| 334 | return NULL; |
| 335 | |
| 336 | ts = stktable_lookup_key(table, key); |
| 337 | if (ts == NULL) { |
| 338 | /* entry does not exist, initialize a new one */ |
| 339 | ts = stksess_new(table, key); |
| 340 | if (!ts) |
| 341 | return NULL; |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 342 | stktable_store(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 343 | } |
| 344 | else |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 345 | stktable_touch(table, ts, 1); |
Willy Tarreau | 9ba2dcc | 2010-06-14 21:04:55 +0200 | [diff] [blame] | 346 | return ts; |
| 347 | } |
| 348 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 349 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 350 | * Trash expired sticky sessions from table <t>. The next expiration date is |
| 351 | * returned. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 352 | */ |
| 353 | static int stktable_trash_expired(struct stktable *t) |
| 354 | { |
| 355 | struct stksess *ts; |
| 356 | struct eb32_node *eb; |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 357 | int looped = 0; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 358 | |
| 359 | eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK); |
| 360 | |
| 361 | while (1) { |
| 362 | if (unlikely(!eb)) { |
| 363 | /* we might have reached the end of the tree, typically because |
| 364 | * <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] | 365 | * half. Let's loop back to the beginning of the tree now if we |
| 366 | * have not yet visited it. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 367 | */ |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 368 | if (looped) |
| 369 | break; |
| 370 | looped = 1; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 371 | eb = eb32_first(&t->exps); |
| 372 | if (likely(!eb)) |
| 373 | break; |
| 374 | } |
| 375 | |
| 376 | if (likely(tick_is_lt(now_ms, eb->key))) { |
| 377 | /* timer not expired yet, revisit it later */ |
| 378 | t->exp_next = eb->key; |
| 379 | return t->exp_next; |
| 380 | } |
| 381 | |
| 382 | /* timer looks expired, detach it from the queue */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 383 | ts = eb32_entry(eb, struct stksess, exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 384 | eb = eb32_next(eb); |
| 385 | |
Willy Tarreau | e7f3d7a | 2010-06-14 14:53:07 +0200 | [diff] [blame] | 386 | /* don't delete an entry which is currently referenced */ |
| 387 | if (ts->ref_cnt) |
| 388 | continue; |
| 389 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 390 | eb32_delete(&ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 391 | |
| 392 | if (!tick_is_expired(ts->expire, now_ms)) { |
| 393 | if (!tick_isset(ts->expire)) |
| 394 | continue; |
| 395 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 396 | ts->exp.key = ts->expire; |
| 397 | eb32_insert(&t->exps, &ts->exp); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 398 | |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 399 | if (!eb || eb->key > ts->exp.key) |
| 400 | eb = &ts->exp; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 401 | continue; |
| 402 | } |
| 403 | |
| 404 | /* session expired, trash it */ |
Willy Tarreau | 86257dc | 2010-06-06 12:57:10 +0200 | [diff] [blame] | 405 | ebmb_delete(&ts->key); |
Emeric Brun | 85e77c7 | 2010-09-23 18:16:52 +0200 | [diff] [blame] | 406 | eb32_delete(&ts->upd); |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 407 | stksess_free(t, ts); |
| 408 | } |
| 409 | |
| 410 | /* We have found no task to expire in any tree */ |
| 411 | t->exp_next = TICK_ETERNITY; |
| 412 | return t->exp_next; |
| 413 | } |
| 414 | |
| 415 | /* |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 416 | * Task processing function to trash expired sticky sessions. A pointer to the |
| 417 | * task itself is returned since it never dies. |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 418 | */ |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 419 | static struct task *process_table_expire(struct task *task) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 420 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 421 | struct stktable *t = task->context; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 422 | |
| 423 | task->expire = stktable_trash_expired(t); |
| 424 | return task; |
| 425 | } |
| 426 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 427 | /* 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] | 428 | int stktable_init(struct stktable *t) |
| 429 | { |
| 430 | if (t->size) { |
| 431 | memset(&t->keys, 0, sizeof(t->keys)); |
| 432 | memset(&t->exps, 0, sizeof(t->exps)); |
Emeric Brun | 1c6235d | 2015-12-16 15:28:12 +0100 | [diff] [blame] | 433 | t->updates = EB_ROOT_UNIQUE; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 434 | |
Willy Tarreau | 393379c | 2010-06-06 12:11:37 +0200 | [diff] [blame] | 435 | 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] | 436 | |
| 437 | t->exp_next = TICK_ETERNITY; |
| 438 | if ( t->expire ) { |
| 439 | t->exp_task = task_new(); |
| 440 | t->exp_task->process = process_table_expire; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 441 | t->exp_task->context = (void *)t; |
| 442 | } |
Willy Tarreau | c8b6791 | 2015-05-01 18:29:57 +0200 | [diff] [blame] | 443 | 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] | 444 | peers_register_table(t->peers.p, t); |
| 445 | } |
| 446 | |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 447 | return t->pool != NULL; |
| 448 | } |
| 449 | return 1; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Configuration keywords of known table types |
| 454 | */ |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 455 | struct stktable_type stktable_types[SMP_TYPES] = { |
| 456 | [SMP_T_SINT] = { "integer", 0, 4 }, |
| 457 | [SMP_T_IPV4] = { "ip", 0, 4 }, |
| 458 | [SMP_T_IPV6] = { "ipv6", 0, 16 }, |
| 459 | [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 }, |
| 460 | [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 } |
| 461 | }; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 462 | |
| 463 | /* |
| 464 | * Parse table type configuration. |
| 465 | * Returns 0 on successful parsing, else 1. |
| 466 | * <myidx> is set at next configuration <args> index. |
| 467 | */ |
| 468 | int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size) |
| 469 | { |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 470 | for (*type = 0; *type < SMP_TYPES; (*type)++) { |
| 471 | if (!stktable_types[*type].kw) |
| 472 | continue; |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 473 | if (strcmp(args[*myidx], stktable_types[*type].kw) != 0) |
| 474 | continue; |
| 475 | |
| 476 | *key_size = stktable_types[*type].default_size; |
| 477 | (*myidx)++; |
| 478 | |
Willy Tarreau | aea940e | 2010-06-06 11:56:36 +0200 | [diff] [blame] | 479 | if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) { |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 480 | if (strcmp("len", args[*myidx]) == 0) { |
| 481 | (*myidx)++; |
| 482 | *key_size = atol(args[*myidx]); |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 483 | if (!*key_size) |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 484 | break; |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 485 | if (*type == SMP_T_STR) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 486 | /* null terminated string needs +1 for '\0'. */ |
| 487 | (*key_size)++; |
| 488 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 489 | (*myidx)++; |
| 490 | } |
| 491 | } |
| 492 | return 0; |
| 493 | } |
| 494 | return 1; |
| 495 | } |
| 496 | |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 497 | /* Prepares a stktable_key from a sample <smp> to search into table <t>. |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 498 | * Note that the sample *is* modified and that the returned key may point |
| 499 | * to it, so the sample must not be modified afterwards before the lookup. |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 500 | * Returns NULL if the sample could not be converted (eg: no matching type), |
| 501 | * otherwise a pointer to the static stktable_key filled with what is needed |
| 502 | * for the lookup. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 503 | */ |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 504 | struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 505 | { |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 506 | /* Convert sample. */ |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 507 | if (!sample_convert(smp, t->type)) |
Willy Tarreau | 7fc1c6e | 2012-04-26 11:03:17 +0200 | [diff] [blame] | 508 | return NULL; |
| 509 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 510 | /* Fill static_table_key. */ |
| 511 | switch (t->type) { |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 512 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 513 | case SMP_T_IPV4: |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 514 | static_table_key.key = &smp->data.u.ipv4; |
| 515 | static_table_key.key_len = 4; |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 516 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 517 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 518 | case SMP_T_IPV6: |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 519 | static_table_key.key = &smp->data.u.ipv6; |
| 520 | static_table_key.key_len = 16; |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 521 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 522 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 523 | case SMP_T_SINT: |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 524 | /* The stick table require a 32bit unsigned int, "sint" is a |
| 525 | * signed 64 it, so we can convert it inplace. |
| 526 | */ |
| 527 | *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint; |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 528 | static_table_key.key = &smp->data.u.sint; |
| 529 | static_table_key.key_len = 4; |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 530 | break; |
| 531 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 532 | case SMP_T_STR: |
Willy Tarreau | ce6955e | 2016-08-09 11:59:12 +0200 | [diff] [blame] | 533 | if (!smp_make_safe(smp)) |
| 534 | return NULL; |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 535 | static_table_key.key = smp->data.u.str.str; |
| 536 | static_table_key.key_len = smp->data.u.str.len; |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 537 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 538 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 539 | case SMP_T_BIN: |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 540 | if (smp->data.u.str.len < t->key_size) { |
| 541 | /* This type needs padding with 0. */ |
Willy Tarreau | f65c6c0 | 2016-08-09 12:08:41 +0200 | [diff] [blame] | 542 | if (!smp_make_rw(smp)) |
| 543 | return NULL; |
| 544 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 545 | if (smp->data.u.str.size < t->key_size) |
| 546 | if (!smp_dup(smp)) |
| 547 | return NULL; |
| 548 | if (smp->data.u.str.size < t->key_size) |
| 549 | return NULL; |
| 550 | memset(smp->data.u.str.str + smp->data.u.str.len, 0, |
| 551 | t->key_size - smp->data.u.str.len); |
| 552 | smp->data.u.str.len = t->key_size; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 553 | } |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 554 | static_table_key.key = smp->data.u.str.str; |
| 555 | static_table_key.key_len = smp->data.u.str.len; |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 556 | break; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 557 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 558 | default: /* impossible case. */ |
| 559 | return NULL; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 562 | return &static_table_key; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 566 | * Process a fetch + format conversion as defined by the sample expression <expr> |
| 567 | * on request or response considering the <opt> parameter. Returns either NULL if |
| 568 | * no key could be extracted, or a pointer to the converted result stored in |
| 569 | * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset |
| 570 | * 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] | 571 | * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present |
| 572 | * without SMP_OPT_FINAL). The output will be usable like this : |
| 573 | * |
| 574 | * return MAY_CHANGE FINAL Meaning for the sample |
| 575 | * NULL 0 * Not present and will never be (eg: header) |
| 576 | * NULL 1 0 Not present or unstable, could change (eg: req_len) |
| 577 | * NULL 1 1 Not present, will not change anymore |
| 578 | * smp 0 * Present and will not change (eg: header) |
| 579 | * smp 1 0 not possible |
| 580 | * smp 1 1 Present, last known value (eg: request length) |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 581 | */ |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 582 | 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] | 583 | unsigned int opt, struct sample_expr *expr, struct sample *smp) |
| 584 | { |
| 585 | if (smp) |
| 586 | memset(smp, 0, sizeof(*smp)); |
| 587 | |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 588 | smp = sample_process(px, sess, strm, opt, expr, smp); |
Willy Tarreau | 8fed903 | 2014-07-03 17:02:46 +0200 | [diff] [blame] | 589 | if (!smp) |
| 590 | return NULL; |
| 591 | |
| 592 | if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL)) |
| 593 | return NULL; /* we can only use stable samples */ |
| 594 | |
| 595 | return smp_to_stkey(smp, t); |
| 596 | } |
| 597 | |
| 598 | /* |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 599 | * 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] | 600 | * type <table_type>, otherwise zero. Used in configuration check. |
| 601 | */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 602 | int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 603 | { |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 604 | int out_type; |
| 605 | |
Thierry FOURNIER | 5d24ebc | 2015-07-24 08:46:42 +0200 | [diff] [blame] | 606 | if (table_type >= SMP_TYPES || !stktable_types[table_type].kw) |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 607 | return 0; |
| 608 | |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 609 | out_type = smp_expr_output_type(expr); |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 610 | |
Thierry FOURNIER | bc8c404 | 2015-08-10 17:53:45 +0200 | [diff] [blame] | 611 | /* Convert sample. */ |
| 612 | if (!sample_casts[out_type][table_type]) |
Thierry FOURNIER | f73eb8f | 2013-11-27 15:30:55 +0100 | [diff] [blame] | 613 | return 0; |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 614 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 615 | return 1; |
| 616 | } |
Emeric Brun | 3bd697e | 2010-01-04 15:23:48 +0100 | [diff] [blame] | 617 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 618 | /* Extra data types processing : after the last one, some room may remain |
| 619 | * before STKTABLE_DATA_TYPES that may be used to register extra data types |
| 620 | * at run time. |
| 621 | */ |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 622 | struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = { |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 623 | [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT }, |
Thierry FOURNIER | 3cf1111 | 2015-07-28 08:57:05 +0200 | [diff] [blame] | 624 | [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT }, |
Willy Tarreau | 3b9c6e0 | 2010-07-18 08:04:30 +0200 | [diff] [blame] | 625 | [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT }, |
Willy Tarreau | ba2ffd1 | 2013-05-29 15:54:14 +0200 | [diff] [blame] | 626 | [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] | 627 | [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT }, |
| 628 | [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 629 | [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT }, |
| 630 | [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT }, |
| 631 | [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 632 | [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT }, |
| 633 | [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 634 | [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT }, |
| 635 | [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 636 | [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL }, |
| 637 | [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY }, |
| 638 | [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL }, |
| 639 | [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] | 640 | }; |
| 641 | |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 642 | /* Registers stick-table extra data type with index <idx>, name <name>, type |
| 643 | * <std_type> and arg type <arg_type>. If the index is negative, the next free |
| 644 | * index is automatically allocated. The allocated index is returned, or -1 if |
| 645 | * no free index was found or <name> was already registered. The <name> is used |
| 646 | * directly as a pointer, so if it's not stable, the caller must allocate it. |
| 647 | */ |
| 648 | int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type) |
| 649 | { |
| 650 | if (idx < 0) { |
| 651 | for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) { |
| 652 | if (!stktable_data_types[idx].name) |
| 653 | break; |
| 654 | |
| 655 | if (strcmp(stktable_data_types[idx].name, name) == 0) |
| 656 | return -1; |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | if (idx >= STKTABLE_DATA_TYPES) |
| 661 | return -1; |
| 662 | |
| 663 | if (stktable_data_types[idx].name != NULL) |
| 664 | return -1; |
| 665 | |
| 666 | stktable_data_types[idx].name = name; |
| 667 | stktable_data_types[idx].std_type = std_type; |
| 668 | stktable_data_types[idx].arg_type = arg_type; |
| 669 | return idx; |
| 670 | } |
| 671 | |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 672 | /* |
| 673 | * Returns the data type number for the stktable_data_type whose name is <name>, |
| 674 | * or <0 if not found. |
| 675 | */ |
| 676 | int stktable_get_data_type(char *name) |
| 677 | { |
| 678 | int type; |
| 679 | |
| 680 | for (type = 0; type < STKTABLE_DATA_TYPES; type++) { |
Willy Tarreau | edee1d6 | 2014-07-15 16:44:27 +0200 | [diff] [blame] | 681 | if (!stktable_data_types[type].name) |
| 682 | continue; |
Willy Tarreau | 08d5f98 | 2010-06-06 13:34:54 +0200 | [diff] [blame] | 683 | if (strcmp(name, stktable_data_types[type].name) == 0) |
| 684 | return type; |
| 685 | } |
| 686 | return -1; |
| 687 | } |
| 688 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 689 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 690 | * it up into this table. Returns true if found, false otherwise. The input |
| 691 | * type is STR so that input samples are converted to string (since all types |
| 692 | * can be converted to strings), then the function casts the string again into |
| 693 | * the table's type. This is a double conversion, but in the future we might |
| 694 | * support automatic input types to perform the cast on the fly. |
| 695 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 696 | 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] | 697 | { |
| 698 | struct stktable *t; |
| 699 | struct stktable_key *key; |
| 700 | struct stksess *ts; |
| 701 | |
| 702 | t = &arg_p[0].data.prx->table; |
| 703 | |
| 704 | key = smp_to_stkey(smp, t); |
| 705 | if (!key) |
| 706 | return 0; |
| 707 | |
| 708 | ts = stktable_lookup_key(t, key); |
| 709 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 710 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 711 | smp->data.u.sint = !!ts; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 712 | smp->flags = SMP_F_VOL_TEST; |
| 713 | return 1; |
| 714 | } |
| 715 | |
| 716 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 717 | * it up into this table. Returns the data rate received from clients in bytes/s |
| 718 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 719 | * be easily performed. If the inspected parameter is not stored in the table, |
| 720 | * <not found> is returned. |
| 721 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 722 | 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] | 723 | { |
| 724 | struct stktable *t; |
| 725 | struct stktable_key *key; |
| 726 | struct stksess *ts; |
| 727 | void *ptr; |
| 728 | |
| 729 | t = &arg_p[0].data.prx->table; |
| 730 | |
| 731 | key = smp_to_stkey(smp, t); |
| 732 | if (!key) |
| 733 | return 0; |
| 734 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 735 | ts = stktable_lookup_key(t, key); |
| 736 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 737 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 738 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 739 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 740 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 741 | if (!ts) /* key not present */ |
| 742 | return 1; |
| 743 | |
| 744 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE); |
| 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 = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate), |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 749 | t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u); |
| 750 | return 1; |
| 751 | } |
| 752 | |
| 753 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 754 | * it up into this table. Returns the cumulated number of connections for the key |
| 755 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 756 | * be easily performed. If the inspected parameter is not stored in the table, |
| 757 | * <not found> is returned. |
| 758 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 759 | 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] | 760 | { |
| 761 | struct stktable *t; |
| 762 | struct stktable_key *key; |
| 763 | struct stksess *ts; |
| 764 | void *ptr; |
| 765 | |
| 766 | t = &arg_p[0].data.prx->table; |
| 767 | |
| 768 | key = smp_to_stkey(smp, t); |
| 769 | if (!key) |
| 770 | return 0; |
| 771 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 772 | ts = stktable_lookup_key(t, key); |
| 773 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 774 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 775 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 776 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 777 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 778 | if (!ts) /* key not present */ |
| 779 | return 1; |
| 780 | |
| 781 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT); |
| 782 | if (!ptr) |
| 783 | return 0; /* parameter not stored */ |
| 784 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 785 | smp->data.u.sint = stktable_data_cast(ptr, conn_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 786 | return 1; |
| 787 | } |
| 788 | |
| 789 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 790 | * it up into this table. Returns the number of concurrent connections for the |
| 791 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 792 | * can be easily performed. If the inspected parameter is not stored in the |
| 793 | * table, <not found> is returned. |
| 794 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 795 | 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] | 796 | { |
| 797 | struct stktable *t; |
| 798 | struct stktable_key *key; |
| 799 | struct stksess *ts; |
| 800 | void *ptr; |
| 801 | |
| 802 | t = &arg_p[0].data.prx->table; |
| 803 | |
| 804 | key = smp_to_stkey(smp, t); |
| 805 | if (!key) |
| 806 | return 0; |
| 807 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 808 | ts = stktable_lookup_key(t, key); |
| 809 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 810 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 811 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 812 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 813 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 814 | if (!ts) /* key not present */ |
| 815 | return 1; |
| 816 | |
| 817 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR); |
| 818 | if (!ptr) |
| 819 | return 0; /* parameter not stored */ |
| 820 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 821 | smp->data.u.sint = stktable_data_cast(ptr, conn_cur); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 822 | return 1; |
| 823 | } |
| 824 | |
| 825 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 826 | * it up into this table. Returns the rate of incoming connections from the key |
| 827 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 828 | * be easily performed. If the inspected parameter is not stored in the table, |
| 829 | * <not found> is returned. |
| 830 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 831 | 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] | 832 | { |
| 833 | struct stktable *t; |
| 834 | struct stktable_key *key; |
| 835 | struct stksess *ts; |
| 836 | void *ptr; |
| 837 | |
| 838 | t = &arg_p[0].data.prx->table; |
| 839 | |
| 840 | key = smp_to_stkey(smp, t); |
| 841 | if (!key) |
| 842 | return 0; |
| 843 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 844 | ts = stktable_lookup_key(t, key); |
| 845 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 846 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 847 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 848 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 849 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 850 | if (!ts) /* key not present */ |
| 851 | return 1; |
| 852 | |
| 853 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE); |
| 854 | if (!ptr) |
| 855 | return 0; /* parameter not stored */ |
| 856 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 857 | 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] | 858 | t->data_arg[STKTABLE_DT_CONN_RATE].u); |
| 859 | return 1; |
| 860 | } |
| 861 | |
| 862 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 863 | * it up into this table. Returns the data rate sent to clients in bytes/s |
| 864 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 865 | * be easily performed. If the inspected parameter is not stored in the table, |
| 866 | * <not found> is returned. |
| 867 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 868 | 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] | 869 | { |
| 870 | struct stktable *t; |
| 871 | struct stktable_key *key; |
| 872 | struct stksess *ts; |
| 873 | void *ptr; |
| 874 | |
| 875 | t = &arg_p[0].data.prx->table; |
| 876 | |
| 877 | key = smp_to_stkey(smp, t); |
| 878 | if (!key) |
| 879 | return 0; |
| 880 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 881 | ts = stktable_lookup_key(t, key); |
| 882 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 883 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 884 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 885 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 886 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 887 | if (!ts) /* key not present */ |
| 888 | return 1; |
| 889 | |
| 890 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_RATE); |
| 891 | if (!ptr) |
| 892 | return 0; /* parameter not stored */ |
| 893 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 894 | 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] | 895 | t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u); |
| 896 | return 1; |
| 897 | } |
| 898 | |
| 899 | /* 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] | 900 | * it up into this table. Returns the value of the GPT0 tag for the key |
| 901 | * if the key is present in the table, otherwise false, so that comparisons can |
| 902 | * be easily performed. If the inspected parameter is not stored in the table, |
| 903 | * <not found> is returned. |
| 904 | */ |
| 905 | static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private) |
| 906 | { |
| 907 | struct stktable *t; |
| 908 | struct stktable_key *key; |
| 909 | struct stksess *ts; |
| 910 | void *ptr; |
| 911 | |
| 912 | t = &arg_p[0].data.prx->table; |
| 913 | |
| 914 | key = smp_to_stkey(smp, t); |
| 915 | if (!key) |
| 916 | return 0; |
| 917 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 918 | ts = stktable_lookup_key(t, key); |
| 919 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 920 | smp->flags = SMP_F_VOL_TEST; |
| 921 | smp->data.type = SMP_T_SINT; |
| 922 | smp->data.u.sint = 0; |
| 923 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 924 | if (!ts) /* key not present */ |
| 925 | return 1; |
| 926 | |
| 927 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0); |
| 928 | if (!ptr) |
| 929 | return 0; /* parameter not stored */ |
| 930 | |
| 931 | smp->data.u.sint = stktable_data_cast(ptr, gpt0); |
| 932 | return 1; |
| 933 | } |
| 934 | |
| 935 | /* 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] | 936 | * it up into this table. Returns the value of the GPC0 counter for the key |
| 937 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 938 | * be easily performed. If the inspected parameter is not stored in the table, |
| 939 | * <not found> is returned. |
| 940 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 941 | 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] | 942 | { |
| 943 | struct stktable *t; |
| 944 | struct stktable_key *key; |
| 945 | struct stksess *ts; |
| 946 | void *ptr; |
| 947 | |
| 948 | t = &arg_p[0].data.prx->table; |
| 949 | |
| 950 | key = smp_to_stkey(smp, t); |
| 951 | if (!key) |
| 952 | return 0; |
| 953 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 954 | ts = stktable_lookup_key(t, key); |
| 955 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 956 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 957 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 958 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 959 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 960 | if (!ts) /* key not present */ |
| 961 | return 1; |
| 962 | |
| 963 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0); |
| 964 | if (!ptr) |
| 965 | return 0; /* parameter not stored */ |
| 966 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 967 | smp->data.u.sint = stktable_data_cast(ptr, gpc0); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 968 | return 1; |
| 969 | } |
| 970 | |
| 971 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 972 | * it up into this table. Returns the event rate of the GPC0 counter for the key |
| 973 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 974 | * be easily performed. If the inspected parameter is not stored in the table, |
| 975 | * <not found> is returned. |
| 976 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 977 | 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] | 978 | { |
| 979 | struct stktable *t; |
| 980 | struct stktable_key *key; |
| 981 | struct stksess *ts; |
| 982 | void *ptr; |
| 983 | |
| 984 | t = &arg_p[0].data.prx->table; |
| 985 | |
| 986 | key = smp_to_stkey(smp, t); |
| 987 | if (!key) |
| 988 | return 0; |
| 989 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 990 | ts = stktable_lookup_key(t, key); |
| 991 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 992 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 993 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 994 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 995 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 996 | if (!ts) /* key not present */ |
| 997 | return 1; |
| 998 | |
| 999 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE); |
| 1000 | if (!ptr) |
| 1001 | return 0; /* parameter not stored */ |
| 1002 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1003 | 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] | 1004 | t->data_arg[STKTABLE_DT_GPC0_RATE].u); |
| 1005 | return 1; |
| 1006 | } |
| 1007 | |
| 1008 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1009 | * it up into this table. Returns the cumulated number of HTTP request errors |
| 1010 | * for the key if the key is present in the table, otherwise zero, so that |
| 1011 | * comparisons can be easily performed. If the inspected parameter is not stored |
| 1012 | * in the table, <not found> is returned. |
| 1013 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1014 | 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] | 1015 | { |
| 1016 | struct stktable *t; |
| 1017 | struct stktable_key *key; |
| 1018 | struct stksess *ts; |
| 1019 | void *ptr; |
| 1020 | |
| 1021 | t = &arg_p[0].data.prx->table; |
| 1022 | |
| 1023 | key = smp_to_stkey(smp, t); |
| 1024 | if (!key) |
| 1025 | return 0; |
| 1026 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1027 | ts = stktable_lookup_key(t, key); |
| 1028 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1029 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1030 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1031 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1032 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1033 | if (!ts) /* key not present */ |
| 1034 | return 1; |
| 1035 | |
| 1036 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT); |
| 1037 | if (!ptr) |
| 1038 | return 0; /* parameter not stored */ |
| 1039 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1040 | smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1041 | return 1; |
| 1042 | } |
| 1043 | |
| 1044 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1045 | * it up into this table. Returns the HTTP request error rate the key |
| 1046 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1047 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1048 | * <not found> is returned. |
| 1049 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1050 | 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] | 1051 | { |
| 1052 | struct stktable *t; |
| 1053 | struct stktable_key *key; |
| 1054 | struct stksess *ts; |
| 1055 | void *ptr; |
| 1056 | |
| 1057 | t = &arg_p[0].data.prx->table; |
| 1058 | |
| 1059 | key = smp_to_stkey(smp, t); |
| 1060 | if (!key) |
| 1061 | return 0; |
| 1062 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1063 | ts = stktable_lookup_key(t, key); |
| 1064 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1065 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1066 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1067 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1068 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1069 | if (!ts) /* key not present */ |
| 1070 | return 1; |
| 1071 | |
| 1072 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE); |
| 1073 | if (!ptr) |
| 1074 | return 0; /* parameter not stored */ |
| 1075 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1076 | 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] | 1077 | t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u); |
| 1078 | return 1; |
| 1079 | } |
| 1080 | |
| 1081 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1082 | * it up into this table. Returns the cumulated number of HTTP request for the |
| 1083 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1084 | * can be easily performed. If the inspected parameter is not stored in the |
| 1085 | * table, <not found> is returned. |
| 1086 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1087 | 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] | 1088 | { |
| 1089 | struct stktable *t; |
| 1090 | struct stktable_key *key; |
| 1091 | struct stksess *ts; |
| 1092 | void *ptr; |
| 1093 | |
| 1094 | t = &arg_p[0].data.prx->table; |
| 1095 | |
| 1096 | key = smp_to_stkey(smp, t); |
| 1097 | if (!key) |
| 1098 | return 0; |
| 1099 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1100 | ts = stktable_lookup_key(t, key); |
| 1101 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1102 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1103 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1104 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1105 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1106 | if (!ts) /* key not present */ |
| 1107 | return 1; |
| 1108 | |
| 1109 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT); |
| 1110 | if (!ptr) |
| 1111 | return 0; /* parameter not stored */ |
| 1112 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1113 | smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1114 | return 1; |
| 1115 | } |
| 1116 | |
| 1117 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1118 | * it up into this table. Returns the HTTP request rate the key if the key is |
| 1119 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1120 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1121 | * is returned. |
| 1122 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1123 | 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] | 1124 | { |
| 1125 | struct stktable *t; |
| 1126 | struct stktable_key *key; |
| 1127 | struct stksess *ts; |
| 1128 | void *ptr; |
| 1129 | |
| 1130 | t = &arg_p[0].data.prx->table; |
| 1131 | |
| 1132 | key = smp_to_stkey(smp, t); |
| 1133 | if (!key) |
| 1134 | return 0; |
| 1135 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1136 | ts = stktable_lookup_key(t, key); |
| 1137 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1138 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1139 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1140 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1141 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1142 | if (!ts) /* key not present */ |
| 1143 | return 1; |
| 1144 | |
| 1145 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE); |
| 1146 | if (!ptr) |
| 1147 | return 0; /* parameter not stored */ |
| 1148 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1149 | 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] | 1150 | t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u); |
| 1151 | return 1; |
| 1152 | } |
| 1153 | |
| 1154 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1155 | * it up into this table. Returns the volume of datareceived from clients in kbytes |
| 1156 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1157 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1158 | * <not found> is returned. |
| 1159 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1160 | 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] | 1161 | { |
| 1162 | struct stktable *t; |
| 1163 | struct stktable_key *key; |
| 1164 | struct stksess *ts; |
| 1165 | void *ptr; |
| 1166 | |
| 1167 | t = &arg_p[0].data.prx->table; |
| 1168 | |
| 1169 | key = smp_to_stkey(smp, t); |
| 1170 | if (!key) |
| 1171 | return 0; |
| 1172 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1173 | ts = stktable_lookup_key(t, key); |
| 1174 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1175 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1176 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1177 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1178 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1179 | if (!ts) /* key not present */ |
| 1180 | return 1; |
| 1181 | |
| 1182 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT); |
| 1183 | if (!ptr) |
| 1184 | return 0; /* parameter not stored */ |
| 1185 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1186 | smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1187 | return 1; |
| 1188 | } |
| 1189 | |
| 1190 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1191 | * it up into this table. Returns the volume of data sent to clients in kbytes |
| 1192 | * if the key is present in the table, otherwise zero, so that comparisons can |
| 1193 | * be easily performed. If the inspected parameter is not stored in the table, |
| 1194 | * <not found> is returned. |
| 1195 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1196 | 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] | 1197 | { |
| 1198 | struct stktable *t; |
| 1199 | struct stktable_key *key; |
| 1200 | struct stksess *ts; |
| 1201 | void *ptr; |
| 1202 | |
| 1203 | t = &arg_p[0].data.prx->table; |
| 1204 | |
| 1205 | key = smp_to_stkey(smp, t); |
| 1206 | if (!key) |
| 1207 | return 0; |
| 1208 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1209 | ts = stktable_lookup_key(t, key); |
| 1210 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1211 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1212 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1213 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1214 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1215 | if (!ts) /* key not present */ |
| 1216 | return 1; |
| 1217 | |
| 1218 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT); |
| 1219 | if (!ptr) |
| 1220 | return 0; /* parameter not stored */ |
| 1221 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1222 | smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1223 | return 1; |
| 1224 | } |
| 1225 | |
| 1226 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1227 | * it up into this table. Returns the server ID associated with the key if the |
| 1228 | * key is present in the table, otherwise zero, so that comparisons can be |
| 1229 | * easily performed. If the inspected parameter is not stored in the table, |
| 1230 | * <not found> is returned. |
| 1231 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1232 | 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] | 1233 | { |
| 1234 | struct stktable *t; |
| 1235 | struct stktable_key *key; |
| 1236 | struct stksess *ts; |
| 1237 | void *ptr; |
| 1238 | |
| 1239 | t = &arg_p[0].data.prx->table; |
| 1240 | |
| 1241 | key = smp_to_stkey(smp, t); |
| 1242 | if (!key) |
| 1243 | return 0; |
| 1244 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1245 | ts = stktable_lookup_key(t, key); |
| 1246 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1247 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1248 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1249 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1250 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1251 | if (!ts) /* key not present */ |
| 1252 | return 1; |
| 1253 | |
| 1254 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID); |
| 1255 | if (!ptr) |
| 1256 | return 0; /* parameter not stored */ |
| 1257 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1258 | smp->data.u.sint = stktable_data_cast(ptr, server_id); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1259 | return 1; |
| 1260 | } |
| 1261 | |
| 1262 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1263 | * it up into this table. Returns the cumulated number of sessions for the |
| 1264 | * key if the key is present in the table, otherwise zero, so that comparisons |
| 1265 | * can be easily performed. If the inspected parameter is not stored in the |
| 1266 | * table, <not found> is returned. |
| 1267 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1268 | 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] | 1269 | { |
| 1270 | struct stktable *t; |
| 1271 | struct stktable_key *key; |
| 1272 | struct stksess *ts; |
| 1273 | void *ptr; |
| 1274 | |
| 1275 | t = &arg_p[0].data.prx->table; |
| 1276 | |
| 1277 | key = smp_to_stkey(smp, t); |
| 1278 | if (!key) |
| 1279 | return 0; |
| 1280 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1281 | ts = stktable_lookup_key(t, key); |
| 1282 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1283 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1284 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1285 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1286 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1287 | if (!ts) /* key not present */ |
| 1288 | return 1; |
| 1289 | |
| 1290 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT); |
| 1291 | if (!ptr) |
| 1292 | return 0; /* parameter not stored */ |
| 1293 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1294 | smp->data.u.sint = stktable_data_cast(ptr, sess_cnt); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1295 | return 1; |
| 1296 | } |
| 1297 | |
| 1298 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1299 | * it up into this table. Returns the session rate the key if the key is |
| 1300 | * present in the table, otherwise zero, so that comparisons can be easily |
| 1301 | * performed. If the inspected parameter is not stored in the table, <not found> |
| 1302 | * is returned. |
| 1303 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1304 | 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] | 1305 | { |
| 1306 | struct stktable *t; |
| 1307 | struct stktable_key *key; |
| 1308 | struct stksess *ts; |
| 1309 | void *ptr; |
| 1310 | |
| 1311 | t = &arg_p[0].data.prx->table; |
| 1312 | |
| 1313 | key = smp_to_stkey(smp, t); |
| 1314 | if (!key) |
| 1315 | return 0; |
| 1316 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1317 | ts = stktable_lookup_key(t, key); |
| 1318 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1319 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1320 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1321 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1322 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1323 | if (!ts) /* key not present */ |
| 1324 | return 1; |
| 1325 | |
| 1326 | ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE); |
| 1327 | if (!ptr) |
| 1328 | return 0; /* parameter not stored */ |
| 1329 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1330 | 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] | 1331 | t->data_arg[STKTABLE_DT_SESS_RATE].u); |
| 1332 | return 1; |
| 1333 | } |
| 1334 | |
| 1335 | /* Casts sample <smp> to the type of the table specified in arg(0), and looks |
| 1336 | * it up into this table. Returns the amount of concurrent connections tracking |
| 1337 | * the same key if the key is present in the table, otherwise zero, so that |
| 1338 | * comparisons can be easily performed. If the inspected parameter is not |
| 1339 | * stored in the table, <not found> is returned. |
| 1340 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 1341 | 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] | 1342 | { |
| 1343 | struct stktable *t; |
| 1344 | struct stktable_key *key; |
| 1345 | struct stksess *ts; |
| 1346 | |
| 1347 | t = &arg_p[0].data.prx->table; |
| 1348 | |
| 1349 | key = smp_to_stkey(smp, t); |
| 1350 | if (!key) |
| 1351 | return 0; |
| 1352 | |
Willy Tarreau | f0c730a | 2016-05-25 17:07:56 +0200 | [diff] [blame] | 1353 | ts = stktable_lookup_key(t, key); |
| 1354 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1355 | smp->flags = SMP_F_VOL_TEST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1356 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1357 | smp->data.u.sint = 0; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1358 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1359 | if (ts) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1360 | smp->data.u.sint = ts->ref_cnt; |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 1361 | |
| 1362 | return 1; |
| 1363 | } |
| 1364 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1365 | /* Always returns 1. */ |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1366 | static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 1367 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1368 | { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1369 | struct stksess *ts; |
| 1370 | struct stkctr *stkctr; |
| 1371 | |
| 1372 | /* Extract the stksess, return OK if no stksess available. */ |
| 1373 | if (s) |
| 1374 | stkctr = &s->stkctr[rule->arg.gpc.sc]; |
| 1375 | else |
| 1376 | stkctr = &sess->stkctr[rule->arg.gpc.sc]; |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1377 | |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1378 | ts = stkctr_entry(stkctr); |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1379 | if (ts) { |
| 1380 | void *ptr1, *ptr2; |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1381 | |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1382 | /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */ |
| 1383 | ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE); |
| 1384 | if (ptr1) |
| 1385 | update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate), |
| 1386 | stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1); |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1387 | |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1388 | ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0); |
| 1389 | if (ptr2) |
| 1390 | stktable_data_cast(ptr2, gpc0)++; |
| 1391 | |
| 1392 | /* If data was modified, we need to touch to re-schedule sync */ |
| 1393 | if (ptr1 || ptr2) |
| 1394 | stktable_touch(stkctr->table, ts, 1); |
| 1395 | } |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1396 | return ACT_RET_CONT; |
| 1397 | } |
| 1398 | |
| 1399 | /* This function is a common parser for using variables. It understands |
| 1400 | * the formats: |
| 1401 | * |
| 1402 | * sc-inc-gpc0(<stick-table ID>) |
| 1403 | * |
| 1404 | * It returns 0 if fails and <err> is filled with an error message. Otherwise, |
| 1405 | * it returns 1 and the variable <expr> is filled with the pointer to the |
| 1406 | * expression to execute. |
| 1407 | */ |
| 1408 | static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px, |
| 1409 | struct act_rule *rule, char **err) |
| 1410 | { |
| 1411 | const char *cmd_name = args[*arg-1]; |
| 1412 | char *error; |
| 1413 | |
| 1414 | cmd_name += strlen("sc-inc-gpc0"); |
| 1415 | if (*cmd_name == '\0') { |
| 1416 | /* default stick table id. */ |
| 1417 | rule->arg.gpc.sc = 0; |
| 1418 | } else { |
| 1419 | /* parse the stick table id. */ |
| 1420 | if (*cmd_name != '(') { |
| 1421 | memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]); |
| 1422 | return ACT_RET_PRS_ERR; |
| 1423 | } |
| 1424 | cmd_name++; /* jump the '(' */ |
| 1425 | rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */ |
| 1426 | if (*error != ')') { |
| 1427 | memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]); |
| 1428 | return ACT_RET_PRS_ERR; |
| 1429 | } |
| 1430 | |
| 1431 | if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) { |
| 1432 | memprintf(err, "invalid stick table track ID. The max allowed ID is %d", |
| 1433 | ACT_ACTION_TRK_SCMAX-1); |
| 1434 | return ACT_RET_PRS_ERR; |
| 1435 | } |
| 1436 | } |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1437 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 1438 | rule->action_ptr = action_inc_gpc0; |
| 1439 | return ACT_RET_PRS_OK; |
| 1440 | } |
| 1441 | |
| 1442 | /* Always returns 1. */ |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1443 | static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px, |
Willy Tarreau | 658b85b | 2015-09-27 10:00:49 +0200 | [diff] [blame] | 1444 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1445 | { |
| 1446 | void *ptr; |
| 1447 | struct stksess *ts; |
| 1448 | struct stkctr *stkctr; |
| 1449 | |
| 1450 | /* Extract the stksess, return OK if no stksess available. */ |
| 1451 | if (s) |
| 1452 | stkctr = &s->stkctr[rule->arg.gpt.sc]; |
| 1453 | else |
| 1454 | stkctr = &sess->stkctr[rule->arg.gpt.sc]; |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1455 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1456 | ts = stkctr_entry(stkctr); |
| 1457 | if (!ts) |
| 1458 | return ACT_RET_CONT; |
| 1459 | |
| 1460 | /* Store the sample in the required sc, and ignore errors. */ |
| 1461 | ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0); |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1462 | if (ptr) { |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1463 | stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value; |
Willy Tarreau | 79c1e91 | 2016-01-25 14:54:45 +0100 | [diff] [blame] | 1464 | stktable_touch(stkctr->table, ts, 1); |
| 1465 | } |
| 1466 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1467 | return ACT_RET_CONT; |
| 1468 | } |
| 1469 | |
| 1470 | /* This function is a common parser for using variables. It understands |
| 1471 | * the format: |
| 1472 | * |
| 1473 | * set-gpt0(<stick-table ID>) <expression> |
| 1474 | * |
| 1475 | * It returns 0 if fails and <err> is filled with an error message. Otherwise, |
| 1476 | * it returns 1 and the variable <expr> is filled with the pointer to the |
| 1477 | * expression to execute. |
| 1478 | */ |
| 1479 | static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px, |
| 1480 | struct act_rule *rule, char **err) |
| 1481 | |
| 1482 | |
| 1483 | { |
| 1484 | const char *cmd_name = args[*arg-1]; |
| 1485 | char *error; |
| 1486 | |
| 1487 | cmd_name += strlen("sc-set-gpt0"); |
| 1488 | if (*cmd_name == '\0') { |
| 1489 | /* default stick table id. */ |
| 1490 | rule->arg.gpt.sc = 0; |
| 1491 | } else { |
| 1492 | /* parse the stick table id. */ |
| 1493 | if (*cmd_name != '(') { |
| 1494 | memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]); |
| 1495 | return ACT_RET_PRS_ERR; |
| 1496 | } |
| 1497 | cmd_name++; /* jump the '(' */ |
| 1498 | rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */ |
| 1499 | if (*error != ')') { |
| 1500 | memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]); |
| 1501 | return ACT_RET_PRS_ERR; |
| 1502 | } |
| 1503 | |
| 1504 | if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) { |
| 1505 | memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d", |
| 1506 | args[*arg-1], ACT_ACTION_TRK_SCMAX-1); |
| 1507 | return ACT_RET_PRS_ERR; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | rule->arg.gpt.value = strtol(args[*arg], &error, 10); |
| 1512 | if (*error != '\0') { |
| 1513 | memprintf(err, "invalid integer value '%s'", args[*arg]); |
| 1514 | return ACT_RET_PRS_ERR; |
| 1515 | } |
| 1516 | (*arg)++; |
| 1517 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 1518 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 1519 | rule->action_ptr = action_set_gpt0; |
| 1520 | |
| 1521 | return ACT_RET_PRS_OK; |
| 1522 | } |
| 1523 | |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 1524 | /* set temp integer to the number of used entries in the table pointed to by expr. |
| 1525 | * Accepts exactly 1 argument of type table. |
| 1526 | */ |
| 1527 | static int |
| 1528 | smp_fetch_table_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1529 | { |
| 1530 | smp->flags = SMP_F_VOL_TEST; |
| 1531 | smp->data.type = SMP_T_SINT; |
| 1532 | smp->data.u.sint = args->data.prx->table.current; |
| 1533 | return 1; |
| 1534 | } |
| 1535 | |
| 1536 | /* set temp integer to the number of free entries in the table pointed to by expr. |
| 1537 | * Accepts exactly 1 argument of type table. |
| 1538 | */ |
| 1539 | static int |
| 1540 | smp_fetch_table_avl(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1541 | { |
| 1542 | struct proxy *px; |
| 1543 | |
| 1544 | px = args->data.prx; |
| 1545 | smp->flags = SMP_F_VOL_TEST; |
| 1546 | smp->data.type = SMP_T_SINT; |
| 1547 | smp->data.u.sint = px->table.size - px->table.current; |
| 1548 | return 1; |
| 1549 | } |
| 1550 | |
| 1551 | /* Returns a pointer to a stkctr depending on the fetch keyword name. |
| 1552 | * It is designed to be called as sc[0-9]_* sc_* or src_* exclusively. |
| 1553 | * sc[0-9]_* will return a pointer to the respective field in the |
| 1554 | * stream <l4>. sc_* requires an UINT argument specifying the stick |
| 1555 | * counter number. src_* will fill a locally allocated structure with |
| 1556 | * the table and entry corresponding to what is specified with src_*. |
| 1557 | * NULL may be returned if the designated stkctr is not tracked. For |
| 1558 | * the sc_* and sc[0-9]_* forms, an optional table argument may be |
| 1559 | * passed. When present, the currently tracked key is then looked up |
| 1560 | * in the specified table instead of the current table. The purpose is |
| 1561 | * to be able to convery multiple values per key (eg: have gpc0 from |
| 1562 | * multiple tables). <strm> is allowed to be NULL, in which case only |
| 1563 | * the session will be consulted. |
| 1564 | */ |
| 1565 | struct stkctr * |
| 1566 | smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw) |
| 1567 | { |
| 1568 | static struct stkctr stkctr; |
| 1569 | struct stkctr *stkptr; |
| 1570 | struct stksess *stksess; |
| 1571 | unsigned int num = kw[2] - '0'; |
| 1572 | int arg = 0; |
| 1573 | |
| 1574 | if (num == '_' - '0') { |
| 1575 | /* sc_* variant, args[0] = ctr# (mandatory) */ |
| 1576 | num = args[arg++].data.sint; |
| 1577 | if (num >= MAX_SESS_STKCTR) |
| 1578 | return NULL; |
| 1579 | } |
| 1580 | else if (num > 9) { /* src_* variant, args[0] = table */ |
| 1581 | struct stktable_key *key; |
| 1582 | struct connection *conn = objt_conn(sess->origin); |
| 1583 | struct sample smp; |
| 1584 | |
| 1585 | if (!conn) |
| 1586 | return NULL; |
| 1587 | |
| 1588 | /* Fetch source adress in a sample. */ |
| 1589 | smp.px = NULL; |
| 1590 | smp.sess = sess; |
| 1591 | smp.strm = strm; |
| 1592 | if (!smp_fetch_src(NULL, &smp, NULL, NULL)) |
| 1593 | return NULL; |
| 1594 | |
| 1595 | /* Converts into key. */ |
| 1596 | key = smp_to_stkey(&smp, &args->data.prx->table); |
| 1597 | if (!key) |
| 1598 | return NULL; |
| 1599 | |
| 1600 | stkctr.table = &args->data.prx->table; |
| 1601 | stkctr_set_entry(&stkctr, stktable_lookup_key(stkctr.table, key)); |
| 1602 | return &stkctr; |
| 1603 | } |
| 1604 | |
| 1605 | /* Here, <num> contains the counter number from 0 to 9 for |
| 1606 | * the sc[0-9]_ form, or even higher using sc_(num) if needed. |
| 1607 | * args[arg] is the first optional argument. We first lookup the |
| 1608 | * ctr form the stream, then from the session if it was not there. |
| 1609 | */ |
| 1610 | |
| 1611 | if (strm) |
| 1612 | stkptr = &strm->stkctr[num]; |
| 1613 | if (!strm || !stkctr_entry(stkptr)) { |
| 1614 | stkptr = &sess->stkctr[num]; |
| 1615 | if (!stkctr_entry(stkptr)) |
| 1616 | return NULL; |
| 1617 | } |
| 1618 | |
| 1619 | stksess = stkctr_entry(stkptr); |
| 1620 | if (!stksess) |
| 1621 | return NULL; |
| 1622 | |
| 1623 | if (unlikely(args[arg].type == ARGT_TAB)) { |
| 1624 | /* an alternate table was specified, let's look up the same key there */ |
| 1625 | stkctr.table = &args[arg].data.prx->table; |
| 1626 | stkctr_set_entry(&stkctr, stktable_lookup(stkctr.table, stksess)); |
| 1627 | return &stkctr; |
| 1628 | } |
| 1629 | return stkptr; |
| 1630 | } |
| 1631 | |
| 1632 | /* same as smp_fetch_sc_stkctr() but dedicated to src_* and can create |
| 1633 | * the entry if it doesn't exist yet. This is needed for a few fetch |
| 1634 | * functions which need to create an entry, such as src_inc_gpc* and |
| 1635 | * src_clr_gpc*. |
| 1636 | */ |
| 1637 | struct stkctr * |
| 1638 | smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw) |
| 1639 | { |
| 1640 | static struct stkctr stkctr; |
| 1641 | struct stktable_key *key; |
| 1642 | struct connection *conn = objt_conn(sess->origin); |
| 1643 | struct sample smp; |
| 1644 | |
| 1645 | if (strncmp(kw, "src_", 4) != 0) |
| 1646 | return NULL; |
| 1647 | |
| 1648 | if (!conn) |
| 1649 | return NULL; |
| 1650 | |
| 1651 | /* Fetch source adress in a sample. */ |
| 1652 | smp.px = NULL; |
| 1653 | smp.sess = sess; |
| 1654 | smp.strm = strm; |
| 1655 | if (!smp_fetch_src(NULL, &smp, NULL, NULL)) |
| 1656 | return NULL; |
| 1657 | |
| 1658 | /* Converts into key. */ |
| 1659 | key = smp_to_stkey(&smp, &args->data.prx->table); |
| 1660 | if (!key) |
| 1661 | return NULL; |
| 1662 | |
| 1663 | stkctr.table = &args->data.prx->table; |
| 1664 | stkctr_set_entry(&stkctr, stktable_update_key(stkctr.table, key)); |
| 1665 | return &stkctr; |
| 1666 | } |
| 1667 | |
| 1668 | /* set return a boolean indicating if the requested stream counter is |
| 1669 | * currently being tracked or not. |
| 1670 | * Supports being called as "sc[0-9]_tracked" only. |
| 1671 | */ |
| 1672 | static int |
| 1673 | smp_fetch_sc_tracked(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1674 | { |
| 1675 | smp->flags = SMP_F_VOL_TEST; |
| 1676 | smp->data.type = SMP_T_BOOL; |
| 1677 | smp->data.u.sint = !!smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1678 | return 1; |
| 1679 | } |
| 1680 | |
| 1681 | /* set <smp> to the General Purpose Flag 0 value from the stream's tracked |
| 1682 | * frontend counters or from the src. |
| 1683 | * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpt0" only. Value |
| 1684 | * zero is returned if the key is new. |
| 1685 | */ |
| 1686 | static int |
| 1687 | smp_fetch_sc_get_gpt0(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1688 | { |
| 1689 | struct stkctr *stkctr; |
| 1690 | |
| 1691 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1692 | if (!stkctr) |
| 1693 | return 0; |
| 1694 | |
| 1695 | smp->flags = SMP_F_VOL_TEST; |
| 1696 | smp->data.type = SMP_T_SINT; |
| 1697 | smp->data.u.sint = 0; |
| 1698 | |
| 1699 | if (stkctr_entry(stkctr) != NULL) { |
| 1700 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPT0); |
| 1701 | if (!ptr) |
| 1702 | return 0; /* parameter not stored */ |
| 1703 | smp->data.u.sint = stktable_data_cast(ptr, gpt0); |
| 1704 | } |
| 1705 | return 1; |
| 1706 | } |
| 1707 | |
| 1708 | /* set <smp> to the General Purpose Counter 0 value from the stream's tracked |
| 1709 | * frontend counters or from the src. |
| 1710 | * Supports being called as "sc[0-9]_get_gpc0" or "src_get_gpc0" only. Value |
| 1711 | * zero is returned if the key is new. |
| 1712 | */ |
| 1713 | static int |
| 1714 | smp_fetch_sc_get_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1715 | { |
| 1716 | struct stkctr *stkctr; |
| 1717 | |
| 1718 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1719 | if (!stkctr) |
| 1720 | return 0; |
| 1721 | |
| 1722 | smp->flags = SMP_F_VOL_TEST; |
| 1723 | smp->data.type = SMP_T_SINT; |
| 1724 | smp->data.u.sint = 0; |
| 1725 | |
| 1726 | if (stkctr_entry(stkctr) != NULL) { |
| 1727 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0); |
| 1728 | if (!ptr) |
| 1729 | return 0; /* parameter not stored */ |
| 1730 | smp->data.u.sint = stktable_data_cast(ptr, gpc0); |
| 1731 | } |
| 1732 | return 1; |
| 1733 | } |
| 1734 | |
| 1735 | /* set <smp> to the General Purpose Counter 0's event rate from the stream's |
| 1736 | * tracked frontend counters or from the src. |
| 1737 | * Supports being called as "sc[0-9]_gpc0_rate" or "src_gpc0_rate" only. |
| 1738 | * Value zero is returned if the key is new. |
| 1739 | */ |
| 1740 | static int |
| 1741 | smp_fetch_sc_gpc0_rate(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1742 | { |
| 1743 | struct stkctr *stkctr; |
| 1744 | |
| 1745 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1746 | if (!stkctr) |
| 1747 | return 0; |
| 1748 | |
| 1749 | smp->flags = SMP_F_VOL_TEST; |
| 1750 | smp->data.type = SMP_T_SINT; |
| 1751 | smp->data.u.sint = 0; |
| 1752 | if (stkctr_entry(stkctr) != NULL) { |
| 1753 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE); |
| 1754 | if (!ptr) |
| 1755 | return 0; /* parameter not stored */ |
| 1756 | smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate), |
| 1757 | stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u); |
| 1758 | } |
| 1759 | return 1; |
| 1760 | } |
| 1761 | |
| 1762 | /* Increment the General Purpose Counter 0 value from the stream's tracked |
| 1763 | * frontend counters and return it into temp integer. |
| 1764 | * Supports being called as "sc[0-9]_inc_gpc0" or "src_inc_gpc0" only. |
| 1765 | */ |
| 1766 | static int |
| 1767 | smp_fetch_sc_inc_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1768 | { |
| 1769 | struct stkctr *stkctr; |
| 1770 | |
| 1771 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1772 | if (!stkctr) |
| 1773 | return 0; |
| 1774 | |
| 1775 | smp->flags = SMP_F_VOL_TEST; |
| 1776 | smp->data.type = SMP_T_SINT; |
| 1777 | smp->data.u.sint = 0; |
| 1778 | |
| 1779 | if (stkctr_entry(stkctr) == NULL) |
| 1780 | stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw); |
| 1781 | |
| 1782 | if (stkctr && stkctr_entry(stkctr)) { |
| 1783 | void *ptr1,*ptr2; |
| 1784 | |
| 1785 | /* First, update gpc0_rate if it's tracked. Second, update its |
| 1786 | * gpc0 if tracked. Returns gpc0's value otherwise the curr_ctr. |
| 1787 | */ |
| 1788 | ptr1 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0_RATE); |
| 1789 | if (ptr1) { |
| 1790 | update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate), |
| 1791 | stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1); |
| 1792 | smp->data.u.sint = (&stktable_data_cast(ptr1, gpc0_rate))->curr_ctr; |
| 1793 | } |
| 1794 | |
| 1795 | ptr2 = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0); |
| 1796 | if (ptr2) |
| 1797 | smp->data.u.sint = ++stktable_data_cast(ptr2, gpc0); |
| 1798 | |
| 1799 | /* If data was modified, we need to touch to re-schedule sync */ |
| 1800 | if (ptr1 || ptr2) |
| 1801 | stktable_touch(stkctr->table, stkctr_entry(stkctr), 1); |
| 1802 | } |
| 1803 | return 1; |
| 1804 | } |
| 1805 | |
| 1806 | /* Clear the General Purpose Counter 0 value from the stream's tracked |
| 1807 | * frontend counters and return its previous value into temp integer. |
| 1808 | * Supports being called as "sc[0-9]_clr_gpc0" or "src_clr_gpc0" only. |
| 1809 | */ |
| 1810 | static int |
| 1811 | smp_fetch_sc_clr_gpc0(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1812 | { |
| 1813 | struct stkctr *stkctr; |
| 1814 | |
| 1815 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1816 | if (!stkctr) |
| 1817 | return 0; |
| 1818 | |
| 1819 | smp->flags = SMP_F_VOL_TEST; |
| 1820 | smp->data.type = SMP_T_SINT; |
| 1821 | smp->data.u.sint = 0; |
| 1822 | |
| 1823 | if (stkctr_entry(stkctr) == NULL) |
| 1824 | stkctr = smp_create_src_stkctr(smp->sess, smp->strm, args, kw); |
| 1825 | |
| 1826 | if (stkctr_entry(stkctr) != NULL) { |
| 1827 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_GPC0); |
| 1828 | if (!ptr) |
| 1829 | return 0; /* parameter not stored */ |
| 1830 | smp->data.u.sint = stktable_data_cast(ptr, gpc0); |
| 1831 | stktable_data_cast(ptr, gpc0) = 0; |
| 1832 | /* If data was modified, we need to touch to re-schedule sync */ |
| 1833 | stktable_touch(stkctr->table, stkctr_entry(stkctr), 1); |
| 1834 | } |
| 1835 | return 1; |
| 1836 | } |
| 1837 | |
| 1838 | /* set <smp> to the cumulated number of connections from the stream's tracked |
| 1839 | * frontend counters. Supports being called as "sc[0-9]_conn_cnt" or |
| 1840 | * "src_conn_cnt" only. |
| 1841 | */ |
| 1842 | static int |
| 1843 | smp_fetch_sc_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1844 | { |
| 1845 | struct stkctr *stkctr; |
| 1846 | |
| 1847 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1848 | if (!stkctr) |
| 1849 | return 0; |
| 1850 | |
| 1851 | smp->flags = SMP_F_VOL_TEST; |
| 1852 | smp->data.type = SMP_T_SINT; |
| 1853 | smp->data.u.sint = 0; |
| 1854 | if (stkctr_entry(stkctr) != NULL) { |
| 1855 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CNT); |
| 1856 | if (!ptr) |
| 1857 | return 0; /* parameter not stored */ |
| 1858 | smp->data.u.sint = stktable_data_cast(ptr, conn_cnt); |
| 1859 | } |
| 1860 | return 1; |
| 1861 | } |
| 1862 | |
| 1863 | /* set <smp> to the connection rate from the stream's tracked frontend |
| 1864 | * counters. Supports being called as "sc[0-9]_conn_rate" or "src_conn_rate" |
| 1865 | * only. |
| 1866 | */ |
| 1867 | static int |
| 1868 | smp_fetch_sc_conn_rate(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1869 | { |
| 1870 | struct stkctr *stkctr; |
| 1871 | |
| 1872 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1873 | if (!stkctr) |
| 1874 | return 0; |
| 1875 | |
| 1876 | smp->flags = SMP_F_VOL_TEST; |
| 1877 | smp->data.type = SMP_T_SINT; |
| 1878 | smp->data.u.sint = 0; |
| 1879 | if (stkctr_entry(stkctr) != NULL) { |
| 1880 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_RATE); |
| 1881 | if (!ptr) |
| 1882 | return 0; /* parameter not stored */ |
| 1883 | smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate), |
| 1884 | stkctr->table->data_arg[STKTABLE_DT_CONN_RATE].u); |
| 1885 | } |
| 1886 | return 1; |
| 1887 | } |
| 1888 | |
| 1889 | /* set temp integer to the number of connections from the stream's source address |
| 1890 | * in the table pointed to by expr, after updating it. |
| 1891 | * Accepts exactly 1 argument of type table. |
| 1892 | */ |
| 1893 | static int |
| 1894 | smp_fetch_src_updt_conn_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1895 | { |
| 1896 | struct connection *conn = objt_conn(smp->sess->origin); |
| 1897 | struct stksess *ts; |
| 1898 | struct stktable_key *key; |
| 1899 | void *ptr; |
| 1900 | struct proxy *px; |
| 1901 | |
| 1902 | if (!conn) |
| 1903 | return 0; |
| 1904 | |
| 1905 | /* Fetch source adress in a sample. */ |
| 1906 | if (!smp_fetch_src(NULL, smp, NULL, NULL)) |
| 1907 | return 0; |
| 1908 | |
| 1909 | /* Converts into key. */ |
| 1910 | key = smp_to_stkey(smp, &args->data.prx->table); |
| 1911 | if (!key) |
| 1912 | return 0; |
| 1913 | |
| 1914 | px = args->data.prx; |
| 1915 | |
| 1916 | if ((ts = stktable_update_key(&px->table, key)) == NULL) |
| 1917 | /* entry does not exist and could not be created */ |
| 1918 | return 0; |
| 1919 | |
| 1920 | ptr = stktable_data_ptr(&px->table, ts, STKTABLE_DT_CONN_CNT); |
| 1921 | if (!ptr) |
| 1922 | return 0; /* parameter not stored in this table */ |
| 1923 | |
| 1924 | smp->data.type = SMP_T_SINT; |
| 1925 | smp->data.u.sint = ++stktable_data_cast(ptr, conn_cnt); |
| 1926 | /* Touch was previously performed by stktable_update_key */ |
| 1927 | smp->flags = SMP_F_VOL_TEST; |
| 1928 | return 1; |
| 1929 | } |
| 1930 | |
| 1931 | /* set <smp> to the number of concurrent connections from the stream's tracked |
| 1932 | * frontend counters. Supports being called as "sc[0-9]_conn_cur" or |
| 1933 | * "src_conn_cur" only. |
| 1934 | */ |
| 1935 | static int |
| 1936 | smp_fetch_sc_conn_cur(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1937 | { |
| 1938 | struct stkctr *stkctr; |
| 1939 | |
| 1940 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1941 | if (!stkctr) |
| 1942 | return 0; |
| 1943 | |
| 1944 | smp->flags = SMP_F_VOL_TEST; |
| 1945 | smp->data.type = SMP_T_SINT; |
| 1946 | smp->data.u.sint = 0; |
| 1947 | if (stkctr_entry(stkctr) != NULL) { |
| 1948 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_CONN_CUR); |
| 1949 | if (!ptr) |
| 1950 | return 0; /* parameter not stored */ |
| 1951 | smp->data.u.sint = stktable_data_cast(ptr, conn_cur); |
| 1952 | } |
| 1953 | return 1; |
| 1954 | } |
| 1955 | |
| 1956 | /* set <smp> to the cumulated number of streams from the stream's tracked |
| 1957 | * frontend counters. Supports being called as "sc[0-9]_sess_cnt" or |
| 1958 | * "src_sess_cnt" only. |
| 1959 | */ |
| 1960 | static int |
| 1961 | smp_fetch_sc_sess_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1962 | { |
| 1963 | struct stkctr *stkctr; |
| 1964 | |
| 1965 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1966 | if (!stkctr) |
| 1967 | return 0; |
| 1968 | |
| 1969 | smp->flags = SMP_F_VOL_TEST; |
| 1970 | smp->data.type = SMP_T_SINT; |
| 1971 | smp->data.u.sint = 0; |
| 1972 | if (stkctr_entry(stkctr) != NULL) { |
| 1973 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_CNT); |
| 1974 | if (!ptr) |
| 1975 | return 0; /* parameter not stored */ |
| 1976 | smp->data.u.sint = stktable_data_cast(ptr, sess_cnt); |
| 1977 | } |
| 1978 | return 1; |
| 1979 | } |
| 1980 | |
| 1981 | /* set <smp> to the stream rate from the stream's tracked frontend counters. |
| 1982 | * Supports being called as "sc[0-9]_sess_rate" or "src_sess_rate" only. |
| 1983 | */ |
| 1984 | static int |
| 1985 | smp_fetch_sc_sess_rate(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 1986 | { |
| 1987 | struct stkctr *stkctr; |
| 1988 | |
| 1989 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 1990 | if (!stkctr) |
| 1991 | return 0; |
| 1992 | |
| 1993 | smp->flags = SMP_F_VOL_TEST; |
| 1994 | smp->data.type = SMP_T_SINT; |
| 1995 | smp->data.u.sint = 0; |
| 1996 | if (stkctr_entry(stkctr) != NULL) { |
| 1997 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_SESS_RATE); |
| 1998 | if (!ptr) |
| 1999 | return 0; /* parameter not stored */ |
| 2000 | smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate), |
| 2001 | stkctr->table->data_arg[STKTABLE_DT_SESS_RATE].u); |
| 2002 | } |
| 2003 | return 1; |
| 2004 | } |
| 2005 | |
| 2006 | /* set <smp> to the cumulated number of HTTP requests from the stream's tracked |
| 2007 | * frontend counters. Supports being called as "sc[0-9]_http_req_cnt" or |
| 2008 | * "src_http_req_cnt" only. |
| 2009 | */ |
| 2010 | static int |
| 2011 | smp_fetch_sc_http_req_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2012 | { |
| 2013 | struct stkctr *stkctr; |
| 2014 | |
| 2015 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2016 | if (!stkctr) |
| 2017 | return 0; |
| 2018 | |
| 2019 | smp->flags = SMP_F_VOL_TEST; |
| 2020 | smp->data.type = SMP_T_SINT; |
| 2021 | smp->data.u.sint = 0; |
| 2022 | if (stkctr_entry(stkctr) != NULL) { |
| 2023 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_CNT); |
| 2024 | if (!ptr) |
| 2025 | return 0; /* parameter not stored */ |
| 2026 | smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt); |
| 2027 | } |
| 2028 | return 1; |
| 2029 | } |
| 2030 | |
| 2031 | /* set <smp> to the HTTP request rate from the stream's tracked frontend |
| 2032 | * counters. Supports being called as "sc[0-9]_http_req_rate" or |
| 2033 | * "src_http_req_rate" only. |
| 2034 | */ |
| 2035 | static int |
| 2036 | smp_fetch_sc_http_req_rate(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2037 | { |
| 2038 | struct stkctr *stkctr; |
| 2039 | |
| 2040 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2041 | if (!stkctr) |
| 2042 | return 0; |
| 2043 | |
| 2044 | smp->flags = SMP_F_VOL_TEST; |
| 2045 | smp->data.type = SMP_T_SINT; |
| 2046 | smp->data.u.sint = 0; |
| 2047 | if (stkctr_entry(stkctr) != NULL) { |
| 2048 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_REQ_RATE); |
| 2049 | if (!ptr) |
| 2050 | return 0; /* parameter not stored */ |
| 2051 | smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate), |
| 2052 | stkctr->table->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u); |
| 2053 | } |
| 2054 | return 1; |
| 2055 | } |
| 2056 | |
| 2057 | /* set <smp> to the cumulated number of HTTP requests errors from the stream's |
| 2058 | * tracked frontend counters. Supports being called as "sc[0-9]_http_err_cnt" or |
| 2059 | * "src_http_err_cnt" only. |
| 2060 | */ |
| 2061 | static int |
| 2062 | smp_fetch_sc_http_err_cnt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2063 | { |
| 2064 | struct stkctr *stkctr; |
| 2065 | |
| 2066 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2067 | if (!stkctr) |
| 2068 | return 0; |
| 2069 | |
| 2070 | smp->flags = SMP_F_VOL_TEST; |
| 2071 | smp->data.type = SMP_T_SINT; |
| 2072 | smp->data.u.sint = 0; |
| 2073 | if (stkctr_entry(stkctr) != NULL) { |
| 2074 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_CNT); |
| 2075 | if (!ptr) |
| 2076 | return 0; /* parameter not stored */ |
| 2077 | smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt); |
| 2078 | } |
| 2079 | return 1; |
| 2080 | } |
| 2081 | |
| 2082 | /* set <smp> to the HTTP request error rate from the stream's tracked frontend |
| 2083 | * counters. Supports being called as "sc[0-9]_http_err_rate" or |
| 2084 | * "src_http_err_rate" only. |
| 2085 | */ |
| 2086 | static int |
| 2087 | smp_fetch_sc_http_err_rate(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2088 | { |
| 2089 | struct stkctr *stkctr; |
| 2090 | |
| 2091 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2092 | if (!stkctr) |
| 2093 | return 0; |
| 2094 | |
| 2095 | smp->flags = SMP_F_VOL_TEST; |
| 2096 | smp->data.type = SMP_T_SINT; |
| 2097 | smp->data.u.sint = 0; |
| 2098 | if (stkctr_entry(stkctr) != NULL) { |
| 2099 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_HTTP_ERR_RATE); |
| 2100 | if (!ptr) |
| 2101 | return 0; /* parameter not stored */ |
| 2102 | smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate), |
| 2103 | stkctr->table->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u); |
| 2104 | } |
| 2105 | return 1; |
| 2106 | } |
| 2107 | |
| 2108 | /* set <smp> to the number of kbytes received from clients, as found in the |
| 2109 | * stream's tracked frontend counters. Supports being called as |
| 2110 | * "sc[0-9]_kbytes_in" or "src_kbytes_in" only. |
| 2111 | */ |
| 2112 | static int |
| 2113 | smp_fetch_sc_kbytes_in(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2114 | { |
| 2115 | struct stkctr *stkctr; |
| 2116 | |
| 2117 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2118 | if (!stkctr) |
| 2119 | return 0; |
| 2120 | |
| 2121 | smp->flags = SMP_F_VOL_TEST; |
| 2122 | smp->data.type = SMP_T_SINT; |
| 2123 | smp->data.u.sint = 0; |
| 2124 | if (stkctr_entry(stkctr) != NULL) { |
| 2125 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_CNT); |
| 2126 | if (!ptr) |
| 2127 | return 0; /* parameter not stored */ |
| 2128 | smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10; |
| 2129 | } |
| 2130 | return 1; |
| 2131 | } |
| 2132 | |
| 2133 | /* set <smp> to the data rate received from clients in bytes/s, as found |
| 2134 | * in the stream's tracked frontend counters. Supports being called as |
| 2135 | * "sc[0-9]_bytes_in_rate" or "src_bytes_in_rate" only. |
| 2136 | */ |
| 2137 | static int |
| 2138 | smp_fetch_sc_bytes_in_rate(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2139 | { |
| 2140 | struct stkctr *stkctr; |
| 2141 | |
| 2142 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2143 | if (!stkctr) |
| 2144 | return 0; |
| 2145 | |
| 2146 | smp->flags = SMP_F_VOL_TEST; |
| 2147 | smp->data.type = SMP_T_SINT; |
| 2148 | smp->data.u.sint = 0; |
| 2149 | if (stkctr_entry(stkctr) != NULL) { |
| 2150 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_IN_RATE); |
| 2151 | if (!ptr) |
| 2152 | return 0; /* parameter not stored */ |
| 2153 | smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate), |
| 2154 | stkctr->table->data_arg[STKTABLE_DT_BYTES_IN_RATE].u); |
| 2155 | } |
| 2156 | return 1; |
| 2157 | } |
| 2158 | |
| 2159 | /* set <smp> to the number of kbytes sent to clients, as found in the |
| 2160 | * stream's tracked frontend counters. Supports being called as |
| 2161 | * "sc[0-9]_kbytes_out" or "src_kbytes_out" only. |
| 2162 | */ |
| 2163 | static int |
| 2164 | smp_fetch_sc_kbytes_out(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2165 | { |
| 2166 | struct stkctr *stkctr; |
| 2167 | |
| 2168 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2169 | if (!stkctr) |
| 2170 | return 0; |
| 2171 | |
| 2172 | smp->flags = SMP_F_VOL_TEST; |
| 2173 | smp->data.type = SMP_T_SINT; |
| 2174 | smp->data.u.sint = 0; |
| 2175 | if (stkctr_entry(stkctr) != NULL) { |
| 2176 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_CNT); |
| 2177 | if (!ptr) |
| 2178 | return 0; /* parameter not stored */ |
| 2179 | smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10; |
| 2180 | } |
| 2181 | return 1; |
| 2182 | } |
| 2183 | |
| 2184 | /* set <smp> to the data rate sent to clients in bytes/s, as found in the |
| 2185 | * stream's tracked frontend counters. Supports being called as |
| 2186 | * "sc[0-9]_bytes_out_rate" or "src_bytes_out_rate" only. |
| 2187 | */ |
| 2188 | static int |
| 2189 | smp_fetch_sc_bytes_out_rate(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2190 | { |
| 2191 | struct stkctr *stkctr; |
| 2192 | |
| 2193 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2194 | if (!stkctr) |
| 2195 | return 0; |
| 2196 | |
| 2197 | smp->flags = SMP_F_VOL_TEST; |
| 2198 | smp->data.type = SMP_T_SINT; |
| 2199 | smp->data.u.sint = 0; |
| 2200 | if (stkctr_entry(stkctr) != NULL) { |
| 2201 | void *ptr = stktable_data_ptr(stkctr->table, stkctr_entry(stkctr), STKTABLE_DT_BYTES_OUT_RATE); |
| 2202 | if (!ptr) |
| 2203 | return 0; /* parameter not stored */ |
| 2204 | smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate), |
| 2205 | stkctr->table->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u); |
| 2206 | } |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
| 2210 | /* set <smp> to the number of active trackers on the SC entry in the stream's |
| 2211 | * tracked frontend counters. Supports being called as "sc[0-9]_trackers" only. |
| 2212 | */ |
| 2213 | static int |
| 2214 | smp_fetch_sc_trackers(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 2215 | { |
| 2216 | struct stkctr *stkctr; |
| 2217 | |
| 2218 | stkctr = smp_fetch_sc_stkctr(smp->sess, smp->strm, args, kw); |
| 2219 | if (!stkctr) |
| 2220 | return 0; |
| 2221 | |
| 2222 | smp->flags = SMP_F_VOL_TEST; |
| 2223 | smp->data.type = SMP_T_SINT; |
| 2224 | smp->data.u.sint = stkctr_entry(stkctr) ? stkctr_entry(stkctr)->ref_cnt : 0; |
| 2225 | return 1; |
| 2226 | } |
| 2227 | |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2228 | |
| 2229 | /* The functions below are used to manipulate table contents from the CLI. |
| 2230 | * There are 3 main actions, "clear", "set" and "show". The code is shared |
| 2231 | * between all actions, and the action is encoded in the void *private in |
| 2232 | * the appctx as well as in the keyword registration, among one of the |
| 2233 | * following values. |
| 2234 | */ |
| 2235 | |
| 2236 | enum { |
| 2237 | STK_CLI_ACT_CLR, |
| 2238 | STK_CLI_ACT_SET, |
| 2239 | STK_CLI_ACT_SHOW, |
| 2240 | }; |
| 2241 | |
| 2242 | /* Dump the status of a table to a stream interface's |
| 2243 | * read buffer. It returns 0 if the output buffer is full |
| 2244 | * and needs to be called again, otherwise non-zero. |
| 2245 | */ |
| 2246 | static int table_dump_head_to_buffer(struct chunk *msg, struct stream_interface *si, |
| 2247 | struct proxy *proxy, struct proxy *target) |
| 2248 | { |
| 2249 | struct stream *s = si_strm(si); |
| 2250 | |
| 2251 | chunk_appendf(msg, "# table: %s, type: %s, size:%d, used:%d\n", |
| 2252 | proxy->id, stktable_types[proxy->table.type].kw, proxy->table.size, proxy->table.current); |
| 2253 | |
| 2254 | /* any other information should be dumped here */ |
| 2255 | |
William Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 2256 | if (target && (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) < ACCESS_LVL_OPER) |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2257 | chunk_appendf(msg, "# contents not dumped due to insufficient privileges\n"); |
| 2258 | |
| 2259 | if (bi_putchk(si_ic(si), msg) == -1) { |
| 2260 | si_applet_cant_put(si); |
| 2261 | return 0; |
| 2262 | } |
| 2263 | |
| 2264 | return 1; |
| 2265 | } |
| 2266 | |
| 2267 | /* Dump a table entry to a stream interface's |
| 2268 | * read buffer. It returns 0 if the output buffer is full |
| 2269 | * and needs to be called again, otherwise non-zero. |
| 2270 | */ |
| 2271 | static int table_dump_entry_to_buffer(struct chunk *msg, struct stream_interface *si, |
| 2272 | struct proxy *proxy, struct stksess *entry) |
| 2273 | { |
| 2274 | int dt; |
| 2275 | |
| 2276 | chunk_appendf(msg, "%p:", entry); |
| 2277 | |
| 2278 | if (proxy->table.type == SMP_T_IPV4) { |
| 2279 | char addr[INET_ADDRSTRLEN]; |
| 2280 | inet_ntop(AF_INET, (const void *)&entry->key.key, addr, sizeof(addr)); |
| 2281 | chunk_appendf(msg, " key=%s", addr); |
| 2282 | } |
| 2283 | else if (proxy->table.type == SMP_T_IPV6) { |
| 2284 | char addr[INET6_ADDRSTRLEN]; |
| 2285 | inet_ntop(AF_INET6, (const void *)&entry->key.key, addr, sizeof(addr)); |
| 2286 | chunk_appendf(msg, " key=%s", addr); |
| 2287 | } |
| 2288 | else if (proxy->table.type == SMP_T_SINT) { |
| 2289 | chunk_appendf(msg, " key=%u", *(unsigned int *)entry->key.key); |
| 2290 | } |
| 2291 | else if (proxy->table.type == SMP_T_STR) { |
| 2292 | chunk_appendf(msg, " key="); |
| 2293 | dump_text(msg, (const char *)entry->key.key, proxy->table.key_size); |
| 2294 | } |
| 2295 | else { |
| 2296 | chunk_appendf(msg, " key="); |
| 2297 | dump_binary(msg, (const char *)entry->key.key, proxy->table.key_size); |
| 2298 | } |
| 2299 | |
| 2300 | chunk_appendf(msg, " use=%d exp=%d", entry->ref_cnt - 1, tick_remain(now_ms, entry->expire)); |
| 2301 | |
| 2302 | for (dt = 0; dt < STKTABLE_DATA_TYPES; dt++) { |
| 2303 | void *ptr; |
| 2304 | |
| 2305 | if (proxy->table.data_ofs[dt] == 0) |
| 2306 | continue; |
| 2307 | if (stktable_data_types[dt].arg_type == ARG_T_DELAY) |
| 2308 | chunk_appendf(msg, " %s(%d)=", stktable_data_types[dt].name, proxy->table.data_arg[dt].u); |
| 2309 | else |
| 2310 | chunk_appendf(msg, " %s=", stktable_data_types[dt].name); |
| 2311 | |
| 2312 | ptr = stktable_data_ptr(&proxy->table, entry, dt); |
| 2313 | switch (stktable_data_types[dt].std_type) { |
| 2314 | case STD_T_SINT: |
| 2315 | chunk_appendf(msg, "%d", stktable_data_cast(ptr, std_t_sint)); |
| 2316 | break; |
| 2317 | case STD_T_UINT: |
| 2318 | chunk_appendf(msg, "%u", stktable_data_cast(ptr, std_t_uint)); |
| 2319 | break; |
| 2320 | case STD_T_ULL: |
| 2321 | chunk_appendf(msg, "%lld", stktable_data_cast(ptr, std_t_ull)); |
| 2322 | break; |
| 2323 | case STD_T_FRQP: |
| 2324 | chunk_appendf(msg, "%d", |
| 2325 | read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 2326 | proxy->table.data_arg[dt].u)); |
| 2327 | break; |
| 2328 | } |
| 2329 | } |
| 2330 | chunk_appendf(msg, "\n"); |
| 2331 | |
| 2332 | if (bi_putchk(si_ic(si), msg) == -1) { |
| 2333 | si_applet_cant_put(si); |
| 2334 | return 0; |
| 2335 | } |
| 2336 | |
| 2337 | return 1; |
| 2338 | } |
| 2339 | |
| 2340 | |
| 2341 | /* Processes a single table entry matching a specific key passed in argument. |
| 2342 | * returns 0 if wants to be called again, 1 if has ended processing. |
| 2343 | */ |
| 2344 | static int table_process_entry_per_key(struct appctx *appctx, char **args) |
| 2345 | { |
| 2346 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2347 | struct proxy *px = appctx->ctx.table.target; |
| 2348 | struct stksess *ts; |
| 2349 | uint32_t uint32_key; |
| 2350 | unsigned char ip6_key[sizeof(struct in6_addr)]; |
| 2351 | long long value; |
| 2352 | int data_type; |
| 2353 | int cur_arg; |
| 2354 | void *ptr; |
| 2355 | struct freq_ctr_period *frqp; |
| 2356 | |
| 2357 | if (!*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2358 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2359 | appctx->ctx.cli.msg = "Key value expected\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2360 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2361 | return 1; |
| 2362 | } |
| 2363 | |
| 2364 | switch (px->table.type) { |
| 2365 | case SMP_T_IPV4: |
| 2366 | uint32_key = htonl(inetaddr_host(args[4])); |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 2367 | static_table_key.key = &uint32_key; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2368 | break; |
| 2369 | case SMP_T_IPV6: |
| 2370 | inet_pton(AF_INET6, args[4], ip6_key); |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 2371 | static_table_key.key = &ip6_key; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2372 | break; |
| 2373 | case SMP_T_SINT: |
| 2374 | { |
| 2375 | char *endptr; |
| 2376 | unsigned long val; |
| 2377 | errno = 0; |
| 2378 | val = strtoul(args[4], &endptr, 10); |
| 2379 | if ((errno == ERANGE && val == ULONG_MAX) || |
| 2380 | (errno != 0 && val == 0) || endptr == args[4] || |
| 2381 | val > 0xffffffff) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2382 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2383 | appctx->ctx.cli.msg = "Invalid key\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2384 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2385 | return 1; |
| 2386 | } |
| 2387 | uint32_key = (uint32_t) val; |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 2388 | static_table_key.key = &uint32_key; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2389 | break; |
| 2390 | } |
| 2391 | break; |
| 2392 | case SMP_T_STR: |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 2393 | static_table_key.key = args[4]; |
| 2394 | static_table_key.key_len = strlen(args[4]); |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2395 | break; |
| 2396 | default: |
Willy Tarreau | a24bc78 | 2016-12-14 15:50:35 +0100 | [diff] [blame] | 2397 | switch (appctx->ctx.table.action) { |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2398 | case STK_CLI_ACT_SHOW: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2399 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2400 | appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n"; |
| 2401 | break; |
| 2402 | case STK_CLI_ACT_CLR: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2403 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2404 | appctx->ctx.cli.msg = "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n"; |
| 2405 | break; |
| 2406 | case STK_CLI_ACT_SET: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2407 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2408 | appctx->ctx.cli.msg = "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n"; |
| 2409 | break; |
| 2410 | default: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2411 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2412 | appctx->ctx.cli.msg = "Unknown action\n"; |
| 2413 | break; |
| 2414 | } |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2415 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2416 | return 1; |
| 2417 | } |
| 2418 | |
| 2419 | /* check permissions */ |
| 2420 | if (!cli_has_level(appctx, ACCESS_LVL_OPER)) |
| 2421 | return 1; |
| 2422 | |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 2423 | ts = stktable_lookup_key(&px->table, &static_table_key); |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2424 | |
Willy Tarreau | a24bc78 | 2016-12-14 15:50:35 +0100 | [diff] [blame] | 2425 | switch (appctx->ctx.table.action) { |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2426 | case STK_CLI_ACT_SHOW: |
| 2427 | if (!ts) |
| 2428 | return 1; |
| 2429 | chunk_reset(&trash); |
| 2430 | if (!table_dump_head_to_buffer(&trash, si, px, px)) |
| 2431 | return 0; |
| 2432 | if (!table_dump_entry_to_buffer(&trash, si, px, ts)) |
| 2433 | return 0; |
| 2434 | break; |
| 2435 | |
| 2436 | case STK_CLI_ACT_CLR: |
| 2437 | if (!ts) |
| 2438 | return 1; |
| 2439 | if (ts->ref_cnt) { |
| 2440 | /* don't delete an entry which is currently referenced */ |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2441 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2442 | appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2443 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2444 | return 1; |
| 2445 | } |
| 2446 | stksess_kill(&px->table, ts); |
| 2447 | break; |
| 2448 | |
| 2449 | case STK_CLI_ACT_SET: |
| 2450 | if (ts) |
| 2451 | stktable_touch(&px->table, ts, 1); |
| 2452 | else { |
Christopher Faulet | ca20d02 | 2017-08-29 15:30:31 +0200 | [diff] [blame] | 2453 | ts = stksess_new(&px->table, &static_table_key); |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2454 | if (!ts) { |
| 2455 | /* don't delete an entry which is currently referenced */ |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2456 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2457 | appctx->ctx.cli.msg = "Unable to allocate a new entry\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2458 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2459 | return 1; |
| 2460 | } |
| 2461 | stktable_store(&px->table, ts, 1); |
| 2462 | } |
| 2463 | |
| 2464 | for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) { |
| 2465 | if (strncmp(args[cur_arg], "data.", 5) != 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2466 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2467 | appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2468 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2469 | return 1; |
| 2470 | } |
| 2471 | |
| 2472 | data_type = stktable_get_data_type(args[cur_arg] + 5); |
| 2473 | if (data_type < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2474 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2475 | appctx->ctx.cli.msg = "Unknown data type\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2476 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2477 | return 1; |
| 2478 | } |
| 2479 | |
| 2480 | if (!px->table.data_ofs[data_type]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2481 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2482 | appctx->ctx.cli.msg = "Data type not stored in this table\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2483 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2484 | return 1; |
| 2485 | } |
| 2486 | |
| 2487 | if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2488 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2489 | appctx->ctx.cli.msg = "Require a valid integer value to store\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2490 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2491 | return 1; |
| 2492 | } |
| 2493 | |
| 2494 | ptr = stktable_data_ptr(&px->table, ts, data_type); |
| 2495 | |
| 2496 | switch (stktable_data_types[data_type].std_type) { |
| 2497 | case STD_T_SINT: |
| 2498 | stktable_data_cast(ptr, std_t_sint) = value; |
| 2499 | break; |
| 2500 | case STD_T_UINT: |
| 2501 | stktable_data_cast(ptr, std_t_uint) = value; |
| 2502 | break; |
| 2503 | case STD_T_ULL: |
| 2504 | stktable_data_cast(ptr, std_t_ull) = value; |
| 2505 | break; |
| 2506 | case STD_T_FRQP: |
| 2507 | /* We set both the current and previous values. That way |
| 2508 | * the reported frequency is stable during all the period |
| 2509 | * then slowly fades out. This allows external tools to |
| 2510 | * push measures without having to update them too often. |
| 2511 | */ |
| 2512 | frqp = &stktable_data_cast(ptr, std_t_frqp); |
| 2513 | frqp->curr_tick = now_ms; |
| 2514 | frqp->prev_ctr = 0; |
| 2515 | frqp->curr_ctr = value; |
| 2516 | break; |
| 2517 | } |
| 2518 | } |
| 2519 | break; |
| 2520 | |
| 2521 | default: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2522 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2523 | appctx->ctx.cli.msg = "Unknown action\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2524 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2525 | break; |
| 2526 | } |
| 2527 | return 1; |
| 2528 | } |
| 2529 | |
| 2530 | /* Prepares the appctx fields with the data-based filters from the command line. |
| 2531 | * Returns 0 if the dump can proceed, 1 if has ended processing. |
| 2532 | */ |
| 2533 | static int table_prepare_data_request(struct appctx *appctx, char **args) |
| 2534 | { |
Willy Tarreau | a24bc78 | 2016-12-14 15:50:35 +0100 | [diff] [blame] | 2535 | if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2536 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2537 | appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2538 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2539 | return 1; |
| 2540 | } |
| 2541 | |
| 2542 | /* condition on stored data value */ |
| 2543 | appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5); |
| 2544 | if (appctx->ctx.table.data_type < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2545 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2546 | appctx->ctx.cli.msg = "Unknown data type\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2547 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2548 | return 1; |
| 2549 | } |
| 2550 | |
| 2551 | if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2552 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2553 | appctx->ctx.cli.msg = "Data type not stored in this table\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2554 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2555 | return 1; |
| 2556 | } |
| 2557 | |
| 2558 | appctx->ctx.table.data_op = get_std_op(args[4]); |
| 2559 | if (appctx->ctx.table.data_op < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2560 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2561 | appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2562 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2563 | return 1; |
| 2564 | } |
| 2565 | |
| 2566 | if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2567 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2568 | appctx->ctx.cli.msg = "Require a valid integer value to compare against\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2569 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2570 | return 1; |
| 2571 | } |
| 2572 | |
| 2573 | /* OK we're done, all the fields are set */ |
| 2574 | return 0; |
| 2575 | } |
| 2576 | |
| 2577 | /* returns 0 if wants to be called, 1 if has ended processing */ |
| 2578 | static int cli_parse_table_req(char **args, struct appctx *appctx, void *private) |
| 2579 | { |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2580 | appctx->ctx.table.data_type = -1; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2581 | appctx->ctx.table.target = NULL; |
| 2582 | appctx->ctx.table.proxy = NULL; |
| 2583 | appctx->ctx.table.entry = NULL; |
Willy Tarreau | a24bc78 | 2016-12-14 15:50:35 +0100 | [diff] [blame] | 2584 | appctx->ctx.table.action = (long)private; // keyword argument, one of STK_CLI_ACT_* |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2585 | |
| 2586 | if (*args[2]) { |
| 2587 | appctx->ctx.table.target = proxy_tbl_by_name(args[2]); |
| 2588 | if (!appctx->ctx.table.target) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2589 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2590 | appctx->ctx.cli.msg = "No such table\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2591 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2592 | return 1; |
| 2593 | } |
| 2594 | } |
| 2595 | else { |
Willy Tarreau | a24bc78 | 2016-12-14 15:50:35 +0100 | [diff] [blame] | 2596 | if (appctx->ctx.table.action != STK_CLI_ACT_SHOW) |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2597 | goto err_args; |
| 2598 | return 0; |
| 2599 | } |
| 2600 | |
| 2601 | if (strcmp(args[3], "key") == 0) |
| 2602 | return table_process_entry_per_key(appctx, args); |
| 2603 | else if (strncmp(args[3], "data.", 5) == 0) |
| 2604 | return table_prepare_data_request(appctx, args); |
| 2605 | else if (*args[3]) |
| 2606 | goto err_args; |
| 2607 | |
| 2608 | return 0; |
| 2609 | |
| 2610 | err_args: |
Willy Tarreau | a24bc78 | 2016-12-14 15:50:35 +0100 | [diff] [blame] | 2611 | switch (appctx->ctx.table.action) { |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2612 | case STK_CLI_ACT_SHOW: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2613 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2614 | appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n"; |
| 2615 | break; |
| 2616 | case STK_CLI_ACT_CLR: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2617 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2618 | appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n"; |
| 2619 | break; |
| 2620 | case STK_CLI_ACT_SET: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2621 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2622 | appctx->ctx.cli.msg = "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n"; |
| 2623 | break; |
| 2624 | default: |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 2625 | appctx->ctx.cli.severity = LOG_ERR; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2626 | appctx->ctx.cli.msg = "Unknown action\n"; |
| 2627 | break; |
| 2628 | } |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 2629 | appctx->st0 = CLI_ST_PRINT; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2630 | return 1; |
| 2631 | } |
| 2632 | |
| 2633 | /* This function is used to deal with table operations (dump or clear depending |
| 2634 | * on the action stored in appctx->private). It returns 0 if the output buffer is |
| 2635 | * full and it needs to be called again, otherwise non-zero. |
| 2636 | */ |
| 2637 | static int cli_io_handler_table(struct appctx *appctx) |
| 2638 | { |
| 2639 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2640 | struct stream *s = si_strm(si); |
| 2641 | struct ebmb_node *eb; |
| 2642 | int dt; |
| 2643 | int skip_entry; |
Willy Tarreau | a24bc78 | 2016-12-14 15:50:35 +0100 | [diff] [blame] | 2644 | int show = appctx->ctx.table.action == STK_CLI_ACT_SHOW; |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2645 | |
| 2646 | /* |
| 2647 | * We have 3 possible states in appctx->st2 : |
| 2648 | * - STAT_ST_INIT : the first call |
| 2649 | * - STAT_ST_INFO : the proxy pointer points to the next table to |
| 2650 | * dump, the entry pointer is NULL ; |
| 2651 | * - STAT_ST_LIST : the proxy pointer points to the current table |
| 2652 | * and the entry pointer points to the next entry to be dumped, |
| 2653 | * and the refcount on the next entry is held ; |
| 2654 | * - STAT_ST_END : nothing left to dump, the buffer may contain some |
| 2655 | * data though. |
| 2656 | */ |
| 2657 | |
| 2658 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) { |
| 2659 | /* in case of abort, remove any refcount we might have set on an entry */ |
| 2660 | if (appctx->st2 == STAT_ST_LIST) { |
| 2661 | appctx->ctx.table.entry->ref_cnt--; |
| 2662 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 2663 | } |
| 2664 | return 1; |
| 2665 | } |
| 2666 | |
| 2667 | chunk_reset(&trash); |
| 2668 | |
| 2669 | while (appctx->st2 != STAT_ST_FIN) { |
| 2670 | switch (appctx->st2) { |
| 2671 | case STAT_ST_INIT: |
| 2672 | appctx->ctx.table.proxy = appctx->ctx.table.target; |
| 2673 | if (!appctx->ctx.table.proxy) |
| 2674 | appctx->ctx.table.proxy = proxy; |
| 2675 | |
| 2676 | appctx->ctx.table.entry = NULL; |
| 2677 | appctx->st2 = STAT_ST_INFO; |
| 2678 | break; |
| 2679 | |
| 2680 | case STAT_ST_INFO: |
| 2681 | if (!appctx->ctx.table.proxy || |
| 2682 | (appctx->ctx.table.target && |
| 2683 | appctx->ctx.table.proxy != appctx->ctx.table.target)) { |
| 2684 | appctx->st2 = STAT_ST_END; |
| 2685 | break; |
| 2686 | } |
| 2687 | |
| 2688 | if (appctx->ctx.table.proxy->table.size) { |
| 2689 | if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.target)) |
| 2690 | return 0; |
| 2691 | |
| 2692 | if (appctx->ctx.table.target && |
William Lallemand | 07a62f7 | 2017-05-24 00:57:40 +0200 | [diff] [blame] | 2693 | (strm_li(s)->bind_conf->level & ACCESS_LVL_MASK) >= ACCESS_LVL_OPER) { |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2694 | /* dump entries only if table explicitly requested */ |
| 2695 | eb = ebmb_first(&appctx->ctx.table.proxy->table.keys); |
| 2696 | if (eb) { |
| 2697 | appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key); |
| 2698 | appctx->ctx.table.entry->ref_cnt++; |
| 2699 | appctx->st2 = STAT_ST_LIST; |
| 2700 | break; |
| 2701 | } |
| 2702 | } |
| 2703 | } |
| 2704 | appctx->ctx.table.proxy = appctx->ctx.table.proxy->next; |
| 2705 | break; |
| 2706 | |
| 2707 | case STAT_ST_LIST: |
| 2708 | skip_entry = 0; |
| 2709 | |
| 2710 | if (appctx->ctx.table.data_type >= 0) { |
| 2711 | /* we're filtering on some data contents */ |
| 2712 | void *ptr; |
| 2713 | long long data; |
| 2714 | |
| 2715 | dt = appctx->ctx.table.data_type; |
| 2716 | ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table, |
| 2717 | appctx->ctx.table.entry, |
| 2718 | dt); |
| 2719 | |
| 2720 | data = 0; |
| 2721 | switch (stktable_data_types[dt].std_type) { |
| 2722 | case STD_T_SINT: |
| 2723 | data = stktable_data_cast(ptr, std_t_sint); |
| 2724 | break; |
| 2725 | case STD_T_UINT: |
| 2726 | data = stktable_data_cast(ptr, std_t_uint); |
| 2727 | break; |
| 2728 | case STD_T_ULL: |
| 2729 | data = stktable_data_cast(ptr, std_t_ull); |
| 2730 | break; |
| 2731 | case STD_T_FRQP: |
| 2732 | data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp), |
| 2733 | appctx->ctx.table.proxy->table.data_arg[dt].u); |
| 2734 | break; |
| 2735 | } |
| 2736 | |
| 2737 | /* skip the entry if the data does not match the test and the value */ |
| 2738 | if ((data < appctx->ctx.table.value && |
| 2739 | (appctx->ctx.table.data_op == STD_OP_EQ || |
| 2740 | appctx->ctx.table.data_op == STD_OP_GT || |
| 2741 | appctx->ctx.table.data_op == STD_OP_GE)) || |
| 2742 | (data == appctx->ctx.table.value && |
| 2743 | (appctx->ctx.table.data_op == STD_OP_NE || |
| 2744 | appctx->ctx.table.data_op == STD_OP_GT || |
| 2745 | appctx->ctx.table.data_op == STD_OP_LT)) || |
| 2746 | (data > appctx->ctx.table.value && |
| 2747 | (appctx->ctx.table.data_op == STD_OP_EQ || |
| 2748 | appctx->ctx.table.data_op == STD_OP_LT || |
| 2749 | appctx->ctx.table.data_op == STD_OP_LE))) |
| 2750 | skip_entry = 1; |
| 2751 | } |
| 2752 | |
| 2753 | if (show && !skip_entry && |
| 2754 | !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.entry)) |
| 2755 | return 0; |
| 2756 | |
| 2757 | appctx->ctx.table.entry->ref_cnt--; |
| 2758 | |
| 2759 | eb = ebmb_next(&appctx->ctx.table.entry->key); |
| 2760 | if (eb) { |
| 2761 | struct stksess *old = appctx->ctx.table.entry; |
| 2762 | appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key); |
| 2763 | if (show) |
| 2764 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old); |
| 2765 | else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt) |
| 2766 | stksess_kill(&appctx->ctx.table.proxy->table, old); |
| 2767 | appctx->ctx.table.entry->ref_cnt++; |
| 2768 | break; |
| 2769 | } |
| 2770 | |
| 2771 | |
| 2772 | if (show) |
| 2773 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 2774 | else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt) |
| 2775 | stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 2776 | |
| 2777 | appctx->ctx.table.proxy = appctx->ctx.table.proxy->next; |
| 2778 | appctx->st2 = STAT_ST_INFO; |
| 2779 | break; |
| 2780 | |
| 2781 | case STAT_ST_END: |
| 2782 | appctx->st2 = STAT_ST_FIN; |
| 2783 | break; |
| 2784 | } |
| 2785 | } |
| 2786 | return 1; |
| 2787 | } |
| 2788 | |
| 2789 | static void cli_release_show_table(struct appctx *appctx) |
| 2790 | { |
| 2791 | if (appctx->st2 == STAT_ST_LIST) { |
| 2792 | appctx->ctx.table.entry->ref_cnt--; |
| 2793 | stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry); |
| 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | /* register cli keywords */ |
| 2798 | static struct cli_kw_list cli_kws = {{ },{ |
| 2799 | { { "clear", "table", NULL }, "clear table : remove an entry from a table", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_CLR }, |
| 2800 | { { "set", "table", NULL }, "set table [id] : update or create a table entry's data", cli_parse_table_req, cli_io_handler_table, NULL, (void *)STK_CLI_ACT_SET }, |
| 2801 | { { "show", "table", NULL }, "show table [id]: report table usage stats or dump this table's contents", cli_parse_table_req, cli_io_handler_table, cli_release_show_table, (void *)STK_CLI_ACT_SHOW }, |
| 2802 | {{},} |
| 2803 | }}; |
| 2804 | |
| 2805 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2806 | static struct action_kw_list tcp_conn_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2807 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2808 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2809 | { /* END */ } |
| 2810 | }}; |
| 2811 | |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 2812 | static struct action_kw_list tcp_sess_kws = { { }, { |
| 2813 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
| 2814 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2815 | { /* END */ } |
| 2816 | }}; |
| 2817 | |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2818 | static struct action_kw_list tcp_req_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2819 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2820 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2821 | { /* END */ } |
| 2822 | }}; |
| 2823 | |
| 2824 | static struct action_kw_list tcp_res_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2825 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2826 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2827 | { /* END */ } |
| 2828 | }}; |
| 2829 | |
| 2830 | static struct action_kw_list http_req_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2831 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2832 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2833 | { /* END */ } |
| 2834 | }}; |
| 2835 | |
| 2836 | static struct action_kw_list http_res_kws = { { }, { |
Thierry FOURNIER | e0627bd | 2015-08-04 08:20:33 +0200 | [diff] [blame] | 2837 | { "sc-inc-gpc0", parse_inc_gpc0, 1 }, |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2838 | { "sc-set-gpt0", parse_set_gpt0, 1 }, |
| 2839 | { /* END */ } |
| 2840 | }}; |
| 2841 | |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 2842 | ///* Note: must not be declared <const> as its list will be overwritten. |
| 2843 | // * Please take care of keeping this list alphabetically sorted. |
| 2844 | // */ |
| 2845 | //static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, { |
| 2846 | // { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2847 | // { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2848 | // { /* END */ }, |
| 2849 | //}}; |
| 2850 | /* Note: must not be declared <const> as its list will be overwritten. |
| 2851 | * Please take care of keeping this list alphabetically sorted. |
| 2852 | */ |
| 2853 | static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, { |
| 2854 | { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2855 | { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2856 | { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2857 | { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2858 | { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2859 | { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Thierry FOURNIER | 401c64b | 2017-01-05 11:44:09 +0100 | [diff] [blame] | 2860 | { "sc_get_gpt0", smp_fetch_sc_get_gpt0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 2861 | { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2862 | { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2863 | { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2864 | { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2865 | { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2866 | { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2867 | { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2868 | { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2869 | { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2870 | { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2871 | { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2872 | { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, }, |
| 2873 | { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2874 | { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2875 | { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2876 | { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2877 | { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2878 | { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2879 | { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Thierry FOURNIER | 401c64b | 2017-01-05 11:44:09 +0100 | [diff] [blame] | 2880 | { "sc0_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 2881 | { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2882 | { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2883 | { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2884 | { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2885 | { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2886 | { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2887 | { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2888 | { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2889 | { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2890 | { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2891 | { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2892 | { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, }, |
| 2893 | { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2894 | { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2895 | { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2896 | { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2897 | { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2898 | { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2899 | { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Thierry FOURNIER | 401c64b | 2017-01-05 11:44:09 +0100 | [diff] [blame] | 2900 | { "sc1_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 2901 | { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2902 | { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2903 | { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2904 | { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2905 | { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2906 | { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2907 | { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2908 | { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2909 | { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2910 | { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2911 | { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2912 | { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, }, |
| 2913 | { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2914 | { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2915 | { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2916 | { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2917 | { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2918 | { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2919 | { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Thierry FOURNIER | 401c64b | 2017-01-05 11:44:09 +0100 | [diff] [blame] | 2920 | { "sc2_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 2921 | { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2922 | { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2923 | { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2924 | { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2925 | { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2926 | { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2927 | { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2928 | { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2929 | { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2930 | { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2931 | { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2932 | { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, }, |
| 2933 | { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2934 | { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2935 | { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2936 | { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2937 | { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2938 | { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2939 | { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
Thierry FOURNIER | 401c64b | 2017-01-05 11:44:09 +0100 | [diff] [blame] | 2940 | { "src_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 2941 | { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2942 | { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2943 | { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2944 | { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2945 | { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2946 | { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2947 | { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2948 | { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2949 | { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2950 | { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2951 | { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2952 | { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, }, |
| 2953 | { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2954 | { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, }, |
| 2955 | { /* END */ }, |
| 2956 | }}; |
| 2957 | |
| 2958 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2959 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 2960 | static struct sample_conv_kw_list sample_conv_kws = {ILH, { |
Willy Tarreau | 2d17db5 | 2016-05-25 17:16:38 +0200 | [diff] [blame] | 2961 | { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL }, |
| 2962 | { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2963 | { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2964 | { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2965 | { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2966 | { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2967 | { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2968 | { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2969 | { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2970 | { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2971 | { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2972 | { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2973 | { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2974 | { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2975 | { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2976 | { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2977 | { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2978 | { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
| 2979 | { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT }, |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2980 | { /* END */ }, |
| 2981 | }}; |
| 2982 | |
| 2983 | __attribute__((constructor)) |
| 2984 | static void __stick_table_init(void) |
| 2985 | { |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2986 | /* register som action keywords. */ |
| 2987 | tcp_req_conn_keywords_register(&tcp_conn_kws); |
Willy Tarreau | 620408f | 2016-10-21 16:37:51 +0200 | [diff] [blame] | 2988 | tcp_req_sess_keywords_register(&tcp_sess_kws); |
Thierry FOURNIER | 236657b | 2015-08-19 08:25:14 +0200 | [diff] [blame] | 2989 | tcp_req_cont_keywords_register(&tcp_req_kws); |
| 2990 | tcp_res_cont_keywords_register(&tcp_res_kws); |
| 2991 | http_req_keywords_register(&http_req_kws); |
| 2992 | http_res_keywords_register(&http_res_kws); |
| 2993 | |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2994 | /* register sample fetch and format conversion keywords */ |
Willy Tarreau | 7d56221 | 2016-11-25 16:10:05 +0100 | [diff] [blame] | 2995 | sample_register_fetches(&smp_fetch_keywords); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2996 | sample_register_convs(&sample_conv_kws); |
Willy Tarreau | f13ebdf | 2016-11-22 18:00:53 +0100 | [diff] [blame] | 2997 | cli_register_kw(&cli_kws); |
Willy Tarreau | d9f316a | 2014-07-10 14:03:38 +0200 | [diff] [blame] | 2998 | } |