blob: edbb459048936792b30e0a2b37cf6fbee29f9421 [file] [log] [blame]
Christopher Faulet1f40b912017-02-17 09:32:19 +01001/*
2 * include/types/spoe.h
3 * Macros, variables and structures for the SPOE filter.
4 *
5 * Copyright (C) 2017 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.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
22#ifndef _TYPES_SPOE_H
23#define _TYPES_SPOE_H
24
25#include <common/buffer.h>
26#include <common/mini-clist.h>
27
28#include <types/filters.h>
29#include <types/freq_ctr.h>
30#include <types/proxy.h>
31#include <types/sample.h>
32#include <types/stream.h>
33#include <types/task.h>
34
35/* Flags set on the SPOE agent */
36#define SPOE_FL_CONT_ON_ERR 0x00000001 /* Do not stop events processing when an error occurred */
Christopher Faulet305c6072017-02-23 16:17:53 +010037#define SPOE_FL_PIPELINING 0x00000002 /* Set when SPOE agent supports pipelining (set by default) */
38#define SPOE_FL_ASYNC 0x00000004 /* Set when SPOE agent supports async (set by default) */
Christopher Fauletcecd8522017-02-24 22:11:21 +010039#define SPOE_FL_SND_FRAGMENTATION 0x00000008 /* Set when SPOE agent supports sending fragmented payload */
40#define SPOE_FL_RCV_FRAGMENTATION 0x00000010 /* Set when SPOE agent supports receiving fragmented payload */
Christopher Faulet1f40b912017-02-17 09:32:19 +010041
42/* Flags set on the SPOE context */
43#define SPOE_CTX_FL_CLI_CONNECTED 0x00000001 /* Set after that on-client-session event was processed */
44#define SPOE_CTX_FL_SRV_CONNECTED 0x00000002 /* Set after that on-server-session event was processed */
45#define SPOE_CTX_FL_REQ_PROCESS 0x00000004 /* Set when SPOE is processing the request */
46#define SPOE_CTX_FL_RSP_PROCESS 0x00000008 /* Set when SPOE is processing the response */
47#define SPOE_CTX_FL_FRAGMENTED 0x00000010 /* Set when a fragmented frame is processing */
48
49#define SPOE_CTX_FL_PROCESS (SPOE_CTX_FL_REQ_PROCESS|SPOE_CTX_FL_RSP_PROCESS)
50
51/* Flags set on the SPOE applet */
52#define SPOE_APPCTX_FL_PIPELINING 0x00000001 /* Set if pipelining is supported */
53#define SPOE_APPCTX_FL_ASYNC 0x00000002 /* Set if asynchronus frames is supported */
54#define SPOE_APPCTX_FL_FRAGMENTATION 0x00000004 /* Set if fragmentation is supported */
55#define SPOE_APPCTX_FL_PERSIST 0x00000008 /* Set if the applet is persistent */
56
57#define SPOE_APPCTX_ERR_NONE 0x00000000 /* no error yet, leave it to zero */
58#define SPOE_APPCTX_ERR_TOUT 0x00000001 /* SPOE applet timeout */
59
60/* Flags set on the SPOE frame */
61#define SPOE_FRM_FL_FIN 0x00000001
62#define SPOE_FRM_FL_ABRT 0x00000002
63
64/* All possible states for a SPOE context */
65enum spoe_ctx_state {
66 SPOE_CTX_ST_NONE = 0,
67 SPOE_CTX_ST_READY,
68 SPOE_CTX_ST_ENCODING_MSGS,
69 SPOE_CTX_ST_SENDING_MSGS,
70 SPOE_CTX_ST_WAITING_ACK,
71 SPOE_CTX_ST_DONE,
72 SPOE_CTX_ST_ERROR,
73};
74
75/* All possible states for a SPOE applet */
76enum spoe_appctx_state {
77 SPOE_APPCTX_ST_CONNECT = 0,
78 SPOE_APPCTX_ST_CONNECTING,
79 SPOE_APPCTX_ST_IDLE,
80 SPOE_APPCTX_ST_PROCESSING,
81 SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY,
82 SPOE_APPCTX_ST_WAITING_SYNC_ACK,
83 SPOE_APPCTX_ST_DISCONNECT,
84 SPOE_APPCTX_ST_DISCONNECTING,
85 SPOE_APPCTX_ST_EXIT,
86 SPOE_APPCTX_ST_END,
87};
88
89/* All supported SPOE actions */
90enum spoe_action_type {
91 SPOE_ACT_T_SET_VAR = 1,
92 SPOE_ACT_T_UNSET_VAR,
93 SPOE_ACT_TYPES,
94};
95
96/* All supported SPOE events */
97enum spoe_event {
98 SPOE_EV_NONE = 0,
99
100 /* Request events */
101 SPOE_EV_ON_CLIENT_SESS = 1,
102 SPOE_EV_ON_TCP_REQ_FE,
103 SPOE_EV_ON_TCP_REQ_BE,
104 SPOE_EV_ON_HTTP_REQ_FE,
105 SPOE_EV_ON_HTTP_REQ_BE,
106
107 /* Response events */
108 SPOE_EV_ON_SERVER_SESS,
109 SPOE_EV_ON_TCP_RSP,
110 SPOE_EV_ON_HTTP_RSP,
111
112 SPOE_EV_EVENTS
113};
114
115/* Errors triggered by streams */
116enum spoe_context_error {
117 SPOE_CTX_ERR_NONE = 0,
118 SPOE_CTX_ERR_TOUT,
119 SPOE_CTX_ERR_RES,
120 SPOE_CTX_ERR_TOO_BIG,
121 SPOE_CTX_ERR_FRAG_FRAME_ABRT,
122 SPOE_CTX_ERR_UNKNOWN = 255,
123 SPOE_CTX_ERRS,
124};
125
126/* Errors triggerd by SPOE applet */
127enum spoe_frame_error {
128 SPOE_FRM_ERR_NONE = 0,
129 SPOE_FRM_ERR_IO,
130 SPOE_FRM_ERR_TOUT,
131 SPOE_FRM_ERR_TOO_BIG,
132 SPOE_FRM_ERR_INVALID,
133 SPOE_FRM_ERR_NO_VSN,
134 SPOE_FRM_ERR_NO_FRAME_SIZE,
135 SPOE_FRM_ERR_NO_CAP,
136 SPOE_FRM_ERR_BAD_VSN,
137 SPOE_FRM_ERR_BAD_FRAME_SIZE,
138 SPOE_FRM_ERR_FRAG_NOT_SUPPORTED,
139 SPOE_FRM_ERR_INTERLACED_FRAMES,
140 SPOE_FRM_ERR_FRAMEID_NOTFOUND,
141 SPOE_FRM_ERR_RES,
142 SPOE_FRM_ERR_UNKNOWN = 99,
143 SPOE_FRM_ERRS,
144};
145
146/* Scopes used for variables set by agents. It is a way to be agnotic to vars
147 * scope. */
148enum spoe_vars_scope {
149 SPOE_SCOPE_PROC = 0, /* <=> SCOPE_PROC */
150 SPOE_SCOPE_SESS, /* <=> SCOPE_SESS */
151 SPOE_SCOPE_TXN, /* <=> SCOPE_TXN */
152 SPOE_SCOPE_REQ, /* <=> SCOPE_REQ */
153 SPOE_SCOPE_RES, /* <=> SCOPE_RES */
154};
155
156
157/* Describe an argument that will be linked to a message. It is a sample fetch,
158 * with an optional name. */
159struct spoe_arg {
160 char *name; /* Name of the argument, may be NULL */
161 unsigned int name_len; /* The name length, 0 if NULL */
162 struct sample_expr *expr; /* Sample expression */
163 struct list list; /* Used to chain SPOE args */
164};
165
166/* Used during the config parsing only because, when a SPOE agent section is
167 * parsed, messages can be undefined. */
168struct spoe_msg_placeholder {
169 char *id; /* SPOE message placeholder id */
170 struct list list; /* Use to chain SPOE message placeholders */
171};
172
173/* Describe a message that will be sent in a NOTIFY frame. A message has a name,
174 * an argument list (see above) and it is linked to a specific event. */
175struct spoe_message {
176 char *id; /* SPOE message id */
177 unsigned int id_len; /* The message id length */
178 struct spoe_agent *agent; /* SPOE agent owning this SPOE message */
179 struct {
180 char *file; /* file where the SPOE message appears */
181 int line; /* line where the SPOE message appears */
182 } conf; /* config information */
183 unsigned int nargs; /* # of arguments */
184 struct list args; /* Arguments added when the SPOE messages is sent */
185 struct list list; /* Used to chain SPOE messages */
186
187 enum spoe_event event; /* SPOE_EV_* */
188};
189
190/* Describe a SPOE agent. */
191struct spoe_agent {
192 char *id; /* SPOE agent id (name) */
193 struct {
194 char *file; /* file where the SPOE agent appears */
195 int line; /* line where the SPOE agent appears */
196 } conf; /* config information */
197 union {
198 struct proxy *be; /* Backend used by this agent */
199 char *name; /* Backend name used during conf parsing */
200 } b;
201 struct {
202 unsigned int hello; /* Max time to receive AGENT-HELLO frame (in SPOE applet) */
203 unsigned int idle; /* Max Idle timeout (in SPOE applet) */
204 unsigned int processing; /* Max time to process an event (in the main stream) */
205 } timeout;
206
207 /* Config info */
208 char *engine_id; /* engine-id string */
209 char *var_pfx; /* Prefix used for vars set by the agent */
210 char *var_on_error; /* Variable to set when an error occurred, in the TXN scope */
211 unsigned int flags; /* SPOE_FL_* */
212 unsigned int cps_max; /* Maximum # of connections per second */
213 unsigned int eps_max; /* Maximum # of errors per second */
214 unsigned int max_frame_size; /* Maximum frame size for this agent, before any negotiation */
215 unsigned int min_applets; /* Minimum # applets alive at a time */
216 unsigned int max_fpa; /* Maximum # of frames handled per applet at once */
217
218 struct list messages[SPOE_EV_EVENTS]; /* List of SPOE messages that will be sent
219 * for each supported events */
220
221 /* running info */
222 unsigned int frame_size; /* current maximum frame size, only used to encode messages */
223 unsigned int applets_act; /* # of applets alive at a time */
224 unsigned int applets_idle; /* # of applets in the state SPOE_APPCTX_ST_IDLE */
225 unsigned int sending_rate; /* the global sending rate */
226
227 struct freq_ctr conn_per_sec; /* connections per second */
228 struct freq_ctr err_per_sec; /* connetion errors per second */
229
230 struct list applets; /* List of available SPOE applets */
231 struct list sending_queue; /* Queue of streams waiting to send data */
232 struct list waiting_queue; /* Queue of streams waiting for a ack, in async mode */
233
234};
235
236/* SPOE filter configuration */
237struct spoe_config {
238 struct proxy *proxy; /* Proxy owning the filter */
239 struct spoe_agent *agent; /* Agent used by this filter */
240 struct proxy agent_fe; /* Agent frontend */
241};
242
243/* SPOE context attached to a stream. It is the main structure that handles the
244 * processing offload */
245struct spoe_context {
246 struct filter *filter; /* The SPOE filter */
247 struct stream *strm; /* The stream that should be offloaded */
248
249 struct list *messages; /* List of messages that will be sent during the stream processing */
250 struct buffer *buffer; /* Buffer used to store a encoded messages */
251 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
252 struct list list;
253
254 enum spoe_ctx_state state; /* SPOE_CTX_ST_* */
255 unsigned int flags; /* SPOE_CTX_FL_* */
256 unsigned int status_code; /* SPOE_CTX_ERR_* */
257
258 unsigned int stream_id; /* stream_id and frame_id are used */
259 unsigned int frame_id; /* to map NOTIFY and ACK frames */
260 unsigned int process_exp; /* expiration date to process an event */
261
262 struct {
263 struct spoe_appctx *spoe_appctx; /* SPOE appctx sending the fragmented frame */
264 struct spoe_message *curmsg; /* SPOE message from which to resume encoding */
265 struct spoe_arg *curarg; /* SPOE arg in <curmsg> from which to resume encoding */
266 unsigned int curoff; /* offset in <curarg> from which to resume encoding */
267 unsigned int flags; /* SPOE_FRM_FL_* */
268 } frag_ctx; /* Info about fragmented frames, valid on if SPOE_CTX_FL_FRAGMENTED is set */
269};
270
271/* SPOE context inside a appctx */
272struct spoe_appctx {
273 struct appctx *owner; /* the owner */
274 struct task *task; /* task to handle applet timeouts */
275 struct spoe_agent *agent; /* agent on which the applet is attached */
276
277 unsigned int version; /* the negotiated version */
278 unsigned int max_frame_size; /* the negotiated max-frame-size value */
279 unsigned int flags; /* SPOE_APPCTX_FL_* */
280
281 unsigned int status_code; /* SPOE_FRM_ERR_* */
282#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
283 char *reason; /* Error message, used for debugging only */
284 int rlen; /* reason length */
285#endif
286
287 struct buffer *buffer; /* Buffer used to store a encoded messages */
288 struct buffer_wait buffer_wait; /* position in the list of ressources waiting for a buffer */
289 struct list waiting_queue; /* list of streams waiting for a ACK frame, in sync and pipelining mode */
290 struct list list; /* next spoe appctx for the same agent */
291
292 struct {
293 struct spoe_context *ctx; /* SPOE context owning the fragmented frame */
294 unsigned int cursid; /* stream-id of the fragmented frame. used if the processing is aborted */
295 unsigned int curfid; /* frame-id of the fragmented frame. used if the processing is aborted */
296 } frag_ctx; /* Info about fragmented frames, unused for unfragmented frames */
297};
298
299/* Frame Types sent by HAProxy and by agents */
300enum spoe_frame_type {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100301 SPOE_FRM_T_UNSET = 0,
302
Christopher Faulet1f40b912017-02-17 09:32:19 +0100303 /* Frames sent by HAProxy */
304 SPOE_FRM_T_HAPROXY_HELLO = 1,
305 SPOE_FRM_T_HAPROXY_DISCON,
306 SPOE_FRM_T_HAPROXY_NOTIFY,
307
308 /* Frames sent by the agents */
309 SPOE_FRM_T_AGENT_HELLO = 101,
310 SPOE_FRM_T_AGENT_DISCON,
311 SPOE_FRM_T_AGENT_ACK
312};
313
314/* All supported data types */
315enum spoe_data_type {
316 SPOE_DATA_T_NULL = 0,
317 SPOE_DATA_T_BOOL,
318 SPOE_DATA_T_INT32,
319 SPOE_DATA_T_UINT32,
320 SPOE_DATA_T_INT64,
321 SPOE_DATA_T_UINT64,
322 SPOE_DATA_T_IPV4,
323 SPOE_DATA_T_IPV6,
324 SPOE_DATA_T_STR,
325 SPOE_DATA_T_BIN,
326 SPOE_DATA_TYPES
327};
328
329/* Masks to get data type or flags value */
330#define SPOE_DATA_T_MASK 0x0F
331#define SPOE_DATA_FL_MASK 0xF0
332
333/* Flags to set Boolean values */
334#define SPOE_DATA_FL_FALSE 0x00
335#define SPOE_DATA_FL_TRUE 0x10
336
337
338#endif /* _TYPES_SPOE_H */