blob: 3014fa8ff31601a444ee45127330ff35bcfeee3c [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/memory.h>
20#include <common/time.h>
21
22#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020023#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010024#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020025
26#include <proto/arg.h>
27#include <proto/backend.h>
28#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010029#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020030#include <proto/frontend.h>
31#include <proto/log.h>
32#include <proto/proto_http.h>
33#include <proto/proxy.h>
34#include <proto/sample.h>
35#include <proto/session.h>
36#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010037#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020038#include <proto/stream.h>
39#include <proto/stream_interface.h>
40#include <proto/task.h>
41#include <proto/vars.h>
42
43#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
44#define SPOE_PRINTF(x...) fprintf(x)
45#else
46#define SPOE_PRINTF(x...)
47#endif
48
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010049/* Reserved 4 bytes to the frame size. So a frame and its size can be written
50 * together in a buffer */
51#define MAX_FRAME_SIZE global.tune.bufsize - 4
52
53/* The minimum size for a frame */
54#define MIN_FRAME_SIZE 256
55
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010056/* Reserved for the metadata and the frame type.
57 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010058#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020059
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010061#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
62
Christopher Faulet3b386a32017-02-23 10:17:15 +010063/* SPOE filter id. Used to identify SPOE filters */
64const char *spoe_filter_id = "SPOE filter";
65
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020066/* Set if the handle on SIGUSR1 is registered */
67static int sighandler_registered = 0;
68
69/* proxy used during the parsing */
70struct proxy *curproxy = NULL;
71
72/* The name of the SPOE engine, used during the parsing */
73char *curengine = NULL;
74
75/* SPOE agent used during the parsing */
76struct spoe_agent *curagent = NULL;
77
78/* SPOE message used during the parsing */
79struct spoe_message *curmsg = NULL;
80
81/* list of SPOE messages and placeholders used during the parsing */
82struct list curmsgs;
83struct list curmps;
84
Christopher Faulet42bfa462017-01-04 14:14:19 +010085/* Pools used to allocate SPOE structs */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086static struct pool_head *pool2_spoe_ctx = NULL;
Christopher Faulet42bfa462017-01-04 14:14:19 +010087static struct pool_head *pool2_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020089struct flt_ops spoe_ops;
90
Christopher Faulet8ef75252017-02-20 22:56:03 +010091static int spoe_queue_context(struct spoe_context *ctx);
92static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
93static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020094
95/********************************************************************
96 * helper functions/globals
97 ********************************************************************/
98static void
Christopher Faulet8ef75252017-02-20 22:56:03 +010099spoe_release_msg_placeholder(struct spoe_msg_placeholder *mp)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100{
101 if (!mp)
102 return;
103 free(mp->id);
104 free(mp);
105}
106
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100108spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109{
110 struct spoe_arg *arg, *back;
111
112 if (!msg)
113 return;
114 free(msg->id);
115 free(msg->conf.file);
116 list_for_each_entry_safe(arg, back, &msg->args, list) {
117 release_sample_expr(arg->expr);
118 free(arg->name);
119 LIST_DEL(&arg->list);
120 free(arg);
121 }
122 free(msg);
123}
124
125static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100126spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127{
128 struct spoe_message *msg, *back;
129 int i;
130
131 if (!agent)
132 return;
133 free(agent->id);
134 free(agent->conf.file);
135 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100136 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100137 free(agent->var_on_error);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200138 for (i = 0; i < SPOE_EV_EVENTS; ++i) {
139 list_for_each_entry_safe(msg, back, &agent->messages[i], list) {
140 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100141 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200142 }
143 }
144 free(agent);
145}
146
147static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100148 [SPOE_FRM_ERR_NONE] = "normal",
149 [SPOE_FRM_ERR_IO] = "I/O error",
150 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
151 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
152 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
153 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
154 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
155 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
156 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
157 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
158 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
159 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100160 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100161 [SPOE_FRM_ERR_RES] = "resource allocation error",
162 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163};
164
165static const char *spoe_event_str[SPOE_EV_EVENTS] = {
166 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
167 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
168 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
169 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
170 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
171
172 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
173 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
174 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
175};
176
177
178#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
179
180static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100181 [SPOE_CTX_ST_NONE] = "NONE",
182 [SPOE_CTX_ST_READY] = "READY",
183 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
184 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
185 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
186 [SPOE_CTX_ST_DONE] = "DONE",
187 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188};
189
190static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100191 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
192 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
193 [SPOE_APPCTX_ST_IDLE] = "IDLE",
194 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
195 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
196 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
197 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
198 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
199 [SPOE_APPCTX_ST_EXIT] = "EXIT",
200 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200201};
202
203#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100204
Christopher Faulet8ef75252017-02-20 22:56:03 +0100205/* Used to generates a unique id for an engine. On success, it returns a
206 * allocated string. So it is the caller's reponsibility to release it. If the
207 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100208static char *
209generate_pseudo_uuid()
210{
211 static int init = 0;
212
213 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
214 const char uuid_chr[] = "0123456789ABCDEF-";
215 char *uuid;
216 int i;
217
218 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
219 return NULL;
220
221 if (!init) {
222 srand(now_ms);
223 init = 1;
224 }
225
226 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
227 int r = rand () % 16;
228
229 switch (uuid_fmt[i]) {
230 case 'x' : uuid[i] = uuid_chr[r]; break;
231 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
232 default : uuid[i] = uuid_fmt[i]; break;
233 }
234 }
235 return uuid;
236}
237
Christopher Faulet8ef75252017-02-20 22:56:03 +0100238/* Returns the minimum number of appets alive at a time. This function is used
239 * to know if more applets should be created for an engine. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100240static inline unsigned int
241min_applets_act(struct spoe_agent *agent)
242{
243 unsigned int nbsrv;
244
Christopher Faulet8ef75252017-02-20 22:56:03 +0100245 /* TODO: Add a config parameter to customize this value. Always 0 for
246 * now */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100247 if (agent->min_applets)
248 return agent->min_applets;
249
Christopher Faulet8ef75252017-02-20 22:56:03 +0100250 /* Get the number of active servers for the backend */
251 nbsrv = (agent->b.be->srv_act
252 ? agent->b.be->srv_act
253 : agent->b.be->srv_bck);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100254 return 2*nbsrv;
255}
256
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200257/********************************************************************
258 * Functions that encode/decode SPOE frames
259 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200260/* Helper to get static string length, excluding the terminating null byte */
261#define SLEN(str) (sizeof(str)-1)
262
263/* Predefined key used in HELLO/DISCONNECT frames */
264#define SUPPORTED_VERSIONS_KEY "supported-versions"
265#define VERSION_KEY "version"
266#define MAX_FRAME_SIZE_KEY "max-frame-size"
267#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100268#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100269#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270#define STATUS_CODE_KEY "status-code"
271#define MSG_KEY "message"
272
273struct spoe_version {
274 char *str;
275 int min;
276 int max;
277};
278
279/* All supported versions */
280static struct spoe_version supported_versions[] = {
281 {"1.0", 1000, 1000},
282 {NULL, 0, 0}
283};
284
285/* Comma-separated list of supported versions */
286#define SUPPORTED_VERSIONS_VAL "1.0"
287
Christopher Faulet8ef75252017-02-20 22:56:03 +0100288/* Convert a string to a SPOE version value. The string must follow the format
289 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
290 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200291static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100292spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100294 const char *p, *end;
295 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296
Christopher Faulet8ef75252017-02-20 22:56:03 +0100297 p = str;
298 end = str+len;
299 maj = min = 0;
300 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301
Christopher Faulet8ef75252017-02-20 22:56:03 +0100302 /* skip leading spaces */
303 while (p < end && isspace(*p))
304 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306 /* parse Major number, until the '.' */
307 while (*p != '.') {
308 if (p >= end || *p < '0' || *p > '9')
309 goto out;
310 maj *= 10;
311 maj += (*p - '0');
312 p++;
313 }
314
315 /* check Major version */
316 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200317 goto out;
318
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 p++; /* skip the '.' */
320 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
321 goto out;
322
323 /* Parse Minor number */
324 while (p < end) {
325 if (*p < '0' || *p > '9')
326 break;
327 min *= 10;
328 min += (*p - '0');
329 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200330 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100331
332 /* check Minor number */
333 if (min > 999)
334 goto out;
335
336 /* skip trailing spaces */
337 while (p < end && isspace(*p))
338 p++;
339 if (p != end)
340 goto out;
341
342 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200343 out:
344 return vsn;
345}
346
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
348 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
349 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200350static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100351spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200352{
Christopher Faulet305c6072017-02-23 16:17:53 +0100353 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100354 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 char *p, *end;
356 unsigned int flags = SPOE_FRM_FL_FIN;
357 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200358
Christopher Faulet8ef75252017-02-20 22:56:03 +0100359 p = frame;
360 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200361
Christopher Faulet8ef75252017-02-20 22:56:03 +0100362 /* Set Frame type */
363 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100365 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366 memcpy(p, (char *)&flags, 4);
367 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200368
369 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200371
372 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
373 * and "capabilities" */
374
375 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 sz = SLEN(SUPPORTED_VERSIONS_KEY);
377 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
378 goto too_big;
379
380 *p++ = SPOE_DATA_T_STR;
381 sz = SLEN(SUPPORTED_VERSIONS_VAL);
382 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
383 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384
385 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 sz = SLEN(MAX_FRAME_SIZE_KEY);
387 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
388 goto too_big;
389
390 *p++ = SPOE_DATA_T_UINT32;
391 if (spoe_encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
392 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200393
394 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 sz = SLEN(CAPABILITIES_KEY);
396 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
397 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100400 chk = get_trash_chunk();
401 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
402 memcpy(chk->str, "pipelining", 10);
403 chk->len += 10;
404 }
405 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
406 if (chk->len) chk->str[chk->len++] = ',';
407 memcpy(chk->str+chk->len, "async", 5);
408 chk->len += 5;
409 }
410 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100411 goto too_big;
412
413 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100414 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 sz = SLEN(ENGINE_ID_KEY);
416 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
417 goto too_big;
418
419 *p++ = SPOE_DATA_T_STR;
420 sz = strlen(agent->engine_id);
421 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
422 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100423 }
424
Christopher Faulet8ef75252017-02-20 22:56:03 +0100425 return (p - frame);
426
427 too_big:
428 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
429 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430}
431
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
433 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
434 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200437{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100438 const char *reason;
439 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100440 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200442
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 p = frame;
444 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200445
Christopher Faulet8ef75252017-02-20 22:56:03 +0100446 /* Set Frame type */
447 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200448
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100449 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100450 memcpy(p, (char *)&flags, 4);
451 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200452
453 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100454 *p++ = 0; *p++ = 0;
455
456 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
457 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458
459 /* There are 2 mandatory items: "status-code" and "message" */
460
461 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 sz = SLEN(STATUS_CODE_KEY);
463 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
464 goto too_big;
465
466 *p++ = SPOE_DATA_T_UINT32;
467 if (spoe_encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
468 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200469
470 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100471 sz = SLEN(MSG_KEY);
472 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
473 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 /*Get the message corresponding to the status code */
476 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
477
478 *p++ = SPOE_DATA_T_STR;
479 sz = strlen(reason);
480 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
481 goto too_big;
482
483 return (p - frame);
484
485 too_big:
486 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
487 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488}
489
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
491 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
492 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100495 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200496{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 char *p, *end;
498 unsigned int stream_id, frame_id;
499 unsigned int flags = SPOE_FRM_FL_FIN;
500 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200501
Christopher Faulet8ef75252017-02-20 22:56:03 +0100502 p = frame;
503 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200504
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100505 stream_id = ctx->stream_id;
506 frame_id = ctx->frame_id;
507
508 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
509 /* The fragmentation is not supported by the applet */
510 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
511 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
512 return -1;
513 }
514 flags = ctx->frag_ctx.flags;
515 }
516
517 /* Set Frame type */
518 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
519
520 /* Set flags */
521 memcpy(p, (char *)&flags, 4);
522 p += 4;
523
524 /* Set stream-id and frame-id */
525 if (spoe_encode_varint(stream_id, &p, end) == -1)
526 goto too_big;
527 if (spoe_encode_varint(frame_id, &p, end) == -1)
528 goto too_big;
529
530 /* Copy encoded messages, if possible */
531 sz = ctx->buffer->i;
532 if (p + sz >= end)
533 goto too_big;
534 memcpy(p, ctx->buffer->p, sz);
535 p += sz;
536
537 return (p - frame);
538
539 too_big:
540 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
541 return 0;
542}
543
544/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
545 * returns the number of encoded bytes in the frame on success, 0 if an encoding
546 * error occurred and -1 if a fatal error occurred. */
547static int
548spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
549 char *frame, size_t size)
550{
551 char *p, *end;
552 unsigned int stream_id, frame_id;
553 unsigned int flags;
554 size_t sz;
555
556 p = frame;
557 end = frame+size;
558
Christopher Faulet8ef75252017-02-20 22:56:03 +0100559 /* <ctx> is null when the stream has aborted the processing of a
560 * fragmented frame. In this case, we must notify the corresponding
561 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100562 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100563 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100564 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
565 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
566 }
567 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100568 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100569 stream_id = ctx->stream_id;
570 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100571 }
572
Christopher Faulet8ef75252017-02-20 22:56:03 +0100573 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100574 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100575
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100576 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100577 memcpy(p, (char *)&flags, 4);
578 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200579
580 /* Set stream-id and frame-id */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100581 if (spoe_encode_varint(stream_id, &p, end) == -1)
582 goto too_big;
583 if (spoe_encode_varint(frame_id, &p, end) == -1)
584 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200585
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100586 if (ctx == NULL)
587 goto end;
588
Christopher Faulet8ef75252017-02-20 22:56:03 +0100589 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100590 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100591 if (p + sz >= end)
592 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100593 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100594 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100595
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100596 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597 return (p - frame);
598
599 too_big:
600 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
601 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200602}
603
Christopher Faulet8ef75252017-02-20 22:56:03 +0100604/* Decode and process the HELLO frame sent by an agent. It returns the number of
605 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
606 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200607static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200609{
Christopher Faulet305c6072017-02-23 16:17:53 +0100610 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
611 char *p, *end;
612 int vsn, max_frame_size;
613 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614
615 p = frame;
616 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200617
618 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
620 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200621 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200623
Christopher Faulet8ef75252017-02-20 22:56:03 +0100624 if (size < 7 /* TYPE + METADATA */) {
625 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
626 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200627 }
628
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100629 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630 memcpy((char *)&flags, p, 4);
631 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100633 /* Fragmentation is not supported for HELLO frame */
634 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100636 return -1;
637 }
638
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640 if (*p != 0 || *(p+1) != 0) {
641 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
642 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200645
646 /* There are 3 mandatory items: "version", "max-frame-size" and
647 * "capabilities" */
648
649 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100650 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 while (p < end) {
652 char *str;
653 size_t sz;
654 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655
656 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100657 ret = spoe_decode_buffer(&p, end, &str, &sz);
658 if (ret == -1 || !sz) {
659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
660 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663 /* Check "version" K/V item */
664 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 int i, type = *p++;
666
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
669 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
670 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
673 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
674 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200675 }
676
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200678 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100679 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 return -1;
681 }
682 for (i = 0; supported_versions[i].str != NULL; ++i) {
683 if (vsn >= supported_versions[i].min &&
684 vsn <= supported_versions[i].max)
685 break;
686 }
687 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 return -1;
690 }
691 }
692 /* Check "max-frame-size" K/V item */
693 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695
696 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
698 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
699 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
700 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
702 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 if (spoe_decode_varint(&p, end, &sz) == -1) {
705 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
706 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708 if (sz < MIN_FRAME_SIZE ||
709 sz > SPOE_APPCTX(appctx)->max_frame_size) {
710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200711 return -1;
712 }
713 max_frame_size = sz;
714 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100715 /* Check "capabilities" K/V item */
716 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100717 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100718
719 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
721 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
722 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100723 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100724 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
726 return 0;
727 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100728
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100730 char *delim;
731
732 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100734
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
736 str += 10; sz -= 10;
737 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100738 flags |= SPOE_APPCTX_FL_PIPELINING;
739 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 else if (sz >= 5 && !strncmp(str, "async", 5)) {
741 str += 5; sz -= 5;
742 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100743 flags |= SPOE_APPCTX_FL_ASYNC;
744 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
746 str += 13; sz -= 13;
747 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100748 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
749 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 /* Get the next comma or break */
752 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100753 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 delim++;
755 sz -= (delim - str);
756 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757 }
758 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200759 else {
760 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 if (spoe_skip_data(&p, end) == -1) {
762 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
763 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200764 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200765 }
766 }
767
768 /* Final checks */
769 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200771 return -1;
772 }
773 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100774 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200775 return -1;
776 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100777 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
778 flags &= ~SPOE_APPCTX_FL_PIPELINING;
779 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
780 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200781
Christopher Faulet42bfa462017-01-04 14:14:19 +0100782 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
783 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
784 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100785
786 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200787}
788
789/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
790 * bytes on success, 0 if the frame can be ignored and -1 if an error
791 * occurred. */
792static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200794{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100796 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797
798 p = frame;
799 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800
801 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
803 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200804 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100805 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200806
Christopher Faulet8ef75252017-02-20 22:56:03 +0100807 if (size < 7 /* TYPE + METADATA */) {
808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
809 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810 }
811
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100812 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100813 memcpy((char *)&flags, p, 4);
814 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100816 /* Fragmentation is not supported for DISCONNECT frame */
817 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100818 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100819 return -1;
820 }
821
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 if (*p != 0 || *(p+1) != 0) {
824 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
825 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100827 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828
829 /* There are 2 mandatory items: "status-code" and "message" */
830
831 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 while (p < end) {
833 char *str;
834 size_t sz;
835 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836
837 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838 ret = spoe_decode_buffer(&p, end, &str, &sz);
839 if (ret == -1 || !sz) {
840 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
841 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842 }
843
844 /* Check "status-code" K/V item */
845 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
848 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
850 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
851 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
852 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
854 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 if (spoe_decode_varint(&p, end, &sz) == -1) {
857 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
858 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200861 }
862
863 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 else if (!memcmp(str, MSG_KEY, sz)) {
865 int type = *p++;
866
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200867 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
869 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
870 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 ret = spoe_decode_buffer(&p, end, &str, &sz);
873 if (ret == -1 || sz > 255) {
874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
875 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100877#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
878 SPOE_APPCTX(appctx)->reason = str;
879 SPOE_APPCTX(appctx)->rlen = sz;
880#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 }
882 else {
883 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100884 if (spoe_skip_data(&p, end) == -1) {
885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888 }
889 }
890
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892}
893
894
Christopher Fauleta1cda022016-12-21 08:58:06 +0100895/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 * success, 0 if the frame can be ignored and -1 if an error occurred. */
897static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100899 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100901 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100903 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100905 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906
907 p = frame;
908 end = frame + size;
909 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200910
911 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100912 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
913 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100915 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200916
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917 if (size < 7 /* TYPE + METADATA */) {
918 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
919 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
921
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100922 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 memcpy((char *)&flags, p, 4);
924 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100926 /* Fragmentation is not supported for now */
927 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100929 return -1;
930 }
931
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932 /* Get the stream-id and the frame-id */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 if (spoe_decode_varint(&p, end, &stream_id) == -1) {
934 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100935 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 }
937 if (spoe_decode_varint(&p, end, &frame_id) == -1) {
938 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100941
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100943 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100944 list_for_each_entry((*ctx), &agent->waiting_queue, list) {
945 if ((*ctx)->stream_id == (unsigned int)stream_id &&
946 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100947 goto found;
948 }
949 }
950 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100951 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
952 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100954 goto found;
955 }
956 }
957
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100958 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
959 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
960 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
961
962 /* ABRT bit is set for an unfinished fragmented frame */
963 if (flags & SPOE_FRM_FL_ABRT) {
964 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
965 (*ctx)->frag_ctx.spoe_appctx = NULL;
966 (*ctx)->state = SPOE_CTX_ST_ERROR;
967 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
968 /* Ignore the payload */
969 goto end;
970 }
971 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
972 /* For now, we ignore the ack */
973 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
974 return 0;
975 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100976
Christopher Fauleta1cda022016-12-21 08:58:06 +0100977 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100978 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
979 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100980 " - stream-id=%u - frame-id=%u\n",
981 (int)now.tv_sec, (int)now.tv_usec, agent->id,
982 __FUNCTION__, appctx,
983 (unsigned int)stream_id, (unsigned int)frame_id);
984
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100985 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100986 return 0;
987
988 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100989 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
990 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100991 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100992 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100993 }
Christopher Faulet4596fb72017-01-11 14:05:19 +0100994
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200995 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100996 len = (end - p);
997 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
998 SPOE_APPCTX(appctx)->buffer->i = len;
999 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001000
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001001 /* Transfer the buffer ownership to the SPOE context */
1002 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1003 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001004
Christopher Faulet8ef75252017-02-20 22:56:03 +01001005 (*ctx)->state = SPOE_CTX_ST_DONE;
1006
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001007 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001009 " - ACK frame received"
1010 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001012 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1013 (*ctx)->frame_id, flags);
1014 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001015}
1016
Christopher Fauletba7bc162016-11-07 21:07:38 +01001017/* This function is used in cfgparse.c and declared in proto/checks.h. It
1018 * prepare the request to send to agents during a healthcheck. It returns 0 on
1019 * success and -1 if an error occurred. */
1020int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001021spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001022{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001023 struct appctx appctx;
1024 struct spoe_appctx spoe_appctx;
1025 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1026 size_t sz;
1027 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001028
Christopher Faulet42bfa462017-01-04 14:14:19 +01001029 memset(&appctx, 0, sizeof(appctx));
1030 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001031 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001032
1033 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001034 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001035
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1037 end = frame + MAX_FRAME_SIZE;
1038
1039 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1040 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001041 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001043
Christopher Faulet8ef75252017-02-20 22:56:03 +01001044 /* Add "healthcheck" K/V item */
1045 sz = SLEN(HEALTHCHECK_KEY);
1046 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1047 return -1;
1048 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001049
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 *len = frame - buf;
1051 sz = htonl(*len - 4);
1052 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001053
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001055 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001057 return 0;
1058}
1059
1060/* This function is used in checks.c and declared in proto/checks.h. It decode
1061 * the response received from an agent during a healthcheck. It returns 0 on
1062 * success and -1 if an error occurred. */
1063int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001066 struct appctx appctx;
1067 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069 memset(&appctx, 0, sizeof(appctx));
1070 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001073 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001074
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1076 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001077 goto error;
1078 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1080 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081
1082 return 0;
1083
1084 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001085 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1086 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1087 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088 return -1;
1089}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001090
Christopher Fauleta1cda022016-12-21 08:58:06 +01001091/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1092 * the frame can be ignored, 1 to retry later, and the frame legnth on
1093 * success. */
1094static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001095spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001096{
1097 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001098 int ret;
1099 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001100
1101 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001102 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001103
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1105 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001106 netint = htonl(framesz);
1107 memcpy(buf, (char *)&netint, 4);
1108 ret = bi_putblk(si_ic(si), buf, framesz+4);
1109
1110 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111 if (ret == -1) {
1112 retry:
1113 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001114 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 }
1116 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001117 return -1; /* error */
1118 }
1119 return framesz;
1120}
1121
1122/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1123 * when the frame can be ignored, 1 to retry later and the frame length on
1124 * success. */
1125static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001127{
1128 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001129 int ret;
1130 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131
1132 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134
1135 ret = bo_getblk(si_oc(si), (char *)&netint, 4, 0);
1136 if (ret > 0) {
1137 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001138 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140 return -1;
1141 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142 ret = bo_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143 }
1144 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001145 if (ret == 0) {
1146 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 }
1149 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 return -1; /* error */
1151 }
1152 return framesz;
1153}
1154
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001155/********************************************************************
1156 * Functions that manage the SPOE applet
1157 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001158static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001160{
1161 si_applet_want_get(appctx->owner);
1162 si_applet_want_put(appctx->owner);
1163 appctx_wakeup(appctx);
1164 return 1;
1165}
1166
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001167/* Callback function that catches applet timeouts. If a timeout occurred, we set
1168 * <appctx->st1> flag and the SPOE applet is woken up. */
1169static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001171{
1172 struct appctx *appctx = task->context;
1173
1174 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1175 if (tick_is_expired(task->expire, now_ms)) {
1176 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001178 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001179 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001180 return task;
1181}
1182
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001183/* Callback function that releases a SPOE applet. This happens when the
1184 * connection with the agent is closed. */
1185static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001187{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001188 struct stream_interface *si = appctx->owner;
1189 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1190 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001191 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001192
1193 if (spoe_appctx == NULL)
1194 return;
1195
1196 appctx->ctx.spoe.ptr = NULL;
1197 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001198
1199 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1200 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1201 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202
Christopher Faulet8ef75252017-02-20 22:56:03 +01001203 /* Remove applet from the list of running applets */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001204 agent->applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001205 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1206 LIST_DEL(&spoe_appctx->list);
1207 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001208 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001212 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
1213 agent->applets_idle--;
1214
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001215 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001216 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1217 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001218
1219 si_shutw(si);
1220 si_shutr(si);
1221 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001222 }
1223
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224 /* Destroy the task attached to this applet */
1225 if (spoe_appctx->task) {
1226 task_delete(spoe_appctx->task);
1227 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001228 }
1229
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230 /* Notify all waiting streams */
1231 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001232 LIST_DEL(&ctx->list);
1233 LIST_INIT(&ctx->list);
1234 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001236 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001237 }
1238
Christopher Faulet8ef75252017-02-20 22:56:03 +01001239 /* If the applet was processing a fragmented frame, notify the
1240 * corresponding stream. */
1241 if (spoe_appctx->frag_ctx.ctx) {
1242 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001243 ctx->frag_ctx.spoe_appctx = NULL;
1244 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001245 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001246 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1247 }
1248
Christopher Faulet8ef75252017-02-20 22:56:03 +01001249 /* Release allocated memory */
1250 spoe_release_buffer(&spoe_appctx->buffer,
1251 &spoe_appctx->buffer_wait);
1252 pool_free2(pool2_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001253
Christopher Fauleta1cda022016-12-21 08:58:06 +01001254 if (!LIST_ISEMPTY(&agent->applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001255 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001256
Christopher Faulet8ef75252017-02-20 22:56:03 +01001257 /* If this was the last running applet, notify all waiting streams */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001258 list_for_each_entry_safe(ctx, back, &agent->sending_queue, list) {
1259 LIST_DEL(&ctx->list);
1260 LIST_INIT(&ctx->list);
1261 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001263 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001265 list_for_each_entry_safe(ctx, back, &agent->waiting_queue, list) {
1266 LIST_DEL(&ctx->list);
1267 LIST_INIT(&ctx->list);
1268 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1271 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001272
1273 end:
1274 /* Update runtinme agent info */
1275 agent->frame_size = agent->max_frame_size;
1276 list_for_each_entry(spoe_appctx, &agent->applets, list)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 agent->frame_size = MIN(spoe_appctx->max_frame_size,
1278 agent->frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001279}
1280
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001281static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001283{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001284 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001285 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001286 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001287 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288
Christopher Fauleta1cda022016-12-21 08:58:06 +01001289 if (si->state <= SI_ST_CON) {
1290 si_applet_want_put(si);
1291 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1292 goto stop;
1293 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001294 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001295 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001296 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001297 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001298
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1301 " - Connection timed out\n",
1302 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1303 __FUNCTION__, appctx);
1304 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001305 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001306 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001307
Christopher Faulet42bfa462017-01-04 14:14:19 +01001308 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001309 SPOE_APPCTX(appctx)->task->expire =
1310 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001311
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1313 * length. */
1314 buf = trash.str; frame = buf+4;
1315 ret = spoe_prepare_hahello_frame(appctx, frame,
1316 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001318 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319
1320 switch (ret) {
1321 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322 case 0: /* ignore => an error, cannot be ignored */
1323 goto exit;
1324
1325 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001326 goto stop;
1327
Christopher Faulet8ef75252017-02-20 22:56:03 +01001328 default:
1329 /* HELLO frame successfully sent, now wait for the
1330 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1332 goto next;
1333 }
1334
1335 next:
1336 return 0;
1337 stop:
1338 return 1;
1339 exit:
1340 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1341 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001342}
1343
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001344static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001345spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001346{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001348 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001349 char *frame;
1350 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001351
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001352
Christopher Fauletb067b062017-01-04 16:39:11 +01001353 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001356 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001357
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1360 " - Connection timed out\n",
1361 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1362 __FUNCTION__, appctx);
1363 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 goto exit;
1365 }
1366
Christopher Faulet8ef75252017-02-20 22:56:03 +01001367 frame = trash.str; trash.len = 0;
1368 ret = spoe_recv_frame(appctx, frame,
1369 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001370 if (ret > 1) {
1371 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1372 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1373 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375 trash.len = ret + 4;
1376 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001377 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378
Christopher Fauleta1cda022016-12-21 08:58:06 +01001379 switch (ret) {
1380 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001382 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1383 goto next;
1384
1385 case 1: /* retry later */
1386 goto stop;
1387
1388 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 /* HELLO handshake is finished, set the idle timeout and
1390 * add the applet in the list of running applets. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001391 agent->applets_idle++;
1392 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001393 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1394 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001395
1396 /* Update runtinme agent info */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001397 agent->frame_size = MIN(SPOE_APPCTX(appctx)->max_frame_size,
1398 agent->frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001399 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001400 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001401
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 /* Do not forget to remove processed frame from the output buffer */
1404 if (trash.len)
1405 bo_skip(si_oc(si), trash.len);
1406
1407 SPOE_APPCTX(appctx)->task->expire =
1408 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001409 return 0;
1410 stop:
1411 return 1;
1412 exit:
1413 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1414 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001415}
1416
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001417
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001419spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001420{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001421 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1422 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001423 char *frame, *buf;
1424 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001425
Christopher Faulet8ef75252017-02-20 22:56:03 +01001426 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1427 * length. */
1428 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001429
1430 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1431 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1432 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1433 SPOE_APPCTX(appctx)->max_frame_size);
1434 }
1435 else if (LIST_ISEMPTY(&agent->sending_queue)) {
1436 *skip = 1;
1437 ret = 1;
1438 goto end;
1439 }
1440 else {
1441 ctx = LIST_NEXT(&agent->sending_queue, typeof(ctx), list);
1442 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1443 SPOE_APPCTX(appctx)->max_frame_size);
1444
1445 }
1446
Christopher Faulet8ef75252017-02-20 22:56:03 +01001447 if (ret > 1)
1448 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001449
Christopher Faulet8ef75252017-02-20 22:56:03 +01001450 switch (ret) {
1451 case -1: /* error */
1452 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1453 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001454
Christopher Faulet8ef75252017-02-20 22:56:03 +01001455 case 0: /* ignore */
1456 if (ctx == NULL)
1457 goto abort_frag_frame;
1458
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001459 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460 LIST_DEL(&ctx->list);
1461 LIST_INIT(&ctx->list);
1462 ctx->state = SPOE_CTX_ST_ERROR;
1463 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1464 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1465 break;
1466
1467 case 1: /* retry */
1468 *skip = 1;
1469 break;
1470
1471 default:
1472 if (ctx == NULL)
1473 goto abort_frag_frame;
1474
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001476 LIST_DEL(&ctx->list);
1477 LIST_INIT(&ctx->list);
1478 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1479 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1480 goto no_frag_frame_sent;
1481 else {
1482 *skip = 1;
1483 goto frag_frame_sent;
1484 }
1485 }
1486 goto end;
1487
1488 frag_frame_sent:
1489 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1490 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1491 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1492 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1493
1494 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1495 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1496 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1497 goto end;
1498
1499 no_frag_frame_sent:
1500 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1501 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1502 LIST_ADDQ(&agent->waiting_queue, &ctx->list);
1503 }
1504 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1505 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1506 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1507 }
1508 else {
1509 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1510 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1511 }
1512 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1513 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1514 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1515
1516 ctx->frag_ctx.spoe_appctx = NULL;
1517 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1518 goto end;
1519
1520 abort_frag_frame:
1521 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1522 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1523 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1524 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1525 goto end;
1526
1527 end:
1528 return ret;
1529}
1530
1531static int
1532spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1533{
1534 struct spoe_context *ctx = NULL;
1535 char *frame;
1536 int ret;
1537
1538 frame = trash.str; trash.len = 0;
1539 ret = spoe_recv_frame(appctx, frame,
1540 SPOE_APPCTX(appctx)->max_frame_size);
1541 if (ret > 1) {
1542 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1543 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1544 goto end;
1545 }
1546 trash.len = ret + 4;
1547 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1548 }
1549 switch (ret) {
1550 case -1: /* error */
1551 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1552 break;
1553
1554 case 0: /* ignore */
1555 break;
1556
1557 case 1: /* retry */
1558 *skip = 1;
1559 break;
1560
1561 default:
1562 LIST_DEL(&ctx->list);
1563 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001564
1565 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1566 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1567 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1568 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1569 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1570 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1571 }
1572 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1573 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1574
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1576 break;
1577 }
1578
1579 /* Do not forget to remove processed frame from the output buffer */
1580 if (trash.len)
1581 bo_skip(si_oc(appctx->owner), trash.len);
1582 end:
1583 return ret;
1584}
1585
1586static int
1587spoe_handle_processing_appctx(struct appctx *appctx)
1588{
1589 struct stream_interface *si = appctx->owner;
1590 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591 unsigned int fpa = 0;
1592 int ret, skip_sending = 0, skip_receiving = 0;
1593
1594 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1595 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1596 goto exit;
1597 }
1598
1599 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1600 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1601 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1602 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1603 goto next;
1604 }
1605
1606 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001607 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1608 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1609 " - appctx-state=%s\n",
1610 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1611 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001612 skip_sending, skip_receiving,
1613 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001614
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001615 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001616 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001617 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001618 if (skip_receiving)
1619 goto stop;
1620 goto recv_frame;
1621 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001622
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001623 /* send_frame */
1624 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001625 switch (ret) {
1626 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001627 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001628
Christopher Fauleta1cda022016-12-21 08:58:06 +01001629 case 0: /* ignore */
1630 agent->sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001631 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001632 break;
1633
Christopher Fauleta1cda022016-12-21 08:58:06 +01001634 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001635 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001636
Christopher Fauleta1cda022016-12-21 08:58:06 +01001637 default:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001638 agent->sending_rate++;
1639 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001640 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001641 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001642 if (fpa > agent->max_fpa)
1643 goto stop;
1644
1645 recv_frame:
1646 if (skip_receiving)
1647 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001648 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001649 switch (ret) {
1650 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001651 goto next;
1652
1653 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001654 fpa++;
1655 break;
1656
1657 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001658 break;
1659
1660 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001661 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001662 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001663 }
1664 goto process;
1665
1666 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001667 SPOE_APPCTX(appctx)->task->expire =
1668 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001669 return 0;
1670 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001671 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001672 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001673 agent->applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001674 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001675 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
1676 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1677 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001678 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001679 SPOE_APPCTX(appctx)->task->expire =
1680 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001681 }
1682 return 1;
1683
1684 exit:
1685 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1686 return 0;
1687}
1688
1689static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001690spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001691{
1692 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001693 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001694 char *frame, *buf;
1695 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001696
Christopher Fauleta1cda022016-12-21 08:58:06 +01001697 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1698 goto exit;
1699
1700 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1701 goto exit;
1702
Christopher Faulet8ef75252017-02-20 22:56:03 +01001703 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1704 * length. */
1705 buf = trash.str; frame = buf+4;
1706 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1707 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001708 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001709 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710
1711 switch (ret) {
1712 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001713 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714 goto exit;
1715
1716 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 goto stop;
1718
1719 default:
1720 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1721 " - disconnected by HAProxy (%d): %s\n",
1722 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001723 __FUNCTION__, appctx,
1724 SPOE_APPCTX(appctx)->status_code,
1725 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001726
1727 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1728 goto next;
1729 }
1730
1731 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001732 SPOE_APPCTX(appctx)->task->expire =
1733 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001734 return 0;
1735 stop:
1736 return 1;
1737 exit:
1738 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1739 return 0;
1740}
1741
1742static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001743spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744{
1745 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001746 char *frame;
1747 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748
Christopher Fauletb067b062017-01-04 16:39:11 +01001749 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001750 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001751 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001752 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753
Christopher Fauletb067b062017-01-04 16:39:11 +01001754 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001757 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 frame = trash.str; trash.len = 0;
1760 ret = spoe_recv_frame(appctx, frame,
1761 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763 trash.len = ret + 4;
1764 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765 }
1766
1767 switch (ret) {
1768 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001769 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1770 " - error on frame (%s)\n",
1771 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001772 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001774 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001775 goto exit;
1776
1777 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 goto next;
1779
1780 case 1: /* retry */
1781 goto stop;
1782
1783 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001785 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001786 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001787 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001788 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1789 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790 goto exit;
1791 }
1792
1793 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794 /* Do not forget to remove processed frame from the output buffer */
1795 if (trash.len)
1796 bo_skip(si_oc(appctx->owner), trash.len);
1797
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798 return 0;
1799 stop:
1800 return 1;
1801 exit:
1802 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1803 return 0;
1804}
1805
1806/* I/O Handler processing messages exchanged with the agent */
1807static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 struct stream_interface *si = appctx->owner;
1811 struct spoe_agent *agent;
1812
1813 if (SPOE_APPCTX(appctx) == NULL)
1814 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815
Christopher Faulet8ef75252017-02-20 22:56:03 +01001816 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1817 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001818
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819 switchstate:
1820 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1821 " - appctx-state=%s\n",
1822 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1823 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1824
1825 switch (appctx->st0) {
1826 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001827 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 goto out;
1829 goto switchstate;
1830
1831 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001832 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 goto out;
1834 goto switchstate;
1835
1836 case SPOE_APPCTX_ST_IDLE:
1837 if (stopping &&
1838 LIST_ISEMPTY(&agent->sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001839 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001840 SPOE_APPCTX(appctx)->task->expire =
1841 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001843 goto switchstate;
1844 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845 agent->applets_idle--;
1846 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1847 /* fall through */
1848
1849 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001850 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1851 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853 goto out;
1854 goto switchstate;
1855
1856 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001857 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 goto out;
1859 goto switchstate;
1860
1861 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863 goto out;
1864 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001865
1866 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 appctx->st0 = SPOE_APPCTX_ST_END;
1868 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1869
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001870 si_shutw(si);
1871 si_shutr(si);
1872 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001873 /* fall through */
1874
1875 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001876 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001877 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878 out:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001879 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1880 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001881 si_oc(si)->flags |= CF_READ_DONTWAIT;
1882 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001883}
1884
1885struct applet spoe_applet = {
1886 .obj_type = OBJ_TYPE_APPLET,
1887 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001888 .fct = spoe_handle_appctx,
1889 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001890};
1891
1892/* Create a SPOE applet. On success, the created applet is returned, else
1893 * NULL. */
1894static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001895spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001896{
1897 struct appctx *appctx;
1898 struct session *sess;
1899 struct task *task;
1900 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001901
1902 if ((appctx = appctx_new(&spoe_applet)) == NULL)
1903 goto out_error;
1904
Christopher Faulet42bfa462017-01-04 14:14:19 +01001905 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx);
1906 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001907 goto out_free_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001908 memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001909
Christopher Faulet42bfa462017-01-04 14:14:19 +01001910 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1911 if ((SPOE_APPCTX(appctx)->task = task_new()) == NULL)
1912 goto out_free_spoe_appctx;
1913
1914 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001915 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001916 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1917 SPOE_APPCTX(appctx)->task->context = appctx;
1918 SPOE_APPCTX(appctx)->agent = conf->agent;
1919 SPOE_APPCTX(appctx)->version = 0;
1920 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1921 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001922 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001923 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1924
1925 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1926 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001927 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001928
1929 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1930 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001931
Willy Tarreau5820a362016-12-22 15:59:02 +01001932 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933 if (!sess)
1934 goto out_free_spoe;
1935
1936 if ((task = task_new()) == NULL)
1937 goto out_free_sess;
1938
1939 if ((strm = stream_new(sess, task, &appctx->obj_type)) == NULL)
1940 goto out_free_task;
1941
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942 stream_set_backend(strm, conf->agent->b.be);
1943
1944 /* applet is waiting for data */
1945 si_applet_cant_get(&strm->si[0]);
1946 appctx_wakeup(appctx);
1947
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948 strm->do_log = NULL;
1949 strm->res.flags |= CF_READ_DONTWAIT;
1950
1951 conf->agent_fe.feconn++;
1952 jobs++;
1953 totalconn++;
1954
Christopher Faulet42bfa462017-01-04 14:14:19 +01001955 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
1956 LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001957 conf->agent->applets_act++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001958 return appctx;
1959
1960 /* Error unrolling */
1961 out_free_task:
1962 task_free(task);
1963 out_free_sess:
1964 session_free(sess);
1965 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001966 task_free(SPOE_APPCTX(appctx)->task);
1967 out_free_spoe_appctx:
1968 pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969 out_free_appctx:
1970 appctx_free(appctx);
1971 out_error:
1972 return NULL;
1973}
1974
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001975static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001976spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977{
1978 struct spoe_config *conf = FLT_CONF(ctx->filter);
1979 struct spoe_agent *agent = conf->agent;
1980 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001981 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001982 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001983
Christopher Fauleta1cda022016-12-21 08:58:06 +01001984 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985
Christopher Fauleta1cda022016-12-21 08:58:06 +01001986 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001987 if (agent->applets_act >= min_applets &&
1988 agent->applets_idle &&
1989 agent->sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001990 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991
1992 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001993 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
1995 ctx->strm);
1996
Christopher Fauleta1cda022016-12-21 08:58:06 +01001997 /* Do not try to create a new applet if there is no server up for the
1998 * agent's backend. */
1999 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2000 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2001 " - cannot create SPOE appctx: no server up\n",
2002 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2003 __FUNCTION__, ctx->strm);
2004 goto end;
2005 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006
Christopher Fauleta1cda022016-12-21 08:58:06 +01002007 /* Do not try to create a new applet if we have reached the maximum of
2008 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002009 if (agent->cps_max > 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002010 if (!freq_ctr_remain(&agent->conn_per_sec, agent->cps_max, 0)) {
2011 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2012 " - cannot create SPOE appctx: max CPS reached\n",
2013 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2014 __FUNCTION__, ctx->strm);
2015 goto end;
2016 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017 }
2018
Christopher Faulet8ef75252017-02-20 22:56:03 +01002019 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002020 if (appctx == NULL) {
2021 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2022 " - failed to create SPOE appctx\n",
2023 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2024 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002025 send_log(ctx->strm->be, LOG_EMERG,
2026 "SPOE: [%s] failed to create SPOE applet\n",
2027 agent->id);
2028
Christopher Fauleta1cda022016-12-21 08:58:06 +01002029 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002030 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002031 if (agent->applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002032 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033
Christopher Fauleta1cda022016-12-21 08:58:06 +01002034 /* Increase the per-process number of cumulated connections */
2035 if (agent->cps_max > 0)
2036 update_freq_ctr(&agent->conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002037
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 end:
2039 /* The only reason to return an error is when there is no applet */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002040 if (LIST_ISEMPTY(&agent->applets)) {
2041 ctx->status_code = SPOE_CTX_ERR_RES;
2042 return -1;
2043 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002044
Christopher Fauleta1cda022016-12-21 08:58:06 +01002045 /* Add the SPOE context in the sending queue and update all running
2046 * info */
2047 LIST_ADDQ(&agent->sending_queue, &ctx->list);
2048 if (agent->sending_rate)
2049 agent->sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002050
2051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002052 " - Add stream in sending queue"
2053 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002054 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002055 ctx->strm, agent->applets_act, agent->applets_idle,
2056 agent->sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002057
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 /* Finally try to wakeup the first IDLE applet found and move it at the
2059 * end of the list. */
Christopher Faulet42bfa462017-01-04 14:14:19 +01002060 list_for_each_entry(spoe_appctx, &agent->applets, list) {
2061 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002063 spoe_wakeup_appctx(appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002064 LIST_DEL(&spoe_appctx->list);
2065 LIST_ADDQ(&agent->applets, &spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002066 break;
2067 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002069 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002070}
2071
2072/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002073 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074 **************************************************************************/
Christopher Faulet8ef75252017-02-20 22:56:03 +01002075/* Encode SPOE messages for a specific event. Info in <ctx->frag_ctx>, if any,
2076 * are used to handle fragmented content. On success it returns 1. If an error
2077 * occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002078static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002079spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002080 struct list *messages, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002081{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002082 struct spoe_config *conf = FLT_CONF(ctx->filter);
2083 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084 struct spoe_message *msg;
2085 struct sample *smp;
2086 struct spoe_arg *arg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002087 char *p, *end;
2088 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002089
Christopher Faulet8ef75252017-02-20 22:56:03 +01002090 p = ctx->buffer->p;
2091 end = p + agent->frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002092
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002093 /* Resume encoding of a SPOE message */
2094 if (ctx->frag_ctx.curmsg != NULL) {
2095 msg = ctx->frag_ctx.curmsg;
2096 goto encode_message;
2097 }
2098
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002099 /* Loop on messages */
2100 list_for_each_entry(msg, messages, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002101 ctx->frag_ctx.curmsg = msg;
2102 ctx->frag_ctx.curarg = NULL;
2103 ctx->frag_ctx.curoff = UINT_MAX;
2104
2105 encode_message:
2106 /* Resume encoding of a SPOE argument */
2107 if (ctx->frag_ctx.curarg != NULL) {
2108 arg = ctx->frag_ctx.curarg;
2109 goto encode_argument;
2110 }
2111
2112 if (ctx->frag_ctx.curoff != UINT_MAX)
2113 goto encode_msg_payload;
2114
Christopher Faulet8ef75252017-02-20 22:56:03 +01002115 /* Check if there is enough space for the message name and the
2116 * number of arguments. It implies <msg->id_len> is encoded on 2
2117 * bytes, at most (< 2288). */
2118 if (p + 2 + msg->id_len + 1 > end)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002119 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002120
Christopher Faulet8ef75252017-02-20 22:56:03 +01002121 /* Encode the message name */
2122 if (spoe_encode_buffer(msg->id, msg->id_len, &p, end) == -1)
2123 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002124
Christopher Faulet8ef75252017-02-20 22:56:03 +01002125 /* Set the number of arguments for this message */
2126 *p++ = msg->nargs;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002127
2128 ctx->frag_ctx.curoff = 0;
2129 encode_msg_payload:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002130
2131 /* Loop on arguments */
2132 list_for_each_entry(arg, &msg->args, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002133 ctx->frag_ctx.curarg = arg;
2134 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002135
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002136 encode_argument:
2137 if (ctx->frag_ctx.curoff != UINT_MAX)
2138 goto encode_arg_value;
2139
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002140 /* Encode the arguement name as a string. It can by NULL */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002141 if (spoe_encode_buffer(arg->name, arg->name_len, &p, end) == -1)
2142 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002143
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002144 ctx->frag_ctx.curoff = 0;
2145 encode_arg_value:
2146
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002147 /* Fetch the arguement value */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002148 smp = sample_process(s->be, s->sess, s,
2149 dir|SMP_OPT_FINAL, arg->expr, NULL);
2150 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, &p, end);
2151 if (ret == -1 || ctx->frag_ctx.curoff)
2152 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002153 }
2154 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002155
2156 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002157 " - encode %s messages - spoe_appctx=%p"
2158 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002159 (int)now.tv_sec, (int)now.tv_usec,
2160 agent->id, __FUNCTION__, s,
2161 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002162 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2163 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002164
Christopher Faulet8ef75252017-02-20 22:56:03 +01002165 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002166 ctx->frag_ctx.curmsg = NULL;
2167 ctx->frag_ctx.curarg = NULL;
2168 ctx->frag_ctx.curoff = 0;
2169 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002170 return 1;
2171
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002172 too_big:
2173 // FIXME: if fragmentation not supported =>
2174 // ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2175 // return -1;
2176
2177 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002178 " - encode fragmented messages - spoe_appctx=%p"
2179 " - curmsg=%p - curarg=%p - curoff=%u"
2180 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002181 (int)now.tv_sec, (int)now.tv_usec,
2182 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2183 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002184 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002185
Christopher Faulet8ef75252017-02-20 22:56:03 +01002186 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002187 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2188 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2189 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002190}
2191
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002192
2193/***************************************************************************
2194 * Functions that handle SPOE actions
2195 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002196/* Helper function to set a variable */
2197static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002198spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002199 struct sample *smp)
2200{
2201 struct spoe_config *conf = FLT_CONF(ctx->filter);
2202 struct spoe_agent *agent = conf->agent;
2203 char varname[64];
2204
2205 memset(varname, 0, sizeof(varname));
2206 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2207 scope, agent->var_pfx, len, name);
2208 vars_set_by_name_ifexist(varname, len, smp);
2209}
2210
2211/* Helper function to unset a variable */
2212static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002213spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002214 struct sample *smp)
2215{
2216 struct spoe_config *conf = FLT_CONF(ctx->filter);
2217 struct spoe_agent *agent = conf->agent;
2218 char varname[64];
2219
2220 memset(varname, 0, sizeof(varname));
2221 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2222 scope, agent->var_pfx, len, name);
2223 vars_unset_by_name_ifexist(varname, len, smp);
2224}
2225
2226
Christopher Faulet8ef75252017-02-20 22:56:03 +01002227static inline int
2228spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2229 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002230{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002231 char *str, *scope, *p = *buf;
2232 struct sample smp;
2233 uint64_t sz;
2234 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002235
Christopher Faulet8ef75252017-02-20 22:56:03 +01002236 if (p + 2 >= end)
2237 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002238
Christopher Faulet8ef75252017-02-20 22:56:03 +01002239 /* SET-VAR requires 3 arguments */
2240 if (*p++ != 3)
2241 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002242
Christopher Faulet8ef75252017-02-20 22:56:03 +01002243 switch (*p++) {
2244 case SPOE_SCOPE_PROC: scope = "proc"; break;
2245 case SPOE_SCOPE_SESS: scope = "sess"; break;
2246 case SPOE_SCOPE_TXN : scope = "txn"; break;
2247 case SPOE_SCOPE_REQ : scope = "req"; break;
2248 case SPOE_SCOPE_RES : scope = "res"; break;
2249 default: goto skip;
2250 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002251
Christopher Faulet8ef75252017-02-20 22:56:03 +01002252 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2253 goto skip;
2254 memset(&smp, 0, sizeof(smp));
2255 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002256
Christopher Faulet8ef75252017-02-20 22:56:03 +01002257 if (spoe_decode_data(&p, end, &smp) == -1)
2258 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002259
Christopher Faulet8ef75252017-02-20 22:56:03 +01002260 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2261 " - set-var '%s.%s.%.*s'\n",
2262 (int)now.tv_sec, (int)now.tv_usec,
2263 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2264 __FUNCTION__, s, scope,
2265 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2266 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002267
Christopher Faulet8ef75252017-02-20 22:56:03 +01002268 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002269
Christopher Faulet8ef75252017-02-20 22:56:03 +01002270 ret = (p - *buf);
2271 *buf = p;
2272 return ret;
2273 skip:
2274 return 0;
2275}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002276
Christopher Faulet8ef75252017-02-20 22:56:03 +01002277static inline int
2278spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2279 char **buf, char *end, int dir)
2280{
2281 char *str, *scope, *p = *buf;
2282 struct sample smp;
2283 uint64_t sz;
2284 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002285
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286 if (p + 2 >= end)
2287 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002288
Christopher Faulet8ef75252017-02-20 22:56:03 +01002289 /* UNSET-VAR requires 2 arguments */
2290 if (*p++ != 2)
2291 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002292
Christopher Faulet8ef75252017-02-20 22:56:03 +01002293 switch (*p++) {
2294 case SPOE_SCOPE_PROC: scope = "proc"; break;
2295 case SPOE_SCOPE_SESS: scope = "sess"; break;
2296 case SPOE_SCOPE_TXN : scope = "txn"; break;
2297 case SPOE_SCOPE_REQ : scope = "req"; break;
2298 case SPOE_SCOPE_RES : scope = "res"; break;
2299 default: goto skip;
2300 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002301
Christopher Faulet8ef75252017-02-20 22:56:03 +01002302 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2303 goto skip;
2304 memset(&smp, 0, sizeof(smp));
2305 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002306
Christopher Faulet8ef75252017-02-20 22:56:03 +01002307 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2308 " - unset-var '%s.%s.%.*s'\n",
2309 (int)now.tv_sec, (int)now.tv_usec,
2310 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2311 __FUNCTION__, s, scope,
2312 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2313 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002314
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315 spoe_unset_var(ctx, scope, str, sz, &smp);
2316
2317 ret = (p - *buf);
2318 *buf = p;
2319 return ret;
2320 skip:
2321 return 0;
2322}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324/* Process SPOE actions for a specific event. It returns 1 on success. If an
2325 * error occurred, 0 is returned. */
2326static int
2327spoe_process_actions(struct stream *s, struct spoe_context *ctx,
2328 enum spoe_event ev, int dir)
2329{
2330 char *p, *end;
2331 int ret;
2332
2333 p = ctx->buffer->p;
2334 end = p + ctx->buffer->i;
2335
2336 while (p < end) {
2337 enum spoe_action_type type;
2338
2339 type = *p++;
2340 switch (type) {
2341 case SPOE_ACT_T_SET_VAR:
2342 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2343 if (!ret)
2344 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002345 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002346
Christopher Faulet8ef75252017-02-20 22:56:03 +01002347 case SPOE_ACT_T_UNSET_VAR:
2348 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2349 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002350 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002351 break;
2352
2353 default:
2354 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355 }
2356 }
2357
2358 return 1;
2359 skip:
2360 return 0;
2361}
2362
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002363/***************************************************************************
2364 * Functions that process SPOE events
2365 **************************************************************************/
2366static inline int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367spoe_start_event_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002368{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002369 /* If a process is already started for this SPOE context, retry
2370 * later. */
2371 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002372 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002373
2374 /* Set the right flag to prevent request and response processing
2375 * in same time. */
2376 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2377 ? SPOE_CTX_FL_REQ_PROCESS
2378 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002379 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002380}
2381
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002382static inline void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383spoe_stop_event_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002384{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002385 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2386
2387 if (sa) {
2388 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002389 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002390 }
2391
Christopher Fauleta1cda022016-12-21 08:58:06 +01002392 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002393 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002394
Christopher Fauletb067b062017-01-04 16:39:11 +01002395 ctx->status_code = 0;
2396
Christopher Fauleta1cda022016-12-21 08:58:06 +01002397 /* Reset processing timer */
2398 ctx->process_exp = TICK_ETERNITY;
2399
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002401
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002402 ctx->frag_ctx.spoe_appctx = NULL;
2403 ctx->frag_ctx.curmsg = NULL;
2404 ctx->frag_ctx.curarg = NULL;
2405 ctx->frag_ctx.curoff = 0;
2406 ctx->frag_ctx.flags = 0;
2407
Christopher Fauleta1cda022016-12-21 08:58:06 +01002408 if (!LIST_ISEMPTY(&ctx->list)) {
2409 LIST_DEL(&ctx->list);
2410 LIST_INIT(&ctx->list);
2411 }
2412}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002413
2414/* Process a SPOE event. First, this functions will process messages attached to
2415 * this event and send them to an agent in a NOTIFY frame. Then, it will wait a
2416 * ACK frame to process corresponding actions. During all the processing, it
2417 * returns 0 and it returns 1 when the processing is finished. If an error
2418 * occurred, -1 is returned. */
2419static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002420spoe_process_event(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421 enum spoe_event ev)
2422{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002423 struct spoe_config *conf = FLT_CONF(ctx->filter);
2424 struct spoe_agent *agent = conf->agent;
2425 int dir, ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002426
2427 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2428 " - ctx-state=%s - event=%s\n",
2429 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletf7a30922016-11-10 15:04:51 +01002430 agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002431 spoe_event_str[ev]);
2432
2433 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2434
2435 if (LIST_ISEMPTY(&(ctx->messages[ev])))
2436 goto out;
2437
2438 if (ctx->state == SPOE_CTX_ST_ERROR)
2439 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002440
2441 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2442 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2443 " - failed to process event '%s': timeout\n",
2444 (int)now.tv_sec, (int)now.tv_usec,
2445 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
Christopher Fauletb067b062017-01-04 16:39:11 +01002446 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002447 goto error;
2448 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002449
2450 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002451 if (agent->eps_max > 0) {
2452 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2453 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2454 " - skip event '%s': max EPS reached\n",
2455 (int)now.tv_sec, (int)now.tv_usec,
2456 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
2457 goto skip;
2458 }
2459 }
2460
Christopher Fauletf7a30922016-11-10 15:04:51 +01002461 if (!tick_isset(ctx->process_exp)) {
2462 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2463 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2464 ctx->process_exp);
2465 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01002466 ret = spoe_start_event_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002467 if (!ret)
2468 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002469
Christopher Faulet8ef75252017-02-20 22:56:03 +01002470 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002471 goto error;
2472
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002473 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002474 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002475 }
2476
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002477 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002478 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002479 goto out;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002480 ret = spoe_encode_messages(s, ctx, &(ctx->messages[ev]), dir);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002481 if (ret < 0)
2482 goto error;
2483 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2484 }
2485
2486 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2487 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002488 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002489 ret = 0;
2490 goto out;
2491 }
2492
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002493 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2494 ret = 0;
2495 goto out;
2496 }
2497
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002498 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002499 spoe_process_actions(s, ctx, ev, dir);
2500 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002501 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002502 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002503 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002504 }
2505
2506 out:
2507 return ret;
2508
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002509 error:
Christopher Faulet48026722016-11-16 15:01:12 +01002510 if (agent->eps_max > 0)
2511 update_freq_ctr(&agent->err_per_sec, 1);
2512
Christopher Faulet985532d2016-11-16 15:36:19 +01002513 if (agent->var_on_error) {
2514 struct sample smp;
2515
2516 memset(&smp, 0, sizeof(smp));
2517 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletb067b062017-01-04 16:39:11 +01002518 smp.data.u.sint = ctx->status_code;
Christopher Faulet985532d2016-11-16 15:36:19 +01002519 smp.data.type = SMP_T_BOOL;
2520
Christopher Faulet8ef75252017-02-20 22:56:03 +01002521 spoe_set_var(ctx, "txn", agent->var_on_error,
Christopher Faulet985532d2016-11-16 15:36:19 +01002522 strlen(agent->var_on_error), &smp);
2523 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002524 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2525 " - failed to create process event '%s': code=%u\n",
2526 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2527 __FUNCTION__, ctx->strm, spoe_event_str[ev],
2528 ctx->status_code);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002529 send_log(ctx->strm->be, LOG_WARNING,
2530 "SPOE: [%s] failed to process event '%s': code=%u\n",
2531 agent->id, spoe_event_str[ev], ctx->status_code);
Christopher Faulet985532d2016-11-16 15:36:19 +01002532
Christopher Fauletea62c2a2016-11-14 10:54:21 +01002533 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2534 ? SPOE_CTX_ST_READY
Christopher Fauletb067b062017-01-04 16:39:11 +01002535 : SPOE_CTX_ST_NONE);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002536 ret = 1;
2537 goto end;
2538
2539 skip:
2540 ctx->state = SPOE_CTX_ST_READY;
2541 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002542
Christopher Fauleta1cda022016-12-21 08:58:06 +01002543 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002544 spoe_stop_event_processing(ctx);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002545 return ret;
2546}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002547
2548/***************************************************************************
2549 * Functions that create/destroy SPOE contexts
2550 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002551static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002552spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002553{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002554 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002555 return 1;
2556
Christopher Faulet4596fb72017-01-11 14:05:19 +01002557 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2558 LIST_DEL(&buffer_wait->list);
2559 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002560 }
2561
Christopher Faulet4596fb72017-01-11 14:05:19 +01002562 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563 return 1;
2564
Christopher Faulet4596fb72017-01-11 14:05:19 +01002565 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002566 return 0;
2567}
2568
2569static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002570spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002571{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002572 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2573 LIST_DEL(&buffer_wait->list);
2574 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002575 }
2576
2577 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002578 if (*buf != &buf_empty) {
2579 b_free(buf);
2580 offer_buffers(buffer_wait->target,
2581 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582 }
2583}
2584
Christopher Faulet4596fb72017-01-11 14:05:19 +01002585static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002586spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002587{
2588 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2589 return 1;
2590}
2591
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002592static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002593spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002594{
2595 struct spoe_config *conf = FLT_CONF(filter);
2596 struct spoe_context *ctx;
2597
2598 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2599 if (ctx == NULL) {
2600 return NULL;
2601 }
2602 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002603 ctx->filter = filter;
2604 ctx->state = SPOE_CTX_ST_NONE;
2605 ctx->status_code = SPOE_CTX_ERR_NONE;
2606 ctx->flags = 0;
2607 ctx->messages = conf->agent->messages;
2608 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002609 LIST_INIT(&ctx->buffer_wait.list);
2610 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002611 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002612 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002613
Christopher Fauletf7a30922016-11-10 15:04:51 +01002614 ctx->stream_id = 0;
2615 ctx->frame_id = 1;
2616 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002617
2618 return ctx;
2619}
2620
2621static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002622spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002623{
2624 if (!ctx)
2625 return;
2626
Christopher Faulet8ef75252017-02-20 22:56:03 +01002627 spoe_stop_event_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002628 pool_free2(pool2_spoe_ctx, ctx);
2629}
2630
2631static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002632spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002633{
2634 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002635 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002636}
2637
2638
2639/***************************************************************************
2640 * Hooks that manage the filter lifecycle (init/check/deinit)
2641 **************************************************************************/
2642/* Signal handler: Do a soft stop, wakeup SPOE applet */
2643static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002644spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002645{
2646 struct proxy *p;
2647
2648 p = proxy;
2649 while (p) {
2650 struct flt_conf *fconf;
2651
2652 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002653 struct spoe_config *conf;
2654 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002655 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002656
Christopher Faulet3b386a32017-02-23 10:17:15 +01002657 if (fconf->id != spoe_filter_id)
2658 continue;
2659
2660 conf = fconf->conf;
2661 agent = conf->agent;
2662
Christopher Faulet42bfa462017-01-04 14:14:19 +01002663 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002664 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002665 }
2666 }
2667 p = p->next;
2668 }
2669}
2670
2671
2672/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2673static int
2674spoe_init(struct proxy *px, struct flt_conf *fconf)
2675{
2676 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002677
2678 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2679 init_new_proxy(&conf->agent_fe);
2680 conf->agent_fe.parent = conf->agent;
2681 conf->agent_fe.last_change = now.tv_sec;
2682 conf->agent_fe.id = conf->agent->id;
2683 conf->agent_fe.cap = PR_CAP_FE;
2684 conf->agent_fe.mode = PR_MODE_TCP;
2685 conf->agent_fe.maxconn = 0;
2686 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2687 conf->agent_fe.conn_retries = CONN_RETRIES;
2688 conf->agent_fe.accept = frontend_accept;
2689 conf->agent_fe.srv = NULL;
2690 conf->agent_fe.timeout.client = TICK_ETERNITY;
2691 conf->agent_fe.default_target = &spoe_applet.obj_type;
2692 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2693
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002694 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002695 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002696 sighandler_registered = 1;
2697 }
2698
2699 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700}
2701
2702/* Free ressources allocated by the SPOE filter. */
2703static void
2704spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2705{
2706 struct spoe_config *conf = fconf->conf;
2707
2708 if (conf) {
2709 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002710
Christopher Faulet8ef75252017-02-20 22:56:03 +01002711 spoe_release_agent(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002712 free(conf);
2713 }
2714 fconf->conf = NULL;
2715}
2716
2717/* Check configuration of a SPOE filter for a specified proxy.
2718 * Return 1 on error, else 0. */
2719static int
2720spoe_check(struct proxy *px, struct flt_conf *fconf)
2721{
2722 struct spoe_config *conf = fconf->conf;
2723 struct proxy *target;
2724
2725 target = proxy_be_by_name(conf->agent->b.name);
2726 if (target == NULL) {
2727 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2728 " declared at %s:%d.\n",
2729 px->id, conf->agent->b.name, conf->agent->id,
2730 conf->agent->conf.file, conf->agent->conf.line);
2731 return 1;
2732 }
2733 if (target->mode != PR_MODE_TCP) {
2734 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2735 " at %s:%d does not support HTTP mode.\n",
2736 px->id, target->id, conf->agent->id,
2737 conf->agent->conf.file, conf->agent->conf.line);
2738 return 1;
2739 }
2740
2741 free(conf->agent->b.name);
2742 conf->agent->b.name = NULL;
2743 conf->agent->b.be = target;
2744 return 0;
2745}
2746
2747/**************************************************************************
2748 * Hooks attached to a stream
2749 *************************************************************************/
2750/* Called when a filter instance is created and attach to a stream. It creates
2751 * the context that will be used to process this stream. */
2752static int
2753spoe_start(struct stream *s, struct filter *filter)
2754{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002755 struct spoe_config *conf = FLT_CONF(filter);
2756 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002757 struct spoe_context *ctx;
2758
2759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002760 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002761 __FUNCTION__, s);
2762
Christopher Faulet8ef75252017-02-20 22:56:03 +01002763 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002764 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002765 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2766 " - failed to create SPOE context\n",
2767 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2768 __FUNCTION__, ctx->strm);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002769 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002770 "SPOE: [%s] failed to create SPOE context\n",
2771 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002772 return 0;
2773 }
2774
2775 ctx->strm = s;
2776 ctx->state = SPOE_CTX_ST_READY;
2777 filter->ctx = ctx;
2778
2779 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_FE]))
2780 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2781
2782 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_BE]))
2783 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2784
2785 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_RSP]))
2786 filter->pre_analyzers |= AN_RES_INSPECT;
2787
2788 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_FE]))
2789 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2790
2791 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_BE]))
2792 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2793
2794 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_RSP]))
2795 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2796
2797 return 1;
2798}
2799
2800/* Called when a filter instance is detached from a stream. It release the
2801 * attached SPOE context. */
2802static void
2803spoe_stop(struct stream *s, struct filter *filter)
2804{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002805 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2806 (int)now.tv_sec, (int)now.tv_usec,
2807 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2808 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002809 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002810}
2811
Christopher Fauletf7a30922016-11-10 15:04:51 +01002812
2813/*
2814 * Called when the stream is woken up because of expired timer.
2815 */
2816static void
2817spoe_check_timeouts(struct stream *s, struct filter *filter)
2818{
2819 struct spoe_context *ctx = filter->ctx;
2820
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002821 if (tick_is_expired(ctx->process_exp, now_ms)) {
2822 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002823 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002824 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002825}
2826
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002827/* Called when we are ready to filter data on a channel */
2828static int
2829spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2830{
2831 struct spoe_context *ctx = filter->ctx;
2832 int ret = 1;
2833
2834 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2835 " - ctx-flags=0x%08x\n",
2836 (int)now.tv_sec, (int)now.tv_usec,
2837 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2838 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2839
Christopher Fauletb067b062017-01-04 16:39:11 +01002840 if (ctx->state == SPOE_CTX_ST_NONE)
2841 goto out;
2842
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002843 if (!(chn->flags & CF_ISRESP)) {
2844 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2845 chn->analysers |= AN_REQ_INSPECT_FE;
2846 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2847 chn->analysers |= AN_REQ_INSPECT_BE;
2848
2849 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2850 goto out;
2851
2852 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002853 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01002854 if (!ret)
2855 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002856 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
2857 }
2858 else {
2859 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
2860 chn->analysers |= AN_RES_INSPECT;
2861
2862 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
2863 goto out;
2864
Christopher Faulet8ef75252017-02-20 22:56:03 +01002865 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002866 if (!ret) {
2867 channel_dont_read(chn);
2868 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01002869 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002870 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002871 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002872 }
2873
2874 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002875 return ret;
2876}
2877
2878/* Called before a processing happens on a given channel */
2879static int
2880spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
2881 struct channel *chn, unsigned an_bit)
2882{
2883 struct spoe_context *ctx = filter->ctx;
2884 int ret = 1;
2885
2886 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2887 " - ctx-flags=0x%08x - ana=0x%08x\n",
2888 (int)now.tv_sec, (int)now.tv_usec,
2889 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2890 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2891 ctx->flags, an_bit);
2892
Christopher Fauletb067b062017-01-04 16:39:11 +01002893 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002894 goto out;
2895
2896 switch (an_bit) {
2897 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002898 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002899 break;
2900 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002901 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902 break;
2903 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002904 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002905 break;
2906 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002907 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908 break;
2909 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002910 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002911 break;
2912 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002913 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914 break;
2915 }
2916
2917 out:
2918 if (!ret) {
2919 channel_dont_read(chn);
2920 channel_dont_close(chn);
2921 }
2922 return ret;
2923}
2924
2925/* Called when the filtering on the channel ends. */
2926static int
2927spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2928{
2929 struct spoe_context *ctx = filter->ctx;
2930
2931 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2932 " - ctx-flags=0x%08x\n",
2933 (int)now.tv_sec, (int)now.tv_usec,
2934 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2935 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2936
2937 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002938 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002939 }
2940
2941 return 1;
2942}
2943
2944/********************************************************************
2945 * Functions that manage the filter initialization
2946 ********************************************************************/
2947struct flt_ops spoe_ops = {
2948 /* Manage SPOE filter, called for each filter declaration */
2949 .init = spoe_init,
2950 .deinit = spoe_deinit,
2951 .check = spoe_check,
2952
2953 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01002954 .attach = spoe_start,
2955 .detach = spoe_stop,
2956 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957
2958 /* Handle channels activity */
2959 .channel_start_analyze = spoe_start_analyze,
2960 .channel_pre_analyze = spoe_chn_pre_analyze,
2961 .channel_end_analyze = spoe_end_analyze,
2962};
2963
2964
2965static int
2966cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
2967{
2968 const char *err;
2969 int i, err_code = 0;
2970
2971 if ((cfg_scope == NULL && curengine != NULL) ||
2972 (cfg_scope != NULL && curengine == NULL) ||
2973 strcmp(curengine, cfg_scope))
2974 goto out;
2975
2976 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
2977 if (!*args[1]) {
2978 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
2979 file, linenum);
2980 err_code |= ERR_ALERT | ERR_ABORT;
2981 goto out;
2982 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01002983 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
2984 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002985 goto out;
2986 }
2987
2988 err = invalid_char(args[1]);
2989 if (err) {
2990 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2991 file, linenum, *err, args[0], args[1]);
2992 err_code |= ERR_ALERT | ERR_ABORT;
2993 goto out;
2994 }
2995
2996 if (curagent != NULL) {
2997 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
2998 file, linenum);
2999 err_code |= ERR_ALERT | ERR_ABORT;
3000 goto out;
3001 }
3002 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
3003 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3004 err_code |= ERR_ALERT | ERR_ABORT;
3005 goto out;
3006 }
3007
3008 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003009
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 curagent->conf.file = strdup(file);
3011 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003012
3013 curagent->timeout.hello = TICK_ETERNITY;
3014 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003015 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003016
3017 curagent->engine_id = NULL;
3018 curagent->var_pfx = NULL;
3019 curagent->var_on_error = NULL;
Christopher Faulet305c6072017-02-23 16:17:53 +01003020 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003021 curagent->cps_max = 0;
3022 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003023 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003024 curagent->min_applets = 0;
3025 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003026
3027 for (i = 0; i < SPOE_EV_EVENTS; ++i)
3028 LIST_INIT(&curagent->messages[i]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003029
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003030 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003031 curagent->applets_act = 0;
3032 curagent->applets_idle = 0;
3033 curagent->sending_rate = 0;
3034
3035 LIST_INIT(&curagent->applets);
3036 LIST_INIT(&curagent->sending_queue);
3037 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038 }
3039 else if (!strcmp(args[0], "use-backend")) {
3040 if (!*args[1]) {
3041 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3042 file, linenum, args[0]);
3043 err_code |= ERR_ALERT | ERR_FATAL;
3044 goto out;
3045 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003046 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003047 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048 free(curagent->b.name);
3049 curagent->b.name = strdup(args[1]);
3050 }
3051 else if (!strcmp(args[0], "messages")) {
3052 int cur_arg = 1;
3053 while (*args[cur_arg]) {
3054 struct spoe_msg_placeholder *mp = NULL;
3055
3056 list_for_each_entry(mp, &curmps, list) {
3057 if (!strcmp(mp->id, args[cur_arg])) {
3058 Alert("parsing [%s:%d]: spoe-message message '%s' already declared.\n",
3059 file, linenum, args[cur_arg]);
3060 err_code |= ERR_ALERT | ERR_FATAL;
3061 goto out;
3062 }
3063 }
3064
3065 if ((mp = calloc(1, sizeof(*mp))) == NULL) {
3066 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3067 err_code |= ERR_ALERT | ERR_ABORT;
3068 goto out;
3069 }
3070 mp->id = strdup(args[cur_arg]);
3071 LIST_ADDQ(&curmps, &mp->list);
3072 cur_arg++;
3073 }
3074 }
3075 else if (!strcmp(args[0], "timeout")) {
3076 unsigned int *tv = NULL;
3077 const char *res;
3078 unsigned timeout;
3079
3080 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003081 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003082 file, linenum);
3083 err_code |= ERR_ALERT | ERR_FATAL;
3084 goto out;
3085 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003086 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3087 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003088 if (!strcmp(args[1], "hello"))
3089 tv = &curagent->timeout.hello;
3090 else if (!strcmp(args[1], "idle"))
3091 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003092 else if (!strcmp(args[1], "processing"))
3093 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003094 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003095 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003096 file, linenum, args[1]);
3097 err_code |= ERR_ALERT | ERR_FATAL;
3098 goto out;
3099 }
3100 if (!*args[2]) {
3101 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3102 file, linenum, args[1]);
3103 err_code |= ERR_ALERT | ERR_FATAL;
3104 goto out;
3105 }
3106 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3107 if (res) {
3108 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3109 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003110 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003111 goto out;
3112 }
3113 *tv = MS_TO_TICKS(timeout);
3114 }
3115 else if (!strcmp(args[0], "option")) {
3116 if (!*args[1]) {
3117 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3118 file, linenum, args[0]);
3119 err_code |= ERR_ALERT | ERR_FATAL;
3120 goto out;
3121 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003122
Christopher Faulet305c6072017-02-23 16:17:53 +01003123 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003124 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003125 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003126 if (kwm == 1)
3127 curagent->flags &= ~SPOE_FL_PIPELINING;
3128 else
3129 curagent->flags |= SPOE_FL_PIPELINING;
3130 goto out;
3131 }
3132 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003133 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003134 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003135 if (kwm == 1)
3136 curagent->flags &= ~SPOE_FL_ASYNC;
3137 else
3138 curagent->flags |= SPOE_FL_ASYNC;
3139 goto out;
3140 }
3141
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003142 /* Following options does not support negation */
3143 if (kwm == 1) {
3144 Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3145 file, linenum, args[1]);
3146 err_code |= ERR_ALERT | ERR_FATAL;
3147 goto out;
3148 }
3149
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003150 if (!strcmp(args[1], "var-prefix")) {
3151 char *tmp;
3152
3153 if (!*args[2]) {
3154 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3155 file, linenum, args[0],
3156 args[1]);
3157 err_code |= ERR_ALERT | ERR_FATAL;
3158 goto out;
3159 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003160 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3161 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003162 tmp = args[2];
3163 while (*tmp) {
3164 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3165 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3166 file, linenum, args[0], args[1]);
3167 err_code |= ERR_ALERT | ERR_FATAL;
3168 goto out;
3169 }
3170 tmp++;
3171 }
3172 curagent->var_pfx = strdup(args[2]);
3173 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003174 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003175 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003176 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003177 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3178 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003179 else if (!strcmp(args[1], "set-on-error")) {
3180 char *tmp;
3181
3182 if (!*args[2]) {
3183 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3184 file, linenum, args[0],
3185 args[1]);
3186 err_code |= ERR_ALERT | ERR_FATAL;
3187 goto out;
3188 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003189 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3190 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003191 tmp = args[2];
3192 while (*tmp) {
3193 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3194 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3195 file, linenum, args[0], args[1]);
3196 err_code |= ERR_ALERT | ERR_FATAL;
3197 goto out;
3198 }
3199 tmp++;
3200 }
3201 curagent->var_on_error = strdup(args[2]);
3202 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003203 else {
3204 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3205 file, linenum, args[1]);
3206 err_code |= ERR_ALERT | ERR_FATAL;
3207 goto out;
3208 }
Christopher Faulet48026722016-11-16 15:01:12 +01003209 }
3210 else if (!strcmp(args[0], "maxconnrate")) {
3211 if (!*args[1]) {
3212 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3213 file, linenum, args[0]);
3214 err_code |= ERR_ALERT | ERR_FATAL;
3215 goto out;
3216 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003217 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003218 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003219 curagent->cps_max = atol(args[1]);
3220 }
3221 else if (!strcmp(args[0], "maxerrrate")) {
3222 if (!*args[1]) {
3223 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3224 file, linenum, args[0]);
3225 err_code |= ERR_ALERT | ERR_FATAL;
3226 goto out;
3227 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003228 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003229 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003230 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003231 }
3232 else if (*args[0]) {
3233 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3234 file, linenum, args[0]);
3235 err_code |= ERR_ALERT | ERR_FATAL;
3236 goto out;
3237 }
3238 out:
3239 return err_code;
3240}
3241
3242static int
3243cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3244{
3245 struct spoe_message *msg;
3246 struct spoe_arg *arg;
3247 const char *err;
3248 char *errmsg = NULL;
3249 int err_code = 0;
3250
3251 if ((cfg_scope == NULL && curengine != NULL) ||
3252 (cfg_scope != NULL && curengine == NULL) ||
3253 strcmp(curengine, cfg_scope))
3254 goto out;
3255
3256 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3257 if (!*args[1]) {
3258 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3259 file, linenum);
3260 err_code |= ERR_ALERT | ERR_ABORT;
3261 goto out;
3262 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003263 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3264 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003265 goto out;
3266 }
3267
3268 err = invalid_char(args[1]);
3269 if (err) {
3270 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3271 file, linenum, *err, args[0], args[1]);
3272 err_code |= ERR_ALERT | ERR_ABORT;
3273 goto out;
3274 }
3275
3276 list_for_each_entry(msg, &curmsgs, list) {
3277 if (!strcmp(msg->id, args[1])) {
3278 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3279 " name as another one declared at %s:%d.\n",
3280 file, linenum, args[1], msg->conf.file, msg->conf.line);
3281 err_code |= ERR_ALERT | ERR_FATAL;
3282 goto out;
3283 }
3284 }
3285
3286 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3287 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3288 err_code |= ERR_ALERT | ERR_ABORT;
3289 goto out;
3290 }
3291
3292 curmsg->id = strdup(args[1]);
3293 curmsg->id_len = strlen(curmsg->id);
3294 curmsg->event = SPOE_EV_NONE;
3295 curmsg->conf.file = strdup(file);
3296 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003297 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003298 LIST_INIT(&curmsg->args);
3299 LIST_ADDQ(&curmsgs, &curmsg->list);
3300 }
3301 else if (!strcmp(args[0], "args")) {
3302 int cur_arg = 1;
3303
3304 curproxy->conf.args.ctx = ARGC_SPOE;
3305 curproxy->conf.args.file = file;
3306 curproxy->conf.args.line = linenum;
3307 while (*args[cur_arg]) {
3308 char *delim = strchr(args[cur_arg], '=');
3309 int idx = 0;
3310
3311 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3312 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3313 err_code |= ERR_ALERT | ERR_ABORT;
3314 goto out;
3315 }
3316
3317 if (!delim) {
3318 arg->name = NULL;
3319 arg->name_len = 0;
3320 delim = args[cur_arg];
3321 }
3322 else {
3323 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3324 arg->name_len = delim - args[cur_arg];
3325 delim++;
3326 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003327 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3328 &idx, file, linenum, &errmsg,
3329 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003330 if (arg->expr == NULL) {
3331 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3332 err_code |= ERR_ALERT | ERR_FATAL;
3333 free(arg->name);
3334 free(arg);
3335 goto out;
3336 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003337 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003338 LIST_ADDQ(&curmsg->args, &arg->list);
3339 cur_arg++;
3340 }
3341 curproxy->conf.args.file = NULL;
3342 curproxy->conf.args.line = 0;
3343 }
3344 else if (!strcmp(args[0], "event")) {
3345 if (!*args[1]) {
3346 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003347 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003348 goto out;
3349 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003350 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3351 goto out;
3352
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003353 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3354 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3355 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3356 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3357
3358 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3359 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3360 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3361 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3362 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3363 curmsg->event = SPOE_EV_ON_TCP_RSP;
3364
3365 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3366 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3367 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3368 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3369 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3370 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3371 else {
3372 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3373 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003374 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003375 goto out;
3376 }
3377 }
3378 else if (!*args[0]) {
3379 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3380 file, linenum, args[0]);
3381 err_code |= ERR_ALERT | ERR_FATAL;
3382 goto out;
3383 }
3384 out:
3385 free(errmsg);
3386 return err_code;
3387}
3388
3389/* Return -1 on error, else 0 */
3390static int
3391parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3392 struct flt_conf *fconf, char **err, void *private)
3393{
3394 struct list backup_sections;
3395 struct spoe_config *conf;
3396 struct spoe_message *msg, *msgback;
3397 struct spoe_msg_placeholder *mp, *mpback;
3398 char *file = NULL, *engine = NULL;
3399 int ret, pos = *cur_arg + 1;
3400
3401 conf = calloc(1, sizeof(*conf));
3402 if (conf == NULL) {
3403 memprintf(err, "%s: out of memory", args[*cur_arg]);
3404 goto error;
3405 }
3406 conf->proxy = px;
3407
3408 while (*args[pos]) {
3409 if (!strcmp(args[pos], "config")) {
3410 if (!*args[pos+1]) {
3411 memprintf(err, "'%s' : '%s' option without value",
3412 args[*cur_arg], args[pos]);
3413 goto error;
3414 }
3415 file = args[pos+1];
3416 pos += 2;
3417 }
3418 else if (!strcmp(args[pos], "engine")) {
3419 if (!*args[pos+1]) {
3420 memprintf(err, "'%s' : '%s' option without value",
3421 args[*cur_arg], args[pos]);
3422 goto error;
3423 }
3424 engine = args[pos+1];
3425 pos += 2;
3426 }
3427 else {
3428 memprintf(err, "unknown keyword '%s'", args[pos]);
3429 goto error;
3430 }
3431 }
3432 if (file == NULL) {
3433 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3434 goto error;
3435 }
3436
3437 /* backup sections and register SPOE sections */
3438 LIST_INIT(&backup_sections);
3439 cfg_backup_sections(&backup_sections);
3440 cfg_register_section("spoe-agent", cfg_parse_spoe_agent);
3441 cfg_register_section("spoe-message", cfg_parse_spoe_message);
3442
3443 /* Parse SPOE filter configuration file */
3444 curengine = engine;
3445 curproxy = px;
3446 curagent = NULL;
3447 curmsg = NULL;
3448 ret = readcfgfile(file);
3449 curproxy = NULL;
3450
3451 /* unregister SPOE sections and restore previous sections */
3452 cfg_unregister_sections();
3453 cfg_restore_sections(&backup_sections);
3454
3455 if (ret == -1) {
3456 memprintf(err, "Could not open configuration file %s : %s",
3457 file, strerror(errno));
3458 goto error;
3459 }
3460 if (ret & (ERR_ABORT|ERR_FATAL)) {
3461 memprintf(err, "Error(s) found in configuration file %s", file);
3462 goto error;
3463 }
3464
3465 /* Check SPOE agent */
3466 if (curagent == NULL) {
3467 memprintf(err, "No SPOE agent found in file %s", file);
3468 goto error;
3469 }
3470 if (curagent->b.name == NULL) {
3471 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3472 curagent->id, curagent->conf.file, curagent->conf.line);
3473 goto error;
3474 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003475 if (curagent->timeout.hello == TICK_ETERNITY ||
3476 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003477 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003478 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3479 " | While not properly invalid, you will certainly encounter various problems\n"
3480 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003481 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003482 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3483 }
3484 if (curagent->var_pfx == NULL) {
3485 char *tmp = curagent->id;
3486
3487 while (*tmp) {
3488 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3489 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3490 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3491 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3492 goto error;
3493 }
3494 tmp++;
3495 }
3496 curagent->var_pfx = strdup(curagent->id);
3497 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003498 if (curagent->engine_id == NULL)
3499 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003500
3501 if (LIST_ISEMPTY(&curmps)) {
3502 Warning("Proxy '%s': No message used by SPOE agent '%s' declared at %s:%d.\n",
3503 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3504 goto finish;
3505 }
3506
3507 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3508 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003509 struct spoe_arg *arg;
3510 unsigned int where;
3511
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003512 if (!strcmp(msg->id, mp->id)) {
3513 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3514 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3515 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3516 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3517 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3518 }
3519 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3520 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3521 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3522 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3523 px->id, msg->conf.file, msg->conf.line);
3524 goto next;
3525 }
3526 if (msg->event == SPOE_EV_NONE) {
3527 Warning("Proxy '%s': Ignore SPOE message without event at %s:%d.\n",
3528 px->id, msg->conf.file, msg->conf.line);
3529 goto next;
3530 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003531
3532 where = 0;
3533 switch (msg->event) {
3534 case SPOE_EV_ON_CLIENT_SESS:
3535 where |= SMP_VAL_FE_CON_ACC;
3536 break;
3537
3538 case SPOE_EV_ON_TCP_REQ_FE:
3539 where |= SMP_VAL_FE_REQ_CNT;
3540 break;
3541
3542 case SPOE_EV_ON_HTTP_REQ_FE:
3543 where |= SMP_VAL_FE_HRQ_HDR;
3544 break;
3545
3546 case SPOE_EV_ON_TCP_REQ_BE:
3547 if (px->cap & PR_CAP_FE)
3548 where |= SMP_VAL_FE_REQ_CNT;
3549 if (px->cap & PR_CAP_BE)
3550 where |= SMP_VAL_BE_REQ_CNT;
3551 break;
3552
3553 case SPOE_EV_ON_HTTP_REQ_BE:
3554 if (px->cap & PR_CAP_FE)
3555 where |= SMP_VAL_FE_HRQ_HDR;
3556 if (px->cap & PR_CAP_BE)
3557 where |= SMP_VAL_BE_HRQ_HDR;
3558 break;
3559
3560 case SPOE_EV_ON_SERVER_SESS:
3561 where |= SMP_VAL_BE_SRV_CON;
3562 break;
3563
3564 case SPOE_EV_ON_TCP_RSP:
3565 if (px->cap & PR_CAP_FE)
3566 where |= SMP_VAL_FE_RES_CNT;
3567 if (px->cap & PR_CAP_BE)
3568 where |= SMP_VAL_BE_RES_CNT;
3569 break;
3570
3571 case SPOE_EV_ON_HTTP_RSP:
3572 if (px->cap & PR_CAP_FE)
3573 where |= SMP_VAL_FE_HRS_HDR;
3574 if (px->cap & PR_CAP_BE)
3575 where |= SMP_VAL_BE_HRS_HDR;
3576 break;
3577
3578 default:
3579 break;
3580 }
3581
3582 list_for_each_entry(arg, &msg->args, list) {
3583 if (!(arg->expr->fetch->val & where)) {
3584 Warning("Proxy '%s': Ignore SPOE message at %s:%d: "
3585 "some args extract information from '%s', "
3586 "none of which is available here ('%s').\n",
3587 px->id, msg->conf.file, msg->conf.line,
3588 sample_ckp_names(arg->expr->fetch->use),
3589 sample_ckp_names(where));
3590 goto next;
3591 }
3592 }
3593
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003594 msg->agent = curagent;
3595 LIST_DEL(&msg->list);
3596 LIST_ADDQ(&curagent->messages[msg->event], &msg->list);
3597 goto next;
3598 }
3599 }
3600 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
3601 curagent->id, mp->id, curagent->conf.file, curagent->conf.line);
3602 goto error;
3603 next:
3604 continue;
3605 }
3606
3607 finish:
3608 conf->agent = curagent;
3609 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3610 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003611 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003612 }
3613 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3614 Warning("Proxy '%s': Ignore unused SPOE messages '%s' declared at %s:%d.\n",
3615 px->id, msg->id, msg->conf.file, msg->conf.line);
3616 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003617 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003618 }
3619
3620 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01003621 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003622 fconf->ops = &spoe_ops;
3623 fconf->conf = conf;
3624 return 0;
3625
3626 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003627 spoe_release_agent(curagent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003628 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3629 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003630 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003631 }
3632 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3633 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003634 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003635 }
3636 free(conf);
3637 return -1;
3638}
3639
3640
3641/* Declare the filter parser for "spoe" keyword */
3642static struct flt_kw_list flt_kws = { "SPOE", { }, {
3643 { "spoe", parse_spoe_flt, NULL },
3644 { NULL, NULL, NULL },
3645 }
3646};
3647
3648__attribute__((constructor))
3649static void __spoe_init(void)
3650{
3651 flt_register_keywords(&flt_kws);
3652
3653 LIST_INIT(&curmsgs);
3654 LIST_INIT(&curmps);
3655 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003656 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003657}
3658
3659__attribute__((destructor))
3660static void
3661__spoe_deinit(void)
3662{
3663 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003664 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003665}