blob: ebc4d675441204b9894c2b9837a0e679ae769a4e [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 Tarreau3afc4c42020-06-03 18:23:19 +020026#include <haproxy/dict-t.h>
Willy Tarreau8d366972020-05-27 16:10:29 +020027#include <haproxy/errors.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020028#include <haproxy/tools.h>
Willy Tarreauc2f7c582020-06-02 18:15:32 +020029#include <haproxy/ticks.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020030#include <haproxy/time.h>
Emeric Brun3bd697e2010-01-04 15:23:48 +010031#include <types/stick_table.h>
32
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010033extern struct stktable *stktables_list;
34
Willy Tarreau68129b92010-06-06 16:06:52 +020035#define stktable_data_size(type) (sizeof(((union stktable_data*)0)->type))
36#define stktable_data_cast(ptr, type) ((union stktable_data*)(ptr))->type
37
Frédéric Lécaille1b8e68e2019-03-14 07:07:41 +010038void stktable_store_name(struct stktable *t);
39struct stktable *stktable_find_by_name(const char *name);
Emeric Brun3bd697e2010-01-04 15:23:48 +010040struct stksess *stksess_new(struct stktable *t, struct stktable_key *key);
Willy Tarreau393379c2010-06-06 12:11:37 +020041void stksess_setkey(struct stktable *t, struct stksess *ts, struct stktable_key *key);
Emeric Brun3bd697e2010-01-04 15:23:48 +010042void stksess_free(struct stktable *t, struct stksess *ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +020043int stksess_kill(struct stktable *t, struct stksess *ts, int decrefcount);
Emeric Brun3bd697e2010-01-04 15:23:48 +010044
45int stktable_init(struct stktable *t);
46int stktable_parse_type(char **args, int *idx, unsigned long *type, size_t *key_size);
Frédéric Lécailled456aa42019-03-08 14:47:00 +010047int parse_stick_table(const char *file, int linenum, char **args,
Frédéric Lécaillec02766a2019-03-20 15:06:55 +010048 struct stktable *t, char *id, char *nid, struct peers *peers);
Willy Tarreau9ba2dcc2010-06-14 21:04:55 +020049struct stksess *stktable_get_entry(struct stktable *table, struct stktable_key *key);
Emeric Brun819fc6f2017-06-13 19:37:32 +020050struct stksess *stktable_set_entry(struct stktable *table, struct stksess *nts);
51void stktable_touch_with_exp(struct stktable *t, struct stksess *ts, int decrefcount, int expire);
52void stktable_touch_remote(struct stktable *t, struct stksess *ts, int decrefcnt);
53void stktable_touch_local(struct stktable *t, struct stksess *ts, int decrefccount);
Willy Tarreauf16d2b82010-06-06 15:38:59 +020054struct stksess *stktable_lookup(struct stktable *t, struct stksess *ts);
55struct stksess *stktable_lookup_key(struct stktable *t, struct stktable_key *key);
Willy Tarreau1f7e9252010-06-20 12:27:21 +020056struct stksess *stktable_update_key(struct stktable *table, struct stktable_key *key);
Willy Tarreau8fed9032014-07-03 17:02:46 +020057struct stktable_key *smp_to_stkey(struct sample *smp, struct stktable *t);
Willy Tarreau192252e2015-04-04 01:47:55 +020058struct stktable_key *stktable_fetch_key(struct stktable *t, struct proxy *px, struct session *sess,
Willy Tarreau15e91e12015-04-04 00:52:09 +020059 struct stream *strm, unsigned int opt,
60 struct sample_expr *expr, struct sample *smp);
Emeric Brun819fc6f2017-06-13 19:37:32 +020061struct stkctr *smp_fetch_sc_stkctr(struct session *sess, struct stream *strm, const struct arg *args, const char *kw, struct stkctr *stkctr);
62struct 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 +020063int stktable_compatible_sample(struct sample_expr *expr, unsigned long table_type);
Willy Tarreauedee1d62014-07-15 16:44:27 +020064int stktable_register_data_store(int idx, const char *name, int std_type, int arg_type);
Willy Tarreau08d5f982010-06-06 13:34:54 +020065int stktable_get_data_type(char *name);
Willy Tarreau3a925c12013-09-04 17:54:01 +020066int stktable_trash_oldest(struct stktable *t, int to_batch);
Emeric Brun819fc6f2017-06-13 19:37:32 +020067int __stksess_kill(struct stktable *t, struct stksess *ts);
Willy Tarreau08d5f982010-06-06 13:34:54 +020068
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020069/* return allocation size for standard data type <type> */
70static inline int stktable_type_size(int type)
71{
72 switch(type) {
73 case STD_T_SINT:
74 case STD_T_UINT:
75 return sizeof(int);
76 case STD_T_ULL:
77 return sizeof(unsigned long long);
78 case STD_T_FRQP:
79 return sizeof(struct freq_ctr_period);
Frédéric Lécaille5ad57ea2019-05-17 10:08:29 +020080 case STD_T_DICT:
81 return sizeof(struct dict_entry *);
Willy Tarreau3b9c6e02010-07-18 08:04:30 +020082 }
83 return 0;
84}
85
Willy Tarreau888617d2010-06-20 09:11:39 +020086/* reserve some space for data type <type>, and associate argument at <sa> if
87 * not NULL. Returns PE_NONE (0) if OK or an error code among :
88 * - PE_ENUM_OOR if <type> does not exist
89 * - PE_EXIST if <type> is already registered
Willy Tarreauac782882010-06-20 10:41:54 +020090 * - PE_ARG_NOT_USE if <sa> was provided but not expected
91 * - PE_ARG_MISSING if <sa> was expected but not provided
Willy Tarreau08d5f982010-06-06 13:34:54 +020092 */
Willy Tarreau888617d2010-06-20 09:11:39 +020093static inline int stktable_alloc_data_type(struct stktable *t, int type, const char *sa)
Willy Tarreau08d5f982010-06-06 13:34:54 +020094{
95 if (type >= STKTABLE_DATA_TYPES)
Willy Tarreau888617d2010-06-20 09:11:39 +020096 return PE_ENUM_OOR;
Willy Tarreau08d5f982010-06-06 13:34:54 +020097
98 if (t->data_ofs[type])
99 /* already allocated */
Willy Tarreau888617d2010-06-20 09:11:39 +0200100 return PE_EXIST;
Emeric Brun3bd697e2010-01-04 15:23:48 +0100101
Willy Tarreauac782882010-06-20 10:41:54 +0200102 switch (stktable_data_types[type].arg_type) {
103 case ARG_T_NONE:
104 if (sa)
105 return PE_ARG_NOT_USED;
106 break;
107 case ARG_T_INT:
108 if (!sa)
109 return PE_ARG_MISSING;
110 t->data_arg[type].i = atoi(sa);
111 break;
112 case ARG_T_DELAY:
113 if (!sa)
114 return PE_ARG_MISSING;
115 sa = parse_time_err(sa, &t->data_arg[type].u, TIME_UNIT_MS);
116 if (sa)
117 return PE_ARG_INVC; /* invalid char */
118 break;
119 }
120
Willy Tarreau3b9c6e02010-07-18 08:04:30 +0200121 t->data_size += stktable_type_size(stktable_data_types[type].std_type);
Willy Tarreau08d5f982010-06-06 13:34:54 +0200122 t->data_ofs[type] = -t->data_size;
Willy Tarreau888617d2010-06-20 09:11:39 +0200123 return PE_NONE;
Willy Tarreau08d5f982010-06-06 13:34:54 +0200124}
Emeric Brun3bd697e2010-01-04 15:23:48 +0100125
Willy Tarreaubabc15e2018-09-20 11:06:33 +0200126/* return pointer for data type <type> in sticky session <ts> of table <t>, all
127 * of which must exist (otherwise use stktable_data_ptr() if unsure).
128 */
129static inline void *__stktable_data_ptr(struct stktable *t, struct stksess *ts, int type)
130{
131 return (void *)ts + t->data_ofs[type];
132}
133
Willy Tarreau68129b92010-06-06 16:06:52 +0200134/* return pointer for data type <type> in sticky session <ts> of table <t>, or
135 * NULL if either <ts> is NULL or the type is not stored.
136 */
137static inline void *stktable_data_ptr(struct stktable *t, struct stksess *ts, int type)
138{
139 if (type >= STKTABLE_DATA_TYPES)
140 return NULL;
141
142 if (!t->data_ofs[type]) /* type not stored */
143 return NULL;
144
145 if (!ts)
146 return NULL;
147
Willy Tarreaubabc15e2018-09-20 11:06:33 +0200148 return __stktable_data_ptr(t, ts, type);
Willy Tarreau68129b92010-06-06 16:06:52 +0200149}
150
Willy Tarreauf6efda12010-08-03 20:34:06 +0200151/* kill an entry if it's expired and its ref_cnt is zero */
Emeric Brun819fc6f2017-06-13 19:37:32 +0200152static inline int __stksess_kill_if_expired(struct stktable *t, struct stksess *ts)
Willy Tarreauf6efda12010-08-03 20:34:06 +0200153{
Emeric Brunc89a5722010-09-23 18:11:05 +0200154 if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms))
Emeric Brun819fc6f2017-06-13 19:37:32 +0200155 return __stksess_kill(t, ts);
156
157 return 0;
158}
159
Willy Tarreaua5e05902017-10-31 15:45:42 +0100160static inline void stksess_kill_if_expired(struct stktable *t, struct stksess *ts, int decrefcnt)
Emeric Brun819fc6f2017-06-13 19:37:32 +0200161{
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100162 HA_SPIN_LOCK(STK_TABLE_LOCK, &t->lock);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200163
164 if (decrefcnt)
165 ts->ref_cnt--;
166
167 if (t->expire != TICK_ETERNITY && tick_is_expired(ts->expire, now_ms))
Willy Tarreaua5e05902017-10-31 15:45:42 +0100168 __stksess_kill_if_expired(t, ts);
Emeric Brun819fc6f2017-06-13 19:37:32 +0200169
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100170 HA_SPIN_UNLOCK(STK_TABLE_LOCK, &t->lock);
Willy Tarreauf6efda12010-08-03 20:34:06 +0200171}
172
Willy Tarreau7698c902015-04-04 16:24:42 +0200173/* sets the stick counter's entry pointer */
174static inline void stkctr_set_entry(struct stkctr *stkctr, struct stksess *entry)
175{
176 stkctr->entry = caddr_from_ptr(entry, 0);
177}
178
179/* returns the entry pointer from a stick counter */
180static inline struct stksess *stkctr_entry(struct stkctr *stkctr)
181{
182 return caddr_to_ptr(stkctr->entry);
183}
184
185/* returns the two flags from a stick counter */
186static inline unsigned int stkctr_flags(struct stkctr *stkctr)
187{
188 return caddr_to_data(stkctr->entry);
189}
190
191/* sets up to two flags at a time on a composite address */
192static inline void stkctr_set_flags(struct stkctr *stkctr, unsigned int flags)
193{
194 stkctr->entry = caddr_set_flags(stkctr->entry, flags);
195}
196
197/* returns the two flags from a stick counter */
198static inline void stkctr_clr_flags(struct stkctr *stkctr, unsigned int flags)
199{
200 stkctr->entry = caddr_clr_flags(stkctr->entry, flags);
201}
202
Emeric Brun3bd697e2010-01-04 15:23:48 +0100203#endif /* _PROTO_STICK_TABLE_H */