Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 1 | /* |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 2 | * include/types/sample.h |
| 3 | * Macros, variables and structures for sample management. |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2009-2010 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 6 | * Copyright (C) 2012 Willy Tarreau <w@1wt.eu> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 7 | * |
| 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 Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 23 | #ifndef _TYPES_SAMPLE_H |
| 24 | #define _TYPES_SAMPLE_H |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 25 | |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 26 | #include <sys/socket.h> |
| 27 | #include <netinet/in.h> |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 28 | |
| 29 | #include <common/chunk.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 30 | #include <common/mini-clist.h> |
Willy Tarreau | ecfb8e8 | 2012-04-20 12:29:52 +0200 | [diff] [blame] | 31 | #include <types/arg.h> |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 32 | |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 33 | /* input and output sample types */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 34 | enum { |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 35 | SMP_T_BOOL = 0, /* boolean */ |
| 36 | SMP_T_UINT, /* unsigned 32bits integer type */ |
| 37 | SMP_T_SINT, /* signed 32bits integer type */ |
| 38 | SMP_T_IPV4, /* ipv4 type */ |
| 39 | SMP_T_IPV6, /* ipv6 type */ |
| 40 | SMP_T_STR, /* char string type */ |
| 41 | SMP_T_BIN, /* buffer type */ |
| 42 | SMP_T_CSTR, /* constant char string type, data need dup before conversion */ |
| 43 | SMP_T_CBIN, /* constant buffer type, data need dup before conversion */ |
| 44 | SMP_TYPES /* number of types, must always be last */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 45 | }; |
| 46 | |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 47 | /* Sample fetch capabilities are used to declare keywords. Right now only |
| 48 | * the supportd fetch directions are specified. |
| 49 | */ |
| 50 | enum { |
| 51 | SMP_CAP_REQ = 1 << 0, /* fetch supported on request */ |
| 52 | SMP_CAP_RES = 1 << 1, /* fetch supported on response */ |
Willy Tarreau | 1b6c00c | 2012-10-05 22:41:26 +0200 | [diff] [blame] | 53 | SMP_CAP_L7 = 1 << 2, /* fetch may require access to L7 */ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /* Sample fetch options are passed to sample fetch functions to add precision |
| 57 | * about what is desired : |
| 58 | * - fetch direction (req/resp) |
| 59 | * - intermediary / final fetch |
| 60 | */ |
| 61 | enum { |
| 62 | SMP_OPT_DIR_REQ = 0, /* direction = request */ |
| 63 | SMP_OPT_DIR_RES = 1, /* direction = response */ |
| 64 | SMP_OPT_DIR = (SMP_OPT_DIR_REQ|SMP_OPT_DIR_RES), /* mask to get direction */ |
| 65 | SMP_OPT_FINAL = 2, /* final fetch, contents won't change anymore */ |
Willy Tarreau | 7a777ed | 2012-04-26 11:44:02 +0200 | [diff] [blame] | 66 | SMP_OPT_ITERATE = 4, /* fetches may be iterated if supported (for ACLs) */ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 67 | }; |
| 68 | |
Willy Tarreau | 16c31b0 | 2012-04-23 14:24:58 +0200 | [diff] [blame] | 69 | /* Flags used to describe fetched samples. MAY_CHANGE indicates that the result |
| 70 | * of the fetch might still evolve, for instance because of more data expected, |
| 71 | * even if the fetch has failed. VOL_* indicates how long a result may be cached. |
| 72 | */ |
| 73 | enum { |
| 74 | SMP_F_NOT_LAST = 1 << 0, /* other occurrences might exist for this sample */ |
| 75 | SMP_F_MAY_CHANGE = 1 << 1, /* sample is unstable and might change (eg: request length) */ |
| 76 | SMP_F_VOL_TEST = 1 << 2, /* result must not survive longer than the test (eg: time) */ |
| 77 | SMP_F_VOL_1ST = 1 << 3, /* result sensitive to changes in first line (eg: URI) */ |
| 78 | SMP_F_VOL_HDR = 1 << 4, /* result sensitive to changes in headers */ |
| 79 | SMP_F_VOL_TXN = 1 << 5, /* result sensitive to new transaction (eg: HTTP version) */ |
| 80 | SMP_F_VOL_SESS = 1 << 6, /* result sensitive to new session (eg: src IP) */ |
| 81 | SMP_F_VOLATILE = (1<<2)|(1<<3)|(1<<4)|(1<<5)|(1<<6), /* any volatility condition */ |
Willy Tarreau | 16c31b0 | 2012-04-23 14:24:58 +0200 | [diff] [blame] | 82 | }; |
Emeric Brun | 485479d | 2010-09-23 18:02:19 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | c7e4238 | 2012-08-24 19:22:53 +0200 | [diff] [blame] | 84 | /* needed below */ |
| 85 | struct session; |
| 86 | |
Willy Tarreau | 16c31b0 | 2012-04-23 14:24:58 +0200 | [diff] [blame] | 87 | /* a sample context might be used by any sample fetch function in order to |
| 88 | * store information needed across multiple calls (eg: restart point for a |
| 89 | * next occurrence). By definition it may store up to 8 pointers, or any |
| 90 | * scalar (double, int, long long). |
| 91 | */ |
| 92 | union smp_ctx { |
| 93 | void *p; /* any pointer */ |
| 94 | int i; /* any integer */ |
| 95 | long long ll; /* any long long or smaller */ |
| 96 | double d; /* any float or double */ |
| 97 | void *a[8]; /* any array of up to 8 pointers */ |
| 98 | }; |
| 99 | |
| 100 | /* a sample is a typed data extracted from a stream. It has a type, contents, |
| 101 | * validity constraints, a context for use in iterative calls. |
| 102 | */ |
| 103 | struct sample { |
| 104 | unsigned int flags; /* SMP_F_* */ |
Willy Tarreau | 422aa07 | 2012-04-20 20:49:27 +0200 | [diff] [blame] | 105 | int type; /* SMP_T_* */ |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 106 | union { |
| 107 | unsigned int uint; /* used for unsigned 32bits integers and booleans */ |
| 108 | int sint; /* used for signed 32bits integers */ |
| 109 | struct in_addr ipv4; /* used for ipv4 addresses */ |
| 110 | struct in6_addr ipv6; /* used for ipv6 addresses */ |
| 111 | struct chunk str; /* used for char strings or buffers */ |
| 112 | } data; /* sample data */ |
Willy Tarreau | 16c31b0 | 2012-04-23 14:24:58 +0200 | [diff] [blame] | 113 | union smp_ctx ctx; |
| 114 | }; |
| 115 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 116 | /* Descriptor for a sample conversion */ |
| 117 | struct sample_conv { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 118 | const char *kw; /* configuration keyword */ |
Willy Tarreau | ecfb8e8 | 2012-04-20 12:29:52 +0200 | [diff] [blame] | 119 | int (*process)(const struct arg *arg_p, |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 120 | struct sample *smp); /* process function */ |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 121 | unsigned int arg_mask; /* arguments (ARG*()) */ |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 122 | int (*val_args)(struct arg *arg_p, |
| 123 | char **err_msg); /* argument validation function */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 124 | unsigned int in_type; /* expected input sample type */ |
| 125 | unsigned int out_type; /* output sample type */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 126 | }; |
| 127 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 128 | /* sample conversion expression */ |
| 129 | struct sample_conv_expr { |
| 130 | struct list list; /* member of a sample_expr */ |
| 131 | struct sample_conv *conv; /* sample conversion used */ |
| 132 | struct arg *arg_p; /* optional arguments */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 133 | }; |
| 134 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 135 | /* Descriptor for a sample fetch method */ |
| 136 | struct sample_fetch { |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 137 | const char *kw; /* configuration keyword */ |
| 138 | int (*process)(struct proxy *px, |
| 139 | struct session *l4, |
| 140 | void *l7, |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 141 | unsigned int opt, /* fetch options (SMP_OPT_*) */ |
| 142 | const struct arg *arg_p, |
Willy Tarreau | 342acb4 | 2012-04-23 22:03:39 +0200 | [diff] [blame] | 143 | struct sample *smp); /* fetch processing function */ |
Willy Tarreau | 9fcb984 | 2012-04-20 14:45:49 +0200 | [diff] [blame] | 144 | unsigned int arg_mask; /* arguments (ARG*()) */ |
Willy Tarreau | 21d68a6 | 2012-04-20 15:52:36 +0200 | [diff] [blame] | 145 | int (*val_args)(struct arg *arg_p, |
| 146 | char **err_msg); /* argument validation function */ |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 147 | unsigned long out_type; /* output sample type */ |
Willy Tarreau | 32a6f2e | 2012-04-25 10:13:36 +0200 | [diff] [blame] | 148 | unsigned int cap; /* fetch capabilities (SMP_CAP_*) */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 149 | }; |
| 150 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 151 | /* sample expression */ |
| 152 | struct sample_expr { |
| 153 | struct list list; /* member of list of sample, currently not used */ |
| 154 | struct sample_fetch *fetch; /* sample fetch method */ |
| 155 | struct arg *arg_p; /* optional pointer to arguments to fetch function */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 156 | struct list conv_exprs; /* list of conversion expression to apply */ |
| 157 | }; |
| 158 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 159 | /* sample fetch keywords list */ |
| 160 | struct sample_fetch_kw_list { |
| 161 | struct list list; /* head of sample fetch keyword list */ |
| 162 | struct sample_fetch kw[VAR_ARRAY]; /* array of sample fetch descriptors */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 163 | }; |
| 164 | |
Willy Tarreau | 1278578 | 2012-04-27 21:37:17 +0200 | [diff] [blame] | 165 | /* sample conversion keywords list */ |
| 166 | struct sample_conv_kw_list { |
| 167 | struct list list; /* head of sample conversion keyword list */ |
| 168 | struct sample_conv kw[VAR_ARRAY]; /* array of sample conversion descriptors */ |
Emeric Brun | 107ca30 | 2010-01-04 16:16:05 +0100 | [diff] [blame] | 169 | }; |
| 170 | |
Willy Tarreau | cd3b094 | 2012-04-27 21:52:18 +0200 | [diff] [blame] | 171 | #endif /* _TYPES_SAMPLE_H */ |