blob: 9e8cc74f886032901d7cef8d12563d56a3b005be [file] [log] [blame]
Christopher Fauletd7c91962015-04-30 11:48:27 +02001/*
2 * include/proto/filters.h
3 * This file defines function prototypes for stream filters management.
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 _PROTO_FILTERS_H
22#define _PROTO_FILTERS_H
23
Willy Tarreauf1d32c42020-06-04 21:07:02 +020024#include <haproxy/channel.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020025#include <types/filters.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020026#include <types/http_ana.h>
Christopher Fauletd7c91962015-04-30 11:48:27 +020027#include <types/proxy.h>
28#include <types/stream.h>
29
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010030extern const char *trace_flt_id;
31extern const char *http_comp_flt_id;
32extern const char *cache_store_flt_id;
33extern const char *spoe_filter_id;
Christopher Faulet78fbb9f2019-08-11 23:11:03 +020034extern const char *fcgi_flt_id;
Christopher Fauletf4a4ef72018-12-07 17:39:53 +010035
Christopher Faulet443ea1a2016-02-04 13:40:26 +010036#define FLT_ID(flt) (flt)->config->id
37#define FLT_CONF(flt) (flt)->config->conf
38#define FLT_OPS(flt) (flt)->config->ops
39
Christopher Fauletd7c91962015-04-30 11:48:27 +020040/* Useful macros to access per-channel values. It can be safely used inside
41 * filters. */
42#define CHN_IDX(chn) (((chn)->flags & CF_ISRESP) == CF_ISRESP)
Christopher Faulet75bc9132018-11-30 15:18:09 +010043#define FLT_STRM_OFF(s, chn) (strm_flt(s)->offset[CHN_IDX(chn)])
44#define FLT_OFF(flt, chn) ((flt)->offset[CHN_IDX(chn)])
45
Christopher Fauletda02e172015-12-04 09:25:05 +010046#define HAS_FILTERS(strm) ((strm)->strm_flt.flags & STRM_FLT_FL_HAS_FILTERS)
Christopher Faulet3e344292015-11-24 16:24:13 +010047
Christopher Fauletda02e172015-12-04 09:25:05 +010048#define HAS_REQ_DATA_FILTERS(strm) ((strm)->strm_flt.nb_req_data_filters != 0)
49#define HAS_RSP_DATA_FILTERS(strm) ((strm)->strm_flt.nb_rsp_data_filters != 0)
Christopher Faulet4aad8332016-11-28 10:01:32 +010050#define HAS_DATA_FILTERS(strm, chn) (((chn)->flags & CF_ISRESP) ? HAS_RSP_DATA_FILTERS(strm) : HAS_REQ_DATA_FILTERS(strm))
Christopher Fauletda02e172015-12-04 09:25:05 +010051
52#define IS_REQ_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_REQ_DATA_FILTER)
53#define IS_RSP_DATA_FILTER(flt) ((flt)->flags & FLT_FL_IS_RSP_DATA_FILTER)
Christopher Faulet4aad8332016-11-28 10:01:32 +010054#define IS_DATA_FILTER(flt, chn) (((chn)->flags & CF_ISRESP) ? IS_RSP_DATA_FILTER(flt) : IS_REQ_DATA_FILTER(flt))
Christopher Fauletda02e172015-12-04 09:25:05 +010055
56#define FLT_STRM_CB(strm, call) \
Christopher Faulet3e344292015-11-24 16:24:13 +010057 do { \
58 if (HAS_FILTERS(strm)) { call; } \
59 } while (0)
Christopher Fauletda02e172015-12-04 09:25:05 +010060
61#define FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, default_ret) \
62 (HAS_DATA_FILTERS(strm, chn) ? call : default_ret)
63#define FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, default_ret, on_error) \
Christopher Faulet3e344292015-11-24 16:24:13 +010064 ({ \
65 int _ret; \
Christopher Fauletda02e172015-12-04 09:25:05 +010066 if (HAS_DATA_FILTERS(strm, chn)) { \
Christopher Faulet3e344292015-11-24 16:24:13 +010067 _ret = call; \
68 if (_ret < 0) { on_error; } \
69 } \
70 else \
71 _ret = default_ret; \
72 _ret; \
73 })
Christopher Fauletda02e172015-12-04 09:25:05 +010074#define FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, default_ret, on_error, on_wait) \
Christopher Faulet3e344292015-11-24 16:24:13 +010075 ({ \
76 int _ret; \
Christopher Fauletda02e172015-12-04 09:25:05 +010077 if (HAS_DATA_FILTERS(strm, chn)) { \
Christopher Faulet3e344292015-11-24 16:24:13 +010078 _ret = call; \
79 if (_ret < 0) { on_error; } \
80 if (!_ret) { on_wait; } \
81 } \
82 else \
83 _ret = default_ret; \
84 _ret; \
85 })
86
Christopher Fauletda02e172015-12-04 09:25:05 +010087#define FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, A, B, C, DATA_CB_IMPL, ...) \
88 DATA_CB_IMPL
Christopher Faulet3e344292015-11-24 16:24:13 +010089
Christopher Fauletda02e172015-12-04 09:25:05 +010090#define FLT_STRM_DATA_CB(strm, chn, call, ...) \
91 FLT_STRM_DATA_CB_IMPL_X(strm, chn, call, ##__VA_ARGS__, \
92 FLT_STRM_DATA_CB_IMPL_3(strm, chn, call, ##__VA_ARGS__), \
93 FLT_STRM_DATA_CB_IMPL_2(strm, chn, call, ##__VA_ARGS__), \
94 FLT_STRM_DATA_CB_IMPL_1(strm, chn, call, ##__VA_ARGS__))
Christopher Faulet3e344292015-11-24 16:24:13 +010095
Christopher Fauletd7c91962015-04-30 11:48:27 +020096void flt_deinit(struct proxy *p);
97int flt_check(struct proxy *p);
98
99int flt_stream_start(struct stream *s);
100void flt_stream_stop(struct stream *s);
Christopher Faulet92d36382015-11-05 13:35:03 +0100101int flt_set_stream_backend(struct stream *s, struct proxy *be);
102int flt_stream_init(struct stream *s);
103void flt_stream_release(struct stream *s, int only_backend);
Christopher Fauleta00d8172016-11-10 14:58:05 +0100104void flt_stream_check_timeouts(struct stream *s);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200105
Christopher Faulet75bc9132018-11-30 15:18:09 +0100106int flt_http_payload(struct stream *s, struct http_msg *msg, unsigned int len);
107int flt_http_end(struct stream *s, struct http_msg *msg);
108
Christopher Faulet309c6412015-12-02 09:57:32 +0100109void flt_http_reset(struct stream *s, struct http_msg *msg);
Willy Tarreau83061a82018-07-13 11:56:34 +0200110void flt_http_reply(struct stream *s, short status, const struct buffer *msg);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200111
112int flt_start_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Faulet3a394fa2016-05-11 17:13:39 +0200113int flt_pre_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
114int flt_post_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Faulet309c6412015-12-02 09:57:32 +0100115int flt_analyze_http_headers(struct stream *s, struct channel *chn, unsigned int an_bit);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200116int flt_end_analyze(struct stream *s, struct channel *chn, unsigned int an_bit);
117
118int flt_xfer_data(struct stream *s, struct channel *chn, unsigned int an_bit);
119
120void flt_register_keywords(struct flt_kw_list *kwl);
121struct flt_kw *flt_find_kw(const char *kw);
122void flt_dump_kws(char **out);
Christopher Fauletb3f4e142016-03-07 12:46:38 +0100123void list_filters(FILE *out);
Christopher Fauletd7c91962015-04-30 11:48:27 +0200124
Christopher Fauletfcf035c2015-12-03 11:48:03 +0100125/* Helper function that returns the "global" state of filters attached to a
126 * stream. */
127static inline struct strm_flt *
128strm_flt(struct stream *s)
129{
130 return &s->strm_flt;
131}
132
Christopher Fauletda02e172015-12-04 09:25:05 +0100133/* Registers a filter to a channel. If a filter was already registered, this
134 * function do nothing. Once registered, the filter becomes a "data" filter for
135 * this channel. */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200136static inline void
Christopher Fauletda02e172015-12-04 09:25:05 +0100137register_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200138{
Christopher Fauletda02e172015-12-04 09:25:05 +0100139 if (!IS_DATA_FILTER(filter, chn)) {
140 if (chn->flags & CF_ISRESP) {
141 filter->flags |= FLT_FL_IS_RSP_DATA_FILTER;
142 strm_flt(s)->nb_rsp_data_filters++;
143 }
144 else {
145 filter->flags |= FLT_FL_IS_REQ_DATA_FILTER;
146 strm_flt(s)->nb_req_data_filters++;
147 }
148 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200149}
150
Christopher Fauletda02e172015-12-04 09:25:05 +0100151/* Unregisters a "data" filter from a channel. */
Christopher Fauletd7c91962015-04-30 11:48:27 +0200152static inline void
Christopher Fauletda02e172015-12-04 09:25:05 +0100153unregister_data_filter(struct stream *s, struct channel *chn, struct filter *filter)
Christopher Fauletd7c91962015-04-30 11:48:27 +0200154{
Christopher Fauletda02e172015-12-04 09:25:05 +0100155 if (IS_DATA_FILTER(filter, chn)) {
156 if (chn->flags & CF_ISRESP) {
157 filter->flags &= ~FLT_FL_IS_RSP_DATA_FILTER;
158 strm_flt(s)->nb_rsp_data_filters--;
Christopher Fauletd7c91962015-04-30 11:48:27 +0200159
Christopher Fauletda02e172015-12-04 09:25:05 +0100160 }
161 else {
162 filter->flags &= ~FLT_FL_IS_REQ_DATA_FILTER;
163 strm_flt(s)->nb_req_data_filters--;
164 }
165 }
Christopher Fauletd7c91962015-04-30 11:48:27 +0200166}
167
Christopher Faulet75bc9132018-11-30 15:18:09 +0100168/* This function must be called when a filter alter payload data. It updates
169 * offsets of all previous filters and the offset of the stream. Do not call
170 * this function when a filter change the size of payload data leads to an
171 * undefined behavior.
172 *
173 * This is the filter's responsiblitiy to update data itself..
174 */
175static inline void
176flt_update_offsets(struct filter *filter, struct channel *chn, int len)
177{
178 struct stream *s = chn_strm(chn);
179 struct filter *f;
180
181 list_for_each_entry(f, &strm_flt(s)->filters, list) {
182 if (f == filter)
183 break;
184 if (IS_DATA_FILTER(filter, chn))
185 FLT_OFF(f, chn) += len;
186 }
187}
188
Christopher Fauletd7c91962015-04-30 11:48:27 +0200189
190#endif /* _PROTO_FILTERS_H */