blob: 62b457de7ad6d0888544dbad4377f8adf103fcc7 [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
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
27struct http_msg;
28struct proxy;
29struct stream;
30struct channel;
Christopher Faulet443ea1a2016-02-04 13:40:26 +010031struct flt_conf;
Christopher Fauletd7c91962015-04-30 11:48:27 +020032struct 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 Fauletb3f4e142016-03-07 12:46:38 +010036 * function pointer can be NULL if not implemented.
Christopher Fauletd7c91962015-04-30 11:48:27 +020037 */
38struct flt_kw {
39 const char *kw;
40 int (*parse)(char **args, int *cur_arg, struct proxy *px,
Thierry Fournier3610c392016-04-13 18:27:51 +020041 struct flt_conf *fconf, char **err, void *private);
42 void *private;
Christopher Fauletd7c91962015-04-30 11:48:27 +020043};
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 */
51struct flt_kw_list {
52 const char *scope;
53 struct list list;
54 struct flt_kw kw[VAR_ARRAY];
55};
56
57/*
Christopher Fauletd7c91962015-04-30 11:48:27 +020058 * 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 Faulet31ed32d2016-06-21 11:42:37 +020067 * - 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 Fauletd7c91962015-04-30 11:48:27 +020075 * - stream_start : Called when a stream is started. This callback will
Christopher Faulet31ed32d2016-06-21 11:42:37 +020076 * 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 Fauletd7c91962015-04-30 11:48:27 +020083 * 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 Faulet31ed32d2016-06-21 11:42:37 +020086 * 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 Fauletd7c91962015-04-30 11:48:27 +020093 *
94 *
95 * - channel_start_analyze: Called when a filter starts to analyze a channel.
96 * Returns a negative value if an error occurs, 0 if
97 * it needs to wait, any other value otherwise.
Christopher Faulet3a394fa2016-05-11 17:13:39 +020098 * - channel_pre_analyze : Called before each analyzer attached to a channel,
Christopher Fauletd7c91962015-04-30 11:48:27 +020099 * expects analyzers responsible for data sending.
100 * Returns a negative value if an error occurs, 0 if
101 * it needs to wait, any other value otherwise.
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200102 * - channel_post_analyze: Called after each analyzer attached to a channel,
103 * expects analyzers responsible for data sending.
104 * Returns a negative value if an error occurs,
105 * any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200106 * - channel_end_analyze : Called when all other analyzers have finished their
107 * processing.
108 * Returns a negative value if an error occurs, 0 if
109 * it needs to wait, any other value otherwise.
110 *
111 *
Christopher Faulet1339d742016-05-11 16:48:33 +0200112 * - http_headers : Called before the body parsing, after all HTTP
113 * headers was parsed and analyzed.
114 * Returns a negative value if an error occurs, 0 if
115 * it needs to wait, any other value otherwise.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200116 * - http_data : Called when unparsed body data are available.
117 * Returns a negative value if an error occurs, else
118 * the number of consumed bytes.
Christopher Fauletd7c91962015-04-30 11:48:27 +0200119 * - http_chunk_trailers : Called when part of trailer headers of a
120 * chunk-encoded request/response are ready to be
121 * processed.
122 * Returns a negative value if an error occurs, any
123 * other value otherwise.
124 * - http_end : Called when all the request/response has been
125 * processed and all body data has been forwarded.
126 * Returns a negative value if an error occurs, 0 if
127 * it needs to wait for some reason, any other value
128 * otherwise.
129 * - http_reset : Called when the HTTP message is reseted. It happens
130 * when a 100-continue response is received.
131 * Returns nothing.
132 * - http_reply : Called when, at any time, HA proxy decides to stop
133 * the HTTP message's processing and to send a message
134 * to the client (mainly, when an error or a redirect
135 * occur).
136 * Returns nothing.
137 * - http_forward_data : Called when some data can be consumed.
138 * Returns a negative value if an error occurs, else
139 * the number of forwarded bytes.
140 * - tcp_data : Called when unparsed data are available.
141 * Returns a negative value if an error occurs, else
142 * the number of consumed bytes.
143 * - tcp_forward_data : Called when some data can be consumed.
144 * Returns a negative value if an error occurs, else
145 * or the number of forwarded bytes.
146 */
147struct flt_ops {
148 /*
149 * Callbacks to manage the filter lifecycle
150 */
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100151 int (*init) (struct proxy *p, struct flt_conf *fconf);
152 void (*deinit)(struct proxy *p, struct flt_conf *fconf);
153 int (*check) (struct proxy *p, struct flt_conf *fconf);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200154 /*
155 * Stream callbacks
156 */
Christopher Faulet31ed32d2016-06-21 11:42:37 +0200157 int (*attach) (struct stream *s, struct filter *f);
158 int (*stream_start) (struct stream *s, struct filter *f);
159 int (*stream_set_backend)(struct stream *s, struct filter *f, struct proxy *be);
160 void (*stream_stop) (struct stream *s, struct filter *f);
161 void (*detach) (struct stream *s, struct filter *f);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200162
163 /*
164 * Channel callbacks
165 */
166 int (*channel_start_analyze)(struct stream *s, struct filter *f, struct channel *chn);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200167 int (*channel_pre_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit);
168 int (*channel_post_analyze) (struct stream *s, struct filter *f, struct channel *chn, unsigned int an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200169 int (*channel_end_analyze) (struct stream *s, struct filter *f, struct channel *chn);
170
171 /*
172 * HTTP callbacks
173 */
Christopher Faulet1339d742016-05-11 16:48:33 +0200174 int (*http_headers) (struct stream *s, struct filter *f, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200175 int (*http_data) (struct stream *s, struct filter *f, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200176 int (*http_chunk_trailers)(struct stream *s, struct filter *f, struct http_msg *msg);
177 int (*http_end) (struct stream *s, struct filter *f, struct http_msg *msg);
Christopher Faulet309c6412015-12-02 09:57:32 +0100178 int (*http_forward_data) (struct stream *s, struct filter *f, struct http_msg *msg,
179 unsigned int len);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200180
Christopher Faulet309c6412015-12-02 09:57:32 +0100181 void (*http_reset) (struct stream *s, struct filter *f, struct http_msg *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200182 void (*http_reply) (struct stream *s, struct filter *f, short status,
183 const struct chunk *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200184
185 /*
186 * TCP callbacks
187 */
188 int (*tcp_data) (struct stream *s, struct filter *f, struct channel *chn);
189 int (*tcp_forward_data)(struct stream *s, struct filter *f, struct channel *chn,
190 unsigned int len);
191};
192
Christopher Fauletda02e172015-12-04 09:25:05 +0100193/* Flags set on a filter instance */
194#define FLT_FL_IS_BACKEND_FILTER 0x0001 /* The filter is a backend filter */
195#define FLT_FL_IS_REQ_DATA_FILTER 0x0002 /* The filter will parse data on the request channel */
196#define FLT_FL_IS_RSP_DATA_FILTER 0x0004 /* The filter will parse data on the response channel */
197
198
199/* Flags set on the stream, common to all filters attached to its stream */
200#define STRM_FLT_FL_HAS_FILTERS 0x0001 /* The stream has at least one filter */
201
Christopher Fauletd7c91962015-04-30 11:48:27 +0200202/*
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100203 * Structure representing the filter configuration, attached to a proxy and
204 * accessible from a filter when instantiated in a stream
205 */
206struct flt_conf {
207 const char *id; /* The filter id */
208 struct flt_ops *ops; /* The filter callbacks */
209 void *conf; /* The filter configuration */
210 struct list list; /* Next filter for the same proxy */
211};
212
213/*
214 * Structure reprensenting a filter instance attached to a stream
Christopher Fauletd7c91962015-04-30 11:48:27 +0200215 *
216 * 2D-Array fields are used to store info per channel. The first index stands
217 * for the request channel, and the second one for the response channel.
218 * Especially, <next> and <fwd> are offets representing amount of data that the
219 * filter are, respectively, parsed and forwarded on a channel. Filters can
220 * access these values using FLT_NXT and FLT_FWD macros.
221 */
222struct filter {
Christopher Faulet443ea1a2016-02-04 13:40:26 +0100223 struct flt_conf *config; /* the filter's configuration */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200224 void *ctx; /* The filter context (opaque) */
Christopher Fauletda02e172015-12-04 09:25:05 +0100225 unsigned short flags; /* FLT_FL_* */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200226 unsigned int next[2]; /* Offset, relative to buf->p, to the next byte to parse for a specific channel
227 * 0: request channel, 1: response channel */
228 unsigned int fwd[2]; /* Offset, relative to buf->p, to the next byte to forward for a specific channel
229 * 0: request channel, 1: response channel */
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200230 unsigned int pre_analyzers; /* bit field indicating analyzers to pre-process */
231 unsigned int post_analyzers; /* bit field indicating analyzers to post-process */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200232 struct list list; /* Next filter for the same proxy/stream */
233};
234
Christopher Fauletda02e172015-12-04 09:25:05 +0100235/*
236 * Structure reprensenting the "global" state of filters attached to a stream.
237 */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100238struct strm_flt {
Christopher Fauletda02e172015-12-04 09:25:05 +0100239 struct list filters; /* List of filters attached to a stream */
240 struct filter *current[2]; /* From which filter resume processing, for a specific channel.
241 * This is used for resumable callbacks only,
242 * If NULL, we start from the first filter.
243 * 0: request channel, 1: response channel */
244 unsigned short flags; /* STRM_FL_* */
245 unsigned char nb_req_data_filters; /* Number of data filters registerd on the request channel */
246 unsigned char nb_rsp_data_filters; /* Number of data filters registerd on the response channel */
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100247};
248
Christopher Fauletd7c91962015-04-30 11:48:27 +0200249#endif /* _TYPES_FILTERS_H */
250
251/*
252 * Local variables:
253 * c-indent-level: 8
254 * c-basic-offset: 8
255 * End:
256 */