blob: 8156287c66661aa9a7721531ba14a84dcd17da55 [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
288/* Comma-separated list of supported capabilities (none for now) */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100289#define CAPABILITIES_VAL "pipelining,async"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200290
Christopher Faulet8ef75252017-02-20 22:56:03 +0100291/* Convert a string to a SPOE version value. The string must follow the format
292 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
293 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200294static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100295spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100297 const char *p, *end;
298 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200299
Christopher Faulet8ef75252017-02-20 22:56:03 +0100300 p = str;
301 end = str+len;
302 maj = min = 0;
303 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304
Christopher Faulet8ef75252017-02-20 22:56:03 +0100305 /* skip leading spaces */
306 while (p < end && isspace(*p))
307 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309 /* parse Major number, until the '.' */
310 while (*p != '.') {
311 if (p >= end || *p < '0' || *p > '9')
312 goto out;
313 maj *= 10;
314 maj += (*p - '0');
315 p++;
316 }
317
318 /* check Major version */
319 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320 goto out;
321
Christopher Faulet8ef75252017-02-20 22:56:03 +0100322 p++; /* skip the '.' */
323 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
324 goto out;
325
326 /* Parse Minor number */
327 while (p < end) {
328 if (*p < '0' || *p > '9')
329 break;
330 min *= 10;
331 min += (*p - '0');
332 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200333 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100334
335 /* check Minor number */
336 if (min > 999)
337 goto out;
338
339 /* skip trailing spaces */
340 while (p < end && isspace(*p))
341 p++;
342 if (p != end)
343 goto out;
344
345 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346 out:
347 return vsn;
348}
349
Christopher Faulet8ef75252017-02-20 22:56:03 +0100350/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
351 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
352 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200353static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100354spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200355{
Christopher Faulet42bfa462017-01-04 14:14:19 +0100356 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100357 char *p, *end;
358 unsigned int flags = SPOE_FRM_FL_FIN;
359 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200360
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361 p = frame;
362 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200363
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364 /* Set Frame type */
365 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100367 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100368 memcpy(p, (char *)&flags, 4);
369 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200370
371 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100372 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200373
374 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
375 * and "capabilities" */
376
377 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100378 sz = SLEN(SUPPORTED_VERSIONS_KEY);
379 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
380 goto too_big;
381
382 *p++ = SPOE_DATA_T_STR;
383 sz = SLEN(SUPPORTED_VERSIONS_VAL);
384 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
385 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386
387 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 sz = SLEN(MAX_FRAME_SIZE_KEY);
389 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
390 goto too_big;
391
392 *p++ = SPOE_DATA_T_UINT32;
393 if (spoe_encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
394 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200395
396 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100397 sz = SLEN(CAPABILITIES_KEY);
398 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
399 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
Christopher Faulet8ef75252017-02-20 22:56:03 +0100401 *p++ = SPOE_DATA_T_STR;
402 sz = SLEN(CAPABILITIES_VAL);
403 if (spoe_encode_buffer(CAPABILITIES_VAL, sz, &p, end) == -1)
404 goto too_big;
405
406 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100407 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 sz = SLEN(ENGINE_ID_KEY);
409 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
410 goto too_big;
411
412 *p++ = SPOE_DATA_T_STR;
413 sz = strlen(agent->engine_id);
414 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
415 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100416 }
417
Christopher Faulet8ef75252017-02-20 22:56:03 +0100418 return (p - frame);
419
420 too_big:
421 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
422 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200423}
424
Christopher Faulet8ef75252017-02-20 22:56:03 +0100425/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
426 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
427 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200428static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100429spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 const char *reason;
432 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100433 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 p = frame;
437 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200438
Christopher Faulet8ef75252017-02-20 22:56:03 +0100439 /* Set Frame type */
440 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200441
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100442 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 memcpy(p, (char *)&flags, 4);
444 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200445
446 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100447 *p++ = 0; *p++ = 0;
448
449 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
450 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200451
452 /* There are 2 mandatory items: "status-code" and "message" */
453
454 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455 sz = SLEN(STATUS_CODE_KEY);
456 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
457 goto too_big;
458
459 *p++ = SPOE_DATA_T_UINT32;
460 if (spoe_encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
461 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462
463 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 sz = SLEN(MSG_KEY);
465 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
466 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467
Christopher Faulet8ef75252017-02-20 22:56:03 +0100468 /*Get the message corresponding to the status code */
469 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
470
471 *p++ = SPOE_DATA_T_STR;
472 sz = strlen(reason);
473 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
474 goto too_big;
475
476 return (p - frame);
477
478 too_big:
479 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
480 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200481}
482
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
484 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
485 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100488 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 char *p, *end;
491 unsigned int stream_id, frame_id;
492 unsigned int flags = SPOE_FRM_FL_FIN;
493 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200494
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 p = frame;
496 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200497
Christopher Faulet8ef75252017-02-20 22:56:03 +0100498 /* <ctx> is null when the stream has aborted the processing of a
499 * fragmented frame. In this case, we must notify the corresponding
500 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100501 if (ctx == NULL) {
502 flags |= SPOE_FRM_FL_ABRT;
503 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
504 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
505 }
506 else {
507 stream_id = ctx->stream_id;
508 frame_id = ctx->frame_id;
509
510 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511 /* The fragmentation is not supported by the applet */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100512 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
514 return -1;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100515 }
516 flags = ctx->frag_ctx.flags;
517 }
518 }
519
Christopher Faulet8ef75252017-02-20 22:56:03 +0100520 /* Set Frame type */
521 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
522
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100523 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100524 memcpy(p, (char *)&flags, 4);
525 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200526
527 /* Set stream-id and frame-id */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100528 if (spoe_encode_varint(stream_id, &p, end) == -1)
529 goto too_big;
530 if (spoe_encode_varint(frame_id, &p, end) == -1)
531 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200532
Christopher Faulet8ef75252017-02-20 22:56:03 +0100533 /* Copy encoded messages, if possible */
534 sz = SPOE_APPCTX(appctx)->buffer->i;
535 if (p + sz >= end)
536 goto too_big;
537 memcpy(p, SPOE_APPCTX(appctx)->buffer->p, sz);
538 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100539
Christopher Faulet8ef75252017-02-20 22:56:03 +0100540 return (p - frame);
541
542 too_big:
543 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
544 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200545}
546
Christopher Faulet8ef75252017-02-20 22:56:03 +0100547/* Decode and process the HELLO frame sent by an agent. It returns the number of
548 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
549 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200550static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100551spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200552{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100553 char *p, *end;
554 int vsn, max_frame_size;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100555 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100556
557 p = frame;
558 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200559
560 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100561 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
562 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200563 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100564 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200565
Christopher Faulet8ef75252017-02-20 22:56:03 +0100566 if (size < 7 /* TYPE + METADATA */) {
567 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
568 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200569 }
570
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100571 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100572 memcpy((char *)&flags, p, 4);
573 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200574
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100575 /* Fragmentation is not supported for HELLO frame */
576 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100577 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100578 return -1;
579 }
580
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200581 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100582 if (*p != 0 || *(p+1) != 0) {
583 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
584 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200585 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100586 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200587
588 /* There are 3 mandatory items: "version", "max-frame-size" and
589 * "capabilities" */
590
591 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100592 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100593 while (p < end) {
594 char *str;
595 size_t sz;
596 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200597
598 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100599 ret = spoe_decode_buffer(&p, end, &str, &sz);
600 if (ret == -1 || !sz) {
601 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
602 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200603 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100604
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200605 /* Check "version" K/V item */
606 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607 int i, type = *p++;
608
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200609 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
611 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
612 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200613 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
615 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
616 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200617 }
618
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200620 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200622 return -1;
623 }
624 for (i = 0; supported_versions[i].str != NULL; ++i) {
625 if (vsn >= supported_versions[i].min &&
626 vsn <= supported_versions[i].max)
627 break;
628 }
629 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200631 return -1;
632 }
633 }
634 /* Check "max-frame-size" K/V item */
635 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200637
638 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
640 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
641 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
642 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
644 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200645 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646 if (spoe_decode_varint(&p, end, &sz) == -1) {
647 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
648 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650 if (sz < MIN_FRAME_SIZE ||
651 sz > SPOE_APPCTX(appctx)->max_frame_size) {
652 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653 return -1;
654 }
655 max_frame_size = sz;
656 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100657 /* Check "capabilities" K/V item */
658 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100660
661 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
663 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
664 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100665 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100666 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
667 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
668 return 0;
669 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100670
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100672 char *delim;
673
674 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100676
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
678 str += 10; sz -= 10;
679 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100680 flags |= SPOE_APPCTX_FL_PIPELINING;
681 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 else if (sz >= 5 && !strncmp(str, "async", 5)) {
683 str += 5; sz -= 5;
684 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100685 flags |= SPOE_APPCTX_FL_ASYNC;
686 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
688 str += 13; sz -= 13;
689 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100690 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
691 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100692
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 /* Get the next comma or break */
694 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100695 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696 delim++;
697 sz -= (delim - str);
698 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100699 }
700 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701 else {
702 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 if (spoe_skip_data(&p, end) == -1) {
704 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
705 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200706 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 }
708 }
709
710 /* Final checks */
711 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 return -1;
714 }
715 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717 return -1;
718 }
719
Christopher Faulet42bfa462017-01-04 14:14:19 +0100720 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
721 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
722 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723
724 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200725}
726
727/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
728 * bytes on success, 0 if the frame can be ignored and -1 if an error
729 * occurred. */
730static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100731spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100734 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735
736 p = frame;
737 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200738
739 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
741 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200742 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 if (size < 7 /* TYPE + METADATA */) {
746 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
747 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200748 }
749
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100750 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 memcpy((char *)&flags, p, 4);
752 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200753
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100754 /* Fragmentation is not supported for DISCONNECT frame */
755 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100757 return -1;
758 }
759
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200760 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 if (*p != 0 || *(p+1) != 0) {
762 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
763 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200764 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200766
767 /* There are 2 mandatory items: "status-code" and "message" */
768
769 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 while (p < end) {
771 char *str;
772 size_t sz;
773 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200774
775 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 ret = spoe_decode_buffer(&p, end, &str, &sz);
777 if (ret == -1 || !sz) {
778 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
779 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200780 }
781
782 /* Check "status-code" K/V item */
783 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100784 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200785
786 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200787 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
788 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
789 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
790 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
792 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200793 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100794 if (spoe_decode_varint(&p, end, &sz) == -1) {
795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
796 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200799 }
800
801 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 else if (!memcmp(str, MSG_KEY, sz)) {
803 int type = *p++;
804
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100806 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
807 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
808 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200809 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100810 ret = spoe_decode_buffer(&p, end, &str, &sz);
811 if (ret == -1 || sz > 255) {
812 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
813 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200814 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100815#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
816 SPOE_APPCTX(appctx)->reason = str;
817 SPOE_APPCTX(appctx)->rlen = sz;
818#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819 }
820 else {
821 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822 if (spoe_skip_data(&p, end) == -1) {
823 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
824 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826 }
827 }
828
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830}
831
832
Christopher Fauleta1cda022016-12-21 08:58:06 +0100833/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834 * success, 0 if the frame can be ignored and -1 if an error occurred. */
835static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100837 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100839 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100841 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100843 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844
845 p = frame;
846 end = frame + size;
847 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200848
849 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
851 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 if (size < 7 /* TYPE + METADATA */) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
859
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100860 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 memcpy((char *)&flags, p, 4);
862 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100864 /* Fragmentation is not supported for now */
865 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100867 return -1;
868 }
869
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200870 /* Get the stream-id and the frame-id */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100871 if (spoe_decode_varint(&p, end, &stream_id) == -1) {
872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100873 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 }
875 if (spoe_decode_varint(&p, end, &frame_id) == -1) {
876 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100879
Christopher Faulet8ef75252017-02-20 22:56:03 +0100880 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100881 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100882 list_for_each_entry((*ctx), &agent->waiting_queue, list) {
883 if ((*ctx)->stream_id == (unsigned int)stream_id &&
884 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100885 goto found;
886 }
887 }
888 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100889 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
890 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100892 goto found;
893 }
894 }
895
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100896 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
897 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
898 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
899
900 /* ABRT bit is set for an unfinished fragmented frame */
901 if (flags & SPOE_FRM_FL_ABRT) {
902 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
903 (*ctx)->frag_ctx.spoe_appctx = NULL;
904 (*ctx)->state = SPOE_CTX_ST_ERROR;
905 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
906 /* Ignore the payload */
907 goto end;
908 }
909 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
910 /* For now, we ignore the ack */
911 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
912 return 0;
913 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100914
Christopher Fauleta1cda022016-12-21 08:58:06 +0100915 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
917 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100918 " - stream-id=%u - frame-id=%u\n",
919 (int)now.tv_sec, (int)now.tv_usec, agent->id,
920 __FUNCTION__, appctx,
921 (unsigned int)stream_id, (unsigned int)frame_id);
922
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100923 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100924 return 0;
925
926 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100927 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
928 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100929 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100930 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 }
Christopher Faulet4596fb72017-01-11 14:05:19 +0100932
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 len = (end - p);
935 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
936 SPOE_APPCTX(appctx)->buffer->i = len;
937 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200938
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100939 /* Transfer the buffer ownership to the SPOE context */
940 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
941 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100942
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 (*ctx)->state = SPOE_CTX_ST_DONE;
944
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100945 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100946 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 " - ACK frame received"
948 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100949 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
951 (*ctx)->frame_id, flags);
952 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200953}
954
Christopher Fauletba7bc162016-11-07 21:07:38 +0100955/* This function is used in cfgparse.c and declared in proto/checks.h. It
956 * prepare the request to send to agents during a healthcheck. It returns 0 on
957 * success and -1 if an error occurred. */
958int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100960{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 struct appctx appctx;
962 struct spoe_appctx spoe_appctx;
963 char *frame, *end, buf[MAX_FRAME_SIZE+4];
964 size_t sz;
965 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100966
Christopher Faulet42bfa462017-01-04 14:14:19 +0100967 memset(&appctx, 0, sizeof(appctx));
968 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +0100969 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +0100970
971 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +0100972 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100973
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
975 end = frame + MAX_FRAME_SIZE;
976
977 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
978 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100979 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100980 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +0100981
Christopher Faulet8ef75252017-02-20 22:56:03 +0100982 /* Add "healthcheck" K/V item */
983 sz = SLEN(HEALTHCHECK_KEY);
984 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
985 return -1;
986 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +0100987
Christopher Faulet8ef75252017-02-20 22:56:03 +0100988 *len = frame - buf;
989 sz = htonl(*len - 4);
990 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +0100991
Christopher Faulet8ef75252017-02-20 22:56:03 +0100992 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +0100993 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100994 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +0100995 return 0;
996}
997
998/* This function is used in checks.c and declared in proto/checks.h. It decode
999 * the response received from an agent during a healthcheck. It returns 0 on
1000 * success and -1 if an error occurred. */
1001int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001002spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001003{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001004 struct appctx appctx;
1005 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001006
Christopher Faulet42bfa462017-01-04 14:14:19 +01001007 memset(&appctx, 0, sizeof(appctx));
1008 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001009
Christopher Faulet42bfa462017-01-04 14:14:19 +01001010 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001011 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001012
Christopher Faulet8ef75252017-02-20 22:56:03 +01001013 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1014 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001015 goto error;
1016 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001017 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1018 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001019
1020 return 0;
1021
1022 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001023 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1024 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1025 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001026 return -1;
1027}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001028
Christopher Fauleta1cda022016-12-21 08:58:06 +01001029/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1030 * the frame can be ignored, 1 to retry later, and the frame legnth on
1031 * success. */
1032static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001033spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001034{
1035 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 int ret;
1037 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001038
1039 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001040 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001041
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1043 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044 netint = htonl(framesz);
1045 memcpy(buf, (char *)&netint, 4);
1046 ret = bi_putblk(si_ic(si), buf, framesz+4);
1047
1048 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 if (ret == -1) {
1050 retry:
1051 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001052 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001053 }
1054 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001055 return -1; /* error */
1056 }
1057 return framesz;
1058}
1059
1060/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1061 * when the frame can be ignored, 1 to retry later and the frame length on
1062 * success. */
1063static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001065{
1066 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001067 int ret;
1068 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001069
1070 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001071 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001072
1073 ret = bo_getblk(si_oc(si), (char *)&netint, 4, 0);
1074 if (ret > 0) {
1075 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001076 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001077 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001078 return -1;
1079 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 ret = bo_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001081 }
1082 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 if (ret == 0) {
1084 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001085 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001086 }
1087 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001088 return -1; /* error */
1089 }
1090 return framesz;
1091}
1092
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001093/********************************************************************
1094 * Functions that manage the SPOE applet
1095 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001096static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001098{
1099 si_applet_want_get(appctx->owner);
1100 si_applet_want_put(appctx->owner);
1101 appctx_wakeup(appctx);
1102 return 1;
1103}
1104
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001105/* Callback function that catches applet timeouts. If a timeout occurred, we set
1106 * <appctx->st1> flag and the SPOE applet is woken up. */
1107static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001108spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001109{
1110 struct appctx *appctx = task->context;
1111
1112 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1113 if (tick_is_expired(task->expire, now_ms)) {
1114 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001116 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001118 return task;
1119}
1120
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001121/* Callback function that releases a SPOE applet. This happens when the
1122 * connection with the agent is closed. */
1123static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001124spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001125{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 struct stream_interface *si = appctx->owner;
1127 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1128 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001130
1131 if (spoe_appctx == NULL)
1132 return;
1133
1134 appctx->ctx.spoe.ptr = NULL;
1135 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136
1137 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1138 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1139 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001140
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 /* Remove applet from the list of running applets */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142 agent->applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001143 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1144 LIST_DEL(&spoe_appctx->list);
1145 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001147
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001149 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
1151 agent->applets_idle--;
1152
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001153 si_shutw(si);
1154 si_shutr(si);
1155 si_ic(si)->flags |= CF_READ_NULL;
1156 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1158 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001159 }
1160
Christopher Faulet8ef75252017-02-20 22:56:03 +01001161 /* Destroy the task attached to this applet */
1162 if (spoe_appctx->task) {
1163 task_delete(spoe_appctx->task);
1164 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001165 }
1166
Christopher Faulet8ef75252017-02-20 22:56:03 +01001167 /* Notify all waiting streams */
1168 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169 LIST_DEL(&ctx->list);
1170 LIST_INIT(&ctx->list);
1171 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001172 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001173 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001174 }
1175
Christopher Faulet8ef75252017-02-20 22:56:03 +01001176 /* If the applet was processing a fragmented frame, notify the
1177 * corresponding stream. */
1178 if (spoe_appctx->frag_ctx.ctx) {
1179 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001180 ctx->frag_ctx.spoe_appctx = NULL;
1181 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001183 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1184 }
1185
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186 /* Release allocated memory */
1187 spoe_release_buffer(&spoe_appctx->buffer,
1188 &spoe_appctx->buffer_wait);
1189 pool_free2(pool2_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001190
Christopher Fauleta1cda022016-12-21 08:58:06 +01001191 if (!LIST_ISEMPTY(&agent->applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001192 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001193
Christopher Faulet8ef75252017-02-20 22:56:03 +01001194 /* If this was the last running applet, notify all waiting streams */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001195 list_for_each_entry_safe(ctx, back, &agent->sending_queue, list) {
1196 LIST_DEL(&ctx->list);
1197 LIST_INIT(&ctx->list);
1198 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001199 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001200 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001201 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001202 list_for_each_entry_safe(ctx, back, &agent->waiting_queue, list) {
1203 LIST_DEL(&ctx->list);
1204 LIST_INIT(&ctx->list);
1205 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001206 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001207 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1208 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001209
1210 end:
1211 /* Update runtinme agent info */
1212 agent->frame_size = agent->max_frame_size;
1213 list_for_each_entry(spoe_appctx, &agent->applets, list)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001214 agent->frame_size = MIN(spoe_appctx->max_frame_size,
1215 agent->frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216}
1217
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001221 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001222 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001224 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225
Christopher Fauleta1cda022016-12-21 08:58:06 +01001226 if (si->state <= SI_ST_CON) {
1227 si_applet_want_put(si);
1228 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1229 goto stop;
1230 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001231 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001232 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001233 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001234 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001235
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1238 " - Connection timed out\n",
1239 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1240 __FUNCTION__, appctx);
1241 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001242 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001244
Christopher Faulet42bfa462017-01-04 14:14:19 +01001245 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001246 SPOE_APPCTX(appctx)->task->expire =
1247 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001248
Christopher Faulet8ef75252017-02-20 22:56:03 +01001249 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1250 * length. */
1251 buf = trash.str; frame = buf+4;
1252 ret = spoe_prepare_hahello_frame(appctx, frame,
1253 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001254 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001256
1257 switch (ret) {
1258 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001259 case 0: /* ignore => an error, cannot be ignored */
1260 goto exit;
1261
1262 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001263 goto stop;
1264
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 default:
1266 /* HELLO frame successfully sent, now wait for the
1267 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001268 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1269 goto next;
1270 }
1271
1272 next:
1273 return 0;
1274 stop:
1275 return 1;
1276 exit:
1277 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1278 return 0;
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_connecting_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;
1287 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001289
Christopher Fauletb067b062017-01-04 16:39:11 +01001290 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001291 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001293 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001294
Christopher Fauleta1cda022016-12-21 08:58:06 +01001295 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001296 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1297 " - Connection timed out\n",
1298 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1299 __FUNCTION__, appctx);
1300 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001301 goto exit;
1302 }
1303
Christopher Faulet8ef75252017-02-20 22:56:03 +01001304 frame = trash.str; trash.len = 0;
1305 ret = spoe_recv_frame(appctx, frame,
1306 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 if (ret > 1) {
1308 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1309 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1310 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312 trash.len = ret + 4;
1313 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001314 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001315
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 switch (ret) {
1317 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001318 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1320 goto next;
1321
1322 case 1: /* retry later */
1323 goto stop;
1324
1325 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001326 /* HELLO handshake is finished, set the idle timeout and
1327 * add the applet in the list of running applets. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001328 agent->applets_idle++;
1329 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001330 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1331 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001332
1333 /* Update runtinme agent info */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001334 agent->frame_size = MIN(SPOE_APPCTX(appctx)->max_frame_size,
1335 agent->frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001338
Christopher Fauleta1cda022016-12-21 08:58:06 +01001339 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001340 /* Do not forget to remove processed frame from the output buffer */
1341 if (trash.len)
1342 bo_skip(si_oc(si), trash.len);
1343
1344 SPOE_APPCTX(appctx)->task->expire =
1345 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 return 0;
1347 stop:
1348 return 1;
1349 exit:
1350 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1351 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001352}
1353
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355spoe_handle_sending_frame_appctx(struct appctx *appctx, struct spoe_context *ctx,
1356 int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001357{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001358 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1359 char *frame, *buf;
1360 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001361
Christopher Faulet8ef75252017-02-20 22:56:03 +01001362 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1363 * length. */
1364 buf = trash.str; frame = buf+4;
1365 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1366 SPOE_APPCTX(appctx)->max_frame_size);
1367 if (ret > 1)
1368 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001369
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 switch (ret) {
1371 case -1: /* error */
1372 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1373 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375 case 0: /* ignore */
1376 if (ctx == NULL)
1377 goto abort_frag_frame;
1378
1379 LIST_DEL(&ctx->list);
1380 LIST_INIT(&ctx->list);
1381 ctx->state = SPOE_CTX_ST_ERROR;
1382 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1383 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1384 break;
1385
1386 case 1: /* retry */
1387 *skip = 1;
1388 break;
1389
1390 default:
1391 if (ctx == NULL)
1392 goto abort_frag_frame;
1393
1394 LIST_DEL(&ctx->list);
1395 LIST_INIT(&ctx->list);
1396 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1397 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1398 goto no_frag_frame_sent;
1399 else {
1400 *skip = 1;
1401 goto frag_frame_sent;
1402 }
1403 }
1404 goto end;
1405
1406 frag_frame_sent:
1407 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1408 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1409 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1410 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1411
1412 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1413 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1414 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1415 goto end;
1416
1417 no_frag_frame_sent:
1418 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1419 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1420 LIST_ADDQ(&agent->waiting_queue, &ctx->list);
1421 }
1422 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1423 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1424 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1425 }
1426 else {
1427 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1428 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1429 }
1430 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1431 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1432 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1433
1434 ctx->frag_ctx.spoe_appctx = NULL;
1435 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1436 goto end;
1437
1438 abort_frag_frame:
1439 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1440 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1441 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1442 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1443 goto end;
1444
1445 end:
1446 return ret;
1447}
1448
1449static int
1450spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1451{
1452 struct spoe_context *ctx = NULL;
1453 char *frame;
1454 int ret;
1455
1456 frame = trash.str; trash.len = 0;
1457 ret = spoe_recv_frame(appctx, frame,
1458 SPOE_APPCTX(appctx)->max_frame_size);
1459 if (ret > 1) {
1460 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1461 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1462 goto end;
1463 }
1464 trash.len = ret + 4;
1465 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1466 }
1467 switch (ret) {
1468 case -1: /* error */
1469 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1470 break;
1471
1472 case 0: /* ignore */
1473 break;
1474
1475 case 1: /* retry */
1476 *skip = 1;
1477 break;
1478
1479 default:
1480 LIST_DEL(&ctx->list);
1481 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001482
1483 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1484 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1485 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1486 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1487 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1488 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1489 }
1490 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1491 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1492
Christopher Faulet8ef75252017-02-20 22:56:03 +01001493 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1494 break;
1495 }
1496
1497 /* Do not forget to remove processed frame from the output buffer */
1498 if (trash.len)
1499 bo_skip(si_oc(appctx->owner), trash.len);
1500 end:
1501 return ret;
1502}
1503
1504static int
1505spoe_handle_processing_appctx(struct appctx *appctx)
1506{
1507 struct stream_interface *si = appctx->owner;
1508 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1509 struct spoe_context *ctx = NULL;
1510 unsigned int fpa = 0;
1511 int ret, skip_sending = 0, skip_receiving = 0;
1512
1513 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1514 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1515 goto exit;
1516 }
1517
1518 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1519 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1520 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1521 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1522 goto next;
1523 }
1524
1525 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001526 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1527 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1528 " - appctx-state=%s\n",
1529 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1530 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 skip_sending, skip_receiving,
1532 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001533
Christopher Fauleta1cda022016-12-21 08:58:06 +01001534 if (fpa > agent->max_fpa || (skip_sending && skip_receiving))
1535 goto stop;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001536 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001537 if (skip_receiving)
1538 goto stop;
1539 goto recv_frame;
1540 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001541 else if (skip_sending)
1542 goto recv_frame;
1543 else if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1544 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1545 goto send_frame;
1546 }
1547 else if (LIST_ISEMPTY(&agent->sending_queue)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001548 skip_sending = 1;
1549 goto recv_frame;
1550 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001551 ctx = LIST_NEXT(&agent->sending_queue, typeof(ctx), list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001552
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001553 send_frame:
Christopher Faulet4596fb72017-01-11 14:05:19 +01001554 /* Transfer the buffer ownership to the SPOE appctx */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001555 if (ctx) {
1556 SPOE_APPCTX(appctx)->buffer = ctx->buffer;
1557 ctx->buffer = &buf_empty;
1558 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001559 ret = spoe_handle_sending_frame_appctx(appctx, ctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001560 switch (ret) {
1561 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001562 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001563
Christopher Fauleta1cda022016-12-21 08:58:06 +01001564 case 0: /* ignore */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001565 spoe_release_buffer(&SPOE_APPCTX(appctx)->buffer,
1566 &SPOE_APPCTX(appctx)->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001567 agent->sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001568 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001569 break;
1570
Christopher Fauleta1cda022016-12-21 08:58:06 +01001571 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001572 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001573
Christopher Fauleta1cda022016-12-21 08:58:06 +01001574 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 spoe_release_buffer(&SPOE_APPCTX(appctx)->buffer,
1576 &SPOE_APPCTX(appctx)->buffer_wait);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001577 agent->sending_rate++;
1578 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001580 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001581 if (fpa > agent->max_fpa)
1582 goto stop;
1583
1584 recv_frame:
1585 if (skip_receiving)
1586 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001587 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001588 switch (ret) {
1589 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001590 goto next;
1591
1592 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001593 fpa++;
1594 break;
1595
1596 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001597 break;
1598
1599 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001600 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001601 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001602 }
1603 goto process;
1604
1605 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001606 SPOE_APPCTX(appctx)->task->expire =
1607 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001608 return 0;
1609 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001610 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001611 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001612 agent->applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001613 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001614 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
1615 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1616 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001617 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001618 SPOE_APPCTX(appctx)->task->expire =
1619 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001620 }
1621 return 1;
1622
1623 exit:
1624 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1625 return 0;
1626}
1627
1628static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001629spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001630{
1631 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001632 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001633 char *frame, *buf;
1634 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001635
Christopher Fauleta1cda022016-12-21 08:58:06 +01001636 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1637 goto exit;
1638
1639 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1640 goto exit;
1641
Christopher Faulet8ef75252017-02-20 22:56:03 +01001642 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1643 * length. */
1644 buf = trash.str; frame = buf+4;
1645 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1646 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001647 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001648 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001649
1650 switch (ret) {
1651 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001653 goto exit;
1654
1655 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001656 goto stop;
1657
1658 default:
1659 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1660 " - disconnected by HAProxy (%d): %s\n",
1661 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001662 __FUNCTION__, appctx,
1663 SPOE_APPCTX(appctx)->status_code,
1664 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001665
1666 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1667 goto next;
1668 }
1669
1670 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001671 SPOE_APPCTX(appctx)->task->expire =
1672 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001673 return 0;
1674 stop:
1675 return 1;
1676 exit:
1677 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1678 return 0;
1679}
1680
1681static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001682spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001683{
1684 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001685 char *frame;
1686 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001687
Christopher Fauletb067b062017-01-04 16:39:11 +01001688 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001689 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001690 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001691 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001692
Christopher Fauletb067b062017-01-04 16:39:11 +01001693 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001694 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001696 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001697
Christopher Faulet8ef75252017-02-20 22:56:03 +01001698 frame = trash.str; trash.len = 0;
1699 ret = spoe_recv_frame(appctx, frame,
1700 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001701 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001702 trash.len = ret + 4;
1703 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704 }
1705
1706 switch (ret) {
1707 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001708 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1709 " - error on frame (%s)\n",
1710 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001711 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001712 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001713 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714 goto exit;
1715
1716 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 goto next;
1718
1719 case 1: /* retry */
1720 goto stop;
1721
1722 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001723 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001724 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001725 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001726 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001727 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1728 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001729 goto exit;
1730 }
1731
1732 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001733 /* Do not forget to remove processed frame from the output buffer */
1734 if (trash.len)
1735 bo_skip(si_oc(appctx->owner), trash.len);
1736
Christopher Fauleta1cda022016-12-21 08:58:06 +01001737 return 0;
1738 stop:
1739 return 1;
1740 exit:
1741 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1742 return 0;
1743}
1744
1745/* I/O Handler processing messages exchanged with the agent */
1746static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001747spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 struct stream_interface *si = appctx->owner;
1750 struct spoe_agent *agent;
1751
1752 if (SPOE_APPCTX(appctx) == NULL)
1753 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001754
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1756 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001757
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758 switchstate:
1759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1760 " - appctx-state=%s\n",
1761 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1762 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1763
1764 switch (appctx->st0) {
1765 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001766 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 goto out;
1768 goto switchstate;
1769
1770 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772 goto out;
1773 goto switchstate;
1774
1775 case SPOE_APPCTX_ST_IDLE:
1776 if (stopping &&
1777 LIST_ISEMPTY(&agent->sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001778 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779 SPOE_APPCTX(appctx)->task->expire =
1780 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001782 goto switchstate;
1783 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784 agent->applets_idle--;
1785 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1786 /* fall through */
1787
1788 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001789 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1790 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001791 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792 goto out;
1793 goto switchstate;
1794
1795 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797 goto out;
1798 goto switchstate;
1799
1800 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 goto out;
1803 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001804
1805 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001806 appctx->st0 = SPOE_APPCTX_ST_END;
1807 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1808
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001809 si_shutw(si);
1810 si_shutr(si);
1811 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001812 /* fall through */
1813
1814 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001815 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001816 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 out:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001818 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1819 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001820 si_oc(si)->flags |= CF_READ_DONTWAIT;
1821 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001822}
1823
1824struct applet spoe_applet = {
1825 .obj_type = OBJ_TYPE_APPLET,
1826 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001827 .fct = spoe_handle_appctx,
1828 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001829};
1830
1831/* Create a SPOE applet. On success, the created applet is returned, else
1832 * NULL. */
1833static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001835{
1836 struct appctx *appctx;
1837 struct session *sess;
1838 struct task *task;
1839 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001840
1841 if ((appctx = appctx_new(&spoe_applet)) == NULL)
1842 goto out_error;
1843
Christopher Faulet42bfa462017-01-04 14:14:19 +01001844 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx);
1845 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001846 goto out_free_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001847 memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848
Christopher Faulet42bfa462017-01-04 14:14:19 +01001849 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1850 if ((SPOE_APPCTX(appctx)->task = task_new()) == NULL)
1851 goto out_free_spoe_appctx;
1852
1853 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001854 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001855 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1856 SPOE_APPCTX(appctx)->task->context = appctx;
1857 SPOE_APPCTX(appctx)->agent = conf->agent;
1858 SPOE_APPCTX(appctx)->version = 0;
1859 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1860 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001861 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001862 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1863
1864 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1865 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001867
1868 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1869 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001870
Willy Tarreau5820a362016-12-22 15:59:02 +01001871 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001872 if (!sess)
1873 goto out_free_spoe;
1874
1875 if ((task = task_new()) == NULL)
1876 goto out_free_sess;
1877
1878 if ((strm = stream_new(sess, task, &appctx->obj_type)) == NULL)
1879 goto out_free_task;
1880
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001881 stream_set_backend(strm, conf->agent->b.be);
1882
1883 /* applet is waiting for data */
1884 si_applet_cant_get(&strm->si[0]);
1885 appctx_wakeup(appctx);
1886
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001887 strm->do_log = NULL;
1888 strm->res.flags |= CF_READ_DONTWAIT;
1889
1890 conf->agent_fe.feconn++;
1891 jobs++;
1892 totalconn++;
1893
Christopher Faulet42bfa462017-01-04 14:14:19 +01001894 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
1895 LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001896 conf->agent->applets_act++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001897 return appctx;
1898
1899 /* Error unrolling */
1900 out_free_task:
1901 task_free(task);
1902 out_free_sess:
1903 session_free(sess);
1904 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001905 task_free(SPOE_APPCTX(appctx)->task);
1906 out_free_spoe_appctx:
1907 pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001908 out_free_appctx:
1909 appctx_free(appctx);
1910 out_error:
1911 return NULL;
1912}
1913
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001915spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916{
1917 struct spoe_config *conf = FLT_CONF(ctx->filter);
1918 struct spoe_agent *agent = conf->agent;
1919 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001920 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001921 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922
Christopher Fauleta1cda022016-12-21 08:58:06 +01001923 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001924
Christopher Fauleta1cda022016-12-21 08:58:06 +01001925 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001926 if (agent->applets_act >= min_applets &&
1927 agent->applets_idle &&
1928 agent->sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001930
1931 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001932 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
1934 ctx->strm);
1935
Christopher Fauleta1cda022016-12-21 08:58:06 +01001936 /* Do not try to create a new applet if there is no server up for the
1937 * agent's backend. */
1938 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
1939 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
1940 " - cannot create SPOE appctx: no server up\n",
1941 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1942 __FUNCTION__, ctx->strm);
1943 goto end;
1944 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945
Christopher Fauleta1cda022016-12-21 08:58:06 +01001946 /* Do not try to create a new applet if we have reached the maximum of
1947 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01001948 if (agent->cps_max > 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001949 if (!freq_ctr_remain(&agent->conn_per_sec, agent->cps_max, 0)) {
1950 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
1951 " - cannot create SPOE appctx: max CPS reached\n",
1952 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1953 __FUNCTION__, ctx->strm);
1954 goto end;
1955 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001956 }
1957
Christopher Faulet8ef75252017-02-20 22:56:03 +01001958 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001959 if (appctx == NULL) {
1960 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
1961 " - failed to create SPOE appctx\n",
1962 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1963 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01001964 send_log(ctx->strm->be, LOG_EMERG,
1965 "SPOE: [%s] failed to create SPOE applet\n",
1966 agent->id);
1967
Christopher Fauleta1cda022016-12-21 08:58:06 +01001968 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001970 if (agent->applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001971 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001972
Christopher Fauleta1cda022016-12-21 08:58:06 +01001973 /* Increase the per-process number of cumulated connections */
1974 if (agent->cps_max > 0)
1975 update_freq_ctr(&agent->conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001976
Christopher Fauleta1cda022016-12-21 08:58:06 +01001977 end:
1978 /* The only reason to return an error is when there is no applet */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001979 if (LIST_ISEMPTY(&agent->applets)) {
1980 ctx->status_code = SPOE_CTX_ERR_RES;
1981 return -1;
1982 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001983
Christopher Fauleta1cda022016-12-21 08:58:06 +01001984 /* Add the SPOE context in the sending queue and update all running
1985 * info */
1986 LIST_ADDQ(&agent->sending_queue, &ctx->list);
1987 if (agent->sending_rate)
1988 agent->sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001989
1990 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001991 " - Add stream in sending queue"
1992 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001993 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001994 ctx->strm, agent->applets_act, agent->applets_idle,
1995 agent->sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01001996
Christopher Fauleta1cda022016-12-21 08:58:06 +01001997 /* Finally try to wakeup the first IDLE applet found and move it at the
1998 * end of the list. */
Christopher Faulet42bfa462017-01-04 14:14:19 +01001999 list_for_each_entry(spoe_appctx, &agent->applets, list) {
2000 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002001 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002002 spoe_wakeup_appctx(appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002003 LIST_DEL(&spoe_appctx->list);
2004 LIST_ADDQ(&agent->applets, &spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002005 break;
2006 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002007 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002008 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009}
2010
2011/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002012 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013 **************************************************************************/
Christopher Faulet8ef75252017-02-20 22:56:03 +01002014/* Encode SPOE messages for a specific event. Info in <ctx->frag_ctx>, if any,
2015 * are used to handle fragmented content. On success it returns 1. If an error
2016 * occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002018spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002019 struct list *messages, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002020{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002021 struct spoe_config *conf = FLT_CONF(ctx->filter);
2022 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023 struct spoe_message *msg;
2024 struct sample *smp;
2025 struct spoe_arg *arg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002026 char *p, *end;
2027 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028
Christopher Faulet8ef75252017-02-20 22:56:03 +01002029 p = ctx->buffer->p;
2030 end = p + agent->frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002031
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002032 /* Resume encoding of a SPOE message */
2033 if (ctx->frag_ctx.curmsg != NULL) {
2034 msg = ctx->frag_ctx.curmsg;
2035 goto encode_message;
2036 }
2037
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038 /* Loop on messages */
2039 list_for_each_entry(msg, messages, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002040 ctx->frag_ctx.curmsg = msg;
2041 ctx->frag_ctx.curarg = NULL;
2042 ctx->frag_ctx.curoff = UINT_MAX;
2043
2044 encode_message:
2045 /* Resume encoding of a SPOE argument */
2046 if (ctx->frag_ctx.curarg != NULL) {
2047 arg = ctx->frag_ctx.curarg;
2048 goto encode_argument;
2049 }
2050
2051 if (ctx->frag_ctx.curoff != UINT_MAX)
2052 goto encode_msg_payload;
2053
Christopher Faulet8ef75252017-02-20 22:56:03 +01002054 /* Check if there is enough space for the message name and the
2055 * number of arguments. It implies <msg->id_len> is encoded on 2
2056 * bytes, at most (< 2288). */
2057 if (p + 2 + msg->id_len + 1 > end)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002058 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002059
Christopher Faulet8ef75252017-02-20 22:56:03 +01002060 /* Encode the message name */
2061 if (spoe_encode_buffer(msg->id, msg->id_len, &p, end) == -1)
2062 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002063
Christopher Faulet8ef75252017-02-20 22:56:03 +01002064 /* Set the number of arguments for this message */
2065 *p++ = msg->nargs;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002066
2067 ctx->frag_ctx.curoff = 0;
2068 encode_msg_payload:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002069
2070 /* Loop on arguments */
2071 list_for_each_entry(arg, &msg->args, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002072 ctx->frag_ctx.curarg = arg;
2073 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002075 encode_argument:
2076 if (ctx->frag_ctx.curoff != UINT_MAX)
2077 goto encode_arg_value;
2078
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079 /* Encode the arguement name as a string. It can by NULL */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002080 if (spoe_encode_buffer(arg->name, arg->name_len, &p, end) == -1)
2081 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002082
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002083 ctx->frag_ctx.curoff = 0;
2084 encode_arg_value:
2085
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002086 /* Fetch the arguement value */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002087 smp = sample_process(s->be, s->sess, s,
2088 dir|SMP_OPT_FINAL, arg->expr, NULL);
2089 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, &p, end);
2090 if (ret == -1 || ctx->frag_ctx.curoff)
2091 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002092 }
2093 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002094
2095 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002096 " - encode %s messages - spoe_appctx=%p"
2097 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002098 (int)now.tv_sec, (int)now.tv_usec,
2099 agent->id, __FUNCTION__, s,
2100 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002101 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2102 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002103
Christopher Faulet8ef75252017-02-20 22:56:03 +01002104 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002105 ctx->frag_ctx.curmsg = NULL;
2106 ctx->frag_ctx.curarg = NULL;
2107 ctx->frag_ctx.curoff = 0;
2108 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002109 return 1;
2110
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002111 too_big:
2112 // FIXME: if fragmentation not supported =>
2113 // ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2114 // return -1;
2115
2116 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002117 " - encode fragmented messages - spoe_appctx=%p"
2118 " - curmsg=%p - curarg=%p - curoff=%u"
2119 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002120 (int)now.tv_sec, (int)now.tv_usec,
2121 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2122 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002123 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002124
Christopher Faulet8ef75252017-02-20 22:56:03 +01002125 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002126 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2127 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2128 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002129}
2130
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002131
2132/***************************************************************************
2133 * Functions that handle SPOE actions
2134 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002135/* Helper function to set a variable */
2136static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002137spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002138 struct sample *smp)
2139{
2140 struct spoe_config *conf = FLT_CONF(ctx->filter);
2141 struct spoe_agent *agent = conf->agent;
2142 char varname[64];
2143
2144 memset(varname, 0, sizeof(varname));
2145 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2146 scope, agent->var_pfx, len, name);
2147 vars_set_by_name_ifexist(varname, len, smp);
2148}
2149
2150/* Helper function to unset a variable */
2151static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002152spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002153 struct sample *smp)
2154{
2155 struct spoe_config *conf = FLT_CONF(ctx->filter);
2156 struct spoe_agent *agent = conf->agent;
2157 char varname[64];
2158
2159 memset(varname, 0, sizeof(varname));
2160 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2161 scope, agent->var_pfx, len, name);
2162 vars_unset_by_name_ifexist(varname, len, smp);
2163}
2164
2165
Christopher Faulet8ef75252017-02-20 22:56:03 +01002166static inline int
2167spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2168 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002169{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002170 char *str, *scope, *p = *buf;
2171 struct sample smp;
2172 uint64_t sz;
2173 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002174
Christopher Faulet8ef75252017-02-20 22:56:03 +01002175 if (p + 2 >= end)
2176 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002177
Christopher Faulet8ef75252017-02-20 22:56:03 +01002178 /* SET-VAR requires 3 arguments */
2179 if (*p++ != 3)
2180 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002181
Christopher Faulet8ef75252017-02-20 22:56:03 +01002182 switch (*p++) {
2183 case SPOE_SCOPE_PROC: scope = "proc"; break;
2184 case SPOE_SCOPE_SESS: scope = "sess"; break;
2185 case SPOE_SCOPE_TXN : scope = "txn"; break;
2186 case SPOE_SCOPE_REQ : scope = "req"; break;
2187 case SPOE_SCOPE_RES : scope = "res"; break;
2188 default: goto skip;
2189 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002190
Christopher Faulet8ef75252017-02-20 22:56:03 +01002191 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2192 goto skip;
2193 memset(&smp, 0, sizeof(smp));
2194 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002195
Christopher Faulet8ef75252017-02-20 22:56:03 +01002196 if (spoe_decode_data(&p, end, &smp) == -1)
2197 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002198
Christopher Faulet8ef75252017-02-20 22:56:03 +01002199 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2200 " - set-var '%s.%s.%.*s'\n",
2201 (int)now.tv_sec, (int)now.tv_usec,
2202 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2203 __FUNCTION__, s, scope,
2204 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2205 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002206
Christopher Faulet8ef75252017-02-20 22:56:03 +01002207 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002208
Christopher Faulet8ef75252017-02-20 22:56:03 +01002209 ret = (p - *buf);
2210 *buf = p;
2211 return ret;
2212 skip:
2213 return 0;
2214}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002215
Christopher Faulet8ef75252017-02-20 22:56:03 +01002216static inline int
2217spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2218 char **buf, char *end, int dir)
2219{
2220 char *str, *scope, *p = *buf;
2221 struct sample smp;
2222 uint64_t sz;
2223 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002224
Christopher Faulet8ef75252017-02-20 22:56:03 +01002225 if (p + 2 >= end)
2226 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002227
Christopher Faulet8ef75252017-02-20 22:56:03 +01002228 /* UNSET-VAR requires 2 arguments */
2229 if (*p++ != 2)
2230 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002231
Christopher Faulet8ef75252017-02-20 22:56:03 +01002232 switch (*p++) {
2233 case SPOE_SCOPE_PROC: scope = "proc"; break;
2234 case SPOE_SCOPE_SESS: scope = "sess"; break;
2235 case SPOE_SCOPE_TXN : scope = "txn"; break;
2236 case SPOE_SCOPE_REQ : scope = "req"; break;
2237 case SPOE_SCOPE_RES : scope = "res"; break;
2238 default: goto skip;
2239 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002240
Christopher Faulet8ef75252017-02-20 22:56:03 +01002241 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2242 goto skip;
2243 memset(&smp, 0, sizeof(smp));
2244 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002245
Christopher Faulet8ef75252017-02-20 22:56:03 +01002246 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2247 " - unset-var '%s.%s.%.*s'\n",
2248 (int)now.tv_sec, (int)now.tv_usec,
2249 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2250 __FUNCTION__, s, scope,
2251 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2252 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002253
Christopher Faulet8ef75252017-02-20 22:56:03 +01002254 spoe_unset_var(ctx, scope, str, sz, &smp);
2255
2256 ret = (p - *buf);
2257 *buf = p;
2258 return ret;
2259 skip:
2260 return 0;
2261}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002262
Christopher Faulet8ef75252017-02-20 22:56:03 +01002263/* Process SPOE actions for a specific event. It returns 1 on success. If an
2264 * error occurred, 0 is returned. */
2265static int
2266spoe_process_actions(struct stream *s, struct spoe_context *ctx,
2267 enum spoe_event ev, int dir)
2268{
2269 char *p, *end;
2270 int ret;
2271
2272 p = ctx->buffer->p;
2273 end = p + ctx->buffer->i;
2274
2275 while (p < end) {
2276 enum spoe_action_type type;
2277
2278 type = *p++;
2279 switch (type) {
2280 case SPOE_ACT_T_SET_VAR:
2281 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2282 if (!ret)
2283 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002284 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002285
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286 case SPOE_ACT_T_UNSET_VAR:
2287 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2288 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002289 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002290 break;
2291
2292 default:
2293 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002294 }
2295 }
2296
2297 return 1;
2298 skip:
2299 return 0;
2300}
2301
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002302/***************************************************************************
2303 * Functions that process SPOE events
2304 **************************************************************************/
2305static inline int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002306spoe_start_event_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002307{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002308 /* If a process is already started for this SPOE context, retry
2309 * later. */
2310 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002311 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002312
2313 /* Set the right flag to prevent request and response processing
2314 * in same time. */
2315 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2316 ? SPOE_CTX_FL_REQ_PROCESS
2317 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002318 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002319}
2320
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002321static inline void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002322spoe_stop_event_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002323{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002324 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2325
2326 if (sa) {
2327 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002328 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002329 }
2330
Christopher Fauleta1cda022016-12-21 08:58:06 +01002331 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002332 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002333
Christopher Fauletb067b062017-01-04 16:39:11 +01002334 ctx->status_code = 0;
2335
Christopher Fauleta1cda022016-12-21 08:58:06 +01002336 /* Reset processing timer */
2337 ctx->process_exp = TICK_ETERNITY;
2338
Christopher Faulet8ef75252017-02-20 22:56:03 +01002339 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002340
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002341 ctx->frag_ctx.spoe_appctx = NULL;
2342 ctx->frag_ctx.curmsg = NULL;
2343 ctx->frag_ctx.curarg = NULL;
2344 ctx->frag_ctx.curoff = 0;
2345 ctx->frag_ctx.flags = 0;
2346
Christopher Fauleta1cda022016-12-21 08:58:06 +01002347 if (!LIST_ISEMPTY(&ctx->list)) {
2348 LIST_DEL(&ctx->list);
2349 LIST_INIT(&ctx->list);
2350 }
2351}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002352
2353/* Process a SPOE event. First, this functions will process messages attached to
2354 * this event and send them to an agent in a NOTIFY frame. Then, it will wait a
2355 * ACK frame to process corresponding actions. During all the processing, it
2356 * returns 0 and it returns 1 when the processing is finished. If an error
2357 * occurred, -1 is returned. */
2358static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359spoe_process_event(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002360 enum spoe_event ev)
2361{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002362 struct spoe_config *conf = FLT_CONF(ctx->filter);
2363 struct spoe_agent *agent = conf->agent;
2364 int dir, ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002365
2366 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2367 " - ctx-state=%s - event=%s\n",
2368 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletf7a30922016-11-10 15:04:51 +01002369 agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002370 spoe_event_str[ev]);
2371
2372 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2373
2374 if (LIST_ISEMPTY(&(ctx->messages[ev])))
2375 goto out;
2376
2377 if (ctx->state == SPOE_CTX_ST_ERROR)
2378 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002379
2380 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2381 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2382 " - failed to process event '%s': timeout\n",
2383 (int)now.tv_sec, (int)now.tv_usec,
2384 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
Christopher Fauletb067b062017-01-04 16:39:11 +01002385 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002386 goto error;
2387 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002388
2389 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002390 if (agent->eps_max > 0) {
2391 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2392 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2393 " - skip event '%s': max EPS reached\n",
2394 (int)now.tv_sec, (int)now.tv_usec,
2395 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
2396 goto skip;
2397 }
2398 }
2399
Christopher Fauletf7a30922016-11-10 15:04:51 +01002400 if (!tick_isset(ctx->process_exp)) {
2401 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2402 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2403 ctx->process_exp);
2404 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01002405 ret = spoe_start_event_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002406 if (!ret)
2407 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002408
Christopher Faulet8ef75252017-02-20 22:56:03 +01002409 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002410 goto error;
2411
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002412 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002413 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414 }
2415
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002416 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002417 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002418 goto out;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002419 ret = spoe_encode_messages(s, ctx, &(ctx->messages[ev]), dir);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002420 if (ret < 0)
2421 goto error;
2422 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2423 }
2424
2425 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2426 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002427 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428 ret = 0;
2429 goto out;
2430 }
2431
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002432 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2433 ret = 0;
2434 goto out;
2435 }
2436
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002438 spoe_process_actions(s, ctx, ev, dir);
2439 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002440 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002441 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002442 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002443 }
2444
2445 out:
2446 return ret;
2447
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002448 error:
Christopher Faulet48026722016-11-16 15:01:12 +01002449 if (agent->eps_max > 0)
2450 update_freq_ctr(&agent->err_per_sec, 1);
2451
Christopher Faulet985532d2016-11-16 15:36:19 +01002452 if (agent->var_on_error) {
2453 struct sample smp;
2454
2455 memset(&smp, 0, sizeof(smp));
2456 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletb067b062017-01-04 16:39:11 +01002457 smp.data.u.sint = ctx->status_code;
Christopher Faulet985532d2016-11-16 15:36:19 +01002458 smp.data.type = SMP_T_BOOL;
2459
Christopher Faulet8ef75252017-02-20 22:56:03 +01002460 spoe_set_var(ctx, "txn", agent->var_on_error,
Christopher Faulet985532d2016-11-16 15:36:19 +01002461 strlen(agent->var_on_error), &smp);
2462 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002463 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2464 " - failed to create process event '%s': code=%u\n",
2465 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2466 __FUNCTION__, ctx->strm, spoe_event_str[ev],
2467 ctx->status_code);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002468 send_log(ctx->strm->be, LOG_WARNING,
2469 "SPOE: [%s] failed to process event '%s': code=%u\n",
2470 agent->id, spoe_event_str[ev], ctx->status_code);
Christopher Faulet985532d2016-11-16 15:36:19 +01002471
Christopher Fauletea62c2a2016-11-14 10:54:21 +01002472 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2473 ? SPOE_CTX_ST_READY
Christopher Fauletb067b062017-01-04 16:39:11 +01002474 : SPOE_CTX_ST_NONE);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002475 ret = 1;
2476 goto end;
2477
2478 skip:
2479 ctx->state = SPOE_CTX_ST_READY;
2480 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002481
Christopher Fauleta1cda022016-12-21 08:58:06 +01002482 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002483 spoe_stop_event_processing(ctx);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002484 return ret;
2485}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002486
2487/***************************************************************************
2488 * Functions that create/destroy SPOE contexts
2489 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002490static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002491spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002492{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002493 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002494 return 1;
2495
Christopher Faulet4596fb72017-01-11 14:05:19 +01002496 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2497 LIST_DEL(&buffer_wait->list);
2498 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002499 }
2500
Christopher Faulet4596fb72017-01-11 14:05:19 +01002501 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002502 return 1;
2503
Christopher Faulet4596fb72017-01-11 14:05:19 +01002504 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002505 return 0;
2506}
2507
2508static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002509spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002510{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002511 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2512 LIST_DEL(&buffer_wait->list);
2513 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002514 }
2515
2516 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002517 if (*buf != &buf_empty) {
2518 b_free(buf);
2519 offer_buffers(buffer_wait->target,
2520 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002521 }
2522}
2523
Christopher Faulet4596fb72017-01-11 14:05:19 +01002524static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002525spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002526{
2527 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2528 return 1;
2529}
2530
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002531static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002532spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002533{
2534 struct spoe_config *conf = FLT_CONF(filter);
2535 struct spoe_context *ctx;
2536
2537 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2538 if (ctx == NULL) {
2539 return NULL;
2540 }
2541 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002542 ctx->filter = filter;
2543 ctx->state = SPOE_CTX_ST_NONE;
2544 ctx->status_code = SPOE_CTX_ERR_NONE;
2545 ctx->flags = 0;
2546 ctx->messages = conf->agent->messages;
2547 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002548 LIST_INIT(&ctx->buffer_wait.list);
2549 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002550 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002551 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002552
Christopher Fauletf7a30922016-11-10 15:04:51 +01002553 ctx->stream_id = 0;
2554 ctx->frame_id = 1;
2555 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002556
2557 return ctx;
2558}
2559
2560static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002561spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002562{
2563 if (!ctx)
2564 return;
2565
Christopher Faulet8ef75252017-02-20 22:56:03 +01002566 spoe_stop_event_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002567 pool_free2(pool2_spoe_ctx, ctx);
2568}
2569
2570static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002571spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002572{
2573 ctx->state = SPOE_CTX_ST_READY;
2574 ctx->flags &= ~SPOE_CTX_FL_PROCESS;
2575}
2576
2577
2578/***************************************************************************
2579 * Hooks that manage the filter lifecycle (init/check/deinit)
2580 **************************************************************************/
2581/* Signal handler: Do a soft stop, wakeup SPOE applet */
2582static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002583spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002584{
2585 struct proxy *p;
2586
2587 p = proxy;
2588 while (p) {
2589 struct flt_conf *fconf;
2590
2591 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002592 struct spoe_config *conf;
2593 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002594 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002595
Christopher Faulet3b386a32017-02-23 10:17:15 +01002596 if (fconf->id != spoe_filter_id)
2597 continue;
2598
2599 conf = fconf->conf;
2600 agent = conf->agent;
2601
Christopher Faulet42bfa462017-01-04 14:14:19 +01002602 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002603 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002604 }
2605 }
2606 p = p->next;
2607 }
2608}
2609
2610
2611/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2612static int
2613spoe_init(struct proxy *px, struct flt_conf *fconf)
2614{
2615 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002616
2617 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2618 init_new_proxy(&conf->agent_fe);
2619 conf->agent_fe.parent = conf->agent;
2620 conf->agent_fe.last_change = now.tv_sec;
2621 conf->agent_fe.id = conf->agent->id;
2622 conf->agent_fe.cap = PR_CAP_FE;
2623 conf->agent_fe.mode = PR_MODE_TCP;
2624 conf->agent_fe.maxconn = 0;
2625 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2626 conf->agent_fe.conn_retries = CONN_RETRIES;
2627 conf->agent_fe.accept = frontend_accept;
2628 conf->agent_fe.srv = NULL;
2629 conf->agent_fe.timeout.client = TICK_ETERNITY;
2630 conf->agent_fe.default_target = &spoe_applet.obj_type;
2631 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2632
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002633 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002634 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002635 sighandler_registered = 1;
2636 }
2637
2638 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002639}
2640
2641/* Free ressources allocated by the SPOE filter. */
2642static void
2643spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2644{
2645 struct spoe_config *conf = fconf->conf;
2646
2647 if (conf) {
2648 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002649
Christopher Faulet8ef75252017-02-20 22:56:03 +01002650 spoe_release_agent(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002651 free(conf);
2652 }
2653 fconf->conf = NULL;
2654}
2655
2656/* Check configuration of a SPOE filter for a specified proxy.
2657 * Return 1 on error, else 0. */
2658static int
2659spoe_check(struct proxy *px, struct flt_conf *fconf)
2660{
2661 struct spoe_config *conf = fconf->conf;
2662 struct proxy *target;
2663
2664 target = proxy_be_by_name(conf->agent->b.name);
2665 if (target == NULL) {
2666 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2667 " declared at %s:%d.\n",
2668 px->id, conf->agent->b.name, conf->agent->id,
2669 conf->agent->conf.file, conf->agent->conf.line);
2670 return 1;
2671 }
2672 if (target->mode != PR_MODE_TCP) {
2673 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2674 " at %s:%d does not support HTTP mode.\n",
2675 px->id, target->id, conf->agent->id,
2676 conf->agent->conf.file, conf->agent->conf.line);
2677 return 1;
2678 }
2679
2680 free(conf->agent->b.name);
2681 conf->agent->b.name = NULL;
2682 conf->agent->b.be = target;
2683 return 0;
2684}
2685
2686/**************************************************************************
2687 * Hooks attached to a stream
2688 *************************************************************************/
2689/* Called when a filter instance is created and attach to a stream. It creates
2690 * the context that will be used to process this stream. */
2691static int
2692spoe_start(struct stream *s, struct filter *filter)
2693{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002694 struct spoe_config *conf = FLT_CONF(filter);
2695 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002696 struct spoe_context *ctx;
2697
2698 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002699 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700 __FUNCTION__, s);
2701
Christopher Faulet8ef75252017-02-20 22:56:03 +01002702 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002703 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002704 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2705 " - failed to create SPOE context\n",
2706 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2707 __FUNCTION__, ctx->strm);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002708 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002709 "SPOE: [%s] failed to create SPOE context\n",
2710 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002711 return 0;
2712 }
2713
2714 ctx->strm = s;
2715 ctx->state = SPOE_CTX_ST_READY;
2716 filter->ctx = ctx;
2717
2718 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_FE]))
2719 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2720
2721 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_BE]))
2722 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2723
2724 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_RSP]))
2725 filter->pre_analyzers |= AN_RES_INSPECT;
2726
2727 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_FE]))
2728 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2729
2730 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_BE]))
2731 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2732
2733 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_RSP]))
2734 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2735
2736 return 1;
2737}
2738
2739/* Called when a filter instance is detached from a stream. It release the
2740 * attached SPOE context. */
2741static void
2742spoe_stop(struct stream *s, struct filter *filter)
2743{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2745 (int)now.tv_sec, (int)now.tv_usec,
2746 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2747 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002748 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002749}
2750
Christopher Fauletf7a30922016-11-10 15:04:51 +01002751
2752/*
2753 * Called when the stream is woken up because of expired timer.
2754 */
2755static void
2756spoe_check_timeouts(struct stream *s, struct filter *filter)
2757{
2758 struct spoe_context *ctx = filter->ctx;
2759
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002760 if (tick_is_expired(ctx->process_exp, now_ms)) {
2761 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002762 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002763 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002764}
2765
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002766/* Called when we are ready to filter data on a channel */
2767static int
2768spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2769{
2770 struct spoe_context *ctx = filter->ctx;
2771 int ret = 1;
2772
2773 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2774 " - ctx-flags=0x%08x\n",
2775 (int)now.tv_sec, (int)now.tv_usec,
2776 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2777 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2778
Christopher Fauletb067b062017-01-04 16:39:11 +01002779 if (ctx->state == SPOE_CTX_ST_NONE)
2780 goto out;
2781
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002782 if (!(chn->flags & CF_ISRESP)) {
2783 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2784 chn->analysers |= AN_REQ_INSPECT_FE;
2785 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2786 chn->analysers |= AN_REQ_INSPECT_BE;
2787
2788 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2789 goto out;
2790
2791 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002792 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01002793 if (!ret)
2794 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002795 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
2796 }
2797 else {
2798 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
2799 chn->analysers |= AN_RES_INSPECT;
2800
2801 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
2802 goto out;
2803
Christopher Faulet8ef75252017-02-20 22:56:03 +01002804 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002805 if (!ret) {
2806 channel_dont_read(chn);
2807 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01002808 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002809 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002810 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002811 }
2812
2813 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002814 return ret;
2815}
2816
2817/* Called before a processing happens on a given channel */
2818static int
2819spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
2820 struct channel *chn, unsigned an_bit)
2821{
2822 struct spoe_context *ctx = filter->ctx;
2823 int ret = 1;
2824
2825 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2826 " - ctx-flags=0x%08x - ana=0x%08x\n",
2827 (int)now.tv_sec, (int)now.tv_usec,
2828 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2829 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2830 ctx->flags, an_bit);
2831
Christopher Fauletb067b062017-01-04 16:39:11 +01002832 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002833 goto out;
2834
2835 switch (an_bit) {
2836 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002837 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002838 break;
2839 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002840 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002841 break;
2842 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002843 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002844 break;
2845 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002846 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002847 break;
2848 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002849 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002850 break;
2851 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002852 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002853 break;
2854 }
2855
2856 out:
2857 if (!ret) {
2858 channel_dont_read(chn);
2859 channel_dont_close(chn);
2860 }
2861 return ret;
2862}
2863
2864/* Called when the filtering on the channel ends. */
2865static int
2866spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2867{
2868 struct spoe_context *ctx = filter->ctx;
2869
2870 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2871 " - ctx-flags=0x%08x\n",
2872 (int)now.tv_sec, (int)now.tv_usec,
2873 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2874 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2875
2876 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002877 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002878 }
2879
2880 return 1;
2881}
2882
2883/********************************************************************
2884 * Functions that manage the filter initialization
2885 ********************************************************************/
2886struct flt_ops spoe_ops = {
2887 /* Manage SPOE filter, called for each filter declaration */
2888 .init = spoe_init,
2889 .deinit = spoe_deinit,
2890 .check = spoe_check,
2891
2892 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01002893 .attach = spoe_start,
2894 .detach = spoe_stop,
2895 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896
2897 /* Handle channels activity */
2898 .channel_start_analyze = spoe_start_analyze,
2899 .channel_pre_analyze = spoe_chn_pre_analyze,
2900 .channel_end_analyze = spoe_end_analyze,
2901};
2902
2903
2904static int
2905cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
2906{
2907 const char *err;
2908 int i, err_code = 0;
2909
2910 if ((cfg_scope == NULL && curengine != NULL) ||
2911 (cfg_scope != NULL && curengine == NULL) ||
2912 strcmp(curengine, cfg_scope))
2913 goto out;
2914
2915 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
2916 if (!*args[1]) {
2917 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
2918 file, linenum);
2919 err_code |= ERR_ALERT | ERR_ABORT;
2920 goto out;
2921 }
2922 if (*args[2]) {
2923 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
2924 file, linenum, args[2]);
2925 err_code |= ERR_ALERT | ERR_ABORT;
2926 goto out;
2927 }
2928
2929 err = invalid_char(args[1]);
2930 if (err) {
2931 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2932 file, linenum, *err, args[0], args[1]);
2933 err_code |= ERR_ALERT | ERR_ABORT;
2934 goto out;
2935 }
2936
2937 if (curagent != NULL) {
2938 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
2939 file, linenum);
2940 err_code |= ERR_ALERT | ERR_ABORT;
2941 goto out;
2942 }
2943 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
2944 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2945 err_code |= ERR_ALERT | ERR_ABORT;
2946 goto out;
2947 }
2948
2949 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002950
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002951 curagent->conf.file = strdup(file);
2952 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002953
2954 curagent->timeout.hello = TICK_ETERNITY;
2955 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002956 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002957
2958 curagent->engine_id = NULL;
2959 curagent->var_pfx = NULL;
2960 curagent->var_on_error = NULL;
2961 curagent->flags = 0;
2962 curagent->cps_max = 0;
2963 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01002964 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002965 curagent->min_applets = 0;
2966 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967
2968 for (i = 0; i < SPOE_EV_EVENTS; ++i)
2969 LIST_INIT(&curagent->messages[i]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002970
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01002971 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002972 curagent->applets_act = 0;
2973 curagent->applets_idle = 0;
2974 curagent->sending_rate = 0;
2975
2976 LIST_INIT(&curagent->applets);
2977 LIST_INIT(&curagent->sending_queue);
2978 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002979 }
2980 else if (!strcmp(args[0], "use-backend")) {
2981 if (!*args[1]) {
2982 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
2983 file, linenum, args[0]);
2984 err_code |= ERR_ALERT | ERR_FATAL;
2985 goto out;
2986 }
2987 if (*args[2]) {
2988 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
2989 file, linenum, args[2]);
2990 err_code |= ERR_ALERT | ERR_ABORT;
2991 goto out;
2992 }
2993 free(curagent->b.name);
2994 curagent->b.name = strdup(args[1]);
2995 }
2996 else if (!strcmp(args[0], "messages")) {
2997 int cur_arg = 1;
2998 while (*args[cur_arg]) {
2999 struct spoe_msg_placeholder *mp = NULL;
3000
3001 list_for_each_entry(mp, &curmps, list) {
3002 if (!strcmp(mp->id, args[cur_arg])) {
3003 Alert("parsing [%s:%d]: spoe-message message '%s' already declared.\n",
3004 file, linenum, args[cur_arg]);
3005 err_code |= ERR_ALERT | ERR_FATAL;
3006 goto out;
3007 }
3008 }
3009
3010 if ((mp = calloc(1, sizeof(*mp))) == NULL) {
3011 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3012 err_code |= ERR_ALERT | ERR_ABORT;
3013 goto out;
3014 }
3015 mp->id = strdup(args[cur_arg]);
3016 LIST_ADDQ(&curmps, &mp->list);
3017 cur_arg++;
3018 }
3019 }
3020 else if (!strcmp(args[0], "timeout")) {
3021 unsigned int *tv = NULL;
3022 const char *res;
3023 unsigned timeout;
3024
3025 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003026 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003027 file, linenum);
3028 err_code |= ERR_ALERT | ERR_FATAL;
3029 goto out;
3030 }
3031 if (!strcmp(args[1], "hello"))
3032 tv = &curagent->timeout.hello;
3033 else if (!strcmp(args[1], "idle"))
3034 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003035 else if (!strcmp(args[1], "processing"))
3036 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003037 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003038 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003039 file, linenum, args[1]);
3040 err_code |= ERR_ALERT | ERR_FATAL;
3041 goto out;
3042 }
3043 if (!*args[2]) {
3044 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3045 file, linenum, args[1]);
3046 err_code |= ERR_ALERT | ERR_FATAL;
3047 goto out;
3048 }
3049 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3050 if (res) {
3051 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3052 file, linenum, *res, args[1]);
3053 err_code |= ERR_ALERT | ERR_ABORT;
3054 goto out;
3055 }
3056 if (*args[3]) {
3057 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3058 file, linenum, args[3]);
3059 err_code |= ERR_ALERT | ERR_ABORT;
3060 goto out;
3061 }
3062 *tv = MS_TO_TICKS(timeout);
3063 }
3064 else if (!strcmp(args[0], "option")) {
3065 if (!*args[1]) {
3066 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3067 file, linenum, args[0]);
3068 err_code |= ERR_ALERT | ERR_FATAL;
3069 goto out;
3070 }
3071 if (!strcmp(args[1], "var-prefix")) {
3072 char *tmp;
3073
3074 if (!*args[2]) {
3075 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3076 file, linenum, args[0],
3077 args[1]);
3078 err_code |= ERR_ALERT | ERR_FATAL;
3079 goto out;
3080 }
3081 tmp = args[2];
3082 while (*tmp) {
3083 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3084 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3085 file, linenum, args[0], args[1]);
3086 err_code |= ERR_ALERT | ERR_FATAL;
3087 goto out;
3088 }
3089 tmp++;
3090 }
3091 curagent->var_pfx = strdup(args[2]);
3092 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003093 else if (!strcmp(args[1], "continue-on-error")) {
3094 if (*args[2]) {
3095 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
Christopher Faulet48026722016-11-16 15:01:12 +01003096 file, linenum, args[2]);
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003097 err_code |= ERR_ALERT | ERR_ABORT;
3098 goto out;
3099 }
3100 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3101 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003102 else if (!strcmp(args[1], "set-on-error")) {
3103 char *tmp;
3104
3105 if (!*args[2]) {
3106 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3107 file, linenum, args[0],
3108 args[1]);
3109 err_code |= ERR_ALERT | ERR_FATAL;
3110 goto out;
3111 }
3112 tmp = args[2];
3113 while (*tmp) {
3114 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3115 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3116 file, linenum, args[0], args[1]);
3117 err_code |= ERR_ALERT | ERR_FATAL;
3118 goto out;
3119 }
3120 tmp++;
3121 }
3122 curagent->var_on_error = strdup(args[2]);
3123 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124 else {
3125 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3126 file, linenum, args[1]);
3127 err_code |= ERR_ALERT | ERR_FATAL;
3128 goto out;
3129 }
Christopher Faulet48026722016-11-16 15:01:12 +01003130 }
3131 else if (!strcmp(args[0], "maxconnrate")) {
3132 if (!*args[1]) {
3133 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3134 file, linenum, args[0]);
3135 err_code |= ERR_ALERT | ERR_FATAL;
3136 goto out;
3137 }
3138 if (*args[2]) {
3139 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3140 file, linenum, args[2]);
3141 err_code |= ERR_ALERT | ERR_ABORT;
3142 goto out;
3143 }
3144 curagent->cps_max = atol(args[1]);
3145 }
3146 else if (!strcmp(args[0], "maxerrrate")) {
3147 if (!*args[1]) {
3148 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3149 file, linenum, args[0]);
3150 err_code |= ERR_ALERT | ERR_FATAL;
3151 goto out;
3152 }
3153 if (*args[2]) {
3154 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3155 file, linenum, args[2]);
3156 err_code |= ERR_ALERT | ERR_ABORT;
3157 goto out;
3158 }
3159 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003160 }
3161 else if (*args[0]) {
3162 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3163 file, linenum, args[0]);
3164 err_code |= ERR_ALERT | ERR_FATAL;
3165 goto out;
3166 }
3167 out:
3168 return err_code;
3169}
3170
3171static int
3172cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3173{
3174 struct spoe_message *msg;
3175 struct spoe_arg *arg;
3176 const char *err;
3177 char *errmsg = NULL;
3178 int err_code = 0;
3179
3180 if ((cfg_scope == NULL && curengine != NULL) ||
3181 (cfg_scope != NULL && curengine == NULL) ||
3182 strcmp(curengine, cfg_scope))
3183 goto out;
3184
3185 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3186 if (!*args[1]) {
3187 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3188 file, linenum);
3189 err_code |= ERR_ALERT | ERR_ABORT;
3190 goto out;
3191 }
3192 if (*args[2]) {
3193 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3194 file, linenum, args[2]);
3195 err_code |= ERR_ALERT | ERR_ABORT;
3196 goto out;
3197 }
3198
3199 err = invalid_char(args[1]);
3200 if (err) {
3201 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3202 file, linenum, *err, args[0], args[1]);
3203 err_code |= ERR_ALERT | ERR_ABORT;
3204 goto out;
3205 }
3206
3207 list_for_each_entry(msg, &curmsgs, list) {
3208 if (!strcmp(msg->id, args[1])) {
3209 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3210 " name as another one declared at %s:%d.\n",
3211 file, linenum, args[1], msg->conf.file, msg->conf.line);
3212 err_code |= ERR_ALERT | ERR_FATAL;
3213 goto out;
3214 }
3215 }
3216
3217 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3218 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3219 err_code |= ERR_ALERT | ERR_ABORT;
3220 goto out;
3221 }
3222
3223 curmsg->id = strdup(args[1]);
3224 curmsg->id_len = strlen(curmsg->id);
3225 curmsg->event = SPOE_EV_NONE;
3226 curmsg->conf.file = strdup(file);
3227 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003228 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 LIST_INIT(&curmsg->args);
3230 LIST_ADDQ(&curmsgs, &curmsg->list);
3231 }
3232 else if (!strcmp(args[0], "args")) {
3233 int cur_arg = 1;
3234
3235 curproxy->conf.args.ctx = ARGC_SPOE;
3236 curproxy->conf.args.file = file;
3237 curproxy->conf.args.line = linenum;
3238 while (*args[cur_arg]) {
3239 char *delim = strchr(args[cur_arg], '=');
3240 int idx = 0;
3241
3242 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3243 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3244 err_code |= ERR_ALERT | ERR_ABORT;
3245 goto out;
3246 }
3247
3248 if (!delim) {
3249 arg->name = NULL;
3250 arg->name_len = 0;
3251 delim = args[cur_arg];
3252 }
3253 else {
3254 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3255 arg->name_len = delim - args[cur_arg];
3256 delim++;
3257 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003258 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3259 &idx, file, linenum, &errmsg,
3260 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003261 if (arg->expr == NULL) {
3262 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3263 err_code |= ERR_ALERT | ERR_FATAL;
3264 free(arg->name);
3265 free(arg);
3266 goto out;
3267 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003268 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003269 LIST_ADDQ(&curmsg->args, &arg->list);
3270 cur_arg++;
3271 }
3272 curproxy->conf.args.file = NULL;
3273 curproxy->conf.args.line = 0;
3274 }
3275 else if (!strcmp(args[0], "event")) {
3276 if (!*args[1]) {
3277 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
3278 err_code |= ERR_ALERT | ERR_ABORT;
3279 goto out;
3280 }
3281 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3282 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3283 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3284 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3285
3286 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3287 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3288 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3289 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3290 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3291 curmsg->event = SPOE_EV_ON_TCP_RSP;
3292
3293 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3294 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3295 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3296 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3297 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3298 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3299 else {
3300 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3301 file, linenum, args[1]);
3302 err_code |= ERR_ALERT | ERR_ABORT;
3303 goto out;
3304 }
3305 }
3306 else if (!*args[0]) {
3307 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3308 file, linenum, args[0]);
3309 err_code |= ERR_ALERT | ERR_FATAL;
3310 goto out;
3311 }
3312 out:
3313 free(errmsg);
3314 return err_code;
3315}
3316
3317/* Return -1 on error, else 0 */
3318static int
3319parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3320 struct flt_conf *fconf, char **err, void *private)
3321{
3322 struct list backup_sections;
3323 struct spoe_config *conf;
3324 struct spoe_message *msg, *msgback;
3325 struct spoe_msg_placeholder *mp, *mpback;
3326 char *file = NULL, *engine = NULL;
3327 int ret, pos = *cur_arg + 1;
3328
3329 conf = calloc(1, sizeof(*conf));
3330 if (conf == NULL) {
3331 memprintf(err, "%s: out of memory", args[*cur_arg]);
3332 goto error;
3333 }
3334 conf->proxy = px;
3335
3336 while (*args[pos]) {
3337 if (!strcmp(args[pos], "config")) {
3338 if (!*args[pos+1]) {
3339 memprintf(err, "'%s' : '%s' option without value",
3340 args[*cur_arg], args[pos]);
3341 goto error;
3342 }
3343 file = args[pos+1];
3344 pos += 2;
3345 }
3346 else if (!strcmp(args[pos], "engine")) {
3347 if (!*args[pos+1]) {
3348 memprintf(err, "'%s' : '%s' option without value",
3349 args[*cur_arg], args[pos]);
3350 goto error;
3351 }
3352 engine = args[pos+1];
3353 pos += 2;
3354 }
3355 else {
3356 memprintf(err, "unknown keyword '%s'", args[pos]);
3357 goto error;
3358 }
3359 }
3360 if (file == NULL) {
3361 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3362 goto error;
3363 }
3364
3365 /* backup sections and register SPOE sections */
3366 LIST_INIT(&backup_sections);
3367 cfg_backup_sections(&backup_sections);
3368 cfg_register_section("spoe-agent", cfg_parse_spoe_agent);
3369 cfg_register_section("spoe-message", cfg_parse_spoe_message);
3370
3371 /* Parse SPOE filter configuration file */
3372 curengine = engine;
3373 curproxy = px;
3374 curagent = NULL;
3375 curmsg = NULL;
3376 ret = readcfgfile(file);
3377 curproxy = NULL;
3378
3379 /* unregister SPOE sections and restore previous sections */
3380 cfg_unregister_sections();
3381 cfg_restore_sections(&backup_sections);
3382
3383 if (ret == -1) {
3384 memprintf(err, "Could not open configuration file %s : %s",
3385 file, strerror(errno));
3386 goto error;
3387 }
3388 if (ret & (ERR_ABORT|ERR_FATAL)) {
3389 memprintf(err, "Error(s) found in configuration file %s", file);
3390 goto error;
3391 }
3392
3393 /* Check SPOE agent */
3394 if (curagent == NULL) {
3395 memprintf(err, "No SPOE agent found in file %s", file);
3396 goto error;
3397 }
3398 if (curagent->b.name == NULL) {
3399 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3400 curagent->id, curagent->conf.file, curagent->conf.line);
3401 goto error;
3402 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003403 if (curagent->timeout.hello == TICK_ETERNITY ||
3404 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003405 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003406 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3407 " | While not properly invalid, you will certainly encounter various problems\n"
3408 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003409 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3411 }
3412 if (curagent->var_pfx == NULL) {
3413 char *tmp = curagent->id;
3414
3415 while (*tmp) {
3416 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3417 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3418 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3419 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3420 goto error;
3421 }
3422 tmp++;
3423 }
3424 curagent->var_pfx = strdup(curagent->id);
3425 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003426 if (curagent->engine_id == NULL)
3427 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003428
3429 if (LIST_ISEMPTY(&curmps)) {
3430 Warning("Proxy '%s': No message used by SPOE agent '%s' declared at %s:%d.\n",
3431 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3432 goto finish;
3433 }
3434
3435 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3436 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003437 struct spoe_arg *arg;
3438 unsigned int where;
3439
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003440 if (!strcmp(msg->id, mp->id)) {
3441 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3442 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3443 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3444 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3445 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3446 }
3447 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3448 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3449 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3450 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3451 px->id, msg->conf.file, msg->conf.line);
3452 goto next;
3453 }
3454 if (msg->event == SPOE_EV_NONE) {
3455 Warning("Proxy '%s': Ignore SPOE message without event at %s:%d.\n",
3456 px->id, msg->conf.file, msg->conf.line);
3457 goto next;
3458 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003459
3460 where = 0;
3461 switch (msg->event) {
3462 case SPOE_EV_ON_CLIENT_SESS:
3463 where |= SMP_VAL_FE_CON_ACC;
3464 break;
3465
3466 case SPOE_EV_ON_TCP_REQ_FE:
3467 where |= SMP_VAL_FE_REQ_CNT;
3468 break;
3469
3470 case SPOE_EV_ON_HTTP_REQ_FE:
3471 where |= SMP_VAL_FE_HRQ_HDR;
3472 break;
3473
3474 case SPOE_EV_ON_TCP_REQ_BE:
3475 if (px->cap & PR_CAP_FE)
3476 where |= SMP_VAL_FE_REQ_CNT;
3477 if (px->cap & PR_CAP_BE)
3478 where |= SMP_VAL_BE_REQ_CNT;
3479 break;
3480
3481 case SPOE_EV_ON_HTTP_REQ_BE:
3482 if (px->cap & PR_CAP_FE)
3483 where |= SMP_VAL_FE_HRQ_HDR;
3484 if (px->cap & PR_CAP_BE)
3485 where |= SMP_VAL_BE_HRQ_HDR;
3486 break;
3487
3488 case SPOE_EV_ON_SERVER_SESS:
3489 where |= SMP_VAL_BE_SRV_CON;
3490 break;
3491
3492 case SPOE_EV_ON_TCP_RSP:
3493 if (px->cap & PR_CAP_FE)
3494 where |= SMP_VAL_FE_RES_CNT;
3495 if (px->cap & PR_CAP_BE)
3496 where |= SMP_VAL_BE_RES_CNT;
3497 break;
3498
3499 case SPOE_EV_ON_HTTP_RSP:
3500 if (px->cap & PR_CAP_FE)
3501 where |= SMP_VAL_FE_HRS_HDR;
3502 if (px->cap & PR_CAP_BE)
3503 where |= SMP_VAL_BE_HRS_HDR;
3504 break;
3505
3506 default:
3507 break;
3508 }
3509
3510 list_for_each_entry(arg, &msg->args, list) {
3511 if (!(arg->expr->fetch->val & where)) {
3512 Warning("Proxy '%s': Ignore SPOE message at %s:%d: "
3513 "some args extract information from '%s', "
3514 "none of which is available here ('%s').\n",
3515 px->id, msg->conf.file, msg->conf.line,
3516 sample_ckp_names(arg->expr->fetch->use),
3517 sample_ckp_names(where));
3518 goto next;
3519 }
3520 }
3521
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003522 msg->agent = curagent;
3523 LIST_DEL(&msg->list);
3524 LIST_ADDQ(&curagent->messages[msg->event], &msg->list);
3525 goto next;
3526 }
3527 }
3528 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
3529 curagent->id, mp->id, curagent->conf.file, curagent->conf.line);
3530 goto error;
3531 next:
3532 continue;
3533 }
3534
3535 finish:
3536 conf->agent = curagent;
3537 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3538 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003539 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003540 }
3541 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3542 Warning("Proxy '%s': Ignore unused SPOE messages '%s' declared at %s:%d.\n",
3543 px->id, msg->id, msg->conf.file, msg->conf.line);
3544 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003545 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003546 }
3547
3548 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01003549 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003550 fconf->ops = &spoe_ops;
3551 fconf->conf = conf;
3552 return 0;
3553
3554 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003555 spoe_release_agent(curagent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003556 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3557 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003558 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003559 }
3560 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3561 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003562 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003563 }
3564 free(conf);
3565 return -1;
3566}
3567
3568
3569/* Declare the filter parser for "spoe" keyword */
3570static struct flt_kw_list flt_kws = { "SPOE", { }, {
3571 { "spoe", parse_spoe_flt, NULL },
3572 { NULL, NULL, NULL },
3573 }
3574};
3575
3576__attribute__((constructor))
3577static void __spoe_init(void)
3578{
3579 flt_register_keywords(&flt_kws);
3580
3581 LIST_INIT(&curmsgs);
3582 LIST_INIT(&curmps);
3583 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003584 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003585}
3586
3587__attribute__((destructor))
3588static void
3589__spoe_deinit(void)
3590{
3591 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003592 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003593}