blob: 2f5ce86d5fdc8d46a055ee2ed69fe18fb94cc293 [file] [log] [blame]
Emeric Brun107ca302010-01-04 16:16:05 +01001/*
Willy Tarreaucd3b0942012-04-27 21:52:18 +02002 * include/types/sample.h
3 * Macros, variables and structures for sample management.
Emeric Brun107ca302010-01-04 16:16:05 +01004 *
5 * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr>
Willy Tarreaucd3b0942012-04-27 21:52:18 +02006 * Copyright (C) 2012 Willy Tarreau <w@1wt.eu>
Emeric Brun107ca302010-01-04 16:16:05 +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
Willy Tarreaucd3b0942012-04-27 21:52:18 +020023#ifndef _TYPES_SAMPLE_H
24#define _TYPES_SAMPLE_H
Emeric Brun107ca302010-01-04 16:16:05 +010025
Emeric Brun107ca302010-01-04 16:16:05 +010026#include <sys/socket.h>
27#include <netinet/in.h>
Willy Tarreauc7e42382012-08-24 19:22:53 +020028
29#include <common/chunk.h>
Willy Tarreauecfb8e82012-04-20 12:29:52 +020030#include <types/arg.h>
Emeric Brun107ca302010-01-04 16:16:05 +010031
Willy Tarreau422aa072012-04-20 20:49:27 +020032/* input and output sample types */
Emeric Brun107ca302010-01-04 16:16:05 +010033enum {
Willy Tarreau422aa072012-04-20 20:49:27 +020034 SMP_T_BOOL = 0, /* boolean */
35 SMP_T_UINT, /* unsigned 32bits integer type */
36 SMP_T_SINT, /* signed 32bits integer type */
37 SMP_T_IPV4, /* ipv4 type */
38 SMP_T_IPV6, /* ipv6 type */
39 SMP_T_STR, /* char string type */
40 SMP_T_BIN, /* buffer type */
41 SMP_T_CSTR, /* constant char string type, data need dup before conversion */
42 SMP_T_CBIN, /* constant buffer type, data need dup before conversion */
43 SMP_TYPES /* number of types, must always be last */
Emeric Brun107ca302010-01-04 16:16:05 +010044};
45
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020046/* Sample fetch capabilities are used to declare keywords. Right now only
47 * the supportd fetch directions are specified.
48 */
49enum {
50 SMP_CAP_REQ = 1 << 0, /* fetch supported on request */
51 SMP_CAP_RES = 1 << 1, /* fetch supported on response */
52};
53
54/* Sample fetch options are passed to sample fetch functions to add precision
55 * about what is desired :
56 * - fetch direction (req/resp)
57 * - intermediary / final fetch
58 */
59enum {
60 SMP_OPT_DIR_REQ = 0, /* direction = request */
61 SMP_OPT_DIR_RES = 1, /* direction = response */
62 SMP_OPT_DIR = (SMP_OPT_DIR_REQ|SMP_OPT_DIR_RES), /* mask to get direction */
63 SMP_OPT_FINAL = 2, /* final fetch, contents won't change anymore */
Willy Tarreau7a777ed2012-04-26 11:44:02 +020064 SMP_OPT_ITERATE = 4, /* fetches may be iterated if supported (for ACLs) */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +020065};
66
Willy Tarreau16c31b02012-04-23 14:24:58 +020067/* Flags used to describe fetched samples. MAY_CHANGE indicates that the result
68 * of the fetch might still evolve, for instance because of more data expected,
69 * even if the fetch has failed. VOL_* indicates how long a result may be cached.
70 */
71enum {
72 SMP_F_NOT_LAST = 1 << 0, /* other occurrences might exist for this sample */
73 SMP_F_MAY_CHANGE = 1 << 1, /* sample is unstable and might change (eg: request length) */
74 SMP_F_VOL_TEST = 1 << 2, /* result must not survive longer than the test (eg: time) */
75 SMP_F_VOL_1ST = 1 << 3, /* result sensitive to changes in first line (eg: URI) */
76 SMP_F_VOL_HDR = 1 << 4, /* result sensitive to changes in headers */
77 SMP_F_VOL_TXN = 1 << 5, /* result sensitive to new transaction (eg: HTTP version) */
78 SMP_F_VOL_SESS = 1 << 6, /* result sensitive to new session (eg: src IP) */
79 SMP_F_VOLATILE = (1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6), /* any volatility condition */
Willy Tarreau16c31b02012-04-23 14:24:58 +020080};
Emeric Brun485479d2010-09-23 18:02:19 +020081
Willy Tarreauc7e42382012-08-24 19:22:53 +020082/* needed below */
83struct session;
84
Willy Tarreau16c31b02012-04-23 14:24:58 +020085/* a sample context might be used by any sample fetch function in order to
86 * store information needed across multiple calls (eg: restart point for a
87 * next occurrence). By definition it may store up to 8 pointers, or any
88 * scalar (double, int, long long).
89 */
90union smp_ctx {
91 void *p; /* any pointer */
92 int i; /* any integer */
93 long long ll; /* any long long or smaller */
94 double d; /* any float or double */
95 void *a[8]; /* any array of up to 8 pointers */
96};
97
98/* a sample is a typed data extracted from a stream. It has a type, contents,
99 * validity constraints, a context for use in iterative calls.
100 */
101struct sample {
102 unsigned int flags; /* SMP_F_* */
Willy Tarreau422aa072012-04-20 20:49:27 +0200103 int type; /* SMP_T_* */
Willy Tarreau342acb42012-04-23 22:03:39 +0200104 union {
105 unsigned int uint; /* used for unsigned 32bits integers and booleans */
106 int sint; /* used for signed 32bits integers */
107 struct in_addr ipv4; /* used for ipv4 addresses */
108 struct in6_addr ipv6; /* used for ipv6 addresses */
109 struct chunk str; /* used for char strings or buffers */
110 } data; /* sample data */
Willy Tarreau16c31b02012-04-23 14:24:58 +0200111 union smp_ctx ctx;
112};
113
Willy Tarreau12785782012-04-27 21:37:17 +0200114/* Descriptor for a sample conversion */
115struct sample_conv {
Emeric Brun107ca302010-01-04 16:16:05 +0100116 const char *kw; /* configuration keyword */
Willy Tarreauecfb8e82012-04-20 12:29:52 +0200117 int (*process)(const struct arg *arg_p,
Willy Tarreau342acb42012-04-23 22:03:39 +0200118 struct sample *smp); /* process function */
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200119 unsigned int arg_mask; /* arguments (ARG*()) */
Willy Tarreau21d68a62012-04-20 15:52:36 +0200120 int (*val_args)(struct arg *arg_p,
121 char **err_msg); /* argument validation function */
Willy Tarreau12785782012-04-27 21:37:17 +0200122 unsigned int in_type; /* expected input sample type */
123 unsigned int out_type; /* output sample type */
Emeric Brun107ca302010-01-04 16:16:05 +0100124};
125
Willy Tarreau12785782012-04-27 21:37:17 +0200126/* sample conversion expression */
127struct sample_conv_expr {
128 struct list list; /* member of a sample_expr */
129 struct sample_conv *conv; /* sample conversion used */
130 struct arg *arg_p; /* optional arguments */
Emeric Brun107ca302010-01-04 16:16:05 +0100131};
132
Willy Tarreau12785782012-04-27 21:37:17 +0200133/* Descriptor for a sample fetch method */
134struct sample_fetch {
Emeric Brun107ca302010-01-04 16:16:05 +0100135 const char *kw; /* configuration keyword */
136 int (*process)(struct proxy *px,
137 struct session *l4,
138 void *l7,
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200139 unsigned int opt, /* fetch options (SMP_OPT_*) */
140 const struct arg *arg_p,
Willy Tarreau342acb42012-04-23 22:03:39 +0200141 struct sample *smp); /* fetch processing function */
Willy Tarreau9fcb9842012-04-20 14:45:49 +0200142 unsigned int arg_mask; /* arguments (ARG*()) */
Willy Tarreau21d68a62012-04-20 15:52:36 +0200143 int (*val_args)(struct arg *arg_p,
144 char **err_msg); /* argument validation function */
Willy Tarreau12785782012-04-27 21:37:17 +0200145 unsigned long out_type; /* output sample type */
Willy Tarreau32a6f2e2012-04-25 10:13:36 +0200146 unsigned int cap; /* fetch capabilities (SMP_CAP_*) */
Emeric Brun107ca302010-01-04 16:16:05 +0100147};
148
Willy Tarreau12785782012-04-27 21:37:17 +0200149/* sample expression */
150struct sample_expr {
151 struct list list; /* member of list of sample, currently not used */
152 struct sample_fetch *fetch; /* sample fetch method */
153 struct arg *arg_p; /* optional pointer to arguments to fetch function */
Emeric Brun107ca302010-01-04 16:16:05 +0100154 struct list conv_exprs; /* list of conversion expression to apply */
155};
156
Willy Tarreau12785782012-04-27 21:37:17 +0200157/* sample fetch keywords list */
158struct sample_fetch_kw_list {
159 struct list list; /* head of sample fetch keyword list */
160 struct sample_fetch kw[VAR_ARRAY]; /* array of sample fetch descriptors */
Emeric Brun107ca302010-01-04 16:16:05 +0100161};
162
Willy Tarreau12785782012-04-27 21:37:17 +0200163/* sample conversion keywords list */
164struct sample_conv_kw_list {
165 struct list list; /* head of sample conversion keyword list */
166 struct sample_conv kw[VAR_ARRAY]; /* array of sample conversion descriptors */
Emeric Brun107ca302010-01-04 16:16:05 +0100167};
168
Willy Tarreaucd3b0942012-04-27 21:52:18 +0200169#endif /* _TYPES_SAMPLE_H */