Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/types/filteers.h |
| 3 | * This file defines everything related to stream filters. |
| 4 | * |
| 5 | * Copyright (C) 2015 Qualys Inc., Christopher Faulet <cfaulet@qualys.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | #ifndef _TYPES_FILTERS_H |
| 22 | #define _TYPES_FILTERS_H |
| 23 | |
| 24 | #include <common/config.h> |
| 25 | #include <common/mini-clist.h> |
| 26 | |
| 27 | struct http_msg; |
| 28 | struct proxy; |
| 29 | struct stream; |
| 30 | struct channel; |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 31 | struct flt_conf; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 32 | struct filter; |
| 33 | |
| 34 | /* Descriptor for a "filter" keyword. The ->parse() function returns 0 in case |
| 35 | * of success, or a combination of ERR_* flags if an error is encountered. The |
Christopher Faulet | b3f4e14 | 2016-03-07 12:46:38 +0100 | [diff] [blame] | 36 | * function pointer can be NULL if not implemented. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 37 | */ |
| 38 | struct flt_kw { |
| 39 | const char *kw; |
| 40 | int (*parse)(char **args, int *cur_arg, struct proxy *px, |
Thierry Fournier | 3610c39 | 2016-04-13 18:27:51 +0200 | [diff] [blame] | 41 | struct flt_conf *fconf, char **err, void *private); |
| 42 | void *private; |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /* |
| 46 | * A keyword list. It is a NULL-terminated array of keywords. It embeds a struct |
| 47 | * list in order to be linked to other lists, allowing it to easily be declared |
| 48 | * where it is needed, and linked without duplicating data nor allocating |
| 49 | * memory. It is also possible to indicate a scope for the keywords. |
| 50 | */ |
| 51 | struct flt_kw_list { |
| 52 | const char *scope; |
| 53 | struct list list; |
| 54 | struct flt_kw kw[VAR_ARRAY]; |
| 55 | }; |
| 56 | |
| 57 | /* |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 58 | * Callbacks available on a filter: |
| 59 | * |
| 60 | * - init : Initializes the filter for a proxy. Returns a |
| 61 | * negative value if an error occurs. |
| 62 | * - deinit : Cleans up what the init function has done. |
| 63 | * - check : Check the filter config for a proxy. Returns the |
| 64 | * number of errors encountered. |
| 65 | * |
| 66 | * |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 67 | * - attach : Called after a filter instance creation, when it is |
| 68 | * attached to a stream. This happens when the stream |
| 69 | * is started for filters defined on the stream's |
| 70 | * frontend and when the backend is set for filters |
| 71 | * declared on the stream's backend. |
| 72 | * Returns a negative value if an error occurs, 0 if |
| 73 | * the filter must be ignored for the stream, any other |
| 74 | * value otherwise. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 75 | * - stream_start : Called when a stream is started. This callback will |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 76 | * only be called for filters defined on the stream's |
| 77 | * frontend. |
| 78 | * Returns a negative value if an error occurs, any |
| 79 | * other value otherwise. |
| 80 | * - stream_set_backend : Called when a backend is set for a stream. This |
| 81 | * callbacks will be called for all filters attached |
| 82 | * to a stream (frontend and backend). |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 83 | * Returns a negative value if an error occurs, any |
| 84 | * other value otherwise. |
| 85 | * - stream_stop : Called when a stream is stopped. This callback will |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 86 | * only be called for filters defined on the stream's |
| 87 | * frontend. |
| 88 | * - detach : Called when a filter instance is detached from a |
| 89 | * stream, before its destruction. This happens when |
| 90 | * the stream is stopped for filters defined on the |
| 91 | * stream's frontend and when the analyze ends for |
| 92 | * filters defined on the stream's backend. |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 93 | * - check_timeouts : Called when a a stream is woken up because of an |
| 94 | * expired timer. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 95 | * |
| 96 | * |
| 97 | * - channel_start_analyze: Called when a filter starts to analyze a channel. |
| 98 | * Returns a negative value if an error occurs, 0 if |
| 99 | * it needs to wait, any other value otherwise. |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 100 | * - channel_pre_analyze : Called before each analyzer attached to a channel, |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 101 | * expects analyzers responsible for data sending. |
| 102 | * Returns a negative value if an error occurs, 0 if |
| 103 | * it needs to wait, any other value otherwise. |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 104 | * - channel_post_analyze: Called after each analyzer attached to a channel, |
| 105 | * expects analyzers responsible for data sending. |
| 106 | * Returns a negative value if an error occurs, |
| 107 | * any other value otherwise. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 108 | * - channel_end_analyze : Called when all other analyzers have finished their |
| 109 | * processing. |
| 110 | * Returns a negative value if an error occurs, 0 if |
| 111 | * it needs to wait, any other value otherwise. |
| 112 | * |
| 113 | * |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 114 | * - http_headers : Called before the body parsing, after all HTTP |
| 115 | * headers was parsed and analyzed. |
| 116 | * Returns a negative value if an error occurs, 0 if |
| 117 | * it needs to wait, any other value otherwise. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 118 | * - http_data : Called when unparsed body data are available. |
| 119 | * Returns a negative value if an error occurs, else |
| 120 | * the number of consumed bytes. |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 121 | * - http_chunk_trailers : Called when part of trailer headers of a |
| 122 | * chunk-encoded request/response are ready to be |
| 123 | * processed. |
| 124 | * Returns a negative value if an error occurs, any |
| 125 | * other value otherwise. |
| 126 | * - http_end : Called when all the request/response has been |
| 127 | * processed and all body data has been forwarded. |
| 128 | * Returns a negative value if an error occurs, 0 if |
| 129 | * it needs to wait for some reason, any other value |
| 130 | * otherwise. |
| 131 | * - http_reset : Called when the HTTP message is reseted. It happens |
| 132 | * when a 100-continue response is received. |
| 133 | * Returns nothing. |
| 134 | * - http_reply : Called when, at any time, HA proxy decides to stop |
| 135 | * the HTTP message's processing and to send a message |
| 136 | * to the client (mainly, when an error or a redirect |
| 137 | * occur). |
| 138 | * Returns nothing. |
| 139 | * - http_forward_data : Called when some data can be consumed. |
| 140 | * Returns a negative value if an error occurs, else |
| 141 | * the number of forwarded bytes. |
| 142 | * - tcp_data : Called when unparsed data are available. |
| 143 | * Returns a negative value if an error occurs, else |
| 144 | * the number of consumed bytes. |
| 145 | * - tcp_forward_data : Called when some data can be consumed. |
| 146 | * Returns a negative value if an error occurs, else |
| 147 | * or the number of forwarded bytes. |
| 148 | */ |
| 149 | struct flt_ops { |
| 150 | /* |
| 151 | * Callbacks to manage the filter lifecycle |
| 152 | */ |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 153 | int (*init) (struct proxy *p, struct flt_conf *fconf); |
| 154 | void (*deinit)(struct proxy *p, struct flt_conf *fconf); |
| 155 | int (*check) (struct proxy *p, struct flt_conf *fconf); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 156 | /* |
| 157 | * Stream callbacks |
| 158 | */ |
Christopher Faulet | 31ed32d | 2016-06-21 11:42:37 +0200 | [diff] [blame] | 159 | int (*attach) (struct stream *s, struct filter *f); |
| 160 | int (*stream_start) (struct stream *s, struct filter *f); |
| 161 | int (*stream_set_backend)(struct stream *s, struct filter *f, struct proxy *be); |
| 162 | void (*stream_stop) (struct stream *s, struct filter *f); |
| 163 | void (*detach) (struct stream *s, struct filter *f); |
Christopher Faulet | a00d817 | 2016-11-10 14:58:05 +0100 | [diff] [blame] | 164 | void (*check_timeouts) (struct stream *s, struct filter *f); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 165 | /* |
| 166 | * Channel callbacks |
| 167 | */ |
| 168 | int (*channel_start_analyze)(struct stream *s, struct filter *f, struct channel *chn); |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 169 | int (*channel_pre_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit); |
| 170 | int (*channel_post_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 171 | int (*channel_end_analyze) (struct stream *s, struct filter *f, struct channel *chn); |
| 172 | |
| 173 | /* |
| 174 | * HTTP callbacks |
| 175 | */ |
Christopher Faulet | 1339d74 | 2016-05-11 16:48:33 +0200 | [diff] [blame] | 176 | int (*http_headers) (struct stream *s, struct filter *f, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 177 | int (*http_data) (struct stream *s, struct filter *f, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 178 | int (*http_chunk_trailers)(struct stream *s, struct filter *f, struct http_msg *msg); |
| 179 | int (*http_end) (struct stream *s, struct filter *f, struct http_msg *msg); |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 180 | int (*http_forward_data) (struct stream *s, struct filter *f, struct http_msg *msg, |
| 181 | unsigned int len); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 182 | |
Christopher Faulet | 309c641 | 2015-12-02 09:57:32 +0100 | [diff] [blame] | 183 | void (*http_reset) (struct stream *s, struct filter *f, struct http_msg *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 184 | void (*http_reply) (struct stream *s, struct filter *f, short status, |
| 185 | const struct chunk *msg); |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 186 | |
| 187 | /* |
| 188 | * TCP callbacks |
| 189 | */ |
| 190 | int (*tcp_data) (struct stream *s, struct filter *f, struct channel *chn); |
| 191 | int (*tcp_forward_data)(struct stream *s, struct filter *f, struct channel *chn, |
| 192 | unsigned int len); |
| 193 | }; |
| 194 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 195 | /* Flags set on a filter instance */ |
| 196 | #define FLT_FL_IS_BACKEND_FILTER 0x0001 /* The filter is a backend filter */ |
| 197 | #define FLT_FL_IS_REQ_DATA_FILTER 0x0002 /* The filter will parse data on the request channel */ |
| 198 | #define FLT_FL_IS_RSP_DATA_FILTER 0x0004 /* The filter will parse data on the response channel */ |
| 199 | |
| 200 | |
| 201 | /* Flags set on the stream, common to all filters attached to its stream */ |
| 202 | #define STRM_FLT_FL_HAS_FILTERS 0x0001 /* The stream has at least one filter */ |
| 203 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 204 | /* |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 205 | * Structure representing the filter configuration, attached to a proxy and |
| 206 | * accessible from a filter when instantiated in a stream |
| 207 | */ |
| 208 | struct flt_conf { |
| 209 | const char *id; /* The filter id */ |
| 210 | struct flt_ops *ops; /* The filter callbacks */ |
| 211 | void *conf; /* The filter configuration */ |
| 212 | struct list list; /* Next filter for the same proxy */ |
| 213 | }; |
| 214 | |
| 215 | /* |
| 216 | * Structure reprensenting a filter instance attached to a stream |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 217 | * |
| 218 | * 2D-Array fields are used to store info per channel. The first index stands |
| 219 | * for the request channel, and the second one for the response channel. |
| 220 | * Especially, <next> and <fwd> are offets representing amount of data that the |
| 221 | * filter are, respectively, parsed and forwarded on a channel. Filters can |
| 222 | * access these values using FLT_NXT and FLT_FWD macros. |
| 223 | */ |
| 224 | struct filter { |
Christopher Faulet | 443ea1a | 2016-02-04 13:40:26 +0100 | [diff] [blame] | 225 | struct flt_conf *config; /* the filter's configuration */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 226 | void *ctx; /* The filter context (opaque) */ |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 227 | unsigned short flags; /* FLT_FL_* */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 228 | unsigned int next[2]; /* Offset, relative to buf->p, to the next byte to parse for a specific channel |
| 229 | * 0: request channel, 1: response channel */ |
| 230 | unsigned int fwd[2]; /* Offset, relative to buf->p, to the next byte to forward for a specific channel |
| 231 | * 0: request channel, 1: response channel */ |
Christopher Faulet | 3a394fa | 2016-05-11 17:13:39 +0200 | [diff] [blame] | 232 | unsigned int pre_analyzers; /* bit field indicating analyzers to pre-process */ |
| 233 | unsigned int post_analyzers; /* bit field indicating analyzers to post-process */ |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 234 | struct list list; /* Next filter for the same proxy/stream */ |
| 235 | }; |
| 236 | |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 237 | /* |
| 238 | * Structure reprensenting the "global" state of filters attached to a stream. |
| 239 | */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 240 | struct strm_flt { |
Christopher Faulet | da02e17 | 2015-12-04 09:25:05 +0100 | [diff] [blame] | 241 | struct list filters; /* List of filters attached to a stream */ |
| 242 | struct filter *current[2]; /* From which filter resume processing, for a specific channel. |
| 243 | * This is used for resumable callbacks only, |
| 244 | * If NULL, we start from the first filter. |
| 245 | * 0: request channel, 1: response channel */ |
| 246 | unsigned short flags; /* STRM_FL_* */ |
| 247 | unsigned char nb_req_data_filters; /* Number of data filters registerd on the request channel */ |
| 248 | unsigned char nb_rsp_data_filters; /* Number of data filters registerd on the response channel */ |
Christopher Faulet | fcf035c | 2015-12-03 11:48:03 +0100 | [diff] [blame] | 249 | }; |
| 250 | |
Christopher Faulet | d7c9196 | 2015-04-30 11:48:27 +0200 | [diff] [blame] | 251 | #endif /* _TYPES_FILTERS_H */ |
| 252 | |
| 253 | /* |
| 254 | * Local variables: |
| 255 | * c-indent-level: 8 |
| 256 | * c-basic-offset: 8 |
| 257 | * End: |
| 258 | */ |