blob: ba45a05cf0fb8cbb44e688b16e9e02fd299e3fec [file] [log] [blame]
Emeric Brun3bd697e2010-01-04 15:23:48 +01001/*
2 * Stick tables management functions.
3 *
4 * Copyright 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreau08d5f982010-06-06 13:34:54 +02005 * Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
Emeric Brun3bd697e2010-01-04 15:23:48 +01006 *
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 Tarreauf13ebdf2016-11-22 18:00:53 +010015#include <errno.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010016
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 Tarreauf13ebdf2016-11-22 18:00:53 +010026#include <types/cli.h>
Willy Tarreau39713102016-11-25 15:49:32 +010027#include <types/global.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010028#include <types/stats.h>
29
Willy Tarreaud9f316a2014-07-10 14:03:38 +020030#include <proto/arg.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010031#include <proto/cli.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020032#include <proto/proto_http.h>
Willy Tarreau7d562212016-11-25 16:10:05 +010033#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010034#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020035#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020036#include <proto/stream.h>
Willy Tarreauf13ebdf2016-11-22 18:00:53 +010037#include <proto/stream_interface.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020038#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010039#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020040#include <proto/peers.h>
Willy Tarreau39713102016-11-25 15:49:32 +010041#include <proto/tcp_rules.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010042
Willy Tarreau12785782012-04-27 21:37:17 +020043/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010044struct stktable_key *static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020045
Emeric Brun3bd697e2010-01-04 15:23:48 +010046/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020047 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
48 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010049 */
50void stksess_free(struct stktable *t, struct stksess *ts)
51{
52 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020053 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010054}
55
56/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020057 * Kill an stksess (only if its ref_cnt is zero).
58 */
59void stksess_kill(struct stktable *t, struct stksess *ts)
60{
61 if (ts->ref_cnt)
62 return;
63
64 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020065 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020066 ebmb_delete(&ts->key);
67 stksess_free(t, ts);
68}
69
70/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020071 * Initialize or update the key in the sticky session <ts> present in table <t>
72 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010073 */
Willy Tarreau393379c2010-06-06 12:11:37 +020074void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010075{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020076 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020077 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010078 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020079 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
80 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010081 }
82}
83
84
85/*
Willy Tarreau393379c2010-06-06 12:11:37 +020086 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
87 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010088 */
Willy Tarreau393379c2010-06-06 12:11:37 +020089static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010090{
Willy Tarreau393379c2010-06-06 12:11:37 +020091 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020092 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020093 ts->key.node.leaf_p = NULL;
94 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020095 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010096 return ts;
97}
98
99/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200100 * Trash oldest <to_batch> sticky sessions from table <t>
101 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100102 */
Willy Tarreau3a925c12013-09-04 17:54:01 +0200103int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100104{
105 struct stksess *ts;
106 struct eb32_node *eb;
107 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200108 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100109
110 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
111
112 while (batched < to_batch) {
113
114 if (unlikely(!eb)) {
115 /* we might have reached the end of the tree, typically because
116 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200117 * half. Let's loop back to the beginning of the tree now if we
118 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100119 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200120 if (looped)
121 break;
122 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100123 eb = eb32_first(&t->exps);
124 if (likely(!eb))
125 break;
126 }
127
128 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100130 eb = eb32_next(eb);
131
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200132 /* don't delete an entry which is currently referenced */
133 if (ts->ref_cnt)
134 continue;
135
Willy Tarreau86257dc2010-06-06 12:57:10 +0200136 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
Willy Tarreau86257dc2010-06-06 12:57:10 +0200138 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100139 if (!tick_isset(ts->expire))
140 continue;
141
Willy Tarreau86257dc2010-06-06 12:57:10 +0200142 ts->exp.key = ts->expire;
143 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100144
Willy Tarreau86257dc2010-06-06 12:57:10 +0200145 if (!eb || eb->key > ts->exp.key)
146 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100147
148 continue;
149 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100150
Willy Tarreauaea940e2010-06-06 11:56:36 +0200151 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200152 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200153 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100154 stksess_free(t, ts);
155 batched++;
156 }
157
158 return batched;
159}
160
161/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200162 * Allocate and initialise a new sticky session.
163 * The new sticky session is returned or NULL in case of lack of memory.
164 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200165 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
166 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100167 */
168struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
169{
170 struct stksess *ts;
171
172 if (unlikely(t->current == t->size)) {
173 if ( t->nopurge )
174 return NULL;
175
Emeric Brunfbce6d02010-09-23 18:10:00 +0200176 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100177 return NULL;
178 }
179
Vincent Bernatef8f4fe2016-11-17 15:42:40 +0100180 ts = pool_alloc2(t->pool);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100181 if (ts) {
182 t->current++;
Willy Tarreau51791462016-11-18 18:21:39 +0100183 ts = (void *)ts + t->data_size;
Willy Tarreau393379c2010-06-06 12:11:37 +0200184 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200185 if (key)
186 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 }
188
189 return ts;
190}
191
192/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200193 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200194 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100195 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200196struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100197{
198 struct ebmb_node *eb;
199
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200200 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200201 eb = ebst_lookup_len(&t->keys, key->key, key->key_len+1 < t->key_size ? key->key_len : t->key_size-1);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100202 else
203 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
204
205 if (unlikely(!eb)) {
206 /* no session found */
207 return NULL;
208 }
209
Willy Tarreau86257dc2010-06-06 12:57:10 +0200210 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100211}
212
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200213/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
214 * This is mainly used for situations where we want to refresh a key's usage so
215 * that it does not expire, and we want to have it created if it was not there.
216 * The stksess is returned, or NULL if it could not be created.
217 */
218struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
219{
220 struct stksess *ts;
221
222 ts = stktable_lookup_key(table, key);
223 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200224 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200225
226 /* entry does not exist, initialize a new one */
227 ts = stksess_new(table, key);
228 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200229 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200230 return ts;
231}
232
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200233/*
234 * Looks in table <t> for a sticky session with same key as <ts>.
235 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100236 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200237struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100238{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100239 struct ebmb_node *eb;
240
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200241 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200242 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200244 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100245
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200246 if (unlikely(!eb))
247 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100248
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200249 return ebmb_entry(eb, struct stksess, key);
250}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100251
Willy Tarreaucb183642010-06-06 17:58:34 +0200252/* Update the expiration timer for <ts> but do not touch its expiration node.
253 * The table's expiration timer is updated if set.
254 */
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200255struct stksess *stktable_touch_with_exp(struct stktable *t, struct stksess *ts,
256 int local, int expire)
Willy Tarreaucb183642010-06-06 17:58:34 +0200257{
Emeric Brun85e77c72010-09-23 18:16:52 +0200258 struct eb32_node * eb;
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200259 ts->expire = expire;
Willy Tarreaucb183642010-06-06 17:58:34 +0200260 if (t->expire) {
261 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
262 task_queue(t->exp_task);
263 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200264
Emeric Brunaaf58602015-06-15 17:23:30 +0200265 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200266 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200267 /* If this entry is not in the tree
268 or not scheduled for at least one peer */
269 if (!ts->upd.node.leaf_p
270 || (int)(t->commitupdate - ts->upd.key) >= 0
271 || (int)(ts->upd.key - t->localupdate) >= 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200272 ts->upd.key = ++t->update;
273 t->localupdate = t->update;
274 eb32_delete(&ts->upd);
275 eb = eb32_insert(&t->updates, &ts->upd);
276 if (eb != &ts->upd) {
277 eb32_delete(eb);
278 eb32_insert(&t->updates, &ts->upd);
279 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200280 }
281 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
282 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200283 return ts;
284}
285
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200286/* Update the expiration timer for <ts> but do not touch its expiration node.
287 * The table's expiration timer is updated if set. The date of expiration coming from
288 * <t> stick-table configuration.
289 */
290struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
291{
292 int expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
293
294 return stktable_touch_with_exp(t, ts, local, expire);
295}
296
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200297/* Insert new sticky session <ts> in the table. It is assumed that it does not
298 * yet exist (the caller must check this). The table's timeout is updated if it
299 * is set. <ts> is returned.
300 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200301struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200302{
303 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200304 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200305 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200306 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200307 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100308}
309
Frédéric Lécaille523cc9e2016-10-12 17:30:30 +0200310/* Same function as stktable_store(), but with <expire> as supplementary argument
311 * to set the date of expiration of <ts> new sticky session thanks to
312 * stktable_touch_with_exp().
313 */
314struct stksess *stktable_store_with_exp(struct stktable *t, struct stksess *ts,
315 int local, int expire)
316{
317 ebmb_insert(&t->keys, &ts->key, t->key_size);
318 stktable_touch_with_exp(t, ts, local, expire);
319 ts->exp.key = ts->expire;
320 eb32_insert(&t->exps, &ts->exp);
321 return ts;
322}
323
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200324/* Returns a valid or initialized stksess for the specified stktable_key in the
325 * specified table, or NULL if the key was NULL, or if no entry was found nor
326 * could be created. The entry's expiration is updated.
327 */
328struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
329{
330 struct stksess *ts;
331
332 if (!key)
333 return NULL;
334
335 ts = stktable_lookup_key(table, key);
336 if (ts == NULL) {
337 /* entry does not exist, initialize a new one */
338 ts = stksess_new(table, key);
339 if (!ts)
340 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200341 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200342 }
343 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200344 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200345 return ts;
346}
347
Emeric Brun3bd697e2010-01-04 15:23:48 +0100348/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200349 * Trash expired sticky sessions from table <t>. The next expiration date is
350 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100351 */
352static int stktable_trash_expired(struct stktable *t)
353{
354 struct stksess *ts;
355 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200356 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100357
358 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
359
360 while (1) {
361 if (unlikely(!eb)) {
362 /* we might have reached the end of the tree, typically because
363 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200364 * half. Let's loop back to the beginning of the tree now if we
365 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100366 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200367 if (looped)
368 break;
369 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100370 eb = eb32_first(&t->exps);
371 if (likely(!eb))
372 break;
373 }
374
375 if (likely(tick_is_lt(now_ms, eb->key))) {
376 /* timer not expired yet, revisit it later */
377 t->exp_next = eb->key;
378 return t->exp_next;
379 }
380
381 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200382 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383 eb = eb32_next(eb);
384
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200385 /* don't delete an entry which is currently referenced */
386 if (ts->ref_cnt)
387 continue;
388
Willy Tarreau86257dc2010-06-06 12:57:10 +0200389 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100390
391 if (!tick_is_expired(ts->expire, now_ms)) {
392 if (!tick_isset(ts->expire))
393 continue;
394
Willy Tarreau86257dc2010-06-06 12:57:10 +0200395 ts->exp.key = ts->expire;
396 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100397
Willy Tarreau86257dc2010-06-06 12:57:10 +0200398 if (!eb || eb->key > ts->exp.key)
399 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100400 continue;
401 }
402
403 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200404 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200405 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100406 stksess_free(t, ts);
407 }
408
409 /* We have found no task to expire in any tree */
410 t->exp_next = TICK_ETERNITY;
411 return t->exp_next;
412}
413
414/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200415 * Task processing function to trash expired sticky sessions. A pointer to the
416 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100417 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200418static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100419{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200420 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100421
422 task->expire = stktable_trash_expired(t);
423 return task;
424}
425
Willy Tarreauaea940e2010-06-06 11:56:36 +0200426/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100427int stktable_init(struct stktable *t)
428{
429 if (t->size) {
430 memset(&t->keys, 0, sizeof(t->keys));
431 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100432 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100433
Willy Tarreau393379c2010-06-06 12:11:37 +0200434 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100435
436 t->exp_next = TICK_ETERNITY;
437 if ( t->expire ) {
438 t->exp_task = task_new();
439 t->exp_task->process = process_table_expire;
440 t->exp_task->expire = TICK_ETERNITY;
441 t->exp_task->context = (void *)t;
442 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200443 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200444 peers_register_table(t->peers.p, t);
445 }
446
Emeric Brun3bd697e2010-01-04 15:23:48 +0100447 return t->pool != NULL;
448 }
449 return 1;
450}
451
452/*
453 * Configuration keywords of known table types
454 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200455struct 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 Brun3bd697e2010-01-04 15:23:48 +0100462
463/*
464 * Parse table type configuration.
465 * Returns 0 on successful parsing, else 1.
466 * <myidx> is set at next configuration <args> index.
467 */
468int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
469{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200470 for (*type = 0; *type < SMP_TYPES; (*type)++) {
471 if (!stktable_types[*type].kw)
472 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100473 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
474 continue;
475
476 *key_size = stktable_types[*type].default_size;
477 (*myidx)++;
478
Willy Tarreauaea940e2010-06-06 11:56:36 +0200479 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100480 if (strcmp("len", args[*myidx]) == 0) {
481 (*myidx)++;
482 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200483 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100484 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200485 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200486 /* null terminated string needs +1 for '\0'. */
487 (*key_size)++;
488 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100489 (*myidx)++;
490 }
491 }
492 return 0;
493 }
494 return 1;
495}
496
Willy Tarreau8fed9032014-07-03 17:02:46 +0200497/* Prepares a stktable_key from a sample <smp> to search into table <t>.
Willy Tarreauf0c730a2016-05-25 17:07:56 +0200498 * 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 Tarreau8fed9032014-07-03 17:02:46 +0200500 * 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 Tarreauf0b38bf2010-06-06 13:22:23 +0200503 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200504struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200505{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200506 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200507 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200508 return NULL;
509
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200510 /* Fill static_table_key. */
511 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200512
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200513 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200514 static_table_key->key = &smp->data.u.ipv4;
515 static_table_key->key_len = 4;
516 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200517
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200518 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200519 static_table_key->key = &smp->data.u.ipv6;
520 static_table_key->key_len = 16;
521 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200522
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200523 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200524 /* 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;
528 static_table_key->key = &smp->data.u.sint;
529 static_table_key->key_len = 4;
530 break;
531
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200532 case SMP_T_STR:
Willy Tarreauce6955e2016-08-09 11:59:12 +0200533 if (!smp_make_safe(smp))
534 return NULL;
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200535 static_table_key->key = smp->data.u.str.str;
536 static_table_key->key_len = smp->data.u.str.len;
537 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200538
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200539 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200540 if (smp->data.u.str.len < t->key_size) {
541 /* This type needs padding with 0. */
Willy Tarreauf65c6c02016-08-09 12:08:41 +0200542 if (!smp_make_rw(smp))
543 return NULL;
544
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200545 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 Brun485479d2010-09-23 18:02:19 +0200553 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200554 static_table_key->key = smp->data.u.str.str;
555 static_table_key->key_len = smp->data.u.str.len;
556 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200557
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200558 default: /* impossible case. */
559 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200560 }
561
Willy Tarreau07115412012-10-29 21:56:59 +0100562 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200563}
564
565/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200566 * 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 Tarreau6bcb0a82014-07-30 08:56:35 +0200571 * 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 Tarreau8fed9032014-07-03 17:02:46 +0200581 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200582struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200583 unsigned int opt, struct sample_expr *expr, struct sample *smp)
584{
585 if (smp)
586 memset(smp, 0, sizeof(*smp));
587
Willy Tarreau192252e2015-04-04 01:47:55 +0200588 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200589 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 Tarreau12785782012-04-27 21:37:17 +0200599 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200600 * type <table_type>, otherwise zero. Used in configuration check.
601 */
Willy Tarreau12785782012-04-27 21:37:17 +0200602int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200603{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100604 int out_type;
605
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200606 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200607 return 0;
608
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100609 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200610
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200611 /* Convert sample. */
612 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100613 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200614
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200615 return 1;
616}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100617
Willy Tarreauedee1d62014-07-15 16:44:27 +0200618/* 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 Tarreau08d5f982010-06-06 13:34:54 +0200622struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200623 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200624 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200625 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200626 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200627 [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 Tarreau08d5f982010-06-06 13:34:54 +0200640};
641
Willy Tarreauedee1d62014-07-15 16:44:27 +0200642/* 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 */
648int 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 Tarreau08d5f982010-06-06 13:34:54 +0200672/*
673 * Returns the data type number for the stktable_data_type whose name is <name>,
674 * or <0 if not found.
675 */
676int stktable_get_data_type(char *name)
677{
678 int type;
679
680 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200681 if (!stktable_data_types[type].name)
682 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200683 if (strcmp(name, stktable_data_types[type].name) == 0)
684 return type;
685 }
686 return -1;
687}
688
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200689/* 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200696static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200697{
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 FOURNIER8c542ca2015-08-19 09:00:18 +0200710 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200711 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200712 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200722static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200723{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200735 ts = stktable_lookup_key(t, key);
736
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200737 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200738 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200739 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200740
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200741 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 FOURNIER136f9d32015-08-19 09:07:19 +0200748 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200749 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200759static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200760{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200772 ts = stktable_lookup_key(t, key);
773
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200774 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200775 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200776 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200777
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200778 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 FOURNIER136f9d32015-08-19 09:07:19 +0200785 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200786 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200795static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200796{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200808 ts = stktable_lookup_key(t, key);
809
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200810 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200811 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200812 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200813
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200814 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 FOURNIER136f9d32015-08-19 09:07:19 +0200821 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200822 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200831static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200832{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200844 ts = stktable_lookup_key(t, key);
845
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200846 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200847 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200848 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200849
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200850 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 FOURNIER136f9d32015-08-19 09:07:19 +0200857 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200858 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200868static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200869{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200881 ts = stktable_lookup_key(t, key);
882
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200883 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200884 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200885 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200886
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200887 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 FOURNIER136f9d32015-08-19 09:07:19 +0200894 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200895 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 FOURNIER236657b2015-08-19 08:25:14 +0200900 * 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 */
905static 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 Tarreauf0c730a2016-05-25 17:07:56 +0200918 ts = stktable_lookup_key(t, key);
919
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200920 smp->flags = SMP_F_VOL_TEST;
921 smp->data.type = SMP_T_SINT;
922 smp->data.u.sint = 0;
923
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200924 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 Tarreaud9f316a2014-07-10 14:03:38 +0200936 * 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200941static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200942{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200954 ts = stktable_lookup_key(t, key);
955
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200956 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200957 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200958 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200959
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200960 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 FOURNIER136f9d32015-08-19 09:07:19 +0200967 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200968 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 FOURNIER0a9a2b82015-05-11 15:20:49 +0200977static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200978{
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 Tarreauf0c730a2016-05-25 17:07:56 +0200990 ts = stktable_lookup_key(t, key);
991
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200992 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200993 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200994 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200995
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200996 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 FOURNIER136f9d32015-08-19 09:07:19 +02001003 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001004 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001014static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001015{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001027 ts = stktable_lookup_key(t, key);
1028
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001029 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001030 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001031 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001032
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001033 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 FOURNIER136f9d32015-08-19 09:07:19 +02001040 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001041 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001050static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001051{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001063 ts = stktable_lookup_key(t, key);
1064
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001065 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001066 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001067 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001068
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001069 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 FOURNIER136f9d32015-08-19 09:07:19 +02001076 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001077 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001087static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001088{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001100 ts = stktable_lookup_key(t, key);
1101
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001102 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001103 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001104 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001105
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001106 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 FOURNIER136f9d32015-08-19 09:07:19 +02001113 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001114 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001123static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001124{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001136 ts = stktable_lookup_key(t, key);
1137
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001138 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001139 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001140 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001141
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001142 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 FOURNIER136f9d32015-08-19 09:07:19 +02001149 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001150 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001160static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001161{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001173 ts = stktable_lookup_key(t, key);
1174
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001175 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001176 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001177 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001178
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001179 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 FOURNIER136f9d32015-08-19 09:07:19 +02001186 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001187 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001196static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001197{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001209 ts = stktable_lookup_key(t, key);
1210
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001211 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001212 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001213 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001214
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001215 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 FOURNIER136f9d32015-08-19 09:07:19 +02001222 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001223 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001232static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001233{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001245 ts = stktable_lookup_key(t, key);
1246
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001247 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001248 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001249 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001250
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001251 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 FOURNIER136f9d32015-08-19 09:07:19 +02001258 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001259 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001268static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001269{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001281 ts = stktable_lookup_key(t, key);
1282
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001283 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001284 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001285 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001286
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001287 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 FOURNIER136f9d32015-08-19 09:07:19 +02001294 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001295 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001304static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001305{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001317 ts = stktable_lookup_key(t, key);
1318
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001319 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001320 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001321 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001322
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001323 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 FOURNIER136f9d32015-08-19 09:07:19 +02001330 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001331 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 FOURNIER0a9a2b82015-05-11 15:20:49 +02001341static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001342{
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 Tarreauf0c730a2016-05-25 17:07:56 +02001353 ts = stktable_lookup_key(t, key);
1354
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001355 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001356 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001357 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001358
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001359 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001360 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001361
1362 return 1;
1363}
1364
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001365/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001366static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001367 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001368{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001369 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 Tarreau79c1e912016-01-25 14:54:45 +01001377
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001378 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001379 if (ts) {
1380 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001381
Willy Tarreau79c1e912016-01-25 14:54:45 +01001382 /* 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 FOURNIERe0627bd2015-08-04 08:20:33 +02001387
Willy Tarreau79c1e912016-01-25 14:54:45 +01001388 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 FOURNIERe0627bd2015-08-04 08:20:33 +02001396 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 */
1408static 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 FOURNIER42148732015-09-02 17:17:33 +02001437 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001438 rule->action_ptr = action_inc_gpc0;
1439 return ACT_RET_PRS_OK;
1440}
1441
1442/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001443static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001444 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001445{
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 Tarreau79c1e912016-01-25 14:54:45 +01001455
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001456 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 Tarreau79c1e912016-01-25 14:54:45 +01001462 if (ptr) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001463 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001464 stktable_touch(stkctr->table, ts, 1);
1465 }
1466
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001467 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 */
1479static 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 FOURNIER42148732015-09-02 17:17:33 +02001518 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001519 rule->action_ptr = action_set_gpt0;
1520
1521 return ACT_RET_PRS_OK;
1522}
1523
Willy Tarreau7d562212016-11-25 16:10:05 +01001524/* 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 */
1527static int
1528smp_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 */
1539static int
1540smp_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 */
1565struct stkctr *
1566smp_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 */
1637struct stkctr *
1638smp_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 */
1672static int
1673smp_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 */
1686static int
1687smp_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 */
1713static int
1714smp_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 */
1740static int
1741smp_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 */
1766static int
1767smp_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 */
1810static int
1811smp_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 */
1842static int
1843smp_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 */
1867static int
1868smp_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 */
1893static int
1894smp_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 */
1935static int
1936smp_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 */
1960static int
1961smp_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 */
1984static int
1985smp_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 */
2010static int
2011smp_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 */
2035static int
2036smp_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 */
2061static int
2062smp_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 */
2086static int
2087smp_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 */
2112static int
2113smp_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 */
2137static int
2138smp_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 */
2163static int
2164smp_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 */
2188static int
2189smp_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 */
2213static int
2214smp_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 Tarreauf13ebdf2016-11-22 18:00:53 +01002228
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
2236enum {
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 */
2246static 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
2256 if (target && strm_li(s)->bind_conf->level < ACCESS_LVL_OPER)
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 */
2271static 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 */
2344static int table_process_entry_per_key(struct appctx *appctx, char **args)
2345{
2346 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002347 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]) {
2358 appctx->ctx.cli.msg = "Key value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002359 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002360 return 1;
2361 }
2362
2363 switch (px->table.type) {
2364 case SMP_T_IPV4:
2365 uint32_key = htonl(inetaddr_host(args[4]));
2366 static_table_key->key = &uint32_key;
2367 break;
2368 case SMP_T_IPV6:
2369 inet_pton(AF_INET6, args[4], ip6_key);
2370 static_table_key->key = &ip6_key;
2371 break;
2372 case SMP_T_SINT:
2373 {
2374 char *endptr;
2375 unsigned long val;
2376 errno = 0;
2377 val = strtoul(args[4], &endptr, 10);
2378 if ((errno == ERANGE && val == ULONG_MAX) ||
2379 (errno != 0 && val == 0) || endptr == args[4] ||
2380 val > 0xffffffff) {
2381 appctx->ctx.cli.msg = "Invalid key\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002382 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002383 return 1;
2384 }
2385 uint32_key = (uint32_t) val;
2386 static_table_key->key = &uint32_key;
2387 break;
2388 }
2389 break;
2390 case SMP_T_STR:
2391 static_table_key->key = args[4];
2392 static_table_key->key_len = strlen(args[4]);
2393 break;
2394 default:
Willy Tarreaua24bc782016-12-14 15:50:35 +01002395 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002396 case STK_CLI_ACT_SHOW:
2397 appctx->ctx.cli.msg = "Showing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
2398 break;
2399 case STK_CLI_ACT_CLR:
2400 appctx->ctx.cli.msg = "Removing keys from tables of type other than ip, ipv6, string and integer is not supported\n";
2401 break;
2402 case STK_CLI_ACT_SET:
2403 appctx->ctx.cli.msg = "Inserting keys into tables of type other than ip, ipv6, string and integer is not supported\n";
2404 break;
2405 default:
2406 appctx->ctx.cli.msg = "Unknown action\n";
2407 break;
2408 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002409 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002410 return 1;
2411 }
2412
2413 /* check permissions */
2414 if (!cli_has_level(appctx, ACCESS_LVL_OPER))
2415 return 1;
2416
2417 ts = stktable_lookup_key(&px->table, static_table_key);
2418
Willy Tarreaua24bc782016-12-14 15:50:35 +01002419 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002420 case STK_CLI_ACT_SHOW:
2421 if (!ts)
2422 return 1;
2423 chunk_reset(&trash);
2424 if (!table_dump_head_to_buffer(&trash, si, px, px))
2425 return 0;
2426 if (!table_dump_entry_to_buffer(&trash, si, px, ts))
2427 return 0;
2428 break;
2429
2430 case STK_CLI_ACT_CLR:
2431 if (!ts)
2432 return 1;
2433 if (ts->ref_cnt) {
2434 /* don't delete an entry which is currently referenced */
2435 appctx->ctx.cli.msg = "Entry currently in use, cannot remove\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002436 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002437 return 1;
2438 }
2439 stksess_kill(&px->table, ts);
2440 break;
2441
2442 case STK_CLI_ACT_SET:
2443 if (ts)
2444 stktable_touch(&px->table, ts, 1);
2445 else {
2446 ts = stksess_new(&px->table, static_table_key);
2447 if (!ts) {
2448 /* don't delete an entry which is currently referenced */
2449 appctx->ctx.cli.msg = "Unable to allocate a new entry\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002450 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002451 return 1;
2452 }
2453 stktable_store(&px->table, ts, 1);
2454 }
2455
2456 for (cur_arg = 5; *args[cur_arg]; cur_arg += 2) {
2457 if (strncmp(args[cur_arg], "data.", 5) != 0) {
2458 appctx->ctx.cli.msg = "\"data.<type>\" followed by a value expected\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002459 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002460 return 1;
2461 }
2462
2463 data_type = stktable_get_data_type(args[cur_arg] + 5);
2464 if (data_type < 0) {
2465 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002466 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002467 return 1;
2468 }
2469
2470 if (!px->table.data_ofs[data_type]) {
2471 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002472 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002473 return 1;
2474 }
2475
2476 if (!*args[cur_arg+1] || strl2llrc(args[cur_arg+1], strlen(args[cur_arg+1]), &value) != 0) {
2477 appctx->ctx.cli.msg = "Require a valid integer value to store\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002478 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002479 return 1;
2480 }
2481
2482 ptr = stktable_data_ptr(&px->table, ts, data_type);
2483
2484 switch (stktable_data_types[data_type].std_type) {
2485 case STD_T_SINT:
2486 stktable_data_cast(ptr, std_t_sint) = value;
2487 break;
2488 case STD_T_UINT:
2489 stktable_data_cast(ptr, std_t_uint) = value;
2490 break;
2491 case STD_T_ULL:
2492 stktable_data_cast(ptr, std_t_ull) = value;
2493 break;
2494 case STD_T_FRQP:
2495 /* We set both the current and previous values. That way
2496 * the reported frequency is stable during all the period
2497 * then slowly fades out. This allows external tools to
2498 * push measures without having to update them too often.
2499 */
2500 frqp = &stktable_data_cast(ptr, std_t_frqp);
2501 frqp->curr_tick = now_ms;
2502 frqp->prev_ctr = 0;
2503 frqp->curr_ctr = value;
2504 break;
2505 }
2506 }
2507 break;
2508
2509 default:
2510 appctx->ctx.cli.msg = "Unknown action\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002511 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002512 break;
2513 }
2514 return 1;
2515}
2516
2517/* Prepares the appctx fields with the data-based filters from the command line.
2518 * Returns 0 if the dump can proceed, 1 if has ended processing.
2519 */
2520static int table_prepare_data_request(struct appctx *appctx, char **args)
2521{
Willy Tarreaua24bc782016-12-14 15:50:35 +01002522 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW && appctx->ctx.table.action != STK_CLI_ACT_CLR) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002523 appctx->ctx.cli.msg = "content-based lookup is only supported with the \"show\" and \"clear\" actions";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002524 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002525 return 1;
2526 }
2527
2528 /* condition on stored data value */
2529 appctx->ctx.table.data_type = stktable_get_data_type(args[3] + 5);
2530 if (appctx->ctx.table.data_type < 0) {
2531 appctx->ctx.cli.msg = "Unknown data type\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002532 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002533 return 1;
2534 }
2535
2536 if (!((struct proxy *)appctx->ctx.table.target)->table.data_ofs[appctx->ctx.table.data_type]) {
2537 appctx->ctx.cli.msg = "Data type not stored in this table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002538 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002539 return 1;
2540 }
2541
2542 appctx->ctx.table.data_op = get_std_op(args[4]);
2543 if (appctx->ctx.table.data_op < 0) {
2544 appctx->ctx.cli.msg = "Require and operator among \"eq\", \"ne\", \"le\", \"ge\", \"lt\", \"gt\"\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002545 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002546 return 1;
2547 }
2548
2549 if (!*args[5] || strl2llrc(args[5], strlen(args[5]), &appctx->ctx.table.value) != 0) {
2550 appctx->ctx.cli.msg = "Require a valid integer value to compare against\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002551 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002552 return 1;
2553 }
2554
2555 /* OK we're done, all the fields are set */
2556 return 0;
2557}
2558
2559/* returns 0 if wants to be called, 1 if has ended processing */
2560static int cli_parse_table_req(char **args, struct appctx *appctx, void *private)
2561{
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002562 appctx->ctx.table.data_type = -1;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002563 appctx->ctx.table.target = NULL;
2564 appctx->ctx.table.proxy = NULL;
2565 appctx->ctx.table.entry = NULL;
Willy Tarreaua24bc782016-12-14 15:50:35 +01002566 appctx->ctx.table.action = (long)private; // keyword argument, one of STK_CLI_ACT_*
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002567
2568 if (*args[2]) {
2569 appctx->ctx.table.target = proxy_tbl_by_name(args[2]);
2570 if (!appctx->ctx.table.target) {
2571 appctx->ctx.cli.msg = "No such table\n";
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002572 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002573 return 1;
2574 }
2575 }
2576 else {
Willy Tarreaua24bc782016-12-14 15:50:35 +01002577 if (appctx->ctx.table.action != STK_CLI_ACT_SHOW)
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002578 goto err_args;
2579 return 0;
2580 }
2581
2582 if (strcmp(args[3], "key") == 0)
2583 return table_process_entry_per_key(appctx, args);
2584 else if (strncmp(args[3], "data.", 5) == 0)
2585 return table_prepare_data_request(appctx, args);
2586 else if (*args[3])
2587 goto err_args;
2588
2589 return 0;
2590
2591err_args:
Willy Tarreaua24bc782016-12-14 15:50:35 +01002592 switch (appctx->ctx.table.action) {
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002593 case STK_CLI_ACT_SHOW:
2594 appctx->ctx.cli.msg = "Optional argument only supports \"data.<store_data_type>\" <operator> <value> and key <key>\n";
2595 break;
2596 case STK_CLI_ACT_CLR:
2597 appctx->ctx.cli.msg = "Required arguments: <table> \"data.<store_data_type>\" <operator> <value> or <table> key <key>\n";
2598 break;
2599 case STK_CLI_ACT_SET:
2600 appctx->ctx.cli.msg = "Required arguments: <table> key <key> [data.<store_data_type> <value>]*\n";
2601 break;
2602 default:
2603 appctx->ctx.cli.msg = "Unknown action\n";
2604 break;
2605 }
Willy Tarreau3b6e5472016-11-24 15:53:53 +01002606 appctx->st0 = CLI_ST_PRINT;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002607 return 1;
2608}
2609
2610/* This function is used to deal with table operations (dump or clear depending
2611 * on the action stored in appctx->private). It returns 0 if the output buffer is
2612 * full and it needs to be called again, otherwise non-zero.
2613 */
2614static int cli_io_handler_table(struct appctx *appctx)
2615{
2616 struct stream_interface *si = appctx->owner;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002617 struct stream *s = si_strm(si);
2618 struct ebmb_node *eb;
2619 int dt;
2620 int skip_entry;
Willy Tarreaua24bc782016-12-14 15:50:35 +01002621 int show = appctx->ctx.table.action == STK_CLI_ACT_SHOW;
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002622
2623 /*
2624 * We have 3 possible states in appctx->st2 :
2625 * - STAT_ST_INIT : the first call
2626 * - STAT_ST_INFO : the proxy pointer points to the next table to
2627 * dump, the entry pointer is NULL ;
2628 * - STAT_ST_LIST : the proxy pointer points to the current table
2629 * and the entry pointer points to the next entry to be dumped,
2630 * and the refcount on the next entry is held ;
2631 * - STAT_ST_END : nothing left to dump, the buffer may contain some
2632 * data though.
2633 */
2634
2635 if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) {
2636 /* in case of abort, remove any refcount we might have set on an entry */
2637 if (appctx->st2 == STAT_ST_LIST) {
2638 appctx->ctx.table.entry->ref_cnt--;
2639 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2640 }
2641 return 1;
2642 }
2643
2644 chunk_reset(&trash);
2645
2646 while (appctx->st2 != STAT_ST_FIN) {
2647 switch (appctx->st2) {
2648 case STAT_ST_INIT:
2649 appctx->ctx.table.proxy = appctx->ctx.table.target;
2650 if (!appctx->ctx.table.proxy)
2651 appctx->ctx.table.proxy = proxy;
2652
2653 appctx->ctx.table.entry = NULL;
2654 appctx->st2 = STAT_ST_INFO;
2655 break;
2656
2657 case STAT_ST_INFO:
2658 if (!appctx->ctx.table.proxy ||
2659 (appctx->ctx.table.target &&
2660 appctx->ctx.table.proxy != appctx->ctx.table.target)) {
2661 appctx->st2 = STAT_ST_END;
2662 break;
2663 }
2664
2665 if (appctx->ctx.table.proxy->table.size) {
2666 if (show && !table_dump_head_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.target))
2667 return 0;
2668
2669 if (appctx->ctx.table.target &&
2670 strm_li(s)->bind_conf->level >= ACCESS_LVL_OPER) {
2671 /* dump entries only if table explicitly requested */
2672 eb = ebmb_first(&appctx->ctx.table.proxy->table.keys);
2673 if (eb) {
2674 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2675 appctx->ctx.table.entry->ref_cnt++;
2676 appctx->st2 = STAT_ST_LIST;
2677 break;
2678 }
2679 }
2680 }
2681 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2682 break;
2683
2684 case STAT_ST_LIST:
2685 skip_entry = 0;
2686
2687 if (appctx->ctx.table.data_type >= 0) {
2688 /* we're filtering on some data contents */
2689 void *ptr;
2690 long long data;
2691
2692 dt = appctx->ctx.table.data_type;
2693 ptr = stktable_data_ptr(&appctx->ctx.table.proxy->table,
2694 appctx->ctx.table.entry,
2695 dt);
2696
2697 data = 0;
2698 switch (stktable_data_types[dt].std_type) {
2699 case STD_T_SINT:
2700 data = stktable_data_cast(ptr, std_t_sint);
2701 break;
2702 case STD_T_UINT:
2703 data = stktable_data_cast(ptr, std_t_uint);
2704 break;
2705 case STD_T_ULL:
2706 data = stktable_data_cast(ptr, std_t_ull);
2707 break;
2708 case STD_T_FRQP:
2709 data = read_freq_ctr_period(&stktable_data_cast(ptr, std_t_frqp),
2710 appctx->ctx.table.proxy->table.data_arg[dt].u);
2711 break;
2712 }
2713
2714 /* skip the entry if the data does not match the test and the value */
2715 if ((data < appctx->ctx.table.value &&
2716 (appctx->ctx.table.data_op == STD_OP_EQ ||
2717 appctx->ctx.table.data_op == STD_OP_GT ||
2718 appctx->ctx.table.data_op == STD_OP_GE)) ||
2719 (data == appctx->ctx.table.value &&
2720 (appctx->ctx.table.data_op == STD_OP_NE ||
2721 appctx->ctx.table.data_op == STD_OP_GT ||
2722 appctx->ctx.table.data_op == STD_OP_LT)) ||
2723 (data > appctx->ctx.table.value &&
2724 (appctx->ctx.table.data_op == STD_OP_EQ ||
2725 appctx->ctx.table.data_op == STD_OP_LT ||
2726 appctx->ctx.table.data_op == STD_OP_LE)))
2727 skip_entry = 1;
2728 }
2729
2730 if (show && !skip_entry &&
2731 !table_dump_entry_to_buffer(&trash, si, appctx->ctx.table.proxy, appctx->ctx.table.entry))
2732 return 0;
2733
2734 appctx->ctx.table.entry->ref_cnt--;
2735
2736 eb = ebmb_next(&appctx->ctx.table.entry->key);
2737 if (eb) {
2738 struct stksess *old = appctx->ctx.table.entry;
2739 appctx->ctx.table.entry = ebmb_entry(eb, struct stksess, key);
2740 if (show)
2741 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, old);
2742 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2743 stksess_kill(&appctx->ctx.table.proxy->table, old);
2744 appctx->ctx.table.entry->ref_cnt++;
2745 break;
2746 }
2747
2748
2749 if (show)
2750 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2751 else if (!skip_entry && !appctx->ctx.table.entry->ref_cnt)
2752 stksess_kill(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2753
2754 appctx->ctx.table.proxy = appctx->ctx.table.proxy->next;
2755 appctx->st2 = STAT_ST_INFO;
2756 break;
2757
2758 case STAT_ST_END:
2759 appctx->st2 = STAT_ST_FIN;
2760 break;
2761 }
2762 }
2763 return 1;
2764}
2765
2766static void cli_release_show_table(struct appctx *appctx)
2767{
2768 if (appctx->st2 == STAT_ST_LIST) {
2769 appctx->ctx.table.entry->ref_cnt--;
2770 stksess_kill_if_expired(&appctx->ctx.table.proxy->table, appctx->ctx.table.entry);
2771 }
2772}
2773
2774/* register cli keywords */
2775static struct cli_kw_list cli_kws = {{ },{
2776 { { "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 },
2777 { { "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 },
2778 { { "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 },
2779 {{},}
2780}};
2781
2782
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002783static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002784 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002785 { "sc-set-gpt0", parse_set_gpt0, 1 },
2786 { /* END */ }
2787}};
2788
Willy Tarreau620408f2016-10-21 16:37:51 +02002789static struct action_kw_list tcp_sess_kws = { { }, {
2790 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
2791 { "sc-set-gpt0", parse_set_gpt0, 1 },
2792 { /* END */ }
2793}};
2794
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002795static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002796 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002797 { "sc-set-gpt0", parse_set_gpt0, 1 },
2798 { /* END */ }
2799}};
2800
2801static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002802 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002803 { "sc-set-gpt0", parse_set_gpt0, 1 },
2804 { /* END */ }
2805}};
2806
2807static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002808 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002809 { "sc-set-gpt0", parse_set_gpt0, 1 },
2810 { /* END */ }
2811}};
2812
2813static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02002814 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002815 { "sc-set-gpt0", parse_set_gpt0, 1 },
2816 { /* END */ }
2817}};
2818
Willy Tarreau7d562212016-11-25 16:10:05 +01002819///* Note: must not be declared <const> as its list will be overwritten.
2820// * Please take care of keeping this list alphabetically sorted.
2821// */
2822//static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
2823// { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2824// { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2825// { /* END */ },
2826//}};
2827/* Note: must not be declared <const> as its list will be overwritten.
2828 * Please take care of keeping this list alphabetically sorted.
2829 */
2830static struct sample_fetch_kw_list smp_fetch_keywords = {ILH, {
2831 { "sc_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2832 { "sc_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2833 { "sc_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2834 { "sc_conn_cnt", smp_fetch_sc_conn_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2835 { "sc_conn_cur", smp_fetch_sc_conn_cur, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2836 { "sc_conn_rate", smp_fetch_sc_conn_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2837 { "sc_get_gpt0", smp_fetch_sc_get_gpt0, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2838 { "sc_get_gpc0", smp_fetch_sc_get_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2839 { "sc_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2840 { "sc_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2841 { "sc_http_err_rate", smp_fetch_sc_http_err_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2842 { "sc_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2843 { "sc_http_req_rate", smp_fetch_sc_http_req_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2844 { "sc_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2845 { "sc_kbytes_in", smp_fetch_sc_kbytes_in, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2846 { "sc_kbytes_out", smp_fetch_sc_kbytes_out, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2847 { "sc_sess_cnt", smp_fetch_sc_sess_cnt, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2848 { "sc_sess_rate", smp_fetch_sc_sess_rate, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2849 { "sc_tracked", smp_fetch_sc_tracked, ARG2(1,SINT,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2850 { "sc_trackers", smp_fetch_sc_trackers, ARG2(1,SINT,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2851 { "sc0_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2852 { "sc0_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2853 { "sc0_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2854 { "sc0_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2855 { "sc0_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2856 { "sc0_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2857 { "sc0_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2858 { "sc0_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2859 { "sc0_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2860 { "sc0_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2861 { "sc0_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2862 { "sc0_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2863 { "sc0_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2864 { "sc0_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2865 { "sc0_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2866 { "sc0_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2867 { "sc0_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2868 { "sc0_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2869 { "sc0_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2870 { "sc0_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2871 { "sc1_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2872 { "sc1_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2873 { "sc1_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2874 { "sc1_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2875 { "sc1_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2876 { "sc1_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2877 { "sc1_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2878 { "sc1_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2879 { "sc1_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2880 { "sc1_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2881 { "sc1_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2882 { "sc1_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2883 { "sc1_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2884 { "sc1_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2885 { "sc1_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2886 { "sc1_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2887 { "sc1_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2888 { "sc1_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2889 { "sc1_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2890 { "sc1_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2891 { "sc2_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2892 { "sc2_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2893 { "sc2_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2894 { "sc2_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2895 { "sc2_conn_cur", smp_fetch_sc_conn_cur, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2896 { "sc2_conn_rate", smp_fetch_sc_conn_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2897 { "sc2_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2898 { "sc2_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2899 { "sc2_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2900 { "sc2_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2901 { "sc2_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2902 { "sc2_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2903 { "sc2_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2904 { "sc2_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2905 { "sc2_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2906 { "sc2_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2907 { "sc2_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2908 { "sc2_sess_rate", smp_fetch_sc_sess_rate, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2909 { "sc2_tracked", smp_fetch_sc_tracked, ARG1(0,TAB), NULL, SMP_T_BOOL, SMP_USE_INTRN, },
2910 { "sc2_trackers", smp_fetch_sc_trackers, ARG1(0,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2911 { "src_bytes_in_rate", smp_fetch_sc_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2912 { "src_bytes_out_rate", smp_fetch_sc_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2913 { "src_clr_gpc0", smp_fetch_sc_clr_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2914 { "src_conn_cnt", smp_fetch_sc_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2915 { "src_conn_cur", smp_fetch_sc_conn_cur, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2916 { "src_conn_rate", smp_fetch_sc_conn_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2917 { "src_get_gpt0", smp_fetch_sc_get_gpt0, ARG1(1,TAB), NULL, SMP_T_BOOL, SMP_USE_L4CLI, },
2918 { "src_get_gpc0", smp_fetch_sc_get_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2919 { "src_gpc0_rate", smp_fetch_sc_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2920 { "src_http_err_cnt", smp_fetch_sc_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2921 { "src_http_err_rate", smp_fetch_sc_http_err_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2922 { "src_http_req_cnt", smp_fetch_sc_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2923 { "src_http_req_rate", smp_fetch_sc_http_req_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2924 { "src_inc_gpc0", smp_fetch_sc_inc_gpc0, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2925 { "src_kbytes_in", smp_fetch_sc_kbytes_in, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2926 { "src_kbytes_out", smp_fetch_sc_kbytes_out, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2927 { "src_sess_cnt", smp_fetch_sc_sess_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2928 { "src_sess_rate", smp_fetch_sc_sess_rate, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2929 { "src_updt_conn_cnt", smp_fetch_src_updt_conn_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_L4CLI, },
2930 { "table_avl", smp_fetch_table_avl, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2931 { "table_cnt", smp_fetch_table_cnt, ARG1(1,TAB), NULL, SMP_T_SINT, SMP_USE_INTRN, },
2932 { /* END */ },
2933}};
2934
2935
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002936/* Note: must not be declared <const> as its list will be overwritten */
2937static struct sample_conv_kw_list sample_conv_kws = {ILH, {
Willy Tarreau2d17db52016-05-25 17:16:38 +02002938 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_BOOL },
2939 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2940 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2941 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2942 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2943 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2944 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2945 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2946 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2947 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2948 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2949 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2950 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2951 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2952 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2953 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2954 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2955 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
2956 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_ANY, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002957 { /* END */ },
2958}};
2959
2960__attribute__((constructor))
2961static void __stick_table_init(void)
2962{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002963 /* register som action keywords. */
2964 tcp_req_conn_keywords_register(&tcp_conn_kws);
Willy Tarreau620408f2016-10-21 16:37:51 +02002965 tcp_req_sess_keywords_register(&tcp_sess_kws);
Thierry FOURNIER236657b2015-08-19 08:25:14 +02002966 tcp_req_cont_keywords_register(&tcp_req_kws);
2967 tcp_res_cont_keywords_register(&tcp_res_kws);
2968 http_req_keywords_register(&http_req_kws);
2969 http_res_keywords_register(&http_res_kws);
2970
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002971 /* register sample fetch and format conversion keywords */
Willy Tarreau7d562212016-11-25 16:10:05 +01002972 sample_register_fetches(&smp_fetch_keywords);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002973 sample_register_convs(&sample_conv_kws);
Willy Tarreauf13ebdf2016-11-22 18:00:53 +01002974 cli_register_kw(&cli_kws);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02002975}