blob: 962c95a1f7220af8d8807c0cf8f257ac138bf737 [file] [log] [blame]
Emeric Brun3bd697e2010-01-04 15:23:48 +01001/*
2 * include/proto/stick_table.h
3 * Functions for stick tables management.
4 *
5 * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreau08d5f982010-06-06 13:34:54 +02006 * Copyright (C) 2010 Willy Tarreau <w@1wt.eu>
Emeric Brun3bd697e2010-01-04 15:23:48 +01007 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation, version 2.1
11 * exclusively.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#ifndef _PROTO_STICK_TABLE_H
24#define _PROTO_STICK_TABLE_H
25
Willy Tarreau888617d2010-06-20 09:11:39 +020026#include <common/errors.h>
Willy Tarreauf6efda12010-08-03 20:34:06 +020027#include <common/ticks.h>
28#include <common/time.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010029#include <types/stick_table.h>
30
Willy Tarreau68129b92010-06-06 16:06:52 +020031#define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type))
32#define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type
33
Emeric Brun3bd697e2010-01-04 15:23:48 +010034struct stksess *stksess_new(struct stktable *t, struct stktable_key *key);
Willy Tarreau393379c2010-06-06 12:11:37 +020035void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key);
Emeric Brun3bd697e2010-01-04 15:23:48 +010036void stksess_free(struct stktable *t, struct stksess *ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +020037int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcount);
Emeric Brun3bd697e2010-01-04 15:23:48 +010038
39int stktable_init(struct stktable *t);
40int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size);
Frédéric Lécailled456aa42019-03-08 14:47:00 +010041int parse_stick_table(const char *file, int linenum, char **args,
42 struct stktable *t, char *id, struct peers *peers);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020043struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key);
Emeric Brun819fc6f2017-06-13 19:37:32 +020044struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts);
45void stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int decrefcount, int expire);
46void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt);
47void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefccount);
Willy Tarreauf16d2b82010-06-06 15:38:59 +020048struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts);
49struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key);
Willy Tarreau1f7e9252010-06-20 12:27:21 +020050struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key);
Willy Tarreau8fed9032014-07-03 17:02:46 +020051struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t);
Willy Tarreau192252e2015-04-04 01:47:55 +020052struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess,
Willy Tarreau15e91e12015-04-04 00:52:09 +020053 struct stream *strm, unsigned int opt,
54 struct sample_expr *expr, struct sample *smp);
Emeric Brun819fc6f2017-06-13 19:37:32 +020055struct stkctr *smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw, struct stkctr *stkctr);
56struct stkctr *smp_create_src_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw, struct stkctr *stkctr);
Willy Tarreau12785782012-04-27 21:37:17 +020057int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type);
Willy Tarreauedee1d62014-07-15 16:44:27 +020058int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type);
Willy Tarreau08d5f982010-06-06 13:34:54 +020059int stktable_get_data_type(char *name);
Willy Tarreau3a925c12013-09-04 17:54:01 +020060int stktable_trash_oldest(struct stktable *t, int to_batch);
Emeric Brun819fc6f2017-06-13 19:37:32 +020061int __stksess_kill(struct stktable *t, struct stksess *ts);
Willy Tarreau08d5f982010-06-06 13:34:54 +020062
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020063/* return allocation size for standard data type <type> */
64static inline int stktable_type_size(int type)
65{
66 switch(type) {
67 case STD_T_SINT:
68 case STD_T_UINT:
69 return sizeof(int);
70 case STD_T_ULL:
71 return sizeof(unsigned long long);
72 case STD_T_FRQP:
73 return sizeof(struct freq_ctr_period);
74 }
75 return 0;
76}
77
Willy Tarreau888617d2010-06-20 09:11:39 +020078/* reserve some space for data type <type>, and associate argument at <sa> if
79 * not NULL. Returns PE_NONE (0) if OK or an error code among :
80 * - PE_ENUM_OOR if <type> does not exist
81 * - PE_EXIST if <type> is already registered
Willy Tarreauac782882010-06-20 10:41:54 +020082 * - PE_ARG_NOT_USE if <sa> was provided but not expected
83 * - PE_ARG_MISSING if <sa> was expected but not provided
Willy Tarreau08d5f982010-06-06 13:34:54 +020084 */
Willy Tarreau888617d2010-06-20 09:11:39 +020085static inline int stktable_alloc_data_type(struct stktable *t, int type, const char *sa)
Willy Tarreau08d5f982010-06-06 13:34:54 +020086{
87 if (type >= STKTABLE_DATA_TYPES)
Willy Tarreau888617d2010-06-20 09:11:39 +020088 return PE_ENUM_OOR;
Willy Tarreau08d5f982010-06-06 13:34:54 +020089
90 if (t->data_ofs[type])
91 /* already allocated */
Willy Tarreau888617d2010-06-20 09:11:39 +020092 return PE_EXIST;
Emeric Brun3bd697e2010-01-04 15:23:48 +010093
Willy Tarreauac782882010-06-20 10:41:54 +020094 switch (stktable_data_types[type].arg_type) {
95 case ARG_T_NONE:
96 if (sa)
97 return PE_ARG_NOT_USED;
98 break;
99 case ARG_T_INT:
100 if (!sa)
101 return PE_ARG_MISSING;
102 t->data_arg[type].i = atoi(sa);
103 break;
104 case ARG_T_DELAY:
105 if (!sa)
106 return PE_ARG_MISSING;
107 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
108 if (sa)
109 return PE_ARG_INVC; /* invalid char */
110 break;
111 }
112
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200113 t->data_size += stktable_type_size(stktable_data_types[type].std_type);
Willy Tarreau08d5f982010-06-06 13:34:54 +0200114 t->data_ofs[type] = -t->data_size;
Willy Tarreau888617d2010-06-20 09:11:39 +0200115 return PE_NONE;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200116}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100117
Willy Tarreaubabc15e2018-09-20 11:06:33 +0200118/* return pointer for data type <type> in sticky session <ts> of table <t>, all
119 * of which must exist (otherwise use stktable_data_ptr() if unsure).
120 */
121static inline void *__stktable_data_ptr(struct stktable *t, struct stksess *ts, int type)
122{
123 return (void *)ts + t->data_ofs[type];
124}
125
Willy Tarreau68129b92010-06-06 16:06:52 +0200126/* return pointer for data type <type> in sticky session <ts> of table <t>, or
127 * NULL if either <ts> is NULL or the type is not stored.
128 */
129static inline void *stktable_data_ptr(struct stktable *t, struct stksess *ts, int type)
130{
131 if (type >= STKTABLE_DATA_TYPES)
132 return NULL;
133
134 if (!t->data_ofs[type]) /* type not stored */
135 return NULL;
136
137 if (!ts)
138 return NULL;
139
Willy Tarreaubabc15e2018-09-20 11:06:33 +0200140 return __stktable_data_ptr(t, ts, type);
Willy Tarreau68129b92010-06-06 16:06:52 +0200141}
142
Willy Tarreauf6efda12010-08-03 20:34:06 +0200143/* kill an entry if it's expired and its ref_cnt is zero */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200144static inline int __stksess_kill_if_expired(struct stktable *t, struct stksess *ts)
Willy Tarreauf6efda12010-08-03 20:34:06 +0200145{
Emeric Brunc89a5722010-09-23 18:11:05 +0200146 if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms))
Emeric Brun819fc6f2017-06-13 19:37:32 +0200147 return __stksess_kill(t, ts);
148
149 return 0;
150}
151
Willy Tarreaua5e05902017-10-31 15:45:42 +0100152static inline void stksess_kill_if_expired(struct stktable *t, struct stksess *ts, int decrefcnt)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200153{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100154 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200155
156 if (decrefcnt)
157 ts->ref_cnt--;
158
159 if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms))
Willy Tarreaua5e05902017-10-31 15:45:42 +0100160 __stksess_kill_if_expired(t, ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200161
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100162 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreauf6efda12010-08-03 20:34:06 +0200163}
164
Willy Tarreau7698c902015-04-04 16:24:42 +0200165/* sets the stick counter's entry pointer */
166static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry)
167{
168 stkctr->entry = caddr_from_ptr(entry, 0);
169}
170
171/* returns the entry pointer from a stick counter */
172static inline struct stksess *stkctr_entry(struct stkctr *stkctr)
173{
174 return caddr_to_ptr(stkctr->entry);
175}
176
177/* returns the two flags from a stick counter */
178static inline unsigned int stkctr_flags(struct stkctr *stkctr)
179{
180 return caddr_to_data(stkctr->entry);
181}
182
183/* sets up to two flags at a time on a composite address */
184static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags)
185{
186 stkctr->entry = caddr_set_flags(stkctr->entry, flags);
187}
188
189/* returns the two flags from a stick counter */
190static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
191{
192 stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
193}
194
Emeric Brun3bd697e2010-01-04 15:23:48 +0100195#endif /* _PROTO_STICK_TABLE_H */