blob: 0d154907b783878db112c0e84db2d8da5d1b7875 [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>
15
16#include <common/config.h>
17#include <common/memory.h>
18#include <common/mini-clist.h>
19#include <common/standard.h>
20#include <common/time.h>
21
22#include <ebmbtree.h>
23#include <ebsttree.h>
24
Willy Tarreaud9f316a2014-07-10 14:03:38 +020025#include <proto/arg.h>
Thierry FOURNIER236657b2015-08-19 08:25:14 +020026#include <proto/proto_http.h>
27#include <proto/proto_tcp.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010028#include <proto/proxy.h>
Willy Tarreaucd3b0942012-04-27 21:52:18 +020029#include <proto/sample.h>
Willy Tarreau87b09662015-04-03 00:22:06 +020030#include <proto/stream.h>
Willy Tarreau68129b92010-06-06 16:06:52 +020031#include <proto/stick_table.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010032#include <proto/task.h>
Emeric Brun32da3c42010-09-23 18:39:19 +020033#include <proto/peers.h>
34#include <types/global.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010035
Willy Tarreau12785782012-04-27 21:37:17 +020036/* structure used to return a table key built from a sample */
Willy Tarreau07115412012-10-29 21:56:59 +010037struct stktable_key *static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +020038
Emeric Brun3bd697e2010-01-04 15:23:48 +010039/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020040 * Free an allocated sticky session <ts>, and decrease sticky sessions counter
41 * in table <t>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010042 */
43void stksess_free(struct stktable *t, struct stksess *ts)
44{
45 t->current--;
Willy Tarreau393379c2010-06-06 12:11:37 +020046 pool_free2(t->pool, (void *)ts - t->data_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010047}
48
49/*
Willy Tarreauf6efda12010-08-03 20:34:06 +020050 * Kill an stksess (only if its ref_cnt is zero).
51 */
52void stksess_kill(struct stktable *t, struct stksess *ts)
53{
54 if (ts->ref_cnt)
55 return;
56
57 eb32_delete(&ts->exp);
Emeric Brun85e77c72010-09-23 18:16:52 +020058 eb32_delete(&ts->upd);
Willy Tarreauf6efda12010-08-03 20:34:06 +020059 ebmb_delete(&ts->key);
60 stksess_free(t, ts);
61}
62
63/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020064 * Initialize or update the key in the sticky session <ts> present in table <t>
65 * from the value present in <key>.
Emeric Brun3bd697e2010-01-04 15:23:48 +010066 */
Willy Tarreau393379c2010-06-06 12:11:37 +020067void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +010068{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +020069 if (t->type != SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +020070 memcpy(ts->key.key, key->key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +010071 else {
Willy Tarreau86257dc2010-06-06 12:57:10 +020072 memcpy(ts->key.key, key->key, MIN(t->key_size - 1, key->key_len));
73 ts->key.key[MIN(t->key_size - 1, key->key_len)] = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +010074 }
75}
76
77
78/*
Willy Tarreau393379c2010-06-06 12:11:37 +020079 * Init sticky session <ts> of table <t>. The data parts are cleared and <ts>
80 * is returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +010081 */
Willy Tarreau393379c2010-06-06 12:11:37 +020082static struct stksess *stksess_init(struct stktable *t, struct stksess * ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +010083{
Willy Tarreau393379c2010-06-06 12:11:37 +020084 memset((void *)ts - t->data_size, 0, t->data_size);
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +020085 ts->ref_cnt = 0;
Willy Tarreau86257dc2010-06-06 12:57:10 +020086 ts->key.node.leaf_p = NULL;
87 ts->exp.node.leaf_p = NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +020088 ts->upd.node.leaf_p = NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +010089 return ts;
90}
91
92/*
Willy Tarreauaea940e2010-06-06 11:56:36 +020093 * Trash oldest <to_batch> sticky sessions from table <t>
94 * Returns number of trashed sticky sessions.
Emeric Brun3bd697e2010-01-04 15:23:48 +010095 */
Willy Tarreau3a925c12013-09-04 17:54:01 +020096int stktable_trash_oldest(struct stktable *t, int to_batch)
Emeric Brun3bd697e2010-01-04 15:23:48 +010097{
98 struct stksess *ts;
99 struct eb32_node *eb;
100 int batched = 0;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200101 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100102
103 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
104
105 while (batched < to_batch) {
106
107 if (unlikely(!eb)) {
108 /* we might have reached the end of the tree, typically because
109 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200110 * half. Let's loop back to the beginning of the tree now if we
111 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100112 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200113 if (looped)
114 break;
115 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100116 eb = eb32_first(&t->exps);
117 if (likely(!eb))
118 break;
119 }
120
121 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200122 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100123 eb = eb32_next(eb);
124
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200125 /* don't delete an entry which is currently referenced */
126 if (ts->ref_cnt)
127 continue;
128
Willy Tarreau86257dc2010-06-06 12:57:10 +0200129 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100130
Willy Tarreau86257dc2010-06-06 12:57:10 +0200131 if (ts->expire != ts->exp.key) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100132 if (!tick_isset(ts->expire))
133 continue;
134
Willy Tarreau86257dc2010-06-06 12:57:10 +0200135 ts->exp.key = ts->expire;
136 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100137
Willy Tarreau86257dc2010-06-06 12:57:10 +0200138 if (!eb || eb->key > ts->exp.key)
139 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100140
141 continue;
142 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100143
Willy Tarreauaea940e2010-06-06 11:56:36 +0200144 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200145 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200146 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100147 stksess_free(t, ts);
148 batched++;
149 }
150
151 return batched;
152}
153
154/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200155 * Allocate and initialise a new sticky session.
156 * The new sticky session is returned or NULL in case of lack of memory.
157 * Sticky sessions should only be allocated this way, and must be freed using
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200158 * stksess_free(). Table <t>'s sticky session counter is increased. If <key>
159 * is not NULL, it is assigned to the new session.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100160 */
161struct stksess *stksess_new(struct stktable *t, struct stktable_key *key)
162{
163 struct stksess *ts;
164
165 if (unlikely(t->current == t->size)) {
166 if ( t->nopurge )
167 return NULL;
168
Emeric Brunfbce6d02010-09-23 18:10:00 +0200169 if (!stktable_trash_oldest(t, (t->size >> 8) + 1))
Emeric Brun3bd697e2010-01-04 15:23:48 +0100170 return NULL;
171 }
172
Willy Tarreau393379c2010-06-06 12:11:37 +0200173 ts = pool_alloc2(t->pool) + t->data_size;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100174 if (ts) {
175 t->current++;
Willy Tarreau393379c2010-06-06 12:11:37 +0200176 stksess_init(t, ts);
Willy Tarreaua7b46b52013-04-11 16:55:37 +0200177 if (key)
178 stksess_setkey(t, ts, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100179 }
180
181 return ts;
182}
183
184/*
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200185 * Looks in table <t> for a sticky session matching key <key>.
Willy Tarreauaea940e2010-06-06 11:56:36 +0200186 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100187 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200188struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100189{
190 struct ebmb_node *eb;
191
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200192 if (t->type == SMP_T_STR)
Emeric Brun485479d2010-09-23 18:02:19 +0200193 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 +0100194 else
195 eb = ebmb_lookup(&t->keys, key->key, t->key_size);
196
197 if (unlikely(!eb)) {
198 /* no session found */
199 return NULL;
200 }
201
Willy Tarreau86257dc2010-06-06 12:57:10 +0200202 return ebmb_entry(eb, struct stksess, key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100203}
204
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200205/* Lookup and touch <key> in <table>, or create the entry if it does not exist.
206 * This is mainly used for situations where we want to refresh a key's usage so
207 * that it does not expire, and we want to have it created if it was not there.
208 * The stksess is returned, or NULL if it could not be created.
209 */
210struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key)
211{
212 struct stksess *ts;
213
214 ts = stktable_lookup_key(table, key);
215 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200216 return stktable_touch(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200217
218 /* entry does not exist, initialize a new one */
219 ts = stksess_new(table, key);
220 if (likely(ts))
Emeric Brun85e77c72010-09-23 18:16:52 +0200221 stktable_store(table, ts, 1);
Willy Tarreau1f7e9252010-06-20 12:27:21 +0200222 return ts;
223}
224
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200225/*
226 * Looks in table <t> for a sticky session with same key as <ts>.
227 * Returns pointer on requested sticky session or NULL if none was found.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100228 */
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200229struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100230{
Emeric Brun3bd697e2010-01-04 15:23:48 +0100231 struct ebmb_node *eb;
232
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200233 if (t->type == SMP_T_STR)
Willy Tarreau86257dc2010-06-06 12:57:10 +0200234 eb = ebst_lookup(&(t->keys), (char *)ts->key.key);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100235 else
Willy Tarreau86257dc2010-06-06 12:57:10 +0200236 eb = ebmb_lookup(&(t->keys), ts->key.key, t->key_size);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100237
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200238 if (unlikely(!eb))
239 return NULL;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100240
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200241 return ebmb_entry(eb, struct stksess, key);
242}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100243
Willy Tarreaucb183642010-06-06 17:58:34 +0200244/* Update the expiration timer for <ts> but do not touch its expiration node.
245 * The table's expiration timer is updated if set.
246 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200247struct stksess *stktable_touch(struct stktable *t, struct stksess *ts, int local)
Willy Tarreaucb183642010-06-06 17:58:34 +0200248{
Emeric Brun85e77c72010-09-23 18:16:52 +0200249 struct eb32_node * eb;
Willy Tarreaucb183642010-06-06 17:58:34 +0200250 ts->expire = tick_add(now_ms, MS_TO_TICKS(t->expire));
251 if (t->expire) {
252 t->exp_task->expire = t->exp_next = tick_first(ts->expire, t->exp_next);
253 task_queue(t->exp_task);
254 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200255
Emeric Brunaaf58602015-06-15 17:23:30 +0200256 /* If sync is enabled and update is local */
Emeric Brun85e77c72010-09-23 18:16:52 +0200257 if (t->sync_task && local) {
Emeric Brunc703a9d2015-09-22 15:05:06 +0200258 /* If this entry is not in the tree
259 or not scheduled for at least one peer */
260 if (!ts->upd.node.leaf_p
261 || (int)(t->commitupdate - ts->upd.key) >= 0
262 || (int)(ts->upd.key - t->localupdate) >= 0) {
Emeric Brunaaf58602015-06-15 17:23:30 +0200263 ts->upd.key = ++t->update;
264 t->localupdate = t->update;
265 eb32_delete(&ts->upd);
266 eb = eb32_insert(&t->updates, &ts->upd);
267 if (eb != &ts->upd) {
268 eb32_delete(eb);
269 eb32_insert(&t->updates, &ts->upd);
270 }
Emeric Brun85e77c72010-09-23 18:16:52 +0200271 }
272 task_wakeup(t->sync_task, TASK_WOKEN_MSG);
273 }
Willy Tarreaucb183642010-06-06 17:58:34 +0200274 return ts;
275}
276
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200277/* Insert new sticky session <ts> in the table. It is assumed that it does not
278 * yet exist (the caller must check this). The table's timeout is updated if it
279 * is set. <ts> is returned.
280 */
Emeric Brun85e77c72010-09-23 18:16:52 +0200281struct stksess *stktable_store(struct stktable *t, struct stksess *ts, int local)
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200282{
283 ebmb_insert(&t->keys, &ts->key, t->key_size);
Emeric Brun85e77c72010-09-23 18:16:52 +0200284 stktable_touch(t, ts, local);
Willy Tarreaucb183642010-06-06 17:58:34 +0200285 ts->exp.key = ts->expire;
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200286 eb32_insert(&t->exps, &ts->exp);
Willy Tarreauf16d2b82010-06-06 15:38:59 +0200287 return ts;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100288}
289
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200290/* Returns a valid or initialized stksess for the specified stktable_key in the
291 * specified table, or NULL if the key was NULL, or if no entry was found nor
292 * could be created. The entry's expiration is updated.
293 */
294struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key)
295{
296 struct stksess *ts;
297
298 if (!key)
299 return NULL;
300
301 ts = stktable_lookup_key(table, key);
302 if (ts == NULL) {
303 /* entry does not exist, initialize a new one */
304 ts = stksess_new(table, key);
305 if (!ts)
306 return NULL;
Emeric Brun85e77c72010-09-23 18:16:52 +0200307 stktable_store(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200308 }
309 else
Emeric Brun85e77c72010-09-23 18:16:52 +0200310 stktable_touch(table, ts, 1);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +0200311 return ts;
312}
313
Emeric Brun3bd697e2010-01-04 15:23:48 +0100314/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200315 * Trash expired sticky sessions from table <t>. The next expiration date is
316 * returned.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100317 */
318static int stktable_trash_expired(struct stktable *t)
319{
320 struct stksess *ts;
321 struct eb32_node *eb;
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200322 int looped = 0;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100323
324 eb = eb32_lookup_ge(&t->exps, now_ms - TIMER_LOOK_BACK);
325
326 while (1) {
327 if (unlikely(!eb)) {
328 /* we might have reached the end of the tree, typically because
329 * <now_ms> is in the first half and we're first scanning the last
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200330 * half. Let's loop back to the beginning of the tree now if we
331 * have not yet visited it.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100332 */
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200333 if (looped)
334 break;
335 looped = 1;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100336 eb = eb32_first(&t->exps);
337 if (likely(!eb))
338 break;
339 }
340
341 if (likely(tick_is_lt(now_ms, eb->key))) {
342 /* timer not expired yet, revisit it later */
343 t->exp_next = eb->key;
344 return t->exp_next;
345 }
346
347 /* timer looks expired, detach it from the queue */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200348 ts = eb32_entry(eb, struct stksess, exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100349 eb = eb32_next(eb);
350
Willy Tarreaue7f3d7a2010-06-14 14:53:07 +0200351 /* don't delete an entry which is currently referenced */
352 if (ts->ref_cnt)
353 continue;
354
Willy Tarreau86257dc2010-06-06 12:57:10 +0200355 eb32_delete(&ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100356
357 if (!tick_is_expired(ts->expire, now_ms)) {
358 if (!tick_isset(ts->expire))
359 continue;
360
Willy Tarreau86257dc2010-06-06 12:57:10 +0200361 ts->exp.key = ts->expire;
362 eb32_insert(&t->exps, &ts->exp);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100363
Willy Tarreau86257dc2010-06-06 12:57:10 +0200364 if (!eb || eb->key > ts->exp.key)
365 eb = &ts->exp;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100366 continue;
367 }
368
369 /* session expired, trash it */
Willy Tarreau86257dc2010-06-06 12:57:10 +0200370 ebmb_delete(&ts->key);
Emeric Brun85e77c72010-09-23 18:16:52 +0200371 eb32_delete(&ts->upd);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100372 stksess_free(t, ts);
373 }
374
375 /* We have found no task to expire in any tree */
376 t->exp_next = TICK_ETERNITY;
377 return t->exp_next;
378}
379
380/*
Willy Tarreauaea940e2010-06-06 11:56:36 +0200381 * Task processing function to trash expired sticky sessions. A pointer to the
382 * task itself is returned since it never dies.
Emeric Brun3bd697e2010-01-04 15:23:48 +0100383 */
Willy Tarreauaea940e2010-06-06 11:56:36 +0200384static struct task *process_table_expire(struct task *task)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100385{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +0200386 struct stktable *t = task->context;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100387
388 task->expire = stktable_trash_expired(t);
389 return task;
390}
391
Willy Tarreauaea940e2010-06-06 11:56:36 +0200392/* Perform minimal stick table intializations, report 0 in case of error, 1 if OK. */
Emeric Brun3bd697e2010-01-04 15:23:48 +0100393int stktable_init(struct stktable *t)
394{
395 if (t->size) {
396 memset(&t->keys, 0, sizeof(t->keys));
397 memset(&t->exps, 0, sizeof(t->exps));
Emeric Brun1c6235d2015-12-16 15:28:12 +0100398 t->updates = EB_ROOT_UNIQUE;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100399
Willy Tarreau393379c2010-06-06 12:11:37 +0200400 t->pool = create_pool("sticktables", sizeof(struct stksess) + t->data_size + t->key_size, MEM_F_SHARED);
Emeric Brun3bd697e2010-01-04 15:23:48 +0100401
402 t->exp_next = TICK_ETERNITY;
403 if ( t->expire ) {
404 t->exp_task = task_new();
405 t->exp_task->process = process_table_expire;
406 t->exp_task->expire = TICK_ETERNITY;
407 t->exp_task->context = (void *)t;
408 }
Willy Tarreauc8b67912015-05-01 18:29:57 +0200409 if (t->peers.p && t->peers.p->peers_fe && t->peers.p->peers_fe->state != PR_STSTOPPED) {
Emeric Brun32da3c42010-09-23 18:39:19 +0200410 peers_register_table(t->peers.p, t);
411 }
412
Emeric Brun3bd697e2010-01-04 15:23:48 +0100413 return t->pool != NULL;
414 }
415 return 1;
416}
417
418/*
419 * Configuration keywords of known table types
420 */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200421struct stktable_type stktable_types[SMP_TYPES] = {
422 [SMP_T_SINT] = { "integer", 0, 4 },
423 [SMP_T_IPV4] = { "ip", 0, 4 },
424 [SMP_T_IPV6] = { "ipv6", 0, 16 },
425 [SMP_T_STR] = { "string", STK_F_CUSTOM_KEYSIZE, 32 },
426 [SMP_T_BIN] = { "binary", STK_F_CUSTOM_KEYSIZE, 32 }
427};
Emeric Brun3bd697e2010-01-04 15:23:48 +0100428
429/*
430 * Parse table type configuration.
431 * Returns 0 on successful parsing, else 1.
432 * <myidx> is set at next configuration <args> index.
433 */
434int stktable_parse_type(char **args, int *myidx, unsigned long *type, size_t *key_size)
435{
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200436 for (*type = 0; *type < SMP_TYPES; (*type)++) {
437 if (!stktable_types[*type].kw)
438 continue;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100439 if (strcmp(args[*myidx], stktable_types[*type].kw) != 0)
440 continue;
441
442 *key_size = stktable_types[*type].default_size;
443 (*myidx)++;
444
Willy Tarreauaea940e2010-06-06 11:56:36 +0200445 if (stktable_types[*type].flags & STK_F_CUSTOM_KEYSIZE) {
Emeric Brun3bd697e2010-01-04 15:23:48 +0100446 if (strcmp("len", args[*myidx]) == 0) {
447 (*myidx)++;
448 *key_size = atol(args[*myidx]);
Emeric Brun485479d2010-09-23 18:02:19 +0200449 if (!*key_size)
Emeric Brun3bd697e2010-01-04 15:23:48 +0100450 break;
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200451 if (*type == SMP_T_STR) {
Emeric Brun485479d2010-09-23 18:02:19 +0200452 /* null terminated string needs +1 for '\0'. */
453 (*key_size)++;
454 }
Emeric Brun3bd697e2010-01-04 15:23:48 +0100455 (*myidx)++;
456 }
457 }
458 return 0;
459 }
460 return 1;
461}
462
Willy Tarreau8fed9032014-07-03 17:02:46 +0200463/* Prepares a stktable_key from a sample <smp> to search into table <t>.
464 * Returns NULL if the sample could not be converted (eg: no matching type),
465 * otherwise a pointer to the static stktable_key filled with what is needed
466 * for the lookup.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200467 */
Willy Tarreau8fed9032014-07-03 17:02:46 +0200468struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200469{
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200470 /* Convert sample. */
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200471 if (!sample_convert(smp, t->type))
Willy Tarreau7fc1c6e2012-04-26 11:03:17 +0200472 return NULL;
473
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200474 /* Fill static_table_key. */
475 switch (t->type) {
Emeric Brun485479d2010-09-23 18:02:19 +0200476
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200477 case SMP_T_IPV4:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200478 static_table_key->key = &smp->data.u.ipv4;
479 static_table_key->key_len = 4;
480 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200481
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200482 case SMP_T_IPV6:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200483 static_table_key->key = &smp->data.u.ipv6;
484 static_table_key->key_len = 16;
485 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200486
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200487 case SMP_T_SINT:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200488 /* The stick table require a 32bit unsigned int, "sint" is a
489 * signed 64 it, so we can convert it inplace.
490 */
491 *(unsigned int *)&smp->data.u.sint = (unsigned int)smp->data.u.sint;
492 static_table_key->key = &smp->data.u.sint;
493 static_table_key->key_len = 4;
494 break;
495
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200496 case SMP_T_STR:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200497 /* Must be NULL terminated. */
498 if (smp->data.u.str.len >= smp->data.u.str.size ||
499 smp->data.u.str.str[smp->data.u.str.len] != '\0') {
500 if (!smp_dup(smp))
501 return NULL;
502 if (smp->data.u.str.len >= smp->data.u.str.size)
503 return NULL;
504 smp->data.u.str.str[smp->data.u.str.len] = '\0';
Emeric Brun485479d2010-09-23 18:02:19 +0200505 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200506 static_table_key->key = smp->data.u.str.str;
507 static_table_key->key_len = smp->data.u.str.len;
508 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200509
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200510 case SMP_T_BIN:
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200511 if (smp->data.u.str.len < t->key_size) {
512 /* This type needs padding with 0. */
513 if (smp->data.u.str.size < t->key_size)
514 if (!smp_dup(smp))
515 return NULL;
516 if (smp->data.u.str.size < t->key_size)
517 return NULL;
518 memset(smp->data.u.str.str + smp->data.u.str.len, 0,
519 t->key_size - smp->data.u.str.len);
520 smp->data.u.str.len = t->key_size;
Emeric Brun485479d2010-09-23 18:02:19 +0200521 }
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200522 static_table_key->key = smp->data.u.str.str;
523 static_table_key->key_len = smp->data.u.str.len;
524 break;
Emeric Brun485479d2010-09-23 18:02:19 +0200525
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200526 default: /* impossible case. */
527 return NULL;
Emeric Brun485479d2010-09-23 18:02:19 +0200528 }
529
Willy Tarreau07115412012-10-29 21:56:59 +0100530 return static_table_key;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200531}
532
533/*
Willy Tarreau8fed9032014-07-03 17:02:46 +0200534 * Process a fetch + format conversion as defined by the sample expression <expr>
535 * on request or response considering the <opt> parameter. Returns either NULL if
536 * no key could be extracted, or a pointer to the converted result stored in
537 * static_table_key in format <table_type>. If <smp> is not NULL, it will be reset
538 * and its flags will be initialized so that the caller gets a copy of the input
Willy Tarreau6bcb0a82014-07-30 08:56:35 +0200539 * sample, and knows why it was not accepted (eg: SMP_F_MAY_CHANGE is present
540 * without SMP_OPT_FINAL). The output will be usable like this :
541 *
542 * return MAY_CHANGE FINAL Meaning for the sample
543 * NULL 0 * Not present and will never be (eg: header)
544 * NULL 1 0 Not present or unstable, could change (eg: req_len)
545 * NULL 1 1 Not present, will not change anymore
546 * smp 0 * Present and will not change (eg: header)
547 * smp 1 0 not possible
548 * smp 1 1 Present, last known value (eg: request length)
Willy Tarreau8fed9032014-07-03 17:02:46 +0200549 */
Willy Tarreau192252e2015-04-04 01:47:55 +0200550struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess, struct stream *strm,
Willy Tarreau8fed9032014-07-03 17:02:46 +0200551 unsigned int opt, struct sample_expr *expr, struct sample *smp)
552{
553 if (smp)
554 memset(smp, 0, sizeof(*smp));
555
Willy Tarreau192252e2015-04-04 01:47:55 +0200556 smp = sample_process(px, sess, strm, opt, expr, smp);
Willy Tarreau8fed9032014-07-03 17:02:46 +0200557 if (!smp)
558 return NULL;
559
560 if ((smp->flags & SMP_F_MAY_CHANGE) && !(opt & SMP_OPT_FINAL))
561 return NULL; /* we can only use stable samples */
562
563 return smp_to_stkey(smp, t);
564}
565
566/*
Willy Tarreau12785782012-04-27 21:37:17 +0200567 * Returns 1 if sample expression <expr> result can be converted to table key of
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200568 * type <table_type>, otherwise zero. Used in configuration check.
569 */
Willy Tarreau12785782012-04-27 21:37:17 +0200570int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200571{
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100572 int out_type;
573
Thierry FOURNIER5d24ebc2015-07-24 08:46:42 +0200574 if (table_type >= SMP_TYPES || !stktable_types[table_type].kw)
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200575 return 0;
576
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100577 out_type = smp_expr_output_type(expr);
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200578
Thierry FOURNIERbc8c4042015-08-10 17:53:45 +0200579 /* Convert sample. */
580 if (!sample_casts[out_type][table_type])
Thierry FOURNIERf73eb8f2013-11-27 15:30:55 +0100581 return 0;
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200582
Willy Tarreauf0b38bf2010-06-06 13:22:23 +0200583 return 1;
584}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100585
Willy Tarreauedee1d62014-07-15 16:44:27 +0200586/* Extra data types processing : after the last one, some room may remain
587 * before STKTABLE_DATA_TYPES that may be used to register extra data types
588 * at run time.
589 */
Willy Tarreau08d5f982010-06-06 13:34:54 +0200590struct stktable_data_type stktable_data_types[STKTABLE_DATA_TYPES] = {
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200591 [STKTABLE_DT_SERVER_ID] = { .name = "server_id", .std_type = STD_T_SINT },
Thierry FOURNIER3cf11112015-07-28 08:57:05 +0200592 [STKTABLE_DT_GPT0] = { .name = "gpt0", .std_type = STD_T_UINT },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200593 [STKTABLE_DT_GPC0] = { .name = "gpc0", .std_type = STD_T_UINT },
Willy Tarreauba2ffd12013-05-29 15:54:14 +0200594 [STKTABLE_DT_GPC0_RATE] = { .name = "gpc0_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200595 [STKTABLE_DT_CONN_CNT] = { .name = "conn_cnt", .std_type = STD_T_UINT },
596 [STKTABLE_DT_CONN_RATE] = { .name = "conn_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
597 [STKTABLE_DT_CONN_CUR] = { .name = "conn_cur", .std_type = STD_T_UINT },
598 [STKTABLE_DT_SESS_CNT] = { .name = "sess_cnt", .std_type = STD_T_UINT },
599 [STKTABLE_DT_SESS_RATE] = { .name = "sess_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
600 [STKTABLE_DT_HTTP_REQ_CNT] = { .name = "http_req_cnt", .std_type = STD_T_UINT },
601 [STKTABLE_DT_HTTP_REQ_RATE] = { .name = "http_req_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
602 [STKTABLE_DT_HTTP_ERR_CNT] = { .name = "http_err_cnt", .std_type = STD_T_UINT },
603 [STKTABLE_DT_HTTP_ERR_RATE] = { .name = "http_err_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
604 [STKTABLE_DT_BYTES_IN_CNT] = { .name = "bytes_in_cnt", .std_type = STD_T_ULL },
605 [STKTABLE_DT_BYTES_IN_RATE] = { .name = "bytes_in_rate", .std_type = STD_T_FRQP, .arg_type = ARG_T_DELAY },
606 [STKTABLE_DT_BYTES_OUT_CNT] = { .name = "bytes_out_cnt", .std_type = STD_T_ULL },
607 [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 +0200608};
609
Willy Tarreauedee1d62014-07-15 16:44:27 +0200610/* Registers stick-table extra data type with index <idx>, name <name>, type
611 * <std_type> and arg type <arg_type>. If the index is negative, the next free
612 * index is automatically allocated. The allocated index is returned, or -1 if
613 * no free index was found or <name> was already registered. The <name> is used
614 * directly as a pointer, so if it's not stable, the caller must allocate it.
615 */
616int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type)
617{
618 if (idx < 0) {
619 for (idx = 0; idx < STKTABLE_DATA_TYPES; idx++) {
620 if (!stktable_data_types[idx].name)
621 break;
622
623 if (strcmp(stktable_data_types[idx].name, name) == 0)
624 return -1;
625 }
626 }
627
628 if (idx >= STKTABLE_DATA_TYPES)
629 return -1;
630
631 if (stktable_data_types[idx].name != NULL)
632 return -1;
633
634 stktable_data_types[idx].name = name;
635 stktable_data_types[idx].std_type = std_type;
636 stktable_data_types[idx].arg_type = arg_type;
637 return idx;
638}
639
Willy Tarreau08d5f982010-06-06 13:34:54 +0200640/*
641 * Returns the data type number for the stktable_data_type whose name is <name>,
642 * or <0 if not found.
643 */
644int stktable_get_data_type(char *name)
645{
646 int type;
647
648 for (type = 0; type < STKTABLE_DATA_TYPES; type++) {
Willy Tarreauedee1d62014-07-15 16:44:27 +0200649 if (!stktable_data_types[type].name)
650 continue;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200651 if (strcmp(name, stktable_data_types[type].name) == 0)
652 return type;
653 }
654 return -1;
655}
656
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200657/* Casts sample <smp> to the type of the table specified in arg(0), and looks
658 * it up into this table. Returns true if found, false otherwise. The input
659 * type is STR so that input samples are converted to string (since all types
660 * can be converted to strings), then the function casts the string again into
661 * the table's type. This is a double conversion, but in the future we might
662 * support automatic input types to perform the cast on the fly.
663 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200664static int sample_conv_in_table(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200665{
666 struct stktable *t;
667 struct stktable_key *key;
668 struct stksess *ts;
669
670 t = &arg_p[0].data.prx->table;
671
672 key = smp_to_stkey(smp, t);
673 if (!key)
674 return 0;
675
676 ts = stktable_lookup_key(t, key);
677
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200678 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200679 smp->data.u.sint = !!ts;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200680 smp->flags = SMP_F_VOL_TEST;
681 return 1;
682}
683
684/* Casts sample <smp> to the type of the table specified in arg(0), and looks
685 * it up into this table. Returns the data rate received from clients in bytes/s
686 * if the key is present in the table, otherwise zero, so that comparisons can
687 * be easily performed. If the inspected parameter is not stored in the table,
688 * <not found> is returned.
689 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200690static int sample_conv_table_bytes_in_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200691{
692 struct stktable *t;
693 struct stktable_key *key;
694 struct stksess *ts;
695 void *ptr;
696
697 t = &arg_p[0].data.prx->table;
698
699 key = smp_to_stkey(smp, t);
700 if (!key)
701 return 0;
702
703 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200704 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200705 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200706
707 ts = stktable_lookup_key(t, key);
708 if (!ts) /* key not present */
709 return 1;
710
711 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_RATE);
712 if (!ptr)
713 return 0; /* parameter not stored */
714
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200715 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, bytes_in_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200716 t->data_arg[STKTABLE_DT_BYTES_IN_RATE].u);
717 return 1;
718}
719
720/* Casts sample <smp> to the type of the table specified in arg(0), and looks
721 * it up into this table. Returns the cumulated number of connections for the key
722 * if the key is present in the table, otherwise zero, so that comparisons can
723 * be easily performed. If the inspected parameter is not stored in the table,
724 * <not found> is returned.
725 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200726static int sample_conv_table_conn_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200727{
728 struct stktable *t;
729 struct stktable_key *key;
730 struct stksess *ts;
731 void *ptr;
732
733 t = &arg_p[0].data.prx->table;
734
735 key = smp_to_stkey(smp, t);
736 if (!key)
737 return 0;
738
739 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200740 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200741 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200742
743 ts = stktable_lookup_key(t, key);
744 if (!ts) /* key not present */
745 return 1;
746
747 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CNT);
748 if (!ptr)
749 return 0; /* parameter not stored */
750
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200751 smp->data.u.sint = stktable_data_cast(ptr, conn_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200752 return 1;
753}
754
755/* Casts sample <smp> to the type of the table specified in arg(0), and looks
756 * it up into this table. Returns the number of concurrent connections for the
757 * key if the key is present in the table, otherwise zero, so that comparisons
758 * can be easily performed. If the inspected parameter is not stored in the
759 * table, <not found> is returned.
760 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200761static int sample_conv_table_conn_cur(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200762{
763 struct stktable *t;
764 struct stktable_key *key;
765 struct stksess *ts;
766 void *ptr;
767
768 t = &arg_p[0].data.prx->table;
769
770 key = smp_to_stkey(smp, t);
771 if (!key)
772 return 0;
773
774 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
778 ts = stktable_lookup_key(t, key);
779 if (!ts) /* key not present */
780 return 1;
781
782 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_CUR);
783 if (!ptr)
784 return 0; /* parameter not stored */
785
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200786 smp->data.u.sint = stktable_data_cast(ptr, conn_cur);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200787 return 1;
788}
789
790/* Casts sample <smp> to the type of the table specified in arg(0), and looks
791 * it up into this table. Returns the rate of incoming connections from the key
792 * if the key is present in the table, otherwise zero, so that comparisons can
793 * be easily performed. If the inspected parameter is not stored in the table,
794 * <not found> is returned.
795 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200796static int sample_conv_table_conn_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200797{
798 struct stktable *t;
799 struct stktable_key *key;
800 struct stksess *ts;
801 void *ptr;
802
803 t = &arg_p[0].data.prx->table;
804
805 key = smp_to_stkey(smp, t);
806 if (!key)
807 return 0;
808
809 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200810 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200811 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200812
813 ts = stktable_lookup_key(t, key);
814 if (!ts) /* key not present */
815 return 1;
816
817 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_CONN_RATE);
818 if (!ptr)
819 return 0; /* parameter not stored */
820
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200821 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, conn_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200822 t->data_arg[STKTABLE_DT_CONN_RATE].u);
823 return 1;
824}
825
826/* Casts sample <smp> to the type of the table specified in arg(0), and looks
827 * it up into this table. Returns the data rate sent to clients in bytes/s
828 * if the key is present in the table, otherwise zero, so that comparisons can
829 * be easily performed. If the inspected parameter is not stored in the table,
830 * <not found> is returned.
831 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200832static int sample_conv_table_bytes_out_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200833{
834 struct stktable *t;
835 struct stktable_key *key;
836 struct stksess *ts;
837 void *ptr;
838
839 t = &arg_p[0].data.prx->table;
840
841 key = smp_to_stkey(smp, t);
842 if (!key)
843 return 0;
844
845 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200846 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200847 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200848
849 ts = stktable_lookup_key(t, key);
850 if (!ts) /* key not present */
851 return 1;
852
853 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_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, bytes_out_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200858 t->data_arg[STKTABLE_DT_BYTES_OUT_RATE].u);
859 return 1;
860}
861
862/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Thierry FOURNIER236657b2015-08-19 08:25:14 +0200863 * it up into this table. Returns the value of the GPT0 tag for the key
864 * if the key is present in the table, otherwise false, so that comparisons can
865 * be easily performed. If the inspected parameter is not stored in the table,
866 * <not found> is returned.
867 */
868static int sample_conv_table_gpt0(const struct arg *arg_p, struct sample *smp, void *private)
869{
870 struct stktable *t;
871 struct stktable_key *key;
872 struct stksess *ts;
873 void *ptr;
874
875 t = &arg_p[0].data.prx->table;
876
877 key = smp_to_stkey(smp, t);
878 if (!key)
879 return 0;
880
881 smp->flags = SMP_F_VOL_TEST;
882 smp->data.type = SMP_T_SINT;
883 smp->data.u.sint = 0;
884
885 ts = stktable_lookup_key(t, key);
886 if (!ts) /* key not present */
887 return 1;
888
889 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPT0);
890 if (!ptr)
891 return 0; /* parameter not stored */
892
893 smp->data.u.sint = stktable_data_cast(ptr, gpt0);
894 return 1;
895}
896
897/* Casts sample <smp> to the type of the table specified in arg(0), and looks
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200898 * it up into this table. Returns the value of the GPC0 counter for the key
899 * if the key is present in the table, otherwise zero, so that comparisons can
900 * be easily performed. If the inspected parameter is not stored in the table,
901 * <not found> is returned.
902 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200903static int sample_conv_table_gpc0(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200904{
905 struct stktable *t;
906 struct stktable_key *key;
907 struct stksess *ts;
908 void *ptr;
909
910 t = &arg_p[0].data.prx->table;
911
912 key = smp_to_stkey(smp, t);
913 if (!key)
914 return 0;
915
916 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200917 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200918 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200919
920 ts = stktable_lookup_key(t, key);
921 if (!ts) /* key not present */
922 return 1;
923
924 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0);
925 if (!ptr)
926 return 0; /* parameter not stored */
927
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200928 smp->data.u.sint = stktable_data_cast(ptr, gpc0);
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200929 return 1;
930}
931
932/* Casts sample <smp> to the type of the table specified in arg(0), and looks
933 * it up into this table. Returns the event rate of the GPC0 counter for the key
934 * if the key is present in the table, otherwise zero, so that comparisons can
935 * be easily performed. If the inspected parameter is not stored in the table,
936 * <not found> is returned.
937 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200938static int sample_conv_table_gpc0_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200939{
940 struct stktable *t;
941 struct stktable_key *key;
942 struct stksess *ts;
943 void *ptr;
944
945 t = &arg_p[0].data.prx->table;
946
947 key = smp_to_stkey(smp, t);
948 if (!key)
949 return 0;
950
951 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200952 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200953 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200954
955 ts = stktable_lookup_key(t, key);
956 if (!ts) /* key not present */
957 return 1;
958
959 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_GPC0_RATE);
960 if (!ptr)
961 return 0; /* parameter not stored */
962
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200963 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, gpc0_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200964 t->data_arg[STKTABLE_DT_GPC0_RATE].u);
965 return 1;
966}
967
968/* Casts sample <smp> to the type of the table specified in arg(0), and looks
969 * it up into this table. Returns the cumulated number of HTTP request errors
970 * for the key if the key is present in the table, otherwise zero, so that
971 * comparisons can be easily performed. If the inspected parameter is not stored
972 * in the table, <not found> is returned.
973 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +0200974static int sample_conv_table_http_err_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200975{
976 struct stktable *t;
977 struct stktable_key *key;
978 struct stksess *ts;
979 void *ptr;
980
981 t = &arg_p[0].data.prx->table;
982
983 key = smp_to_stkey(smp, t);
984 if (!key)
985 return 0;
986
987 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200988 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200989 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +0200990
991 ts = stktable_lookup_key(t, key);
992 if (!ts) /* key not present */
993 return 1;
994
995 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_CNT);
996 if (!ptr)
997 return 0; /* parameter not stored */
998
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200999 smp->data.u.sint = stktable_data_cast(ptr, http_err_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001000 return 1;
1001}
1002
1003/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1004 * it up into this table. Returns the HTTP request error rate the key
1005 * if the key is present in the table, otherwise zero, so that comparisons can
1006 * be easily performed. If the inspected parameter is not stored in the table,
1007 * <not found> is returned.
1008 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001009static int sample_conv_table_http_err_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001010{
1011 struct stktable *t;
1012 struct stktable_key *key;
1013 struct stksess *ts;
1014 void *ptr;
1015
1016 t = &arg_p[0].data.prx->table;
1017
1018 key = smp_to_stkey(smp, t);
1019 if (!key)
1020 return 0;
1021
1022 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001023 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001024 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001025
1026 ts = stktable_lookup_key(t, key);
1027 if (!ts) /* key not present */
1028 return 1;
1029
1030 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_ERR_RATE);
1031 if (!ptr)
1032 return 0; /* parameter not stored */
1033
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001034 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_err_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001035 t->data_arg[STKTABLE_DT_HTTP_ERR_RATE].u);
1036 return 1;
1037}
1038
1039/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1040 * it up into this table. Returns the cumulated number of HTTP request for the
1041 * key if the key is present in the table, otherwise zero, so that comparisons
1042 * can be easily performed. If the inspected parameter is not stored in the
1043 * table, <not found> is returned.
1044 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001045static int sample_conv_table_http_req_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001046{
1047 struct stktable *t;
1048 struct stktable_key *key;
1049 struct stksess *ts;
1050 void *ptr;
1051
1052 t = &arg_p[0].data.prx->table;
1053
1054 key = smp_to_stkey(smp, t);
1055 if (!key)
1056 return 0;
1057
1058 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001059 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001060 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001061
1062 ts = stktable_lookup_key(t, key);
1063 if (!ts) /* key not present */
1064 return 1;
1065
1066 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_CNT);
1067 if (!ptr)
1068 return 0; /* parameter not stored */
1069
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001070 smp->data.u.sint = stktable_data_cast(ptr, http_req_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001071 return 1;
1072}
1073
1074/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1075 * it up into this table. Returns the HTTP request rate the key if the key is
1076 * present in the table, otherwise zero, so that comparisons can be easily
1077 * performed. If the inspected parameter is not stored in the table, <not found>
1078 * is returned.
1079 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001080static int sample_conv_table_http_req_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001081{
1082 struct stktable *t;
1083 struct stktable_key *key;
1084 struct stksess *ts;
1085 void *ptr;
1086
1087 t = &arg_p[0].data.prx->table;
1088
1089 key = smp_to_stkey(smp, t);
1090 if (!key)
1091 return 0;
1092
1093 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001094 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001095 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001096
1097 ts = stktable_lookup_key(t, key);
1098 if (!ts) /* key not present */
1099 return 1;
1100
1101 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_HTTP_REQ_RATE);
1102 if (!ptr)
1103 return 0; /* parameter not stored */
1104
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001105 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, http_req_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001106 t->data_arg[STKTABLE_DT_HTTP_REQ_RATE].u);
1107 return 1;
1108}
1109
1110/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1111 * it up into this table. Returns the volume of datareceived from clients in kbytes
1112 * if the key is present in the table, otherwise zero, so that comparisons can
1113 * be easily performed. If the inspected parameter is not stored in the table,
1114 * <not found> is returned.
1115 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001116static int sample_conv_table_kbytes_in(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001117{
1118 struct stktable *t;
1119 struct stktable_key *key;
1120 struct stksess *ts;
1121 void *ptr;
1122
1123 t = &arg_p[0].data.prx->table;
1124
1125 key = smp_to_stkey(smp, t);
1126 if (!key)
1127 return 0;
1128
1129 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001130 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001131 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001132
1133 ts = stktable_lookup_key(t, key);
1134 if (!ts) /* key not present */
1135 return 1;
1136
1137 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_IN_CNT);
1138 if (!ptr)
1139 return 0; /* parameter not stored */
1140
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001141 smp->data.u.sint = stktable_data_cast(ptr, bytes_in_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001142 return 1;
1143}
1144
1145/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1146 * it up into this table. Returns the volume of data sent to clients in kbytes
1147 * if the key is present in the table, otherwise zero, so that comparisons can
1148 * be easily performed. If the inspected parameter is not stored in the table,
1149 * <not found> is returned.
1150 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001151static int sample_conv_table_kbytes_out(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001152{
1153 struct stktable *t;
1154 struct stktable_key *key;
1155 struct stksess *ts;
1156 void *ptr;
1157
1158 t = &arg_p[0].data.prx->table;
1159
1160 key = smp_to_stkey(smp, t);
1161 if (!key)
1162 return 0;
1163
1164 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001165 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001166 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001167
1168 ts = stktable_lookup_key(t, key);
1169 if (!ts) /* key not present */
1170 return 1;
1171
1172 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_BYTES_OUT_CNT);
1173 if (!ptr)
1174 return 0; /* parameter not stored */
1175
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001176 smp->data.u.sint = stktable_data_cast(ptr, bytes_out_cnt) >> 10;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001177 return 1;
1178}
1179
1180/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1181 * it up into this table. Returns the server ID associated with the key if the
1182 * key is present in the table, otherwise zero, so that comparisons can be
1183 * easily performed. If the inspected parameter is not stored in the table,
1184 * <not found> is returned.
1185 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001186static int sample_conv_table_server_id(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001187{
1188 struct stktable *t;
1189 struct stktable_key *key;
1190 struct stksess *ts;
1191 void *ptr;
1192
1193 t = &arg_p[0].data.prx->table;
1194
1195 key = smp_to_stkey(smp, t);
1196 if (!key)
1197 return 0;
1198
1199 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001200 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001201 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001202
1203 ts = stktable_lookup_key(t, key);
1204 if (!ts) /* key not present */
1205 return 1;
1206
1207 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SERVER_ID);
1208 if (!ptr)
1209 return 0; /* parameter not stored */
1210
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001211 smp->data.u.sint = stktable_data_cast(ptr, server_id);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001212 return 1;
1213}
1214
1215/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1216 * it up into this table. Returns the cumulated number of sessions for the
1217 * key if the key is present in the table, otherwise zero, so that comparisons
1218 * can be easily performed. If the inspected parameter is not stored in the
1219 * table, <not found> is returned.
1220 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001221static int sample_conv_table_sess_cnt(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001222{
1223 struct stktable *t;
1224 struct stktable_key *key;
1225 struct stksess *ts;
1226 void *ptr;
1227
1228 t = &arg_p[0].data.prx->table;
1229
1230 key = smp_to_stkey(smp, t);
1231 if (!key)
1232 return 0;
1233
1234 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001235 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001236 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001237
1238 ts = stktable_lookup_key(t, key);
1239 if (!ts) /* key not present */
1240 return 1;
1241
1242 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_CNT);
1243 if (!ptr)
1244 return 0; /* parameter not stored */
1245
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001246 smp->data.u.sint = stktable_data_cast(ptr, sess_cnt);
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001247 return 1;
1248}
1249
1250/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1251 * it up into this table. Returns the session rate the key if the key is
1252 * present in the table, otherwise zero, so that comparisons can be easily
1253 * performed. If the inspected parameter is not stored in the table, <not found>
1254 * is returned.
1255 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001256static int sample_conv_table_sess_rate(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001257{
1258 struct stktable *t;
1259 struct stktable_key *key;
1260 struct stksess *ts;
1261 void *ptr;
1262
1263 t = &arg_p[0].data.prx->table;
1264
1265 key = smp_to_stkey(smp, t);
1266 if (!key)
1267 return 0;
1268
1269 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001270 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001271 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001272
1273 ts = stktable_lookup_key(t, key);
1274 if (!ts) /* key not present */
1275 return 1;
1276
1277 ptr = stktable_data_ptr(t, ts, STKTABLE_DT_SESS_RATE);
1278 if (!ptr)
1279 return 0; /* parameter not stored */
1280
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001281 smp->data.u.sint = read_freq_ctr_period(&stktable_data_cast(ptr, sess_rate),
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001282 t->data_arg[STKTABLE_DT_SESS_RATE].u);
1283 return 1;
1284}
1285
1286/* Casts sample <smp> to the type of the table specified in arg(0), and looks
1287 * it up into this table. Returns the amount of concurrent connections tracking
1288 * the same key if the key is present in the table, otherwise zero, so that
1289 * comparisons can be easily performed. If the inspected parameter is not
1290 * stored in the table, <not found> is returned.
1291 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02001292static int sample_conv_table_trackers(const struct arg *arg_p, struct sample *smp, void *private)
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001293{
1294 struct stktable *t;
1295 struct stktable_key *key;
1296 struct stksess *ts;
1297
1298 t = &arg_p[0].data.prx->table;
1299
1300 key = smp_to_stkey(smp, t);
1301 if (!key)
1302 return 0;
1303
1304 smp->flags = SMP_F_VOL_TEST;
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001305 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001306 smp->data.u.sint = 0;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001307
1308 ts = stktable_lookup_key(t, key);
1309 if (ts)
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001310 smp->data.u.sint = ts->ref_cnt;
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001311
1312 return 1;
1313}
1314
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001315/* Always returns 1. */
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001316static enum act_return action_inc_gpc0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001317 struct session *sess, struct stream *s, int flags)
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001318{
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001319 struct stksess *ts;
1320 struct stkctr *stkctr;
1321
1322 /* Extract the stksess, return OK if no stksess available. */
1323 if (s)
1324 stkctr = &s->stkctr[rule->arg.gpc.sc];
1325 else
1326 stkctr = &sess->stkctr[rule->arg.gpc.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001327
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001328 ts = stkctr_entry(stkctr);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001329 if (ts) {
1330 void *ptr1, *ptr2;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001331
Willy Tarreau79c1e912016-01-25 14:54:45 +01001332 /* First, update gpc0_rate if it's tracked. Second, update its gpc0 if tracked. */
1333 ptr1 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0_RATE);
1334 if (ptr1)
1335 update_freq_ctr_period(&stktable_data_cast(ptr1, gpc0_rate),
1336 stkctr->table->data_arg[STKTABLE_DT_GPC0_RATE].u, 1);
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001337
Willy Tarreau79c1e912016-01-25 14:54:45 +01001338 ptr2 = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPC0);
1339 if (ptr2)
1340 stktable_data_cast(ptr2, gpc0)++;
1341
1342 /* If data was modified, we need to touch to re-schedule sync */
1343 if (ptr1 || ptr2)
1344 stktable_touch(stkctr->table, ts, 1);
1345 }
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001346 return ACT_RET_CONT;
1347}
1348
1349/* This function is a common parser for using variables. It understands
1350 * the formats:
1351 *
1352 * sc-inc-gpc0(<stick-table ID>)
1353 *
1354 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1355 * it returns 1 and the variable <expr> is filled with the pointer to the
1356 * expression to execute.
1357 */
1358static enum act_parse_ret parse_inc_gpc0(const char **args, int *arg, struct proxy *px,
1359 struct act_rule *rule, char **err)
1360{
1361 const char *cmd_name = args[*arg-1];
1362 char *error;
1363
1364 cmd_name += strlen("sc-inc-gpc0");
1365 if (*cmd_name == '\0') {
1366 /* default stick table id. */
1367 rule->arg.gpc.sc = 0;
1368 } else {
1369 /* parse the stick table id. */
1370 if (*cmd_name != '(') {
1371 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1372 return ACT_RET_PRS_ERR;
1373 }
1374 cmd_name++; /* jump the '(' */
1375 rule->arg.gpc.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1376 if (*error != ')') {
1377 memprintf(err, "invalid stick table track ID. Expects %s(<Track ID>)", args[*arg-1]);
1378 return ACT_RET_PRS_ERR;
1379 }
1380
1381 if (rule->arg.gpc.sc >= ACT_ACTION_TRK_SCMAX) {
1382 memprintf(err, "invalid stick table track ID. The max allowed ID is %d",
1383 ACT_ACTION_TRK_SCMAX-1);
1384 return ACT_RET_PRS_ERR;
1385 }
1386 }
Thierry FOURNIER42148732015-09-02 17:17:33 +02001387 rule->action = ACT_CUSTOM;
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001388 rule->action_ptr = action_inc_gpc0;
1389 return ACT_RET_PRS_OK;
1390}
1391
1392/* Always returns 1. */
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001393static enum act_return action_set_gpt0(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02001394 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001395{
1396 void *ptr;
1397 struct stksess *ts;
1398 struct stkctr *stkctr;
1399
1400 /* Extract the stksess, return OK if no stksess available. */
1401 if (s)
1402 stkctr = &s->stkctr[rule->arg.gpt.sc];
1403 else
1404 stkctr = &sess->stkctr[rule->arg.gpt.sc];
Willy Tarreau79c1e912016-01-25 14:54:45 +01001405
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001406 ts = stkctr_entry(stkctr);
1407 if (!ts)
1408 return ACT_RET_CONT;
1409
1410 /* Store the sample in the required sc, and ignore errors. */
1411 ptr = stktable_data_ptr(stkctr->table, ts, STKTABLE_DT_GPT0);
Willy Tarreau79c1e912016-01-25 14:54:45 +01001412 if (ptr) {
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001413 stktable_data_cast(ptr, gpt0) = rule->arg.gpt.value;
Willy Tarreau79c1e912016-01-25 14:54:45 +01001414 stktable_touch(stkctr->table, ts, 1);
1415 }
1416
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001417 return ACT_RET_CONT;
1418}
1419
1420/* This function is a common parser for using variables. It understands
1421 * the format:
1422 *
1423 * set-gpt0(<stick-table ID>) <expression>
1424 *
1425 * It returns 0 if fails and <err> is filled with an error message. Otherwise,
1426 * it returns 1 and the variable <expr> is filled with the pointer to the
1427 * expression to execute.
1428 */
1429static enum act_parse_ret parse_set_gpt0(const char **args, int *arg, struct proxy *px,
1430 struct act_rule *rule, char **err)
1431
1432
1433{
1434 const char *cmd_name = args[*arg-1];
1435 char *error;
1436
1437 cmd_name += strlen("sc-set-gpt0");
1438 if (*cmd_name == '\0') {
1439 /* default stick table id. */
1440 rule->arg.gpt.sc = 0;
1441 } else {
1442 /* parse the stick table id. */
1443 if (*cmd_name != '(') {
1444 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1445 return ACT_RET_PRS_ERR;
1446 }
1447 cmd_name++; /* jump the '(' */
1448 rule->arg.gpt.sc = strtol(cmd_name, &error, 10); /* Convert stick table id. */
1449 if (*error != ')') {
1450 memprintf(err, "invalid stick table track ID '%s'. Expects sc-set-gpt0(<Track ID>)", args[*arg-1]);
1451 return ACT_RET_PRS_ERR;
1452 }
1453
1454 if (rule->arg.gpt.sc >= ACT_ACTION_TRK_SCMAX) {
1455 memprintf(err, "invalid stick table track ID '%s'. The max allowed ID is %d",
1456 args[*arg-1], ACT_ACTION_TRK_SCMAX-1);
1457 return ACT_RET_PRS_ERR;
1458 }
1459 }
1460
1461 rule->arg.gpt.value = strtol(args[*arg], &error, 10);
1462 if (*error != '\0') {
1463 memprintf(err, "invalid integer value '%s'", args[*arg]);
1464 return ACT_RET_PRS_ERR;
1465 }
1466 (*arg)++;
1467
Thierry FOURNIER42148732015-09-02 17:17:33 +02001468 rule->action = ACT_CUSTOM;
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001469 rule->action_ptr = action_set_gpt0;
1470
1471 return ACT_RET_PRS_OK;
1472}
1473
1474static struct action_kw_list tcp_conn_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001475 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001476 { "sc-set-gpt0", parse_set_gpt0, 1 },
1477 { /* END */ }
1478}};
1479
1480static struct action_kw_list tcp_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001481 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001482 { "sc-set-gpt0", parse_set_gpt0, 1 },
1483 { /* END */ }
1484}};
1485
1486static struct action_kw_list tcp_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001487 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001488 { "sc-set-gpt0", parse_set_gpt0, 1 },
1489 { /* END */ }
1490}};
1491
1492static struct action_kw_list http_req_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001493 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001494 { "sc-set-gpt0", parse_set_gpt0, 1 },
1495 { /* END */ }
1496}};
1497
1498static struct action_kw_list http_res_kws = { { }, {
Thierry FOURNIERe0627bd2015-08-04 08:20:33 +02001499 { "sc-inc-gpc0", parse_inc_gpc0, 1 },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001500 { "sc-set-gpt0", parse_set_gpt0, 1 },
1501 { /* END */ }
1502}};
1503
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001504/* Note: must not be declared <const> as its list will be overwritten */
1505static struct sample_conv_kw_list sample_conv_kws = {ILH, {
1506 { "in_table", sample_conv_in_table, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_BOOL },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001507 { "table_bytes_in_rate", sample_conv_table_bytes_in_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1508 { "table_bytes_out_rate", sample_conv_table_bytes_out_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1509 { "table_conn_cnt", sample_conv_table_conn_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1510 { "table_conn_cur", sample_conv_table_conn_cur, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1511 { "table_conn_rate", sample_conv_table_conn_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001512 { "table_gpt0", sample_conv_table_gpt0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001513 { "table_gpc0", sample_conv_table_gpc0, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1514 { "table_gpc0_rate", sample_conv_table_gpc0_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1515 { "table_http_err_cnt", sample_conv_table_http_err_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1516 { "table_http_err_rate", sample_conv_table_http_err_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1517 { "table_http_req_cnt", sample_conv_table_http_req_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1518 { "table_http_req_rate", sample_conv_table_http_req_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1519 { "table_kbytes_in", sample_conv_table_kbytes_in, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1520 { "table_kbytes_out", sample_conv_table_kbytes_out, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1521 { "table_server_id", sample_conv_table_server_id, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1522 { "table_sess_cnt", sample_conv_table_sess_cnt, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1523 { "table_sess_rate", sample_conv_table_sess_rate, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
1524 { "table_trackers", sample_conv_table_trackers, ARG1(1,TAB), NULL, SMP_T_STR, SMP_T_SINT },
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001525 { /* END */ },
1526}};
1527
1528__attribute__((constructor))
1529static void __stick_table_init(void)
1530{
Thierry FOURNIER236657b2015-08-19 08:25:14 +02001531 /* register som action keywords. */
1532 tcp_req_conn_keywords_register(&tcp_conn_kws);
1533 tcp_req_cont_keywords_register(&tcp_req_kws);
1534 tcp_res_cont_keywords_register(&tcp_res_kws);
1535 http_req_keywords_register(&http_req_kws);
1536 http_res_keywords_register(&http_res_kws);
1537
Willy Tarreaud9f316a2014-07-10 14:03:38 +02001538 /* register sample fetch and format conversion keywords */
1539 sample_register_convs(&sample_conv_kws);
1540}