blob: ddfb67ba2bffe34147ec13b7f8bbe3fef3ea9c24 [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 Fauletf032c3e2017-02-17 15:18:35 +0100498 stream_id = ctx->stream_id;
499 frame_id = ctx->frame_id;
500
501 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
502 /* The fragmentation is not supported by the applet */
503 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
504 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
505 return -1;
506 }
507 flags = ctx->frag_ctx.flags;
508 }
509
510 /* Set Frame type */
511 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
512
513 /* Set flags */
514 memcpy(p, (char *)&flags, 4);
515 p += 4;
516
517 /* Set stream-id and frame-id */
518 if (spoe_encode_varint(stream_id, &p, end) == -1)
519 goto too_big;
520 if (spoe_encode_varint(frame_id, &p, end) == -1)
521 goto too_big;
522
523 /* Copy encoded messages, if possible */
524 sz = ctx->buffer->i;
525 if (p + sz >= end)
526 goto too_big;
527 memcpy(p, ctx->buffer->p, sz);
528 p += sz;
529
530 return (p - frame);
531
532 too_big:
533 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
534 return 0;
535}
536
537/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
538 * returns the number of encoded bytes in the frame on success, 0 if an encoding
539 * error occurred and -1 if a fatal error occurred. */
540static int
541spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
542 char *frame, size_t size)
543{
544 char *p, *end;
545 unsigned int stream_id, frame_id;
546 unsigned int flags;
547 size_t sz;
548
549 p = frame;
550 end = frame+size;
551
Christopher Faulet8ef75252017-02-20 22:56:03 +0100552 /* <ctx> is null when the stream has aborted the processing of a
553 * fragmented frame. In this case, we must notify the corresponding
554 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100555 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100556 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100557 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
558 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
559 }
560 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100561 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100562 stream_id = ctx->stream_id;
563 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100564 }
565
Christopher Faulet8ef75252017-02-20 22:56:03 +0100566 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100567 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100568
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100569 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100570 memcpy(p, (char *)&flags, 4);
571 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200572
573 /* Set stream-id and frame-id */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100574 if (spoe_encode_varint(stream_id, &p, end) == -1)
575 goto too_big;
576 if (spoe_encode_varint(frame_id, &p, end) == -1)
577 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200578
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100579 if (ctx == NULL)
580 goto end;
581
Christopher Faulet8ef75252017-02-20 22:56:03 +0100582 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100583 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100584 if (p + sz >= end)
585 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100586 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100587 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100588
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100589 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100590 return (p - frame);
591
592 too_big:
593 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
594 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200595}
596
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597/* Decode and process the HELLO frame sent by an agent. It returns the number of
598 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
599 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200600static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100601spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200602{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 char *p, *end;
604 int vsn, max_frame_size;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100605 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100606
607 p = frame;
608 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200609
610 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100611 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
612 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200613 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200615
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 if (size < 7 /* TYPE + METADATA */) {
617 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
618 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200619 }
620
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100621 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 memcpy((char *)&flags, p, 4);
623 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200624
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100625 /* Fragmentation is not supported for HELLO frame */
626 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100628 return -1;
629 }
630
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200631 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632 if (*p != 0 || *(p+1) != 0) {
633 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
634 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200635 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200637
638 /* There are 3 mandatory items: "version", "max-frame-size" and
639 * "capabilities" */
640
641 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100642 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 while (p < end) {
644 char *str;
645 size_t sz;
646 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647
648 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649 ret = spoe_decode_buffer(&p, end, &str, &sz);
650 if (ret == -1 || !sz) {
651 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
652 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655 /* Check "version" K/V item */
656 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100657 int i, type = *p++;
658
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
661 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
662 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
666 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667 }
668
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672 return -1;
673 }
674 for (i = 0; supported_versions[i].str != NULL; ++i) {
675 if (vsn >= supported_versions[i].min &&
676 vsn <= supported_versions[i].max)
677 break;
678 }
679 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100680 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200681 return -1;
682 }
683 }
684 /* Check "max-frame-size" K/V item */
685 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687
688 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
690 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
691 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
692 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
694 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696 if (spoe_decode_varint(&p, end, &sz) == -1) {
697 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
698 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 if (sz < MIN_FRAME_SIZE ||
701 sz > SPOE_APPCTX(appctx)->max_frame_size) {
702 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703 return -1;
704 }
705 max_frame_size = sz;
706 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100707 /* Check "capabilities" K/V item */
708 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100710
711 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
713 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
714 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100715 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
717 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
718 return 0;
719 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100720
Christopher Faulet8ef75252017-02-20 22:56:03 +0100721 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100722 char *delim;
723
724 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100726
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
728 str += 10; sz -= 10;
729 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100730 flags |= SPOE_APPCTX_FL_PIPELINING;
731 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 else if (sz >= 5 && !strncmp(str, "async", 5)) {
733 str += 5; sz -= 5;
734 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100735 flags |= SPOE_APPCTX_FL_ASYNC;
736 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100737 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
738 str += 13; sz -= 13;
739 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100740 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
741 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 /* Get the next comma or break */
744 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100745 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 delim++;
747 sz -= (delim - str);
748 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100749 }
750 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200751 else {
752 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100753 if (spoe_skip_data(&p, end) == -1) {
754 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
755 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200756 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200757 }
758 }
759
760 /* Final checks */
761 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200763 return -1;
764 }
765 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200767 return -1;
768 }
769
Christopher Faulet42bfa462017-01-04 14:14:19 +0100770 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
771 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
772 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100773
774 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200775}
776
777/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
778 * bytes on success, 0 if the frame can be ignored and -1 if an error
779 * occurred. */
780static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200782{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100784 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100785
786 p = frame;
787 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200788
789 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100790 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
791 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200792 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200794
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 if (size < 7 /* TYPE + METADATA */) {
796 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
797 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200798 }
799
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100800 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100801 memcpy((char *)&flags, p, 4);
802 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200803
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100804 /* Fragmentation is not supported for DISCONNECT frame */
805 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100806 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100807 return -1;
808 }
809
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811 if (*p != 0 || *(p+1) != 0) {
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 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816
817 /* There are 2 mandatory items: "status-code" and "message" */
818
819 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100820 while (p < end) {
821 char *str;
822 size_t sz;
823 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200824
825 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826 ret = spoe_decode_buffer(&p, end, &str, &sz);
827 if (ret == -1 || !sz) {
828 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
829 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830 }
831
832 /* Check "status-code" K/V item */
833 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835
836 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
838 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
839 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
840 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
842 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200843 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 if (spoe_decode_varint(&p, end, &sz) == -1) {
845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
846 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849 }
850
851 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852 else if (!memcmp(str, MSG_KEY, sz)) {
853 int type = *p++;
854
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
857 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
858 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 ret = spoe_decode_buffer(&p, end, &str, &sz);
861 if (ret == -1 || sz > 255) {
862 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
863 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200864 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
866 SPOE_APPCTX(appctx)->reason = str;
867 SPOE_APPCTX(appctx)->rlen = sz;
868#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 }
870 else {
871 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 if (spoe_skip_data(&p, end) == -1) {
873 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
874 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 }
877 }
878
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200880}
881
882
Christopher Fauleta1cda022016-12-21 08:58:06 +0100883/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884 * success, 0 if the frame can be ignored and -1 if an error occurred. */
885static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100886spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100887 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100889 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100890 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100891 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100893 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894
895 p = frame;
896 end = frame + size;
897 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898
899 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100903 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200904
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 if (size < 7 /* TYPE + METADATA */) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
909
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100910 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911 memcpy((char *)&flags, p, 4);
912 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100914 /* Fragmentation is not supported for now */
915 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100917 return -1;
918 }
919
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 /* Get the stream-id and the frame-id */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 if (spoe_decode_varint(&p, end, &stream_id) == -1) {
922 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100923 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924 }
925 if (spoe_decode_varint(&p, end, &frame_id) == -1) {
926 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100929
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100931 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100932 list_for_each_entry((*ctx), &agent->waiting_queue, list) {
933 if ((*ctx)->stream_id == (unsigned int)stream_id &&
934 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100935 goto found;
936 }
937 }
938 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100939 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
940 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100942 goto found;
943 }
944 }
945
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100946 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
947 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
948 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
949
950 /* ABRT bit is set for an unfinished fragmented frame */
951 if (flags & SPOE_FRM_FL_ABRT) {
952 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
953 (*ctx)->frag_ctx.spoe_appctx = NULL;
954 (*ctx)->state = SPOE_CTX_ST_ERROR;
955 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
956 /* Ignore the payload */
957 goto end;
958 }
959 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
960 /* For now, we ignore the ack */
961 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
962 return 0;
963 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100964
Christopher Fauleta1cda022016-12-21 08:58:06 +0100965 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
967 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100968 " - stream-id=%u - frame-id=%u\n",
969 (int)now.tv_sec, (int)now.tv_usec, agent->id,
970 __FUNCTION__, appctx,
971 (unsigned int)stream_id, (unsigned int)frame_id);
972
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100973 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100974 return 0;
975
976 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
978 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100979 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100980 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100981 }
Christopher Faulet4596fb72017-01-11 14:05:19 +0100982
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200983 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 len = (end - p);
985 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
986 SPOE_APPCTX(appctx)->buffer->i = len;
987 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200988
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100989 /* Transfer the buffer ownership to the SPOE context */
990 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
991 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100992
Christopher Faulet8ef75252017-02-20 22:56:03 +0100993 (*ctx)->state = SPOE_CTX_ST_DONE;
994
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100995 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100996 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +0100997 " - ACK frame received"
998 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100999 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001000 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1001 (*ctx)->frame_id, flags);
1002 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001003}
1004
Christopher Fauletba7bc162016-11-07 21:07:38 +01001005/* This function is used in cfgparse.c and declared in proto/checks.h. It
1006 * prepare the request to send to agents during a healthcheck. It returns 0 on
1007 * success and -1 if an error occurred. */
1008int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001009spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001010{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001011 struct appctx appctx;
1012 struct spoe_appctx spoe_appctx;
1013 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1014 size_t sz;
1015 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001016
Christopher Faulet42bfa462017-01-04 14:14:19 +01001017 memset(&appctx, 0, sizeof(appctx));
1018 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001019 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001020
1021 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001022 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001023
Christopher Faulet8ef75252017-02-20 22:56:03 +01001024 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1025 end = frame + MAX_FRAME_SIZE;
1026
1027 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1028 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001029 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001030 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001031
Christopher Faulet8ef75252017-02-20 22:56:03 +01001032 /* Add "healthcheck" K/V item */
1033 sz = SLEN(HEALTHCHECK_KEY);
1034 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1035 return -1;
1036 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001037
Christopher Faulet8ef75252017-02-20 22:56:03 +01001038 *len = frame - buf;
1039 sz = htonl(*len - 4);
1040 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001041
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001043 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001044 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001045 return 0;
1046}
1047
1048/* This function is used in checks.c and declared in proto/checks.h. It decode
1049 * the response received from an agent during a healthcheck. It returns 0 on
1050 * success and -1 if an error occurred. */
1051int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001053{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001054 struct appctx appctx;
1055 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001056
Christopher Faulet42bfa462017-01-04 14:14:19 +01001057 memset(&appctx, 0, sizeof(appctx));
1058 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001059
Christopher Faulet42bfa462017-01-04 14:14:19 +01001060 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001061 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001062
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1064 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065 goto error;
1066 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001067 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1068 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069
1070 return 0;
1071
1072 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1074 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1075 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001076 return -1;
1077}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001078
Christopher Fauleta1cda022016-12-21 08:58:06 +01001079/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1080 * the frame can be ignored, 1 to retry later, and the frame legnth on
1081 * success. */
1082static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001084{
1085 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001086 int ret;
1087 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001088
1089 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001091
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1093 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001094 netint = htonl(framesz);
1095 memcpy(buf, (char *)&netint, 4);
1096 ret = bi_putblk(si_ic(si), buf, framesz+4);
1097
1098 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001099 if (ret == -1) {
1100 retry:
1101 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001102 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 }
1104 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001105 return -1; /* error */
1106 }
1107 return framesz;
1108}
1109
1110/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1111 * when the frame can be ignored, 1 to retry later and the frame length on
1112 * success. */
1113static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001114spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001115{
1116 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 int ret;
1118 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001119
1120 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001121 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001122
1123 ret = bo_getblk(si_oc(si), (char *)&netint, 4, 0);
1124 if (ret > 0) {
1125 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001126 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001128 return -1;
1129 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001130 ret = bo_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131 }
1132 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133 if (ret == 0) {
1134 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001135 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136 }
1137 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138 return -1; /* error */
1139 }
1140 return framesz;
1141}
1142
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001143/********************************************************************
1144 * Functions that manage the SPOE applet
1145 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001146static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001147spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001148{
1149 si_applet_want_get(appctx->owner);
1150 si_applet_want_put(appctx->owner);
1151 appctx_wakeup(appctx);
1152 return 1;
1153}
1154
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001155/* Callback function that catches applet timeouts. If a timeout occurred, we set
1156 * <appctx->st1> flag and the SPOE applet is woken up. */
1157static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001158spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001159{
1160 struct appctx *appctx = task->context;
1161
1162 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1163 if (tick_is_expired(task->expire, now_ms)) {
1164 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001165 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001166 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001167 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001168 return task;
1169}
1170
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001171/* Callback function that releases a SPOE applet. This happens when the
1172 * connection with the agent is closed. */
1173static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001174spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001175{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001176 struct stream_interface *si = appctx->owner;
1177 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1178 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001180
1181 if (spoe_appctx == NULL)
1182 return;
1183
1184 appctx->ctx.spoe.ptr = NULL;
1185 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001186
1187 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1188 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1189 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001190
Christopher Faulet8ef75252017-02-20 22:56:03 +01001191 /* Remove applet from the list of running applets */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001192 agent->applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001193 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1194 LIST_DEL(&spoe_appctx->list);
1195 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001196 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001197
Christopher Faulet8ef75252017-02-20 22:56:03 +01001198 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001199 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001200 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
1201 agent->applets_idle--;
1202
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001203 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001204 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1205 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001206
1207 si_shutw(si);
1208 si_shutr(si);
1209 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001210 }
1211
Christopher Faulet8ef75252017-02-20 22:56:03 +01001212 /* Destroy the task attached to this applet */
1213 if (spoe_appctx->task) {
1214 task_delete(spoe_appctx->task);
1215 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001216 }
1217
Christopher Faulet8ef75252017-02-20 22:56:03 +01001218 /* Notify all waiting streams */
1219 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001220 LIST_DEL(&ctx->list);
1221 LIST_INIT(&ctx->list);
1222 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001223 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001224 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225 }
1226
Christopher Faulet8ef75252017-02-20 22:56:03 +01001227 /* If the applet was processing a fragmented frame, notify the
1228 * corresponding stream. */
1229 if (spoe_appctx->frag_ctx.ctx) {
1230 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001231 ctx->frag_ctx.spoe_appctx = NULL;
1232 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001233 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001234 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1235 }
1236
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 /* Release allocated memory */
1238 spoe_release_buffer(&spoe_appctx->buffer,
1239 &spoe_appctx->buffer_wait);
1240 pool_free2(pool2_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001241
Christopher Fauleta1cda022016-12-21 08:58:06 +01001242 if (!LIST_ISEMPTY(&agent->applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001243 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001244
Christopher Faulet8ef75252017-02-20 22:56:03 +01001245 /* If this was the last running applet, notify all waiting streams */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001246 list_for_each_entry_safe(ctx, back, &agent->sending_queue, list) {
1247 LIST_DEL(&ctx->list);
1248 LIST_INIT(&ctx->list);
1249 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001251 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001252 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001253 list_for_each_entry_safe(ctx, back, &agent->waiting_queue, list) {
1254 LIST_DEL(&ctx->list);
1255 LIST_INIT(&ctx->list);
1256 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001257 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001258 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1259 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001260
1261 end:
1262 /* Update runtinme agent info */
1263 agent->frame_size = agent->max_frame_size;
1264 list_for_each_entry(spoe_appctx, &agent->applets, list)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 agent->frame_size = MIN(spoe_appctx->max_frame_size,
1266 agent->frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001267}
1268
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001269static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001270spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001271{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001272 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001273 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001275 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001276
Christopher Fauleta1cda022016-12-21 08:58:06 +01001277 if (si->state <= SI_ST_CON) {
1278 si_applet_want_put(si);
1279 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1280 goto stop;
1281 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001282 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001284 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001285 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001286
Christopher Fauleta1cda022016-12-21 08:58:06 +01001287 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001288 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1289 " - Connection timed out\n",
1290 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1291 __FUNCTION__, appctx);
1292 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001293 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001294 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001295
Christopher Faulet42bfa462017-01-04 14:14:19 +01001296 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001297 SPOE_APPCTX(appctx)->task->expire =
1298 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1301 * length. */
1302 buf = trash.str; frame = buf+4;
1303 ret = spoe_prepare_hahello_frame(appctx, frame,
1304 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001305 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001306 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307
1308 switch (ret) {
1309 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 case 0: /* ignore => an error, cannot be ignored */
1311 goto exit;
1312
1313 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 goto stop;
1315
Christopher Faulet8ef75252017-02-20 22:56:03 +01001316 default:
1317 /* HELLO frame successfully sent, now wait for the
1318 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1320 goto next;
1321 }
1322
1323 next:
1324 return 0;
1325 stop:
1326 return 1;
1327 exit:
1328 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1329 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001330}
1331
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001332static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001333spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001334{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001336 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337 char *frame;
1338 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001340
Christopher Fauletb067b062017-01-04 16:39:11 +01001341 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001344 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001345
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001347 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1348 " - Connection timed out\n",
1349 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1350 __FUNCTION__, appctx);
1351 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352 goto exit;
1353 }
1354
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355 frame = trash.str; trash.len = 0;
1356 ret = spoe_recv_frame(appctx, frame,
1357 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 if (ret > 1) {
1359 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1360 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1361 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001362 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001363 trash.len = ret + 4;
1364 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001365 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001366
Christopher Fauleta1cda022016-12-21 08:58:06 +01001367 switch (ret) {
1368 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001369 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001370 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1371 goto next;
1372
1373 case 1: /* retry later */
1374 goto stop;
1375
1376 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377 /* HELLO handshake is finished, set the idle timeout and
1378 * add the applet in the list of running applets. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001379 agent->applets_idle++;
1380 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001381 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1382 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001383
1384 /* Update runtinme agent info */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001385 agent->frame_size = MIN(SPOE_APPCTX(appctx)->max_frame_size,
1386 agent->frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001387 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389
Christopher Fauleta1cda022016-12-21 08:58:06 +01001390 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 /* Do not forget to remove processed frame from the output buffer */
1392 if (trash.len)
1393 bo_skip(si_oc(si), trash.len);
1394
1395 SPOE_APPCTX(appctx)->task->expire =
1396 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001397 return 0;
1398 stop:
1399 return 1;
1400 exit:
1401 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1402 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403}
1404
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001405
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001407spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001408{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001409 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1410 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411 char *frame, *buf;
1412 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1415 * length. */
1416 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001417
1418 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1419 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1420 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1421 SPOE_APPCTX(appctx)->max_frame_size);
1422 }
1423 else if (LIST_ISEMPTY(&agent->sending_queue)) {
1424 *skip = 1;
1425 ret = 1;
1426 goto end;
1427 }
1428 else {
1429 ctx = LIST_NEXT(&agent->sending_queue, typeof(ctx), list);
1430 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1431 SPOE_APPCTX(appctx)->max_frame_size);
1432
1433 }
1434
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 if (ret > 1)
1436 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001437
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 switch (ret) {
1439 case -1: /* error */
1440 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1441 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001442
Christopher Faulet8ef75252017-02-20 22:56:03 +01001443 case 0: /* ignore */
1444 if (ctx == NULL)
1445 goto abort_frag_frame;
1446
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001447 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001448 LIST_DEL(&ctx->list);
1449 LIST_INIT(&ctx->list);
1450 ctx->state = SPOE_CTX_ST_ERROR;
1451 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1452 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1453 break;
1454
1455 case 1: /* retry */
1456 *skip = 1;
1457 break;
1458
1459 default:
1460 if (ctx == NULL)
1461 goto abort_frag_frame;
1462
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001463 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001464 LIST_DEL(&ctx->list);
1465 LIST_INIT(&ctx->list);
1466 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1467 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1468 goto no_frag_frame_sent;
1469 else {
1470 *skip = 1;
1471 goto frag_frame_sent;
1472 }
1473 }
1474 goto end;
1475
1476 frag_frame_sent:
1477 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1478 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1479 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1480 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1481
1482 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1483 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1484 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1485 goto end;
1486
1487 no_frag_frame_sent:
1488 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1489 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1490 LIST_ADDQ(&agent->waiting_queue, &ctx->list);
1491 }
1492 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1493 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1494 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1495 }
1496 else {
1497 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1498 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1499 }
1500 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1501 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1502 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1503
1504 ctx->frag_ctx.spoe_appctx = NULL;
1505 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1506 goto end;
1507
1508 abort_frag_frame:
1509 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1510 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1511 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1512 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1513 goto end;
1514
1515 end:
1516 return ret;
1517}
1518
1519static int
1520spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1521{
1522 struct spoe_context *ctx = NULL;
1523 char *frame;
1524 int ret;
1525
1526 frame = trash.str; trash.len = 0;
1527 ret = spoe_recv_frame(appctx, frame,
1528 SPOE_APPCTX(appctx)->max_frame_size);
1529 if (ret > 1) {
1530 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1531 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1532 goto end;
1533 }
1534 trash.len = ret + 4;
1535 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1536 }
1537 switch (ret) {
1538 case -1: /* error */
1539 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1540 break;
1541
1542 case 0: /* ignore */
1543 break;
1544
1545 case 1: /* retry */
1546 *skip = 1;
1547 break;
1548
1549 default:
1550 LIST_DEL(&ctx->list);
1551 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001552
1553 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1554 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1555 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1556 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1557 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1558 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1559 }
1560 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1561 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1562
Christopher Faulet8ef75252017-02-20 22:56:03 +01001563 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1564 break;
1565 }
1566
1567 /* Do not forget to remove processed frame from the output buffer */
1568 if (trash.len)
1569 bo_skip(si_oc(appctx->owner), trash.len);
1570 end:
1571 return ret;
1572}
1573
1574static int
1575spoe_handle_processing_appctx(struct appctx *appctx)
1576{
1577 struct stream_interface *si = appctx->owner;
1578 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579 unsigned int fpa = 0;
1580 int ret, skip_sending = 0, skip_receiving = 0;
1581
1582 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1583 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1584 goto exit;
1585 }
1586
1587 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1588 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1589 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1590 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1591 goto next;
1592 }
1593
1594 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001595 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1596 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1597 " - appctx-state=%s\n",
1598 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1599 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001600 skip_sending, skip_receiving,
1601 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001602
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001603 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001604 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001605 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001606 if (skip_receiving)
1607 goto stop;
1608 goto recv_frame;
1609 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001610
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001611 /* send_frame */
1612 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001613 switch (ret) {
1614 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001615 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001616
Christopher Fauleta1cda022016-12-21 08:58:06 +01001617 case 0: /* ignore */
1618 agent->sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001619 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001620 break;
1621
Christopher Fauleta1cda022016-12-21 08:58:06 +01001622 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001623 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001624
Christopher Fauleta1cda022016-12-21 08:58:06 +01001625 default:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001626 agent->sending_rate++;
1627 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001629 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001630 if (fpa > agent->max_fpa)
1631 goto stop;
1632
1633 recv_frame:
1634 if (skip_receiving)
1635 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001636 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001637 switch (ret) {
1638 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001639 goto next;
1640
1641 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001642 fpa++;
1643 break;
1644
1645 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001646 break;
1647
1648 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001649 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001650 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001651 }
1652 goto process;
1653
1654 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001655 SPOE_APPCTX(appctx)->task->expire =
1656 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001657 return 0;
1658 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001659 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001660 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001661 agent->applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001662 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001663 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
1664 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1665 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001666 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001667 SPOE_APPCTX(appctx)->task->expire =
1668 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001669 }
1670 return 1;
1671
1672 exit:
1673 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1674 return 0;
1675}
1676
1677static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001678spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679{
1680 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001681 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001682 char *frame, *buf;
1683 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001684
Christopher Fauleta1cda022016-12-21 08:58:06 +01001685 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1686 goto exit;
1687
1688 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1689 goto exit;
1690
Christopher Faulet8ef75252017-02-20 22:56:03 +01001691 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1692 * length. */
1693 buf = trash.str; frame = buf+4;
1694 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1695 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001697 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001698
1699 switch (ret) {
1700 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001701 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001702 goto exit;
1703
1704 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001705 goto stop;
1706
1707 default:
1708 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1709 " - disconnected by HAProxy (%d): %s\n",
1710 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001711 __FUNCTION__, appctx,
1712 SPOE_APPCTX(appctx)->status_code,
1713 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714
1715 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1716 goto next;
1717 }
1718
1719 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001720 SPOE_APPCTX(appctx)->task->expire =
1721 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001722 return 0;
1723 stop:
1724 return 1;
1725 exit:
1726 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1727 return 0;
1728}
1729
1730static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001731spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001732{
1733 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001734 char *frame;
1735 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001736
Christopher Fauletb067b062017-01-04 16:39:11 +01001737 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001738 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001740 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001741
Christopher Fauletb067b062017-01-04 16:39:11 +01001742 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001743 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001745 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746
Christopher Faulet8ef75252017-02-20 22:56:03 +01001747 frame = trash.str; trash.len = 0;
1748 ret = spoe_recv_frame(appctx, frame,
1749 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001751 trash.len = ret + 4;
1752 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753 }
1754
1755 switch (ret) {
1756 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1758 " - error on frame (%s)\n",
1759 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001760 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001762 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763 goto exit;
1764
1765 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766 goto next;
1767
1768 case 1: /* retry */
1769 goto stop;
1770
1771 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001773 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001775 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001776 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1777 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 goto exit;
1779 }
1780
1781 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 /* Do not forget to remove processed frame from the output buffer */
1783 if (trash.len)
1784 bo_skip(si_oc(appctx->owner), trash.len);
1785
Christopher Fauleta1cda022016-12-21 08:58:06 +01001786 return 0;
1787 stop:
1788 return 1;
1789 exit:
1790 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1791 return 0;
1792}
1793
1794/* I/O Handler processing messages exchanged with the agent */
1795static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798 struct stream_interface *si = appctx->owner;
1799 struct spoe_agent *agent;
1800
1801 if (SPOE_APPCTX(appctx) == NULL)
1802 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803
Christopher Faulet8ef75252017-02-20 22:56:03 +01001804 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1805 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001806
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 switchstate:
1808 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1809 " - appctx-state=%s\n",
1810 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1811 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1812
1813 switch (appctx->st0) {
1814 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816 goto out;
1817 goto switchstate;
1818
1819 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001820 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821 goto out;
1822 goto switchstate;
1823
1824 case SPOE_APPCTX_ST_IDLE:
1825 if (stopping &&
1826 LIST_ISEMPTY(&agent->sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001827 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 SPOE_APPCTX(appctx)->task->expire =
1829 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001831 goto switchstate;
1832 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 agent->applets_idle--;
1834 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1835 /* fall through */
1836
1837 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001838 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1839 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001840 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 goto out;
1842 goto switchstate;
1843
1844 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 goto out;
1847 goto switchstate;
1848
1849 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 goto out;
1852 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001853
1854 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001855 appctx->st0 = SPOE_APPCTX_ST_END;
1856 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1857
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001858 si_shutw(si);
1859 si_shutr(si);
1860 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001861 /* fall through */
1862
1863 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001864 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001865 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001866 out:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001867 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1868 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001869 si_oc(si)->flags |= CF_READ_DONTWAIT;
1870 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001871}
1872
1873struct applet spoe_applet = {
1874 .obj_type = OBJ_TYPE_APPLET,
1875 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001876 .fct = spoe_handle_appctx,
1877 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001878};
1879
1880/* Create a SPOE applet. On success, the created applet is returned, else
1881 * NULL. */
1882static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001884{
1885 struct appctx *appctx;
1886 struct session *sess;
1887 struct task *task;
1888 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001889
1890 if ((appctx = appctx_new(&spoe_applet)) == NULL)
1891 goto out_error;
1892
Christopher Faulet42bfa462017-01-04 14:14:19 +01001893 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx);
1894 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001895 goto out_free_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001896 memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001897
Christopher Faulet42bfa462017-01-04 14:14:19 +01001898 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1899 if ((SPOE_APPCTX(appctx)->task = task_new()) == NULL)
1900 goto out_free_spoe_appctx;
1901
1902 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001903 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001904 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1905 SPOE_APPCTX(appctx)->task->context = appctx;
1906 SPOE_APPCTX(appctx)->agent = conf->agent;
1907 SPOE_APPCTX(appctx)->version = 0;
1908 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1909 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001910 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001911 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1912
1913 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1914 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001915 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001916
1917 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1918 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001919
Willy Tarreau5820a362016-12-22 15:59:02 +01001920 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001921 if (!sess)
1922 goto out_free_spoe;
1923
1924 if ((task = task_new()) == NULL)
1925 goto out_free_sess;
1926
1927 if ((strm = stream_new(sess, task, &appctx->obj_type)) == NULL)
1928 goto out_free_task;
1929
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001930 stream_set_backend(strm, conf->agent->b.be);
1931
1932 /* applet is waiting for data */
1933 si_applet_cant_get(&strm->si[0]);
1934 appctx_wakeup(appctx);
1935
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936 strm->do_log = NULL;
1937 strm->res.flags |= CF_READ_DONTWAIT;
1938
1939 conf->agent_fe.feconn++;
1940 jobs++;
1941 totalconn++;
1942
Christopher Faulet42bfa462017-01-04 14:14:19 +01001943 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
1944 LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001945 conf->agent->applets_act++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001946 return appctx;
1947
1948 /* Error unrolling */
1949 out_free_task:
1950 task_free(task);
1951 out_free_sess:
1952 session_free(sess);
1953 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001954 task_free(SPOE_APPCTX(appctx)->task);
1955 out_free_spoe_appctx:
1956 pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957 out_free_appctx:
1958 appctx_free(appctx);
1959 out_error:
1960 return NULL;
1961}
1962
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001964spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965{
1966 struct spoe_config *conf = FLT_CONF(ctx->filter);
1967 struct spoe_agent *agent = conf->agent;
1968 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001970 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971
Christopher Fauleta1cda022016-12-21 08:58:06 +01001972 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001973
Christopher Fauleta1cda022016-12-21 08:58:06 +01001974 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001975 if (agent->applets_act >= min_applets &&
1976 agent->applets_idle &&
1977 agent->sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001978 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001979
1980 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001981 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001982 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
1983 ctx->strm);
1984
Christopher Fauleta1cda022016-12-21 08:58:06 +01001985 /* Do not try to create a new applet if there is no server up for the
1986 * agent's backend. */
1987 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
1988 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
1989 " - cannot create SPOE appctx: no server up\n",
1990 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1991 __FUNCTION__, ctx->strm);
1992 goto end;
1993 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994
Christopher Fauleta1cda022016-12-21 08:58:06 +01001995 /* Do not try to create a new applet if we have reached the maximum of
1996 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01001997 if (agent->cps_max > 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001998 if (!freq_ctr_remain(&agent->conn_per_sec, agent->cps_max, 0)) {
1999 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2000 " - cannot create SPOE appctx: max CPS reached\n",
2001 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2002 __FUNCTION__, ctx->strm);
2003 goto end;
2004 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005 }
2006
Christopher Faulet8ef75252017-02-20 22:56:03 +01002007 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002008 if (appctx == NULL) {
2009 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2010 " - failed to create SPOE appctx\n",
2011 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2012 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002013 send_log(ctx->strm->be, LOG_EMERG,
2014 "SPOE: [%s] failed to create SPOE applet\n",
2015 agent->id);
2016
Christopher Fauleta1cda022016-12-21 08:58:06 +01002017 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002019 if (agent->applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002020 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021
Christopher Fauleta1cda022016-12-21 08:58:06 +01002022 /* Increase the per-process number of cumulated connections */
2023 if (agent->cps_max > 0)
2024 update_freq_ctr(&agent->conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002025
Christopher Fauleta1cda022016-12-21 08:58:06 +01002026 end:
2027 /* The only reason to return an error is when there is no applet */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002028 if (LIST_ISEMPTY(&agent->applets)) {
2029 ctx->status_code = SPOE_CTX_ERR_RES;
2030 return -1;
2031 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002032
Christopher Fauleta1cda022016-12-21 08:58:06 +01002033 /* Add the SPOE context in the sending queue and update all running
2034 * info */
2035 LIST_ADDQ(&agent->sending_queue, &ctx->list);
2036 if (agent->sending_rate)
2037 agent->sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038
2039 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002040 " - Add stream in sending queue"
2041 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002043 ctx->strm, agent->applets_act, agent->applets_idle,
2044 agent->sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002045
Christopher Fauleta1cda022016-12-21 08:58:06 +01002046 /* Finally try to wakeup the first IDLE applet found and move it at the
2047 * end of the list. */
Christopher Faulet42bfa462017-01-04 14:14:19 +01002048 list_for_each_entry(spoe_appctx, &agent->applets, list) {
2049 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002050 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002051 spoe_wakeup_appctx(appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002052 LIST_DEL(&spoe_appctx->list);
2053 LIST_ADDQ(&agent->applets, &spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002054 break;
2055 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002056 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002057 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002058}
2059
2060/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002061 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002062 **************************************************************************/
Christopher Faulet8ef75252017-02-20 22:56:03 +01002063/* Encode SPOE messages for a specific event. Info in <ctx->frag_ctx>, if any,
2064 * are used to handle fragmented content. On success it returns 1. If an error
2065 * occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002067spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002068 struct list *messages, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002069{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002070 struct spoe_config *conf = FLT_CONF(ctx->filter);
2071 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002072 struct spoe_message *msg;
2073 struct sample *smp;
2074 struct spoe_arg *arg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002075 char *p, *end;
2076 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002077
Christopher Faulet8ef75252017-02-20 22:56:03 +01002078 p = ctx->buffer->p;
2079 end = p + agent->frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002081 /* Resume encoding of a SPOE message */
2082 if (ctx->frag_ctx.curmsg != NULL) {
2083 msg = ctx->frag_ctx.curmsg;
2084 goto encode_message;
2085 }
2086
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002087 /* Loop on messages */
2088 list_for_each_entry(msg, messages, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002089 ctx->frag_ctx.curmsg = msg;
2090 ctx->frag_ctx.curarg = NULL;
2091 ctx->frag_ctx.curoff = UINT_MAX;
2092
2093 encode_message:
2094 /* Resume encoding of a SPOE argument */
2095 if (ctx->frag_ctx.curarg != NULL) {
2096 arg = ctx->frag_ctx.curarg;
2097 goto encode_argument;
2098 }
2099
2100 if (ctx->frag_ctx.curoff != UINT_MAX)
2101 goto encode_msg_payload;
2102
Christopher Faulet8ef75252017-02-20 22:56:03 +01002103 /* Check if there is enough space for the message name and the
2104 * number of arguments. It implies <msg->id_len> is encoded on 2
2105 * bytes, at most (< 2288). */
2106 if (p + 2 + msg->id_len + 1 > end)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002107 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002108
Christopher Faulet8ef75252017-02-20 22:56:03 +01002109 /* Encode the message name */
2110 if (spoe_encode_buffer(msg->id, msg->id_len, &p, end) == -1)
2111 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002112
Christopher Faulet8ef75252017-02-20 22:56:03 +01002113 /* Set the number of arguments for this message */
2114 *p++ = msg->nargs;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002115
2116 ctx->frag_ctx.curoff = 0;
2117 encode_msg_payload:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002118
2119 /* Loop on arguments */
2120 list_for_each_entry(arg, &msg->args, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002121 ctx->frag_ctx.curarg = arg;
2122 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002123
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002124 encode_argument:
2125 if (ctx->frag_ctx.curoff != UINT_MAX)
2126 goto encode_arg_value;
2127
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002128 /* Encode the arguement name as a string. It can by NULL */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002129 if (spoe_encode_buffer(arg->name, arg->name_len, &p, end) == -1)
2130 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002131
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002132 ctx->frag_ctx.curoff = 0;
2133 encode_arg_value:
2134
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002135 /* Fetch the arguement value */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002136 smp = sample_process(s->be, s->sess, s,
2137 dir|SMP_OPT_FINAL, arg->expr, NULL);
2138 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, &p, end);
2139 if (ret == -1 || ctx->frag_ctx.curoff)
2140 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002141 }
2142 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002143
2144 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002145 " - encode %s messages - spoe_appctx=%p"
2146 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002147 (int)now.tv_sec, (int)now.tv_usec,
2148 agent->id, __FUNCTION__, s,
2149 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002150 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2151 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002152
Christopher Faulet8ef75252017-02-20 22:56:03 +01002153 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002154 ctx->frag_ctx.curmsg = NULL;
2155 ctx->frag_ctx.curarg = NULL;
2156 ctx->frag_ctx.curoff = 0;
2157 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002158 return 1;
2159
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002160 too_big:
2161 // FIXME: if fragmentation not supported =>
2162 // ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2163 // return -1;
2164
2165 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002166 " - encode fragmented messages - spoe_appctx=%p"
2167 " - curmsg=%p - curarg=%p - curoff=%u"
2168 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002169 (int)now.tv_sec, (int)now.tv_usec,
2170 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2171 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002172 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002173
Christopher Faulet8ef75252017-02-20 22:56:03 +01002174 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002175 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2176 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2177 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002178}
2179
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002180
2181/***************************************************************************
2182 * Functions that handle SPOE actions
2183 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002184/* Helper function to set a variable */
2185static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002186spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002187 struct sample *smp)
2188{
2189 struct spoe_config *conf = FLT_CONF(ctx->filter);
2190 struct spoe_agent *agent = conf->agent;
2191 char varname[64];
2192
2193 memset(varname, 0, sizeof(varname));
2194 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2195 scope, agent->var_pfx, len, name);
2196 vars_set_by_name_ifexist(varname, len, smp);
2197}
2198
2199/* Helper function to unset a variable */
2200static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002201spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002202 struct sample *smp)
2203{
2204 struct spoe_config *conf = FLT_CONF(ctx->filter);
2205 struct spoe_agent *agent = conf->agent;
2206 char varname[64];
2207
2208 memset(varname, 0, sizeof(varname));
2209 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2210 scope, agent->var_pfx, len, name);
2211 vars_unset_by_name_ifexist(varname, len, smp);
2212}
2213
2214
Christopher Faulet8ef75252017-02-20 22:56:03 +01002215static inline int
2216spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2217 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002218{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002219 char *str, *scope, *p = *buf;
2220 struct sample smp;
2221 uint64_t sz;
2222 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002223
Christopher Faulet8ef75252017-02-20 22:56:03 +01002224 if (p + 2 >= end)
2225 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002226
Christopher Faulet8ef75252017-02-20 22:56:03 +01002227 /* SET-VAR requires 3 arguments */
2228 if (*p++ != 3)
2229 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002230
Christopher Faulet8ef75252017-02-20 22:56:03 +01002231 switch (*p++) {
2232 case SPOE_SCOPE_PROC: scope = "proc"; break;
2233 case SPOE_SCOPE_SESS: scope = "sess"; break;
2234 case SPOE_SCOPE_TXN : scope = "txn"; break;
2235 case SPOE_SCOPE_REQ : scope = "req"; break;
2236 case SPOE_SCOPE_RES : scope = "res"; break;
2237 default: goto skip;
2238 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002239
Christopher Faulet8ef75252017-02-20 22:56:03 +01002240 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2241 goto skip;
2242 memset(&smp, 0, sizeof(smp));
2243 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002244
Christopher Faulet8ef75252017-02-20 22:56:03 +01002245 if (spoe_decode_data(&p, end, &smp) == -1)
2246 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002247
Christopher Faulet8ef75252017-02-20 22:56:03 +01002248 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2249 " - set-var '%s.%s.%.*s'\n",
2250 (int)now.tv_sec, (int)now.tv_usec,
2251 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2252 __FUNCTION__, s, scope,
2253 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2254 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002255
Christopher Faulet8ef75252017-02-20 22:56:03 +01002256 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002257
Christopher Faulet8ef75252017-02-20 22:56:03 +01002258 ret = (p - *buf);
2259 *buf = p;
2260 return ret;
2261 skip:
2262 return 0;
2263}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265static inline int
2266spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2267 char **buf, char *end, int dir)
2268{
2269 char *str, *scope, *p = *buf;
2270 struct sample smp;
2271 uint64_t sz;
2272 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002273
Christopher Faulet8ef75252017-02-20 22:56:03 +01002274 if (p + 2 >= end)
2275 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002276
Christopher Faulet8ef75252017-02-20 22:56:03 +01002277 /* UNSET-VAR requires 2 arguments */
2278 if (*p++ != 2)
2279 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002280
Christopher Faulet8ef75252017-02-20 22:56:03 +01002281 switch (*p++) {
2282 case SPOE_SCOPE_PROC: scope = "proc"; break;
2283 case SPOE_SCOPE_SESS: scope = "sess"; break;
2284 case SPOE_SCOPE_TXN : scope = "txn"; break;
2285 case SPOE_SCOPE_REQ : scope = "req"; break;
2286 case SPOE_SCOPE_RES : scope = "res"; break;
2287 default: goto skip;
2288 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002289
Christopher Faulet8ef75252017-02-20 22:56:03 +01002290 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2291 goto skip;
2292 memset(&smp, 0, sizeof(smp));
2293 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002294
Christopher Faulet8ef75252017-02-20 22:56:03 +01002295 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2296 " - unset-var '%s.%s.%.*s'\n",
2297 (int)now.tv_sec, (int)now.tv_usec,
2298 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2299 __FUNCTION__, s, scope,
2300 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2301 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002302
Christopher Faulet8ef75252017-02-20 22:56:03 +01002303 spoe_unset_var(ctx, scope, str, sz, &smp);
2304
2305 ret = (p - *buf);
2306 *buf = p;
2307 return ret;
2308 skip:
2309 return 0;
2310}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002311
Christopher Faulet8ef75252017-02-20 22:56:03 +01002312/* Process SPOE actions for a specific event. It returns 1 on success. If an
2313 * error occurred, 0 is returned. */
2314static int
2315spoe_process_actions(struct stream *s, struct spoe_context *ctx,
2316 enum spoe_event ev, int dir)
2317{
2318 char *p, *end;
2319 int ret;
2320
2321 p = ctx->buffer->p;
2322 end = p + ctx->buffer->i;
2323
2324 while (p < end) {
2325 enum spoe_action_type type;
2326
2327 type = *p++;
2328 switch (type) {
2329 case SPOE_ACT_T_SET_VAR:
2330 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2331 if (!ret)
2332 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002333 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002334
Christopher Faulet8ef75252017-02-20 22:56:03 +01002335 case SPOE_ACT_T_UNSET_VAR:
2336 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2337 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002338 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002339 break;
2340
2341 default:
2342 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343 }
2344 }
2345
2346 return 1;
2347 skip:
2348 return 0;
2349}
2350
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002351/***************************************************************************
2352 * Functions that process SPOE events
2353 **************************************************************************/
2354static inline int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002355spoe_start_event_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002356{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002357 /* If a process is already started for this SPOE context, retry
2358 * later. */
2359 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002360 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002361
2362 /* Set the right flag to prevent request and response processing
2363 * in same time. */
2364 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2365 ? SPOE_CTX_FL_REQ_PROCESS
2366 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002367 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002368}
2369
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002370static inline void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002371spoe_stop_event_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002372{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002373 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2374
2375 if (sa) {
2376 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002378 }
2379
Christopher Fauleta1cda022016-12-21 08:58:06 +01002380 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002381 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002382
Christopher Fauletb067b062017-01-04 16:39:11 +01002383 ctx->status_code = 0;
2384
Christopher Fauleta1cda022016-12-21 08:58:06 +01002385 /* Reset processing timer */
2386 ctx->process_exp = TICK_ETERNITY;
2387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002389
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002390 ctx->frag_ctx.spoe_appctx = NULL;
2391 ctx->frag_ctx.curmsg = NULL;
2392 ctx->frag_ctx.curarg = NULL;
2393 ctx->frag_ctx.curoff = 0;
2394 ctx->frag_ctx.flags = 0;
2395
Christopher Fauleta1cda022016-12-21 08:58:06 +01002396 if (!LIST_ISEMPTY(&ctx->list)) {
2397 LIST_DEL(&ctx->list);
2398 LIST_INIT(&ctx->list);
2399 }
2400}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002401
2402/* Process a SPOE event. First, this functions will process messages attached to
2403 * this event and send them to an agent in a NOTIFY frame. Then, it will wait a
2404 * ACK frame to process corresponding actions. During all the processing, it
2405 * returns 0 and it returns 1 when the processing is finished. If an error
2406 * occurred, -1 is returned. */
2407static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002408spoe_process_event(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002409 enum spoe_event ev)
2410{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002411 struct spoe_config *conf = FLT_CONF(ctx->filter);
2412 struct spoe_agent *agent = conf->agent;
2413 int dir, ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414
2415 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2416 " - ctx-state=%s - event=%s\n",
2417 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletf7a30922016-11-10 15:04:51 +01002418 agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002419 spoe_event_str[ev]);
2420
2421 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2422
2423 if (LIST_ISEMPTY(&(ctx->messages[ev])))
2424 goto out;
2425
2426 if (ctx->state == SPOE_CTX_ST_ERROR)
2427 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002428
2429 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2430 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2431 " - failed to process event '%s': timeout\n",
2432 (int)now.tv_sec, (int)now.tv_usec,
2433 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
Christopher Fauletb067b062017-01-04 16:39:11 +01002434 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002435 goto error;
2436 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437
2438 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002439 if (agent->eps_max > 0) {
2440 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2441 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2442 " - skip event '%s': max EPS reached\n",
2443 (int)now.tv_sec, (int)now.tv_usec,
2444 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
2445 goto skip;
2446 }
2447 }
2448
Christopher Fauletf7a30922016-11-10 15:04:51 +01002449 if (!tick_isset(ctx->process_exp)) {
2450 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2451 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2452 ctx->process_exp);
2453 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01002454 ret = spoe_start_event_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002455 if (!ret)
2456 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002457
Christopher Faulet8ef75252017-02-20 22:56:03 +01002458 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002459 goto error;
2460
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002461 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002462 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002463 }
2464
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002465 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002466 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002467 goto out;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002468 ret = spoe_encode_messages(s, ctx, &(ctx->messages[ev]), dir);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002469 if (ret < 0)
2470 goto error;
2471 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2472 }
2473
2474 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2475 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002476 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002477 ret = 0;
2478 goto out;
2479 }
2480
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002481 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2482 ret = 0;
2483 goto out;
2484 }
2485
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002486 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002487 spoe_process_actions(s, ctx, ev, dir);
2488 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002489 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002490 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002491 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002492 }
2493
2494 out:
2495 return ret;
2496
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002497 error:
Christopher Faulet48026722016-11-16 15:01:12 +01002498 if (agent->eps_max > 0)
2499 update_freq_ctr(&agent->err_per_sec, 1);
2500
Christopher Faulet985532d2016-11-16 15:36:19 +01002501 if (agent->var_on_error) {
2502 struct sample smp;
2503
2504 memset(&smp, 0, sizeof(smp));
2505 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletb067b062017-01-04 16:39:11 +01002506 smp.data.u.sint = ctx->status_code;
Christopher Faulet985532d2016-11-16 15:36:19 +01002507 smp.data.type = SMP_T_BOOL;
2508
Christopher Faulet8ef75252017-02-20 22:56:03 +01002509 spoe_set_var(ctx, "txn", agent->var_on_error,
Christopher Faulet985532d2016-11-16 15:36:19 +01002510 strlen(agent->var_on_error), &smp);
2511 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002512 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2513 " - failed to create process event '%s': code=%u\n",
2514 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2515 __FUNCTION__, ctx->strm, spoe_event_str[ev],
2516 ctx->status_code);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002517 send_log(ctx->strm->be, LOG_WARNING,
2518 "SPOE: [%s] failed to process event '%s': code=%u\n",
2519 agent->id, spoe_event_str[ev], ctx->status_code);
Christopher Faulet985532d2016-11-16 15:36:19 +01002520
Christopher Fauletea62c2a2016-11-14 10:54:21 +01002521 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2522 ? SPOE_CTX_ST_READY
Christopher Fauletb067b062017-01-04 16:39:11 +01002523 : SPOE_CTX_ST_NONE);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002524 ret = 1;
2525 goto end;
2526
2527 skip:
2528 ctx->state = SPOE_CTX_ST_READY;
2529 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002530
Christopher Fauleta1cda022016-12-21 08:58:06 +01002531 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002532 spoe_stop_event_processing(ctx);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002533 return ret;
2534}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002535
2536/***************************************************************************
2537 * Functions that create/destroy SPOE contexts
2538 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002539static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002540spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002541{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002542 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002543 return 1;
2544
Christopher Faulet4596fb72017-01-11 14:05:19 +01002545 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2546 LIST_DEL(&buffer_wait->list);
2547 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002548 }
2549
Christopher Faulet4596fb72017-01-11 14:05:19 +01002550 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002551 return 1;
2552
Christopher Faulet4596fb72017-01-11 14:05:19 +01002553 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002554 return 0;
2555}
2556
2557static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002558spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002560 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2561 LIST_DEL(&buffer_wait->list);
2562 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563 }
2564
2565 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002566 if (*buf != &buf_empty) {
2567 b_free(buf);
2568 offer_buffers(buffer_wait->target,
2569 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570 }
2571}
2572
Christopher Faulet4596fb72017-01-11 14:05:19 +01002573static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002574spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002575{
2576 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2577 return 1;
2578}
2579
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002580static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002581spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002582{
2583 struct spoe_config *conf = FLT_CONF(filter);
2584 struct spoe_context *ctx;
2585
2586 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2587 if (ctx == NULL) {
2588 return NULL;
2589 }
2590 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002591 ctx->filter = filter;
2592 ctx->state = SPOE_CTX_ST_NONE;
2593 ctx->status_code = SPOE_CTX_ERR_NONE;
2594 ctx->flags = 0;
2595 ctx->messages = conf->agent->messages;
2596 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002597 LIST_INIT(&ctx->buffer_wait.list);
2598 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002599 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002600 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002601
Christopher Fauletf7a30922016-11-10 15:04:51 +01002602 ctx->stream_id = 0;
2603 ctx->frame_id = 1;
2604 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002605
2606 return ctx;
2607}
2608
2609static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002610spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002611{
2612 if (!ctx)
2613 return;
2614
Christopher Faulet8ef75252017-02-20 22:56:03 +01002615 spoe_stop_event_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002616 pool_free2(pool2_spoe_ctx, ctx);
2617}
2618
2619static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002620spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002621{
2622 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002623 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002624}
2625
2626
2627/***************************************************************************
2628 * Hooks that manage the filter lifecycle (init/check/deinit)
2629 **************************************************************************/
2630/* Signal handler: Do a soft stop, wakeup SPOE applet */
2631static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002632spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002633{
2634 struct proxy *p;
2635
2636 p = proxy;
2637 while (p) {
2638 struct flt_conf *fconf;
2639
2640 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002641 struct spoe_config *conf;
2642 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002643 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002644
Christopher Faulet3b386a32017-02-23 10:17:15 +01002645 if (fconf->id != spoe_filter_id)
2646 continue;
2647
2648 conf = fconf->conf;
2649 agent = conf->agent;
2650
Christopher Faulet42bfa462017-01-04 14:14:19 +01002651 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002652 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002653 }
2654 }
2655 p = p->next;
2656 }
2657}
2658
2659
2660/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2661static int
2662spoe_init(struct proxy *px, struct flt_conf *fconf)
2663{
2664 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002665
2666 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2667 init_new_proxy(&conf->agent_fe);
2668 conf->agent_fe.parent = conf->agent;
2669 conf->agent_fe.last_change = now.tv_sec;
2670 conf->agent_fe.id = conf->agent->id;
2671 conf->agent_fe.cap = PR_CAP_FE;
2672 conf->agent_fe.mode = PR_MODE_TCP;
2673 conf->agent_fe.maxconn = 0;
2674 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2675 conf->agent_fe.conn_retries = CONN_RETRIES;
2676 conf->agent_fe.accept = frontend_accept;
2677 conf->agent_fe.srv = NULL;
2678 conf->agent_fe.timeout.client = TICK_ETERNITY;
2679 conf->agent_fe.default_target = &spoe_applet.obj_type;
2680 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2681
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002682 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002683 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684 sighandler_registered = 1;
2685 }
2686
2687 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688}
2689
2690/* Free ressources allocated by the SPOE filter. */
2691static void
2692spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2693{
2694 struct spoe_config *conf = fconf->conf;
2695
2696 if (conf) {
2697 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002698
Christopher Faulet8ef75252017-02-20 22:56:03 +01002699 spoe_release_agent(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700 free(conf);
2701 }
2702 fconf->conf = NULL;
2703}
2704
2705/* Check configuration of a SPOE filter for a specified proxy.
2706 * Return 1 on error, else 0. */
2707static int
2708spoe_check(struct proxy *px, struct flt_conf *fconf)
2709{
2710 struct spoe_config *conf = fconf->conf;
2711 struct proxy *target;
2712
2713 target = proxy_be_by_name(conf->agent->b.name);
2714 if (target == NULL) {
2715 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2716 " declared at %s:%d.\n",
2717 px->id, conf->agent->b.name, conf->agent->id,
2718 conf->agent->conf.file, conf->agent->conf.line);
2719 return 1;
2720 }
2721 if (target->mode != PR_MODE_TCP) {
2722 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2723 " at %s:%d does not support HTTP mode.\n",
2724 px->id, target->id, conf->agent->id,
2725 conf->agent->conf.file, conf->agent->conf.line);
2726 return 1;
2727 }
2728
2729 free(conf->agent->b.name);
2730 conf->agent->b.name = NULL;
2731 conf->agent->b.be = target;
2732 return 0;
2733}
2734
2735/**************************************************************************
2736 * Hooks attached to a stream
2737 *************************************************************************/
2738/* Called when a filter instance is created and attach to a stream. It creates
2739 * the context that will be used to process this stream. */
2740static int
2741spoe_start(struct stream *s, struct filter *filter)
2742{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002743 struct spoe_config *conf = FLT_CONF(filter);
2744 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002745 struct spoe_context *ctx;
2746
2747 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002748 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002749 __FUNCTION__, s);
2750
Christopher Faulet8ef75252017-02-20 22:56:03 +01002751 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002752 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002753 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2754 " - failed to create SPOE context\n",
2755 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2756 __FUNCTION__, ctx->strm);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002757 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002758 "SPOE: [%s] failed to create SPOE context\n",
2759 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002760 return 0;
2761 }
2762
2763 ctx->strm = s;
2764 ctx->state = SPOE_CTX_ST_READY;
2765 filter->ctx = ctx;
2766
2767 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_FE]))
2768 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2769
2770 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_BE]))
2771 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2772
2773 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_RSP]))
2774 filter->pre_analyzers |= AN_RES_INSPECT;
2775
2776 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_FE]))
2777 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2778
2779 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_BE]))
2780 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2781
2782 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_RSP]))
2783 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2784
2785 return 1;
2786}
2787
2788/* Called when a filter instance is detached from a stream. It release the
2789 * attached SPOE context. */
2790static void
2791spoe_stop(struct stream *s, struct filter *filter)
2792{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002793 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2794 (int)now.tv_sec, (int)now.tv_usec,
2795 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2796 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002797 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002798}
2799
Christopher Fauletf7a30922016-11-10 15:04:51 +01002800
2801/*
2802 * Called when the stream is woken up because of expired timer.
2803 */
2804static void
2805spoe_check_timeouts(struct stream *s, struct filter *filter)
2806{
2807 struct spoe_context *ctx = filter->ctx;
2808
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002809 if (tick_is_expired(ctx->process_exp, now_ms)) {
2810 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002811 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002812 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002813}
2814
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002815/* Called when we are ready to filter data on a channel */
2816static int
2817spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2818{
2819 struct spoe_context *ctx = filter->ctx;
2820 int ret = 1;
2821
2822 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2823 " - ctx-flags=0x%08x\n",
2824 (int)now.tv_sec, (int)now.tv_usec,
2825 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2826 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2827
Christopher Fauletb067b062017-01-04 16:39:11 +01002828 if (ctx->state == SPOE_CTX_ST_NONE)
2829 goto out;
2830
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002831 if (!(chn->flags & CF_ISRESP)) {
2832 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2833 chn->analysers |= AN_REQ_INSPECT_FE;
2834 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2835 chn->analysers |= AN_REQ_INSPECT_BE;
2836
2837 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2838 goto out;
2839
2840 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002841 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01002842 if (!ret)
2843 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002844 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
2845 }
2846 else {
2847 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
2848 chn->analysers |= AN_RES_INSPECT;
2849
2850 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
2851 goto out;
2852
Christopher Faulet8ef75252017-02-20 22:56:03 +01002853 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002854 if (!ret) {
2855 channel_dont_read(chn);
2856 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01002857 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002858 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002859 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002860 }
2861
2862 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002863 return ret;
2864}
2865
2866/* Called before a processing happens on a given channel */
2867static int
2868spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
2869 struct channel *chn, unsigned an_bit)
2870{
2871 struct spoe_context *ctx = filter->ctx;
2872 int ret = 1;
2873
2874 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2875 " - ctx-flags=0x%08x - ana=0x%08x\n",
2876 (int)now.tv_sec, (int)now.tv_usec,
2877 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2878 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2879 ctx->flags, an_bit);
2880
Christopher Fauletb067b062017-01-04 16:39:11 +01002881 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002882 goto out;
2883
2884 switch (an_bit) {
2885 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002886 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002887 break;
2888 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002889 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002890 break;
2891 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002892 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002893 break;
2894 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002895 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896 break;
2897 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002898 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002899 break;
2900 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002901 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902 break;
2903 }
2904
2905 out:
2906 if (!ret) {
2907 channel_dont_read(chn);
2908 channel_dont_close(chn);
2909 }
2910 return ret;
2911}
2912
2913/* Called when the filtering on the channel ends. */
2914static int
2915spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2916{
2917 struct spoe_context *ctx = filter->ctx;
2918
2919 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2920 " - ctx-flags=0x%08x\n",
2921 (int)now.tv_sec, (int)now.tv_usec,
2922 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2923 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2924
2925 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002926 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002927 }
2928
2929 return 1;
2930}
2931
2932/********************************************************************
2933 * Functions that manage the filter initialization
2934 ********************************************************************/
2935struct flt_ops spoe_ops = {
2936 /* Manage SPOE filter, called for each filter declaration */
2937 .init = spoe_init,
2938 .deinit = spoe_deinit,
2939 .check = spoe_check,
2940
2941 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01002942 .attach = spoe_start,
2943 .detach = spoe_stop,
2944 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002945
2946 /* Handle channels activity */
2947 .channel_start_analyze = spoe_start_analyze,
2948 .channel_pre_analyze = spoe_chn_pre_analyze,
2949 .channel_end_analyze = spoe_end_analyze,
2950};
2951
2952
2953static int
2954cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
2955{
2956 const char *err;
2957 int i, err_code = 0;
2958
2959 if ((cfg_scope == NULL && curengine != NULL) ||
2960 (cfg_scope != NULL && curengine == NULL) ||
2961 strcmp(curengine, cfg_scope))
2962 goto out;
2963
2964 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
2965 if (!*args[1]) {
2966 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
2967 file, linenum);
2968 err_code |= ERR_ALERT | ERR_ABORT;
2969 goto out;
2970 }
2971 if (*args[2]) {
2972 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
2973 file, linenum, args[2]);
2974 err_code |= ERR_ALERT | ERR_ABORT;
2975 goto out;
2976 }
2977
2978 err = invalid_char(args[1]);
2979 if (err) {
2980 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2981 file, linenum, *err, args[0], args[1]);
2982 err_code |= ERR_ALERT | ERR_ABORT;
2983 goto out;
2984 }
2985
2986 if (curagent != NULL) {
2987 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
2988 file, linenum);
2989 err_code |= ERR_ALERT | ERR_ABORT;
2990 goto out;
2991 }
2992 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
2993 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
2994 err_code |= ERR_ALERT | ERR_ABORT;
2995 goto out;
2996 }
2997
2998 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002999
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003000 curagent->conf.file = strdup(file);
3001 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003002
3003 curagent->timeout.hello = TICK_ETERNITY;
3004 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003005 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003006
3007 curagent->engine_id = NULL;
3008 curagent->var_pfx = NULL;
3009 curagent->var_on_error = NULL;
3010 curagent->flags = 0;
3011 curagent->cps_max = 0;
3012 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003013 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003014 curagent->min_applets = 0;
3015 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003016
3017 for (i = 0; i < SPOE_EV_EVENTS; ++i)
3018 LIST_INIT(&curagent->messages[i]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003019
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003020 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003021 curagent->applets_act = 0;
3022 curagent->applets_idle = 0;
3023 curagent->sending_rate = 0;
3024
3025 LIST_INIT(&curagent->applets);
3026 LIST_INIT(&curagent->sending_queue);
3027 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003028 }
3029 else if (!strcmp(args[0], "use-backend")) {
3030 if (!*args[1]) {
3031 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3032 file, linenum, args[0]);
3033 err_code |= ERR_ALERT | ERR_FATAL;
3034 goto out;
3035 }
3036 if (*args[2]) {
3037 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3038 file, linenum, args[2]);
3039 err_code |= ERR_ALERT | ERR_ABORT;
3040 goto out;
3041 }
3042 free(curagent->b.name);
3043 curagent->b.name = strdup(args[1]);
3044 }
3045 else if (!strcmp(args[0], "messages")) {
3046 int cur_arg = 1;
3047 while (*args[cur_arg]) {
3048 struct spoe_msg_placeholder *mp = NULL;
3049
3050 list_for_each_entry(mp, &curmps, list) {
3051 if (!strcmp(mp->id, args[cur_arg])) {
3052 Alert("parsing [%s:%d]: spoe-message message '%s' already declared.\n",
3053 file, linenum, args[cur_arg]);
3054 err_code |= ERR_ALERT | ERR_FATAL;
3055 goto out;
3056 }
3057 }
3058
3059 if ((mp = calloc(1, sizeof(*mp))) == NULL) {
3060 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3061 err_code |= ERR_ALERT | ERR_ABORT;
3062 goto out;
3063 }
3064 mp->id = strdup(args[cur_arg]);
3065 LIST_ADDQ(&curmps, &mp->list);
3066 cur_arg++;
3067 }
3068 }
3069 else if (!strcmp(args[0], "timeout")) {
3070 unsigned int *tv = NULL;
3071 const char *res;
3072 unsigned timeout;
3073
3074 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003075 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 file, linenum);
3077 err_code |= ERR_ALERT | ERR_FATAL;
3078 goto out;
3079 }
3080 if (!strcmp(args[1], "hello"))
3081 tv = &curagent->timeout.hello;
3082 else if (!strcmp(args[1], "idle"))
3083 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003084 else if (!strcmp(args[1], "processing"))
3085 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003086 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003087 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003088 file, linenum, args[1]);
3089 err_code |= ERR_ALERT | ERR_FATAL;
3090 goto out;
3091 }
3092 if (!*args[2]) {
3093 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3094 file, linenum, args[1]);
3095 err_code |= ERR_ALERT | ERR_FATAL;
3096 goto out;
3097 }
3098 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3099 if (res) {
3100 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3101 file, linenum, *res, args[1]);
3102 err_code |= ERR_ALERT | ERR_ABORT;
3103 goto out;
3104 }
3105 if (*args[3]) {
3106 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3107 file, linenum, args[3]);
3108 err_code |= ERR_ALERT | ERR_ABORT;
3109 goto out;
3110 }
3111 *tv = MS_TO_TICKS(timeout);
3112 }
3113 else if (!strcmp(args[0], "option")) {
3114 if (!*args[1]) {
3115 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3116 file, linenum, args[0]);
3117 err_code |= ERR_ALERT | ERR_FATAL;
3118 goto out;
3119 }
3120 if (!strcmp(args[1], "var-prefix")) {
3121 char *tmp;
3122
3123 if (!*args[2]) {
3124 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3125 file, linenum, args[0],
3126 args[1]);
3127 err_code |= ERR_ALERT | ERR_FATAL;
3128 goto out;
3129 }
3130 tmp = args[2];
3131 while (*tmp) {
3132 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3133 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3134 file, linenum, args[0], args[1]);
3135 err_code |= ERR_ALERT | ERR_FATAL;
3136 goto out;
3137 }
3138 tmp++;
3139 }
3140 curagent->var_pfx = strdup(args[2]);
3141 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003142 else if (!strcmp(args[1], "continue-on-error")) {
3143 if (*args[2]) {
3144 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
Christopher Faulet48026722016-11-16 15:01:12 +01003145 file, linenum, args[2]);
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003146 err_code |= ERR_ALERT | ERR_ABORT;
3147 goto out;
3148 }
3149 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3150 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003151 else if (!strcmp(args[1], "set-on-error")) {
3152 char *tmp;
3153
3154 if (!*args[2]) {
3155 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3156 file, linenum, args[0],
3157 args[1]);
3158 err_code |= ERR_ALERT | ERR_FATAL;
3159 goto out;
3160 }
3161 tmp = args[2];
3162 while (*tmp) {
3163 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3164 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3165 file, linenum, args[0], args[1]);
3166 err_code |= ERR_ALERT | ERR_FATAL;
3167 goto out;
3168 }
3169 tmp++;
3170 }
3171 curagent->var_on_error = strdup(args[2]);
3172 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003173 else {
3174 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3175 file, linenum, args[1]);
3176 err_code |= ERR_ALERT | ERR_FATAL;
3177 goto out;
3178 }
Christopher Faulet48026722016-11-16 15:01:12 +01003179 }
3180 else if (!strcmp(args[0], "maxconnrate")) {
3181 if (!*args[1]) {
3182 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3183 file, linenum, args[0]);
3184 err_code |= ERR_ALERT | ERR_FATAL;
3185 goto out;
3186 }
3187 if (*args[2]) {
3188 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3189 file, linenum, args[2]);
3190 err_code |= ERR_ALERT | ERR_ABORT;
3191 goto out;
3192 }
3193 curagent->cps_max = atol(args[1]);
3194 }
3195 else if (!strcmp(args[0], "maxerrrate")) {
3196 if (!*args[1]) {
3197 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3198 file, linenum, args[0]);
3199 err_code |= ERR_ALERT | ERR_FATAL;
3200 goto out;
3201 }
3202 if (*args[2]) {
3203 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3204 file, linenum, args[2]);
3205 err_code |= ERR_ALERT | ERR_ABORT;
3206 goto out;
3207 }
3208 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003209 }
3210 else if (*args[0]) {
3211 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3212 file, linenum, args[0]);
3213 err_code |= ERR_ALERT | ERR_FATAL;
3214 goto out;
3215 }
3216 out:
3217 return err_code;
3218}
3219
3220static int
3221cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3222{
3223 struct spoe_message *msg;
3224 struct spoe_arg *arg;
3225 const char *err;
3226 char *errmsg = NULL;
3227 int err_code = 0;
3228
3229 if ((cfg_scope == NULL && curengine != NULL) ||
3230 (cfg_scope != NULL && curengine == NULL) ||
3231 strcmp(curengine, cfg_scope))
3232 goto out;
3233
3234 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3235 if (!*args[1]) {
3236 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3237 file, linenum);
3238 err_code |= ERR_ALERT | ERR_ABORT;
3239 goto out;
3240 }
3241 if (*args[2]) {
3242 Alert("parsing [%s:%d] : cannot handle unexpected argument '%s'.\n",
3243 file, linenum, args[2]);
3244 err_code |= ERR_ALERT | ERR_ABORT;
3245 goto out;
3246 }
3247
3248 err = invalid_char(args[1]);
3249 if (err) {
3250 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3251 file, linenum, *err, args[0], args[1]);
3252 err_code |= ERR_ALERT | ERR_ABORT;
3253 goto out;
3254 }
3255
3256 list_for_each_entry(msg, &curmsgs, list) {
3257 if (!strcmp(msg->id, args[1])) {
3258 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3259 " name as another one declared at %s:%d.\n",
3260 file, linenum, args[1], msg->conf.file, msg->conf.line);
3261 err_code |= ERR_ALERT | ERR_FATAL;
3262 goto out;
3263 }
3264 }
3265
3266 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3267 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3268 err_code |= ERR_ALERT | ERR_ABORT;
3269 goto out;
3270 }
3271
3272 curmsg->id = strdup(args[1]);
3273 curmsg->id_len = strlen(curmsg->id);
3274 curmsg->event = SPOE_EV_NONE;
3275 curmsg->conf.file = strdup(file);
3276 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003277 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003278 LIST_INIT(&curmsg->args);
3279 LIST_ADDQ(&curmsgs, &curmsg->list);
3280 }
3281 else if (!strcmp(args[0], "args")) {
3282 int cur_arg = 1;
3283
3284 curproxy->conf.args.ctx = ARGC_SPOE;
3285 curproxy->conf.args.file = file;
3286 curproxy->conf.args.line = linenum;
3287 while (*args[cur_arg]) {
3288 char *delim = strchr(args[cur_arg], '=');
3289 int idx = 0;
3290
3291 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3292 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3293 err_code |= ERR_ALERT | ERR_ABORT;
3294 goto out;
3295 }
3296
3297 if (!delim) {
3298 arg->name = NULL;
3299 arg->name_len = 0;
3300 delim = args[cur_arg];
3301 }
3302 else {
3303 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3304 arg->name_len = delim - args[cur_arg];
3305 delim++;
3306 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003307 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3308 &idx, file, linenum, &errmsg,
3309 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003310 if (arg->expr == NULL) {
3311 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3312 err_code |= ERR_ALERT | ERR_FATAL;
3313 free(arg->name);
3314 free(arg);
3315 goto out;
3316 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003317 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003318 LIST_ADDQ(&curmsg->args, &arg->list);
3319 cur_arg++;
3320 }
3321 curproxy->conf.args.file = NULL;
3322 curproxy->conf.args.line = 0;
3323 }
3324 else if (!strcmp(args[0], "event")) {
3325 if (!*args[1]) {
3326 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
3327 err_code |= ERR_ALERT | ERR_ABORT;
3328 goto out;
3329 }
3330 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3331 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3332 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3333 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3334
3335 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3336 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3337 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3338 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3339 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3340 curmsg->event = SPOE_EV_ON_TCP_RSP;
3341
3342 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3343 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3344 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3345 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3346 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3347 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3348 else {
3349 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3350 file, linenum, args[1]);
3351 err_code |= ERR_ALERT | ERR_ABORT;
3352 goto out;
3353 }
3354 }
3355 else if (!*args[0]) {
3356 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3357 file, linenum, args[0]);
3358 err_code |= ERR_ALERT | ERR_FATAL;
3359 goto out;
3360 }
3361 out:
3362 free(errmsg);
3363 return err_code;
3364}
3365
3366/* Return -1 on error, else 0 */
3367static int
3368parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3369 struct flt_conf *fconf, char **err, void *private)
3370{
3371 struct list backup_sections;
3372 struct spoe_config *conf;
3373 struct spoe_message *msg, *msgback;
3374 struct spoe_msg_placeholder *mp, *mpback;
3375 char *file = NULL, *engine = NULL;
3376 int ret, pos = *cur_arg + 1;
3377
3378 conf = calloc(1, sizeof(*conf));
3379 if (conf == NULL) {
3380 memprintf(err, "%s: out of memory", args[*cur_arg]);
3381 goto error;
3382 }
3383 conf->proxy = px;
3384
3385 while (*args[pos]) {
3386 if (!strcmp(args[pos], "config")) {
3387 if (!*args[pos+1]) {
3388 memprintf(err, "'%s' : '%s' option without value",
3389 args[*cur_arg], args[pos]);
3390 goto error;
3391 }
3392 file = args[pos+1];
3393 pos += 2;
3394 }
3395 else if (!strcmp(args[pos], "engine")) {
3396 if (!*args[pos+1]) {
3397 memprintf(err, "'%s' : '%s' option without value",
3398 args[*cur_arg], args[pos]);
3399 goto error;
3400 }
3401 engine = args[pos+1];
3402 pos += 2;
3403 }
3404 else {
3405 memprintf(err, "unknown keyword '%s'", args[pos]);
3406 goto error;
3407 }
3408 }
3409 if (file == NULL) {
3410 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3411 goto error;
3412 }
3413
3414 /* backup sections and register SPOE sections */
3415 LIST_INIT(&backup_sections);
3416 cfg_backup_sections(&backup_sections);
3417 cfg_register_section("spoe-agent", cfg_parse_spoe_agent);
3418 cfg_register_section("spoe-message", cfg_parse_spoe_message);
3419
3420 /* Parse SPOE filter configuration file */
3421 curengine = engine;
3422 curproxy = px;
3423 curagent = NULL;
3424 curmsg = NULL;
3425 ret = readcfgfile(file);
3426 curproxy = NULL;
3427
3428 /* unregister SPOE sections and restore previous sections */
3429 cfg_unregister_sections();
3430 cfg_restore_sections(&backup_sections);
3431
3432 if (ret == -1) {
3433 memprintf(err, "Could not open configuration file %s : %s",
3434 file, strerror(errno));
3435 goto error;
3436 }
3437 if (ret & (ERR_ABORT|ERR_FATAL)) {
3438 memprintf(err, "Error(s) found in configuration file %s", file);
3439 goto error;
3440 }
3441
3442 /* Check SPOE agent */
3443 if (curagent == NULL) {
3444 memprintf(err, "No SPOE agent found in file %s", file);
3445 goto error;
3446 }
3447 if (curagent->b.name == NULL) {
3448 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3449 curagent->id, curagent->conf.file, curagent->conf.line);
3450 goto error;
3451 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003452 if (curagent->timeout.hello == TICK_ETERNITY ||
3453 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003454 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003455 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3456 " | While not properly invalid, you will certainly encounter various problems\n"
3457 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003458 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3460 }
3461 if (curagent->var_pfx == NULL) {
3462 char *tmp = curagent->id;
3463
3464 while (*tmp) {
3465 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3466 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3467 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3468 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3469 goto error;
3470 }
3471 tmp++;
3472 }
3473 curagent->var_pfx = strdup(curagent->id);
3474 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003475 if (curagent->engine_id == NULL)
3476 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003477
3478 if (LIST_ISEMPTY(&curmps)) {
3479 Warning("Proxy '%s': No message used by SPOE agent '%s' declared at %s:%d.\n",
3480 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3481 goto finish;
3482 }
3483
3484 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3485 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003486 struct spoe_arg *arg;
3487 unsigned int where;
3488
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003489 if (!strcmp(msg->id, mp->id)) {
3490 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3491 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3492 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3493 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3494 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3495 }
3496 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3497 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3498 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3499 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3500 px->id, msg->conf.file, msg->conf.line);
3501 goto next;
3502 }
3503 if (msg->event == SPOE_EV_NONE) {
3504 Warning("Proxy '%s': Ignore SPOE message without event at %s:%d.\n",
3505 px->id, msg->conf.file, msg->conf.line);
3506 goto next;
3507 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003508
3509 where = 0;
3510 switch (msg->event) {
3511 case SPOE_EV_ON_CLIENT_SESS:
3512 where |= SMP_VAL_FE_CON_ACC;
3513 break;
3514
3515 case SPOE_EV_ON_TCP_REQ_FE:
3516 where |= SMP_VAL_FE_REQ_CNT;
3517 break;
3518
3519 case SPOE_EV_ON_HTTP_REQ_FE:
3520 where |= SMP_VAL_FE_HRQ_HDR;
3521 break;
3522
3523 case SPOE_EV_ON_TCP_REQ_BE:
3524 if (px->cap & PR_CAP_FE)
3525 where |= SMP_VAL_FE_REQ_CNT;
3526 if (px->cap & PR_CAP_BE)
3527 where |= SMP_VAL_BE_REQ_CNT;
3528 break;
3529
3530 case SPOE_EV_ON_HTTP_REQ_BE:
3531 if (px->cap & PR_CAP_FE)
3532 where |= SMP_VAL_FE_HRQ_HDR;
3533 if (px->cap & PR_CAP_BE)
3534 where |= SMP_VAL_BE_HRQ_HDR;
3535 break;
3536
3537 case SPOE_EV_ON_SERVER_SESS:
3538 where |= SMP_VAL_BE_SRV_CON;
3539 break;
3540
3541 case SPOE_EV_ON_TCP_RSP:
3542 if (px->cap & PR_CAP_FE)
3543 where |= SMP_VAL_FE_RES_CNT;
3544 if (px->cap & PR_CAP_BE)
3545 where |= SMP_VAL_BE_RES_CNT;
3546 break;
3547
3548 case SPOE_EV_ON_HTTP_RSP:
3549 if (px->cap & PR_CAP_FE)
3550 where |= SMP_VAL_FE_HRS_HDR;
3551 if (px->cap & PR_CAP_BE)
3552 where |= SMP_VAL_BE_HRS_HDR;
3553 break;
3554
3555 default:
3556 break;
3557 }
3558
3559 list_for_each_entry(arg, &msg->args, list) {
3560 if (!(arg->expr->fetch->val & where)) {
3561 Warning("Proxy '%s': Ignore SPOE message at %s:%d: "
3562 "some args extract information from '%s', "
3563 "none of which is available here ('%s').\n",
3564 px->id, msg->conf.file, msg->conf.line,
3565 sample_ckp_names(arg->expr->fetch->use),
3566 sample_ckp_names(where));
3567 goto next;
3568 }
3569 }
3570
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003571 msg->agent = curagent;
3572 LIST_DEL(&msg->list);
3573 LIST_ADDQ(&curagent->messages[msg->event], &msg->list);
3574 goto next;
3575 }
3576 }
3577 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
3578 curagent->id, mp->id, curagent->conf.file, curagent->conf.line);
3579 goto error;
3580 next:
3581 continue;
3582 }
3583
3584 finish:
3585 conf->agent = curagent;
3586 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3587 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003588 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003589 }
3590 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3591 Warning("Proxy '%s': Ignore unused SPOE messages '%s' declared at %s:%d.\n",
3592 px->id, msg->id, msg->conf.file, msg->conf.line);
3593 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003594 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003595 }
3596
3597 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01003598 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003599 fconf->ops = &spoe_ops;
3600 fconf->conf = conf;
3601 return 0;
3602
3603 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003604 spoe_release_agent(curagent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003605 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3606 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003607 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003608 }
3609 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3610 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003611 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003612 }
3613 free(conf);
3614 return -1;
3615}
3616
3617
3618/* Declare the filter parser for "spoe" keyword */
3619static struct flt_kw_list flt_kws = { "SPOE", { }, {
3620 { "spoe", parse_spoe_flt, NULL },
3621 { NULL, NULL, NULL },
3622 }
3623};
3624
3625__attribute__((constructor))
3626static void __spoe_init(void)
3627{
3628 flt_register_keywords(&flt_kws);
3629
3630 LIST_INIT(&curmsgs);
3631 LIST_INIT(&curmps);
3632 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003633 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003634}
3635
3636__attribute__((destructor))
3637static void
3638__spoe_deinit(void)
3639{
3640 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003641 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003642}