blob: abaded156c26c0d664f538f63f3552b7df7e5443 [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/memory.h>
20#include <common/time.h>
21
22#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020023#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010024#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020025
26#include <proto/arg.h>
27#include <proto/backend.h>
28#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010029#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020030#include <proto/frontend.h>
31#include <proto/log.h>
32#include <proto/proto_http.h>
33#include <proto/proxy.h>
34#include <proto/sample.h>
35#include <proto/session.h>
36#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010037#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020038#include <proto/stream.h>
39#include <proto/stream_interface.h>
40#include <proto/task.h>
41#include <proto/vars.h>
42
43#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
44#define SPOE_PRINTF(x...) fprintf(x)
45#else
46#define SPOE_PRINTF(x...)
47#endif
48
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010049/* Reserved 4 bytes to the frame size. So a frame and its size can be written
50 * together in a buffer */
51#define MAX_FRAME_SIZE global.tune.bufsize - 4
52
53/* The minimum size for a frame */
54#define MIN_FRAME_SIZE 256
55
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010056/* Reserved for the metadata and the frame type.
57 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010058#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020059
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010061#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
62
Christopher Faulet3b386a32017-02-23 10:17:15 +010063/* SPOE filter id. Used to identify SPOE filters */
64const char *spoe_filter_id = "SPOE filter";
65
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020066/* Set if the handle on SIGUSR1 is registered */
67static int sighandler_registered = 0;
68
69/* proxy used during the parsing */
70struct proxy *curproxy = NULL;
71
72/* The name of the SPOE engine, used during the parsing */
73char *curengine = NULL;
74
75/* SPOE agent used during the parsing */
76struct spoe_agent *curagent = NULL;
77
78/* SPOE message used during the parsing */
79struct spoe_message *curmsg = NULL;
80
81/* list of SPOE messages and placeholders used during the parsing */
82struct list curmsgs;
83struct list curmps;
84
Christopher Faulet42bfa462017-01-04 14:14:19 +010085/* Pools used to allocate SPOE structs */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086static struct pool_head *pool2_spoe_ctx = NULL;
Christopher Faulet42bfa462017-01-04 14:14:19 +010087static struct pool_head *pool2_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020089struct flt_ops spoe_ops;
90
Christopher Faulet8ef75252017-02-20 22:56:03 +010091static int spoe_queue_context(struct spoe_context *ctx);
92static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
93static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020094
95/********************************************************************
96 * helper functions/globals
97 ********************************************************************/
98static void
Christopher Faulet8ef75252017-02-20 22:56:03 +010099spoe_release_msg_placeholder(struct spoe_msg_placeholder *mp)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100{
101 if (!mp)
102 return;
103 free(mp->id);
104 free(mp);
105}
106
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100108spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109{
110 struct spoe_arg *arg, *back;
111
112 if (!msg)
113 return;
114 free(msg->id);
115 free(msg->conf.file);
116 list_for_each_entry_safe(arg, back, &msg->args, list) {
117 release_sample_expr(arg->expr);
118 free(arg->name);
119 LIST_DEL(&arg->list);
120 free(arg);
121 }
122 free(msg);
123}
124
125static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100126spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127{
128 struct spoe_message *msg, *back;
129 int i;
130
131 if (!agent)
132 return;
133 free(agent->id);
134 free(agent->conf.file);
135 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100136 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100137 free(agent->var_on_error);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200138 for (i = 0; i < SPOE_EV_EVENTS; ++i) {
139 list_for_each_entry_safe(msg, back, &agent->messages[i], list) {
140 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100141 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200142 }
143 }
144 free(agent);
145}
146
147static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100148 [SPOE_FRM_ERR_NONE] = "normal",
149 [SPOE_FRM_ERR_IO] = "I/O error",
150 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
151 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
152 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
153 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
154 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
155 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
156 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
157 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
158 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
159 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100160 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100161 [SPOE_FRM_ERR_RES] = "resource allocation error",
162 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163};
164
165static const char *spoe_event_str[SPOE_EV_EVENTS] = {
166 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
167 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
168 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
169 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
170 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
171
172 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
173 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
174 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
175};
176
177
178#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
179
180static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100181 [SPOE_CTX_ST_NONE] = "NONE",
182 [SPOE_CTX_ST_READY] = "READY",
183 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
184 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
185 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
186 [SPOE_CTX_ST_DONE] = "DONE",
187 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188};
189
190static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100191 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
192 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
193 [SPOE_APPCTX_ST_IDLE] = "IDLE",
194 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
195 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
196 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
197 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
198 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
199 [SPOE_APPCTX_ST_EXIT] = "EXIT",
200 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200201};
202
203#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100204
Christopher Faulet8ef75252017-02-20 22:56:03 +0100205/* Used to generates a unique id for an engine. On success, it returns a
206 * allocated string. So it is the caller's reponsibility to release it. If the
207 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100208static char *
209generate_pseudo_uuid()
210{
211 static int init = 0;
212
213 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
214 const char uuid_chr[] = "0123456789ABCDEF-";
215 char *uuid;
216 int i;
217
218 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
219 return NULL;
220
221 if (!init) {
222 srand(now_ms);
223 init = 1;
224 }
225
226 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
227 int r = rand () % 16;
228
229 switch (uuid_fmt[i]) {
230 case 'x' : uuid[i] = uuid_chr[r]; break;
231 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
232 default : uuid[i] = uuid_fmt[i]; break;
233 }
234 }
235 return uuid;
236}
237
Christopher Faulet8ef75252017-02-20 22:56:03 +0100238/* Returns the minimum number of appets alive at a time. This function is used
239 * to know if more applets should be created for an engine. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100240static inline unsigned int
241min_applets_act(struct spoe_agent *agent)
242{
243 unsigned int nbsrv;
244
Christopher Faulet8ef75252017-02-20 22:56:03 +0100245 /* TODO: Add a config parameter to customize this value. Always 0 for
246 * now */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100247 if (agent->min_applets)
248 return agent->min_applets;
249
Christopher Faulet8ef75252017-02-20 22:56:03 +0100250 /* Get the number of active servers for the backend */
251 nbsrv = (agent->b.be->srv_act
252 ? agent->b.be->srv_act
253 : agent->b.be->srv_bck);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100254 return 2*nbsrv;
255}
256
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200257/********************************************************************
258 * Functions that encode/decode SPOE frames
259 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200260/* Helper to get static string length, excluding the terminating null byte */
261#define SLEN(str) (sizeof(str)-1)
262
263/* Predefined key used in HELLO/DISCONNECT frames */
264#define SUPPORTED_VERSIONS_KEY "supported-versions"
265#define VERSION_KEY "version"
266#define MAX_FRAME_SIZE_KEY "max-frame-size"
267#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100268#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100269#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270#define STATUS_CODE_KEY "status-code"
271#define MSG_KEY "message"
272
273struct spoe_version {
274 char *str;
275 int min;
276 int max;
277};
278
279/* All supported versions */
280static struct spoe_version supported_versions[] = {
281 {"1.0", 1000, 1000},
282 {NULL, 0, 0}
283};
284
285/* Comma-separated list of supported versions */
286#define SUPPORTED_VERSIONS_VAL "1.0"
287
Christopher Faulet8ef75252017-02-20 22:56:03 +0100288/* Convert a string to a SPOE version value. The string must follow the format
289 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
290 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200291static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100292spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200293{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100294 const char *p, *end;
295 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296
Christopher Faulet8ef75252017-02-20 22:56:03 +0100297 p = str;
298 end = str+len;
299 maj = min = 0;
300 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301
Christopher Faulet8ef75252017-02-20 22:56:03 +0100302 /* skip leading spaces */
303 while (p < end && isspace(*p))
304 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306 /* parse Major number, until the '.' */
307 while (*p != '.') {
308 if (p >= end || *p < '0' || *p > '9')
309 goto out;
310 maj *= 10;
311 maj += (*p - '0');
312 p++;
313 }
314
315 /* check Major version */
316 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200317 goto out;
318
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 p++; /* skip the '.' */
320 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
321 goto out;
322
323 /* Parse Minor number */
324 while (p < end) {
325 if (*p < '0' || *p > '9')
326 break;
327 min *= 10;
328 min += (*p - '0');
329 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200330 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100331
332 /* check Minor number */
333 if (min > 999)
334 goto out;
335
336 /* skip trailing spaces */
337 while (p < end && isspace(*p))
338 p++;
339 if (p != end)
340 goto out;
341
342 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200343 out:
344 return vsn;
345}
346
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
348 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
349 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200350static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100351spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200352{
Christopher Faulet305c6072017-02-23 16:17:53 +0100353 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100354 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 char *p, *end;
356 unsigned int flags = SPOE_FRM_FL_FIN;
357 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200358
Christopher Faulet8ef75252017-02-20 22:56:03 +0100359 p = frame;
360 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200361
Christopher Faulet8ef75252017-02-20 22:56:03 +0100362 /* Set Frame type */
363 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100365 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366 memcpy(p, (char *)&flags, 4);
367 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200368
369 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200371
372 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
373 * and "capabilities" */
374
375 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 sz = SLEN(SUPPORTED_VERSIONS_KEY);
377 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
378 goto too_big;
379
380 *p++ = SPOE_DATA_T_STR;
381 sz = SLEN(SUPPORTED_VERSIONS_VAL);
382 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
383 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384
385 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 sz = SLEN(MAX_FRAME_SIZE_KEY);
387 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
388 goto too_big;
389
390 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200391 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200393
394 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 sz = SLEN(CAPABILITIES_KEY);
396 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
397 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100400 chk = get_trash_chunk();
401 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
402 memcpy(chk->str, "pipelining", 10);
403 chk->len += 10;
404 }
405 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
406 if (chk->len) chk->str[chk->len++] = ',';
407 memcpy(chk->str+chk->len, "async", 5);
408 chk->len += 5;
409 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100410 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
411 if (chk->len) chk->str[chk->len++] = ',';
412 memcpy(chk->str+chk->len, "fragmentation", 13);
413 chk->len += 5;
414 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100415 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100416 goto too_big;
417
418 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100419 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100420 sz = SLEN(ENGINE_ID_KEY);
421 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
422 goto too_big;
423
424 *p++ = SPOE_DATA_T_STR;
425 sz = strlen(agent->engine_id);
426 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
427 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100428 }
429
Christopher Faulet8ef75252017-02-20 22:56:03 +0100430 return (p - frame);
431
432 too_big:
433 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
434 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435}
436
Christopher Faulet8ef75252017-02-20 22:56:03 +0100437/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
438 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
439 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200440static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200442{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 const char *reason;
444 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100445 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100446 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200447
Christopher Faulet8ef75252017-02-20 22:56:03 +0100448 p = frame;
449 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200450
Christopher Faulet8ef75252017-02-20 22:56:03 +0100451 /* Set Frame type */
452 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200453
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100454 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455 memcpy(p, (char *)&flags, 4);
456 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200457
458 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 *p++ = 0; *p++ = 0;
460
461 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
462 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200463
464 /* There are 2 mandatory items: "status-code" and "message" */
465
466 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100467 sz = SLEN(STATUS_CODE_KEY);
468 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
469 goto too_big;
470
471 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200472 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474
475 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476 sz = SLEN(MSG_KEY);
477 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
478 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 /*Get the message corresponding to the status code */
481 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
482
483 *p++ = SPOE_DATA_T_STR;
484 sz = strlen(reason);
485 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
486 goto too_big;
487
488 return (p - frame);
489
490 too_big:
491 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
492 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493}
494
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
496 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
497 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200498static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100499spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100500 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200501{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100502 char *p, *end;
503 unsigned int stream_id, frame_id;
504 unsigned int flags = SPOE_FRM_FL_FIN;
505 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506
Christopher Faulet8ef75252017-02-20 22:56:03 +0100507 p = frame;
508 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200509
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100510 stream_id = ctx->stream_id;
511 frame_id = ctx->frame_id;
512
513 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
514 /* The fragmentation is not supported by the applet */
515 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
516 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
517 return -1;
518 }
519 flags = ctx->frag_ctx.flags;
520 }
521
522 /* Set Frame type */
523 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
524
525 /* Set flags */
526 memcpy(p, (char *)&flags, 4);
527 p += 4;
528
529 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200530 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100531 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200532 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100533 goto too_big;
534
535 /* Copy encoded messages, if possible */
536 sz = ctx->buffer->i;
537 if (p + sz >= end)
538 goto too_big;
539 memcpy(p, ctx->buffer->p, sz);
540 p += sz;
541
542 return (p - frame);
543
544 too_big:
545 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
546 return 0;
547}
548
549/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
550 * returns the number of encoded bytes in the frame on success, 0 if an encoding
551 * error occurred and -1 if a fatal error occurred. */
552static int
553spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
554 char *frame, size_t size)
555{
556 char *p, *end;
557 unsigned int stream_id, frame_id;
558 unsigned int flags;
559 size_t sz;
560
561 p = frame;
562 end = frame+size;
563
Christopher Faulet8ef75252017-02-20 22:56:03 +0100564 /* <ctx> is null when the stream has aborted the processing of a
565 * fragmented frame. In this case, we must notify the corresponding
566 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100567 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100568 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100569 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
570 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
571 }
572 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100573 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100574 stream_id = ctx->stream_id;
575 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100576 }
577
Christopher Faulet8ef75252017-02-20 22:56:03 +0100578 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100579 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100580
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100581 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100582 memcpy(p, (char *)&flags, 4);
583 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200584
585 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200586 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100587 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200588 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100589 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200590
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100591 if (ctx == NULL)
592 goto end;
593
Christopher Faulet8ef75252017-02-20 22:56:03 +0100594 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100595 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596 if (p + sz >= end)
597 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100598 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100599 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100600
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100601 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100602 return (p - frame);
603
604 too_big:
605 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
606 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200607}
608
Christopher Faulet8ef75252017-02-20 22:56:03 +0100609/* Decode and process the HELLO frame sent by an agent. It returns the number of
610 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
611 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200612static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200614{
Christopher Faulet305c6072017-02-23 16:17:53 +0100615 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
616 char *p, *end;
617 int vsn, max_frame_size;
618 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619
620 p = frame;
621 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200622
623 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100624 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
625 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 if (size < 7 /* TYPE + METADATA */) {
630 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
631 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632 }
633
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100634 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635 memcpy((char *)&flags, p, 4);
636 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200637
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100638 /* Fragmentation is not supported for HELLO frame */
639 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100641 return -1;
642 }
643
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (*p != 0 || *(p+1) != 0) {
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
647 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200650
651 /* There are 3 mandatory items: "version", "max-frame-size" and
652 * "capabilities" */
653
654 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100655 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 while (p < end) {
657 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200658 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200660
661 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 ret = spoe_decode_buffer(&p, end, &str, &sz);
663 if (ret == -1 || !sz) {
664 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
665 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668 /* Check "version" K/V item */
669 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100670 int i, type = *p++;
671
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
674 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
675 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200676 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
678 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
679 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 }
681
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200683 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 return -1;
686 }
687 for (i = 0; supported_versions[i].str != NULL; ++i) {
688 if (vsn >= supported_versions[i].min &&
689 vsn <= supported_versions[i].max)
690 break;
691 }
692 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 return -1;
695 }
696 }
697 /* Check "max-frame-size" K/V item */
698 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700
701 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
703 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
704 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
705 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
707 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200709 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
711 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 if (sz < MIN_FRAME_SIZE ||
714 sz > SPOE_APPCTX(appctx)->max_frame_size) {
715 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 return -1;
717 }
718 max_frame_size = sz;
719 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100720 /* Check "capabilities" K/V item */
721 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100722 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100723
724 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
727 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100728 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
730 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
731 return 0;
732 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100733
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100735 char *delim;
736
737 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100738 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100739
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
741 str += 10; sz -= 10;
742 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100743 flags |= SPOE_APPCTX_FL_PIPELINING;
744 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 else if (sz >= 5 && !strncmp(str, "async", 5)) {
746 str += 5; sz -= 5;
747 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100748 flags |= SPOE_APPCTX_FL_ASYNC;
749 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100750 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
751 str += 13; sz -= 13;
752 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100753 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
754 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 /* Get the next comma or break */
757 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100758 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 delim++;
760 sz -= (delim - str);
761 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 }
763 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200764 else {
765 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 if (spoe_skip_data(&p, end) == -1) {
767 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
768 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200769 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200770 }
771 }
772
773 /* Final checks */
774 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200776 return -1;
777 }
778 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200780 return -1;
781 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100782 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
783 flags &= ~SPOE_APPCTX_FL_PIPELINING;
784 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
785 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786
Christopher Faulet42bfa462017-01-04 14:14:19 +0100787 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
788 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
789 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100790
791 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200792}
793
794/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
795 * bytes on success, 0 if the frame can be ignored and -1 if an error
796 * occurred. */
797static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200799{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100801 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802
803 p = frame;
804 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805
806 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100807 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200809 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100810 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200811
Christopher Faulet8ef75252017-02-20 22:56:03 +0100812 if (size < 7 /* TYPE + METADATA */) {
813 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
814 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815 }
816
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100817 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100818 memcpy((char *)&flags, p, 4);
819 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100821 /* Fragmentation is not supported for DISCONNECT frame */
822 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100824 return -1;
825 }
826
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200827 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828 if (*p != 0 || *(p+1) != 0) {
829 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
830 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833
834 /* There are 2 mandatory items: "status-code" and "message" */
835
836 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100837 while (p < end) {
838 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200839 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841
842 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 ret = spoe_decode_buffer(&p, end, &str, &sz);
844 if (ret == -1 || !sz) {
845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
846 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847 }
848
849 /* Check "status-code" K/V item */
850 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852
853 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
855 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
856 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
857 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100858 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
859 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200861 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 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 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200866 }
867
868 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100869 else if (!memcmp(str, MSG_KEY, sz)) {
870 int type = *p++;
871
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200872 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
875 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100877 ret = spoe_decode_buffer(&p, end, &str, &sz);
878 if (ret == -1 || sz > 255) {
879 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
880 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
883 SPOE_APPCTX(appctx)->reason = str;
884 SPOE_APPCTX(appctx)->rlen = sz;
885#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 }
887 else {
888 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 if (spoe_skip_data(&p, end) == -1) {
890 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
891 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
894 }
895
Christopher Faulet8ef75252017-02-20 22:56:03 +0100896 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897}
898
899
Christopher Fauleta1cda022016-12-21 08:58:06 +0100900/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200901 * success, 0 if the frame can be ignored and -1 if an error occurred. */
902static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100903spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100904 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100906 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100908 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100910 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911
912 p = frame;
913 end = frame + size;
914 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915
916 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
918 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921
Christopher Faulet8ef75252017-02-20 22:56:03 +0100922 if (size < 7 /* TYPE + METADATA */) {
923 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
924 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 }
926
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100927 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 memcpy((char *)&flags, p, 4);
929 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 /* Fragmentation is not supported for now */
932 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100934 return -1;
935 }
936
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200938 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100940 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200942 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100946
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100948 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100949 list_for_each_entry((*ctx), &agent->waiting_queue, list) {
950 if ((*ctx)->stream_id == (unsigned int)stream_id &&
951 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100952 goto found;
953 }
954 }
955 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100956 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
957 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100959 goto found;
960 }
961 }
962
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100963 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
964 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
965 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
966
967 /* ABRT bit is set for an unfinished fragmented frame */
968 if (flags & SPOE_FRM_FL_ABRT) {
969 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
970 (*ctx)->frag_ctx.spoe_appctx = NULL;
971 (*ctx)->state = SPOE_CTX_ST_ERROR;
972 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
973 /* Ignore the payload */
974 goto end;
975 }
976 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
977 /* For now, we ignore the ack */
978 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
979 return 0;
980 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100981
Christopher Fauleta1cda022016-12-21 08:58:06 +0100982 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
984 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100985 " - stream-id=%u - frame-id=%u\n",
986 (int)now.tv_sec, (int)now.tv_usec, agent->id,
987 __FUNCTION__, appctx,
988 (unsigned int)stream_id, (unsigned int)frame_id);
989
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100990 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100991 return 0;
992
993 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100994 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
995 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100996 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100997 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100998 }
Christopher Faulet4596fb72017-01-11 14:05:19 +0100999
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001000 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001001 len = (end - p);
1002 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1003 SPOE_APPCTX(appctx)->buffer->i = len;
1004 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001005
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001006 /* Transfer the buffer ownership to the SPOE context */
1007 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1008 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001009
Christopher Faulet8ef75252017-02-20 22:56:03 +01001010 (*ctx)->state = SPOE_CTX_ST_DONE;
1011
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001012 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001013 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001014 " - ACK frame received"
1015 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001016 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001017 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1018 (*ctx)->frame_id, flags);
1019 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001020}
1021
Christopher Fauletba7bc162016-11-07 21:07:38 +01001022/* This function is used in cfgparse.c and declared in proto/checks.h. It
1023 * prepare the request to send to agents during a healthcheck. It returns 0 on
1024 * success and -1 if an error occurred. */
1025int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001026spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001027{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001028 struct appctx appctx;
1029 struct spoe_appctx spoe_appctx;
1030 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1031 size_t sz;
1032 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001033
Christopher Faulet42bfa462017-01-04 14:14:19 +01001034 memset(&appctx, 0, sizeof(appctx));
1035 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001036 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001037
1038 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001039 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001040
Christopher Faulet8ef75252017-02-20 22:56:03 +01001041 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1042 end = frame + MAX_FRAME_SIZE;
1043
1044 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1045 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001046 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001047 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001048
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 /* Add "healthcheck" K/V item */
1050 sz = SLEN(HEALTHCHECK_KEY);
1051 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1052 return -1;
1053 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001054
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 *len = frame - buf;
1056 sz = htonl(*len - 4);
1057 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001058
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062 return 0;
1063}
1064
1065/* This function is used in checks.c and declared in proto/checks.h. It decode
1066 * the response received from an agent during a healthcheck. It returns 0 on
1067 * success and -1 if an error occurred. */
1068int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001069spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001070{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001071 struct appctx appctx;
1072 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073
Christopher Faulet42bfa462017-01-04 14:14:19 +01001074 memset(&appctx, 0, sizeof(appctx));
1075 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001076
Christopher Faulet42bfa462017-01-04 14:14:19 +01001077 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001078 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001079
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1081 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082 goto error;
1083 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1085 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
1087 return 0;
1088
1089 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1091 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1092 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093 return -1;
1094}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001095
Christopher Fauleta1cda022016-12-21 08:58:06 +01001096/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1097 * the frame can be ignored, 1 to retry later, and the frame legnth on
1098 * success. */
1099static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001100spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001101{
1102 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103 int ret;
1104 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001105
1106 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001108
Christopher Faulet8ef75252017-02-20 22:56:03 +01001109 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1110 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001111 netint = htonl(framesz);
1112 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001113 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001114
1115 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001116 if (ret == -1) {
1117 retry:
1118 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001119 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001120 }
1121 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001122 return -1; /* error */
1123 }
1124 return framesz;
1125}
1126
1127/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1128 * when the frame can be ignored, 1 to retry later and the frame length on
1129 * success. */
1130static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001131spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132{
1133 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001134 int ret;
1135 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136
1137 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139
Willy Tarreau06d80a92017-10-19 14:32:15 +02001140 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141 if (ret > 0) {
1142 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001143 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001144 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145 return -1;
1146 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001147 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001148 }
1149 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 if (ret == 0) {
1151 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001153 }
1154 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155 return -1; /* error */
1156 }
1157 return framesz;
1158}
1159
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001160/********************************************************************
1161 * Functions that manage the SPOE applet
1162 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001163static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001165{
1166 si_applet_want_get(appctx->owner);
1167 si_applet_want_put(appctx->owner);
1168 appctx_wakeup(appctx);
1169 return 1;
1170}
1171
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001172/* Callback function that catches applet timeouts. If a timeout occurred, we set
1173 * <appctx->st1> flag and the SPOE applet is woken up. */
1174static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001176{
1177 struct appctx *appctx = task->context;
1178
1179 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1180 if (tick_is_expired(task->expire, now_ms)) {
1181 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001183 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001184 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001185 return task;
1186}
1187
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001188/* Callback function that releases a SPOE applet. This happens when the
1189 * connection with the agent is closed. */
1190static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001191spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001192{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001193 struct stream_interface *si = appctx->owner;
1194 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1195 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001196 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197
1198 if (spoe_appctx == NULL)
1199 return;
1200
1201 appctx->ctx.spoe.ptr = NULL;
1202 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001203
1204 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1205 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1206 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 /* Remove applet from the list of running applets */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001209 agent->applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1211 LIST_DEL(&spoe_appctx->list);
1212 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001213 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001217 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
1218 agent->applets_idle--;
1219
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1222 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001223
1224 si_shutw(si);
1225 si_shutr(si);
1226 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001227 }
1228
Christopher Faulet8ef75252017-02-20 22:56:03 +01001229 /* Destroy the task attached to this applet */
1230 if (spoe_appctx->task) {
1231 task_delete(spoe_appctx->task);
1232 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001233 }
1234
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 /* Notify all waiting streams */
1236 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001237 LIST_DEL(&ctx->list);
1238 LIST_INIT(&ctx->list);
1239 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001240 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001241 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242 }
1243
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 /* If the applet was processing a fragmented frame, notify the
1245 * corresponding stream. */
1246 if (spoe_appctx->frag_ctx.ctx) {
1247 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001248 ctx->frag_ctx.spoe_appctx = NULL;
1249 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001251 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1252 }
1253
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254 /* Release allocated memory */
1255 spoe_release_buffer(&spoe_appctx->buffer,
1256 &spoe_appctx->buffer_wait);
1257 pool_free2(pool2_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001258
Christopher Fauleta1cda022016-12-21 08:58:06 +01001259 if (!LIST_ISEMPTY(&agent->applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001260 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001261
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 /* If this was the last running applet, notify all waiting streams */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001263 list_for_each_entry_safe(ctx, back, &agent->sending_queue, list) {
1264 LIST_DEL(&ctx->list);
1265 LIST_INIT(&ctx->list);
1266 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001267 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001268 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001269 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 list_for_each_entry_safe(ctx, back, &agent->waiting_queue, list) {
1271 LIST_DEL(&ctx->list);
1272 LIST_INIT(&ctx->list);
1273 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001275 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1276 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001277
1278 end:
1279 /* Update runtinme agent info */
1280 agent->frame_size = agent->max_frame_size;
1281 list_for_each_entry(spoe_appctx, &agent->applets, list)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 agent->frame_size = MIN(spoe_appctx->max_frame_size,
1283 agent->frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001284}
1285
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001286static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001287spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001289 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001290 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001291 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001293
Christopher Fauleta1cda022016-12-21 08:58:06 +01001294 if (si->state <= SI_ST_CON) {
1295 si_applet_want_put(si);
1296 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1297 goto stop;
1298 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001299 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001301 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001302 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001303
Christopher Fauleta1cda022016-12-21 08:58:06 +01001304 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001305 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1306 " - Connection timed out\n",
1307 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1308 __FUNCTION__, appctx);
1309 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001312
Christopher Faulet42bfa462017-01-04 14:14:19 +01001313 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001314 SPOE_APPCTX(appctx)->task->expire =
1315 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316
Christopher Faulet8ef75252017-02-20 22:56:03 +01001317 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1318 * length. */
1319 buf = trash.str; frame = buf+4;
1320 ret = spoe_prepare_hahello_frame(appctx, frame,
1321 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001324
1325 switch (ret) {
1326 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001327 case 0: /* ignore => an error, cannot be ignored */
1328 goto exit;
1329
1330 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 goto stop;
1332
Christopher Faulet8ef75252017-02-20 22:56:03 +01001333 default:
1334 /* HELLO frame successfully sent, now wait for the
1335 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1337 goto next;
1338 }
1339
1340 next:
1341 return 0;
1342 stop:
1343 return 1;
1344 exit:
1345 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1346 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001347}
1348
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001349static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001350spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001351{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001353 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 char *frame;
1355 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001356
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001357
Christopher Fauletb067b062017-01-04 16:39:11 +01001358 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001360 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001361 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001362
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1365 " - Connection timed out\n",
1366 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1367 __FUNCTION__, appctx);
1368 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369 goto exit;
1370 }
1371
Christopher Faulet8ef75252017-02-20 22:56:03 +01001372 frame = trash.str; trash.len = 0;
1373 ret = spoe_recv_frame(appctx, frame,
1374 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001375 if (ret > 1) {
1376 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1377 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1378 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001379 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001380 trash.len = ret + 4;
1381 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001382 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001383
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 switch (ret) {
1385 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001387 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1388 goto next;
1389
1390 case 1: /* retry later */
1391 goto stop;
1392
1393 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001394 /* HELLO handshake is finished, set the idle timeout and
1395 * add the applet in the list of running applets. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001396 agent->applets_idle++;
1397 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001398 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1399 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001400
1401 /* Update runtinme agent info */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001402 agent->frame_size = MIN(SPOE_APPCTX(appctx)->max_frame_size,
1403 agent->frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001405 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001408 /* Do not forget to remove processed frame from the output buffer */
1409 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001410 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411
1412 SPOE_APPCTX(appctx)->task->expire =
1413 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001414 return 0;
1415 stop:
1416 return 1;
1417 exit:
1418 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1419 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001420}
1421
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001422
Christopher Fauleta1cda022016-12-21 08:58:06 +01001423static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001424spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001425{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001426 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1427 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001428 char *frame, *buf;
1429 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001430
Christopher Faulet8ef75252017-02-20 22:56:03 +01001431 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1432 * length. */
1433 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001434
1435 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1436 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1437 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1438 SPOE_APPCTX(appctx)->max_frame_size);
1439 }
1440 else if (LIST_ISEMPTY(&agent->sending_queue)) {
1441 *skip = 1;
1442 ret = 1;
1443 goto end;
1444 }
1445 else {
1446 ctx = LIST_NEXT(&agent->sending_queue, typeof(ctx), list);
1447 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1448 SPOE_APPCTX(appctx)->max_frame_size);
1449
1450 }
1451
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452 if (ret > 1)
1453 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001454
Christopher Faulet8ef75252017-02-20 22:56:03 +01001455 switch (ret) {
1456 case -1: /* error */
1457 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1458 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001459
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460 case 0: /* ignore */
1461 if (ctx == NULL)
1462 goto abort_frag_frame;
1463
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001464 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001465 LIST_DEL(&ctx->list);
1466 LIST_INIT(&ctx->list);
1467 ctx->state = SPOE_CTX_ST_ERROR;
1468 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1469 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1470 break;
1471
1472 case 1: /* retry */
1473 *skip = 1;
1474 break;
1475
1476 default:
1477 if (ctx == NULL)
1478 goto abort_frag_frame;
1479
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001480 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001481 LIST_DEL(&ctx->list);
1482 LIST_INIT(&ctx->list);
1483 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1484 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1485 goto no_frag_frame_sent;
1486 else {
1487 *skip = 1;
1488 goto frag_frame_sent;
1489 }
1490 }
1491 goto end;
1492
1493 frag_frame_sent:
1494 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1495 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1496 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1497 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1498
1499 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1500 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1501 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1502 goto end;
1503
1504 no_frag_frame_sent:
1505 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1506 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1507 LIST_ADDQ(&agent->waiting_queue, &ctx->list);
1508 }
1509 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1510 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1511 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1512 }
1513 else {
1514 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1515 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1516 }
1517 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1518 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1519 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1520
1521 ctx->frag_ctx.spoe_appctx = NULL;
1522 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1523 goto end;
1524
1525 abort_frag_frame:
1526 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1527 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1528 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1529 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1530 goto end;
1531
1532 end:
1533 return ret;
1534}
1535
1536static int
1537spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1538{
1539 struct spoe_context *ctx = NULL;
1540 char *frame;
1541 int ret;
1542
1543 frame = trash.str; trash.len = 0;
1544 ret = spoe_recv_frame(appctx, frame,
1545 SPOE_APPCTX(appctx)->max_frame_size);
1546 if (ret > 1) {
1547 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1548 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1549 goto end;
1550 }
1551 trash.len = ret + 4;
1552 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1553 }
1554 switch (ret) {
1555 case -1: /* error */
1556 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1557 break;
1558
1559 case 0: /* ignore */
1560 break;
1561
1562 case 1: /* retry */
1563 *skip = 1;
1564 break;
1565
1566 default:
1567 LIST_DEL(&ctx->list);
1568 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001569
1570 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1571 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1572 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1573 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1574 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1575 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1576 }
1577 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1578 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1579
Christopher Faulet8ef75252017-02-20 22:56:03 +01001580 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1581 break;
1582 }
1583
1584 /* Do not forget to remove processed frame from the output buffer */
1585 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001586 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001587 end:
1588 return ret;
1589}
1590
1591static int
1592spoe_handle_processing_appctx(struct appctx *appctx)
1593{
1594 struct stream_interface *si = appctx->owner;
1595 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001596 unsigned int fpa = 0;
1597 int ret, skip_sending = 0, skip_receiving = 0;
1598
1599 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1600 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1601 goto exit;
1602 }
1603
1604 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1605 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1606 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1607 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1608 goto next;
1609 }
1610
1611 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001612 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1613 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1614 " - appctx-state=%s\n",
1615 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1616 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001617 skip_sending, skip_receiving,
1618 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001619
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001620 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001621 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001622 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001623 if (skip_receiving)
1624 goto stop;
1625 goto recv_frame;
1626 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001627
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001628 /* send_frame */
1629 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001630 switch (ret) {
1631 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001632 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001633
Christopher Fauleta1cda022016-12-21 08:58:06 +01001634 case 0: /* ignore */
1635 agent->sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001636 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001637 break;
1638
Christopher Fauleta1cda022016-12-21 08:58:06 +01001639 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001640 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001641
Christopher Fauleta1cda022016-12-21 08:58:06 +01001642 default:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001643 agent->sending_rate++;
1644 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001645 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001646 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001647 if (fpa > agent->max_fpa)
1648 goto stop;
1649
1650 recv_frame:
1651 if (skip_receiving)
1652 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001653 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001654 switch (ret) {
1655 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001656 goto next;
1657
1658 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001659 fpa++;
1660 break;
1661
1662 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001663 break;
1664
1665 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001666 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001667 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001668 }
1669 goto process;
1670
1671 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001672 SPOE_APPCTX(appctx)->task->expire =
1673 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001674 return 0;
1675 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001676 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001677 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001678 agent->applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001680 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
1681 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1682 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001683 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001684 SPOE_APPCTX(appctx)->task->expire =
1685 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686 }
1687 return 1;
1688
1689 exit:
1690 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1691 return 0;
1692}
1693
1694static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001695spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696{
1697 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001698 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001699 char *frame, *buf;
1700 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001701
Christopher Fauleta1cda022016-12-21 08:58:06 +01001702 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1703 goto exit;
1704
1705 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1706 goto exit;
1707
Christopher Faulet8ef75252017-02-20 22:56:03 +01001708 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1709 * length. */
1710 buf = trash.str; frame = buf+4;
1711 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1712 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001714 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715
1716 switch (ret) {
1717 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001718 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001719 goto exit;
1720
1721 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001722 goto stop;
1723
1724 default:
1725 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1726 " - disconnected by HAProxy (%d): %s\n",
1727 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001728 __FUNCTION__, appctx,
1729 SPOE_APPCTX(appctx)->status_code,
1730 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731
1732 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1733 goto next;
1734 }
1735
1736 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001737 SPOE_APPCTX(appctx)->task->expire =
1738 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739 return 0;
1740 stop:
1741 return 1;
1742 exit:
1743 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1744 return 0;
1745}
1746
1747static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001748spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001749{
1750 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001751 char *frame;
1752 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753
Christopher Fauletb067b062017-01-04 16:39:11 +01001754 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001757 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758
Christopher Fauletb067b062017-01-04 16:39:11 +01001759 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001760 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001761 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001762 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 frame = trash.str; trash.len = 0;
1765 ret = spoe_recv_frame(appctx, frame,
1766 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 trash.len = ret + 4;
1769 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 }
1771
1772 switch (ret) {
1773 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1775 " - error on frame (%s)\n",
1776 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001777 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001778 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780 goto exit;
1781
1782 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783 goto next;
1784
1785 case 1: /* retry */
1786 goto stop;
1787
1788 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001792 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001793 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1794 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795 goto exit;
1796 }
1797
1798 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 /* Do not forget to remove processed frame from the output buffer */
1800 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001801 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803 return 0;
1804 stop:
1805 return 1;
1806 exit:
1807 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1808 return 0;
1809}
1810
1811/* I/O Handler processing messages exchanged with the agent */
1812static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 struct stream_interface *si = appctx->owner;
1816 struct spoe_agent *agent;
1817
1818 if (SPOE_APPCTX(appctx) == NULL)
1819 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820
Christopher Faulet8ef75252017-02-20 22:56:03 +01001821 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1822 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001823
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 switchstate:
1825 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1826 " - appctx-state=%s\n",
1827 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1828 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1829
1830 switch (appctx->st0) {
1831 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001832 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 goto out;
1834 goto switchstate;
1835
1836 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 goto out;
1839 goto switchstate;
1840
1841 case SPOE_APPCTX_ST_IDLE:
1842 if (stopping &&
1843 LIST_ISEMPTY(&agent->sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001844 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 SPOE_APPCTX(appctx)->task->expire =
1846 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001848 goto switchstate;
1849 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001850 agent->applets_idle--;
1851 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1852 /* fall through */
1853
1854 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001855 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1856 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001857 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001858 goto out;
1859 goto switchstate;
1860
1861 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001862 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863 goto out;
1864 goto switchstate;
1865
1866 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto out;
1869 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001870
1871 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 appctx->st0 = SPOE_APPCTX_ST_END;
1873 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1874
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001875 si_shutw(si);
1876 si_shutr(si);
1877 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001878 /* fall through */
1879
1880 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001881 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001882 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 out:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001884 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1885 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001886 si_oc(si)->flags |= CF_READ_DONTWAIT;
1887 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001888}
1889
1890struct applet spoe_applet = {
1891 .obj_type = OBJ_TYPE_APPLET,
1892 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001893 .fct = spoe_handle_appctx,
1894 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001895};
1896
1897/* Create a SPOE applet. On success, the created applet is returned, else
1898 * NULL. */
1899static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001900spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001901{
1902 struct appctx *appctx;
1903 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001904 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001905
1906 if ((appctx = appctx_new(&spoe_applet)) == NULL)
1907 goto out_error;
1908
Christopher Faulet42bfa462017-01-04 14:14:19 +01001909 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx);
1910 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001911 goto out_free_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001912 memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001913
Christopher Faulet42bfa462017-01-04 14:14:19 +01001914 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1915 if ((SPOE_APPCTX(appctx)->task = task_new()) == NULL)
1916 goto out_free_spoe_appctx;
1917
1918 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001920 SPOE_APPCTX(appctx)->task->context = appctx;
1921 SPOE_APPCTX(appctx)->agent = conf->agent;
1922 SPOE_APPCTX(appctx)->version = 0;
1923 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1924 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001925 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001926 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1927
1928 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1929 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001931
1932 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1933 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001934
Willy Tarreau5820a362016-12-22 15:59:02 +01001935 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936 if (!sess)
1937 goto out_free_spoe;
1938
Willy Tarreau87787ac2017-08-28 16:22:54 +02001939 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001940 goto out_free_sess;
1941
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942 stream_set_backend(strm, conf->agent->b.be);
1943
1944 /* applet is waiting for data */
1945 si_applet_cant_get(&strm->si[0]);
1946 appctx_wakeup(appctx);
1947
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948 strm->do_log = NULL;
1949 strm->res.flags |= CF_READ_DONTWAIT;
1950
Christopher Faulet42bfa462017-01-04 14:14:19 +01001951 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
1952 LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001953 conf->agent->applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001954
Willy Tarreau87787ac2017-08-28 16:22:54 +02001955 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001956 return appctx;
1957
1958 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959 out_free_sess:
1960 session_free(sess);
1961 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001962 task_free(SPOE_APPCTX(appctx)->task);
1963 out_free_spoe_appctx:
1964 pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965 out_free_appctx:
1966 appctx_free(appctx);
1967 out_error:
1968 return NULL;
1969}
1970
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001972spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001973{
1974 struct spoe_config *conf = FLT_CONF(ctx->filter);
1975 struct spoe_agent *agent = conf->agent;
1976 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001977 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001978 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001979
Christopher Fauleta1cda022016-12-21 08:58:06 +01001980 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001981
Christopher Fauleta1cda022016-12-21 08:58:06 +01001982 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001983 if (agent->applets_act >= min_applets &&
1984 agent->applets_idle &&
1985 agent->sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001986 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987
1988 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001989 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
1991 ctx->strm);
1992
Christopher Fauleta1cda022016-12-21 08:58:06 +01001993 /* Do not try to create a new applet if there is no server up for the
1994 * agent's backend. */
1995 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
1996 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
1997 " - cannot create SPOE appctx: no server up\n",
1998 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1999 __FUNCTION__, ctx->strm);
2000 goto end;
2001 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002
Christopher Fauleta1cda022016-12-21 08:58:06 +01002003 /* Do not try to create a new applet if we have reached the maximum of
2004 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002005 if (agent->cps_max > 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002006 if (!freq_ctr_remain(&agent->conn_per_sec, agent->cps_max, 0)) {
2007 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2008 " - cannot create SPOE appctx: max CPS reached\n",
2009 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2010 __FUNCTION__, ctx->strm);
2011 goto end;
2012 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013 }
2014
Christopher Faulet8ef75252017-02-20 22:56:03 +01002015 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002016 if (appctx == NULL) {
2017 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2018 " - failed to create SPOE appctx\n",
2019 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2020 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002021 send_log(ctx->strm->be, LOG_EMERG,
2022 "SPOE: [%s] failed to create SPOE applet\n",
2023 agent->id);
2024
Christopher Fauleta1cda022016-12-21 08:58:06 +01002025 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002027 if (agent->applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002028 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029
Christopher Fauleta1cda022016-12-21 08:58:06 +01002030 /* Increase the per-process number of cumulated connections */
2031 if (agent->cps_max > 0)
2032 update_freq_ctr(&agent->conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033
Christopher Fauleta1cda022016-12-21 08:58:06 +01002034 end:
2035 /* The only reason to return an error is when there is no applet */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002036 if (LIST_ISEMPTY(&agent->applets)) {
2037 ctx->status_code = SPOE_CTX_ERR_RES;
2038 return -1;
2039 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 /* Add the SPOE context in the sending queue and update all running
2042 * info */
2043 LIST_ADDQ(&agent->sending_queue, &ctx->list);
2044 if (agent->sending_rate)
2045 agent->sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002046
2047 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002048 " - Add stream in sending queue"
2049 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002050 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002051 ctx->strm, agent->applets_act, agent->applets_idle,
2052 agent->sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002053
Christopher Fauleta1cda022016-12-21 08:58:06 +01002054 /* Finally try to wakeup the first IDLE applet found and move it at the
2055 * end of the list. */
Christopher Faulet42bfa462017-01-04 14:14:19 +01002056 list_for_each_entry(spoe_appctx, &agent->applets, list) {
2057 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002059 spoe_wakeup_appctx(appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002060 LIST_DEL(&spoe_appctx->list);
2061 LIST_ADDQ(&agent->applets, &spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 break;
2063 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002064 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066}
2067
2068/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002069 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002070 **************************************************************************/
Christopher Faulet8ef75252017-02-20 22:56:03 +01002071/* Encode SPOE messages for a specific event. Info in <ctx->frag_ctx>, if any,
2072 * are used to handle fragmented content. On success it returns 1. If an error
2073 * occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002075spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002076 struct list *messages, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002077{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002078 struct spoe_config *conf = FLT_CONF(ctx->filter);
2079 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080 struct spoe_message *msg;
2081 struct sample *smp;
2082 struct spoe_arg *arg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002083 char *p, *end;
2084 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002085
Christopher Faulet8ef75252017-02-20 22:56:03 +01002086 p = ctx->buffer->p;
2087 end = p + agent->frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002088
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002089 /* Resume encoding of a SPOE message */
2090 if (ctx->frag_ctx.curmsg != NULL) {
2091 msg = ctx->frag_ctx.curmsg;
2092 goto encode_message;
2093 }
2094
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002095 /* Loop on messages */
2096 list_for_each_entry(msg, messages, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002097 ctx->frag_ctx.curmsg = msg;
2098 ctx->frag_ctx.curarg = NULL;
2099 ctx->frag_ctx.curoff = UINT_MAX;
2100
2101 encode_message:
2102 /* Resume encoding of a SPOE argument */
2103 if (ctx->frag_ctx.curarg != NULL) {
2104 arg = ctx->frag_ctx.curarg;
2105 goto encode_argument;
2106 }
2107
2108 if (ctx->frag_ctx.curoff != UINT_MAX)
2109 goto encode_msg_payload;
2110
Christopher Faulet8ef75252017-02-20 22:56:03 +01002111 /* Check if there is enough space for the message name and the
2112 * number of arguments. It implies <msg->id_len> is encoded on 2
2113 * bytes, at most (< 2288). */
2114 if (p + 2 + msg->id_len + 1 > end)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002115 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002116
Christopher Faulet8ef75252017-02-20 22:56:03 +01002117 /* Encode the message name */
2118 if (spoe_encode_buffer(msg->id, msg->id_len, &p, end) == -1)
2119 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002120
Christopher Faulet8ef75252017-02-20 22:56:03 +01002121 /* Set the number of arguments for this message */
2122 *p++ = msg->nargs;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002123
2124 ctx->frag_ctx.curoff = 0;
2125 encode_msg_payload:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002126
2127 /* Loop on arguments */
2128 list_for_each_entry(arg, &msg->args, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002129 ctx->frag_ctx.curarg = arg;
2130 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002131
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002132 encode_argument:
2133 if (ctx->frag_ctx.curoff != UINT_MAX)
2134 goto encode_arg_value;
2135
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002136 /* Encode the arguement name as a string. It can by NULL */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002137 if (spoe_encode_buffer(arg->name, arg->name_len, &p, end) == -1)
2138 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002139
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002140 ctx->frag_ctx.curoff = 0;
2141 encode_arg_value:
2142
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002143 /* Fetch the arguement value */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002144 smp = sample_process(s->be, s->sess, s,
2145 dir|SMP_OPT_FINAL, arg->expr, NULL);
2146 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, &p, end);
2147 if (ret == -1 || ctx->frag_ctx.curoff)
2148 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002149 }
2150 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002151
2152 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002153 " - encode %s messages - spoe_appctx=%p"
2154 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002155 (int)now.tv_sec, (int)now.tv_usec,
2156 agent->id, __FUNCTION__, s,
2157 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002158 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2159 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002160
Christopher Faulet8ef75252017-02-20 22:56:03 +01002161 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002162 ctx->frag_ctx.curmsg = NULL;
2163 ctx->frag_ctx.curarg = NULL;
2164 ctx->frag_ctx.curoff = 0;
2165 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002166 return 1;
2167
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002168 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002169 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2170 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2171 return -1;
2172 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002173
2174 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002175 " - encode fragmented messages - spoe_appctx=%p"
2176 " - curmsg=%p - curarg=%p - curoff=%u"
2177 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002178 (int)now.tv_sec, (int)now.tv_usec,
2179 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2180 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002181 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002182
Christopher Faulet8ef75252017-02-20 22:56:03 +01002183 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002184 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2185 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2186 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002187}
2188
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002189
2190/***************************************************************************
2191 * Functions that handle SPOE actions
2192 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002193/* Helper function to set a variable */
2194static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002195spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002196 struct sample *smp)
2197{
2198 struct spoe_config *conf = FLT_CONF(ctx->filter);
2199 struct spoe_agent *agent = conf->agent;
2200 char varname[64];
2201
2202 memset(varname, 0, sizeof(varname));
2203 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2204 scope, agent->var_pfx, len, name);
2205 vars_set_by_name_ifexist(varname, len, smp);
2206}
2207
2208/* Helper function to unset a variable */
2209static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002210spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002211 struct sample *smp)
2212{
2213 struct spoe_config *conf = FLT_CONF(ctx->filter);
2214 struct spoe_agent *agent = conf->agent;
2215 char varname[64];
2216
2217 memset(varname, 0, sizeof(varname));
2218 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2219 scope, agent->var_pfx, len, name);
2220 vars_unset_by_name_ifexist(varname, len, smp);
2221}
2222
2223
Christopher Faulet8ef75252017-02-20 22:56:03 +01002224static inline int
2225spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2226 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002227{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002228 char *str, *scope, *p = *buf;
2229 struct sample smp;
2230 uint64_t sz;
2231 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002232
Christopher Faulet8ef75252017-02-20 22:56:03 +01002233 if (p + 2 >= end)
2234 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002235
Christopher Faulet8ef75252017-02-20 22:56:03 +01002236 /* SET-VAR requires 3 arguments */
2237 if (*p++ != 3)
2238 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002239
Christopher Faulet8ef75252017-02-20 22:56:03 +01002240 switch (*p++) {
2241 case SPOE_SCOPE_PROC: scope = "proc"; break;
2242 case SPOE_SCOPE_SESS: scope = "sess"; break;
2243 case SPOE_SCOPE_TXN : scope = "txn"; break;
2244 case SPOE_SCOPE_REQ : scope = "req"; break;
2245 case SPOE_SCOPE_RES : scope = "res"; break;
2246 default: goto skip;
2247 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002248
Christopher Faulet8ef75252017-02-20 22:56:03 +01002249 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2250 goto skip;
2251 memset(&smp, 0, sizeof(smp));
2252 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002253
Christopher Faulet8ef75252017-02-20 22:56:03 +01002254 if (spoe_decode_data(&p, end, &smp) == -1)
2255 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002256
Christopher Faulet8ef75252017-02-20 22:56:03 +01002257 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2258 " - set-var '%s.%s.%.*s'\n",
2259 (int)now.tv_sec, (int)now.tv_usec,
2260 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2261 __FUNCTION__, s, scope,
2262 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2263 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002264
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002266
Christopher Faulet8ef75252017-02-20 22:56:03 +01002267 ret = (p - *buf);
2268 *buf = p;
2269 return ret;
2270 skip:
2271 return 0;
2272}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002273
Christopher Faulet8ef75252017-02-20 22:56:03 +01002274static inline int
2275spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2276 char **buf, char *end, int dir)
2277{
2278 char *str, *scope, *p = *buf;
2279 struct sample smp;
2280 uint64_t sz;
2281 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002282
Christopher Faulet8ef75252017-02-20 22:56:03 +01002283 if (p + 2 >= end)
2284 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002285
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286 /* UNSET-VAR requires 2 arguments */
2287 if (*p++ != 2)
2288 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002289
Christopher Faulet8ef75252017-02-20 22:56:03 +01002290 switch (*p++) {
2291 case SPOE_SCOPE_PROC: scope = "proc"; break;
2292 case SPOE_SCOPE_SESS: scope = "sess"; break;
2293 case SPOE_SCOPE_TXN : scope = "txn"; break;
2294 case SPOE_SCOPE_REQ : scope = "req"; break;
2295 case SPOE_SCOPE_RES : scope = "res"; break;
2296 default: goto skip;
2297 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002298
Christopher Faulet8ef75252017-02-20 22:56:03 +01002299 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2300 goto skip;
2301 memset(&smp, 0, sizeof(smp));
2302 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002303
Christopher Faulet8ef75252017-02-20 22:56:03 +01002304 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2305 " - unset-var '%s.%s.%.*s'\n",
2306 (int)now.tv_sec, (int)now.tv_usec,
2307 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2308 __FUNCTION__, s, scope,
2309 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2310 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002311
Christopher Faulet8ef75252017-02-20 22:56:03 +01002312 spoe_unset_var(ctx, scope, str, sz, &smp);
2313
2314 ret = (p - *buf);
2315 *buf = p;
2316 return ret;
2317 skip:
2318 return 0;
2319}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320
Christopher Faulet8ef75252017-02-20 22:56:03 +01002321/* Process SPOE actions for a specific event. It returns 1 on success. If an
2322 * error occurred, 0 is returned. */
2323static int
2324spoe_process_actions(struct stream *s, struct spoe_context *ctx,
2325 enum spoe_event ev, int dir)
2326{
2327 char *p, *end;
2328 int ret;
2329
2330 p = ctx->buffer->p;
2331 end = p + ctx->buffer->i;
2332
2333 while (p < end) {
2334 enum spoe_action_type type;
2335
2336 type = *p++;
2337 switch (type) {
2338 case SPOE_ACT_T_SET_VAR:
2339 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2340 if (!ret)
2341 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002342 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344 case SPOE_ACT_T_UNSET_VAR:
2345 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2346 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348 break;
2349
2350 default:
2351 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002352 }
2353 }
2354
2355 return 1;
2356 skip:
2357 return 0;
2358}
2359
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002360/***************************************************************************
2361 * Functions that process SPOE events
2362 **************************************************************************/
2363static inline int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002364spoe_start_event_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002365{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002366 /* If a process is already started for this SPOE context, retry
2367 * later. */
2368 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002369 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002370
2371 /* Set the right flag to prevent request and response processing
2372 * in same time. */
2373 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2374 ? SPOE_CTX_FL_REQ_PROCESS
2375 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002376 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002377}
2378
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002379static inline void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002380spoe_stop_event_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002381{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002382 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2383
2384 if (sa) {
2385 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002386 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002387 }
2388
Christopher Fauleta1cda022016-12-21 08:58:06 +01002389 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002390 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002391
Christopher Fauletb067b062017-01-04 16:39:11 +01002392 ctx->status_code = 0;
2393
Christopher Fauleta1cda022016-12-21 08:58:06 +01002394 /* Reset processing timer */
2395 ctx->process_exp = TICK_ETERNITY;
2396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002398
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002399 ctx->frag_ctx.spoe_appctx = NULL;
2400 ctx->frag_ctx.curmsg = NULL;
2401 ctx->frag_ctx.curarg = NULL;
2402 ctx->frag_ctx.curoff = 0;
2403 ctx->frag_ctx.flags = 0;
2404
Christopher Fauleta1cda022016-12-21 08:58:06 +01002405 if (!LIST_ISEMPTY(&ctx->list)) {
2406 LIST_DEL(&ctx->list);
2407 LIST_INIT(&ctx->list);
2408 }
2409}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002410
2411/* Process a SPOE event. First, this functions will process messages attached to
2412 * this event and send them to an agent in a NOTIFY frame. Then, it will wait a
2413 * ACK frame to process corresponding actions. During all the processing, it
2414 * returns 0 and it returns 1 when the processing is finished. If an error
2415 * occurred, -1 is returned. */
2416static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002417spoe_process_event(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418 enum spoe_event ev)
2419{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002420 struct spoe_config *conf = FLT_CONF(ctx->filter);
2421 struct spoe_agent *agent = conf->agent;
2422 int dir, ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002423
2424 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2425 " - ctx-state=%s - event=%s\n",
2426 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletf7a30922016-11-10 15:04:51 +01002427 agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428 spoe_event_str[ev]);
2429
2430 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2431
2432 if (LIST_ISEMPTY(&(ctx->messages[ev])))
2433 goto out;
2434
2435 if (ctx->state == SPOE_CTX_ST_ERROR)
2436 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002437
2438 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2439 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2440 " - failed to process event '%s': timeout\n",
2441 (int)now.tv_sec, (int)now.tv_usec,
2442 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
Christopher Fauletb067b062017-01-04 16:39:11 +01002443 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002444 goto error;
2445 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002446
2447 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002448 if (agent->eps_max > 0) {
2449 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2450 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2451 " - skip event '%s': max EPS reached\n",
2452 (int)now.tv_sec, (int)now.tv_usec,
2453 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
2454 goto skip;
2455 }
2456 }
2457
Christopher Fauletf7a30922016-11-10 15:04:51 +01002458 if (!tick_isset(ctx->process_exp)) {
2459 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2460 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2461 ctx->process_exp);
2462 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01002463 ret = spoe_start_event_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002464 if (!ret)
2465 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002466
Christopher Faulet8ef75252017-02-20 22:56:03 +01002467 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002468 goto error;
2469
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002470 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002471 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002472 }
2473
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002474 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002475 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002476 goto out;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002477 ret = spoe_encode_messages(s, ctx, &(ctx->messages[ev]), dir);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002478 if (ret < 0)
2479 goto error;
2480 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2481 }
2482
2483 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2484 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002485 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002486 ret = 0;
2487 goto out;
2488 }
2489
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002490 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2491 ret = 0;
2492 goto out;
2493 }
2494
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002495 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002496 spoe_process_actions(s, ctx, ev, dir);
2497 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002498 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002499 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002500 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002501 }
2502
2503 out:
2504 return ret;
2505
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002506 error:
Christopher Faulet48026722016-11-16 15:01:12 +01002507 if (agent->eps_max > 0)
2508 update_freq_ctr(&agent->err_per_sec, 1);
2509
Christopher Faulet985532d2016-11-16 15:36:19 +01002510 if (agent->var_on_error) {
2511 struct sample smp;
2512
2513 memset(&smp, 0, sizeof(smp));
2514 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletb067b062017-01-04 16:39:11 +01002515 smp.data.u.sint = ctx->status_code;
Christopher Faulet985532d2016-11-16 15:36:19 +01002516 smp.data.type = SMP_T_BOOL;
2517
Christopher Faulet8ef75252017-02-20 22:56:03 +01002518 spoe_set_var(ctx, "txn", agent->var_on_error,
Christopher Faulet985532d2016-11-16 15:36:19 +01002519 strlen(agent->var_on_error), &smp);
2520 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002521 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2522 " - failed to create process event '%s': code=%u\n",
2523 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2524 __FUNCTION__, ctx->strm, spoe_event_str[ev],
2525 ctx->status_code);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002526 send_log(ctx->strm->be, LOG_WARNING,
2527 "SPOE: [%s] failed to process event '%s': code=%u\n",
2528 agent->id, spoe_event_str[ev], ctx->status_code);
Christopher Faulet985532d2016-11-16 15:36:19 +01002529
Christopher Fauletea62c2a2016-11-14 10:54:21 +01002530 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2531 ? SPOE_CTX_ST_READY
Christopher Fauletb067b062017-01-04 16:39:11 +01002532 : SPOE_CTX_ST_NONE);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002533 ret = 1;
2534 goto end;
2535
2536 skip:
2537 ctx->state = SPOE_CTX_ST_READY;
2538 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002539
Christopher Fauleta1cda022016-12-21 08:58:06 +01002540 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002541 spoe_stop_event_processing(ctx);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002542 return ret;
2543}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002544
2545/***************************************************************************
2546 * Functions that create/destroy SPOE contexts
2547 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002548static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002549spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002550{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002551 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002552 return 1;
2553
Christopher Faulet4596fb72017-01-11 14:05:19 +01002554 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2555 LIST_DEL(&buffer_wait->list);
2556 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002557 }
2558
Christopher Faulet4596fb72017-01-11 14:05:19 +01002559 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002560 return 1;
2561
Christopher Faulet4596fb72017-01-11 14:05:19 +01002562 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563 return 0;
2564}
2565
2566static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002567spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002568{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002569 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2570 LIST_DEL(&buffer_wait->list);
2571 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002572 }
2573
2574 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002575 if (*buf != &buf_empty) {
2576 b_free(buf);
2577 offer_buffers(buffer_wait->target,
2578 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002579 }
2580}
2581
Christopher Faulet4596fb72017-01-11 14:05:19 +01002582static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002583spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002584{
2585 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2586 return 1;
2587}
2588
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002589static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002590spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002591{
2592 struct spoe_config *conf = FLT_CONF(filter);
2593 struct spoe_context *ctx;
2594
2595 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2596 if (ctx == NULL) {
2597 return NULL;
2598 }
2599 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002600 ctx->filter = filter;
2601 ctx->state = SPOE_CTX_ST_NONE;
2602 ctx->status_code = SPOE_CTX_ERR_NONE;
2603 ctx->flags = 0;
2604 ctx->messages = conf->agent->messages;
2605 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002606 LIST_INIT(&ctx->buffer_wait.list);
2607 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002608 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002609 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002610
Christopher Fauletf7a30922016-11-10 15:04:51 +01002611 ctx->stream_id = 0;
2612 ctx->frame_id = 1;
2613 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002614
2615 return ctx;
2616}
2617
2618static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002619spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002620{
2621 if (!ctx)
2622 return;
2623
Christopher Faulet8ef75252017-02-20 22:56:03 +01002624 spoe_stop_event_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002625 pool_free2(pool2_spoe_ctx, ctx);
2626}
2627
2628static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002629spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002630{
2631 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002632 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002633}
2634
2635
2636/***************************************************************************
2637 * Hooks that manage the filter lifecycle (init/check/deinit)
2638 **************************************************************************/
2639/* Signal handler: Do a soft stop, wakeup SPOE applet */
2640static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002641spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002642{
2643 struct proxy *p;
2644
2645 p = proxy;
2646 while (p) {
2647 struct flt_conf *fconf;
2648
2649 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002650 struct spoe_config *conf;
2651 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002652 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002653
Christopher Faulet3b386a32017-02-23 10:17:15 +01002654 if (fconf->id != spoe_filter_id)
2655 continue;
2656
2657 conf = fconf->conf;
2658 agent = conf->agent;
2659
Christopher Faulet42bfa462017-01-04 14:14:19 +01002660 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002661 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002662 }
2663 }
2664 p = p->next;
2665 }
2666}
2667
2668
2669/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2670static int
2671spoe_init(struct proxy *px, struct flt_conf *fconf)
2672{
2673 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002674
2675 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2676 init_new_proxy(&conf->agent_fe);
2677 conf->agent_fe.parent = conf->agent;
2678 conf->agent_fe.last_change = now.tv_sec;
2679 conf->agent_fe.id = conf->agent->id;
2680 conf->agent_fe.cap = PR_CAP_FE;
2681 conf->agent_fe.mode = PR_MODE_TCP;
2682 conf->agent_fe.maxconn = 0;
2683 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2684 conf->agent_fe.conn_retries = CONN_RETRIES;
2685 conf->agent_fe.accept = frontend_accept;
2686 conf->agent_fe.srv = NULL;
2687 conf->agent_fe.timeout.client = TICK_ETERNITY;
2688 conf->agent_fe.default_target = &spoe_applet.obj_type;
2689 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2690
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002691 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002692 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693 sighandler_registered = 1;
2694 }
2695
2696 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002697}
2698
2699/* Free ressources allocated by the SPOE filter. */
2700static void
2701spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2702{
2703 struct spoe_config *conf = fconf->conf;
2704
2705 if (conf) {
2706 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002707
Christopher Faulet8ef75252017-02-20 22:56:03 +01002708 spoe_release_agent(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002709 free(conf);
2710 }
2711 fconf->conf = NULL;
2712}
2713
2714/* Check configuration of a SPOE filter for a specified proxy.
2715 * Return 1 on error, else 0. */
2716static int
2717spoe_check(struct proxy *px, struct flt_conf *fconf)
2718{
2719 struct spoe_config *conf = fconf->conf;
2720 struct proxy *target;
2721
2722 target = proxy_be_by_name(conf->agent->b.name);
2723 if (target == NULL) {
2724 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2725 " declared at %s:%d.\n",
2726 px->id, conf->agent->b.name, conf->agent->id,
2727 conf->agent->conf.file, conf->agent->conf.line);
2728 return 1;
2729 }
2730 if (target->mode != PR_MODE_TCP) {
2731 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2732 " at %s:%d does not support HTTP mode.\n",
2733 px->id, target->id, conf->agent->id,
2734 conf->agent->conf.file, conf->agent->conf.line);
2735 return 1;
2736 }
2737
2738 free(conf->agent->b.name);
2739 conf->agent->b.name = NULL;
2740 conf->agent->b.be = target;
2741 return 0;
2742}
2743
2744/**************************************************************************
2745 * Hooks attached to a stream
2746 *************************************************************************/
2747/* Called when a filter instance is created and attach to a stream. It creates
2748 * the context that will be used to process this stream. */
2749static int
2750spoe_start(struct stream *s, struct filter *filter)
2751{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002752 struct spoe_config *conf = FLT_CONF(filter);
2753 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002754 struct spoe_context *ctx;
2755
2756 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002757 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002758 __FUNCTION__, s);
2759
Christopher Faulet8ef75252017-02-20 22:56:03 +01002760 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002761 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002762 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2763 " - failed to create SPOE context\n",
2764 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002765 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002766 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002767 "SPOE: [%s] failed to create SPOE context\n",
2768 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002769 return 0;
2770 }
2771
2772 ctx->strm = s;
2773 ctx->state = SPOE_CTX_ST_READY;
2774 filter->ctx = ctx;
2775
2776 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_FE]))
2777 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2778
2779 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_REQ_BE]))
2780 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2781
2782 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_TCP_RSP]))
2783 filter->pre_analyzers |= AN_RES_INSPECT;
2784
2785 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_FE]))
2786 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2787
2788 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_REQ_BE]))
2789 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2790
2791 if (!LIST_ISEMPTY(&ctx->messages[SPOE_EV_ON_HTTP_RSP]))
2792 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2793
2794 return 1;
2795}
2796
2797/* Called when a filter instance is detached from a stream. It release the
2798 * attached SPOE context. */
2799static void
2800spoe_stop(struct stream *s, struct filter *filter)
2801{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002802 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2803 (int)now.tv_sec, (int)now.tv_usec,
2804 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2805 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002806 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002807}
2808
Christopher Fauletf7a30922016-11-10 15:04:51 +01002809
2810/*
2811 * Called when the stream is woken up because of expired timer.
2812 */
2813static void
2814spoe_check_timeouts(struct stream *s, struct filter *filter)
2815{
2816 struct spoe_context *ctx = filter->ctx;
2817
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002818 if (tick_is_expired(ctx->process_exp, now_ms)) {
2819 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002820 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002821 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002822}
2823
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002824/* Called when we are ready to filter data on a channel */
2825static int
2826spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2827{
2828 struct spoe_context *ctx = filter->ctx;
2829 int ret = 1;
2830
2831 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2832 " - ctx-flags=0x%08x\n",
2833 (int)now.tv_sec, (int)now.tv_usec,
2834 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2835 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2836
Christopher Fauletb067b062017-01-04 16:39:11 +01002837 if (ctx->state == SPOE_CTX_ST_NONE)
2838 goto out;
2839
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002840 if (!(chn->flags & CF_ISRESP)) {
2841 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2842 chn->analysers |= AN_REQ_INSPECT_FE;
2843 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2844 chn->analysers |= AN_REQ_INSPECT_BE;
2845
2846 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2847 goto out;
2848
2849 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002850 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01002851 if (!ret)
2852 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002853 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
2854 }
2855 else {
2856 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
2857 chn->analysers |= AN_RES_INSPECT;
2858
2859 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
2860 goto out;
2861
Christopher Faulet8ef75252017-02-20 22:56:03 +01002862 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002863 if (!ret) {
2864 channel_dont_read(chn);
2865 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01002866 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002867 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002868 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002869 }
2870
2871 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002872 return ret;
2873}
2874
2875/* Called before a processing happens on a given channel */
2876static int
2877spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
2878 struct channel *chn, unsigned an_bit)
2879{
2880 struct spoe_context *ctx = filter->ctx;
2881 int ret = 1;
2882
2883 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2884 " - ctx-flags=0x%08x - ana=0x%08x\n",
2885 (int)now.tv_sec, (int)now.tv_usec,
2886 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2887 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2888 ctx->flags, an_bit);
2889
Christopher Fauletb067b062017-01-04 16:39:11 +01002890 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002891 goto out;
2892
2893 switch (an_bit) {
2894 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002895 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896 break;
2897 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002898 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002899 break;
2900 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002901 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902 break;
2903 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002904 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002905 break;
2906 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002907 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908 break;
2909 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002910 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002911 break;
2912 }
2913
2914 out:
2915 if (!ret) {
2916 channel_dont_read(chn);
2917 channel_dont_close(chn);
2918 }
2919 return ret;
2920}
2921
2922/* Called when the filtering on the channel ends. */
2923static int
2924spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2925{
2926 struct spoe_context *ctx = filter->ctx;
2927
2928 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2929 " - ctx-flags=0x%08x\n",
2930 (int)now.tv_sec, (int)now.tv_usec,
2931 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2932 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2933
2934 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002935 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002936 }
2937
2938 return 1;
2939}
2940
2941/********************************************************************
2942 * Functions that manage the filter initialization
2943 ********************************************************************/
2944struct flt_ops spoe_ops = {
2945 /* Manage SPOE filter, called for each filter declaration */
2946 .init = spoe_init,
2947 .deinit = spoe_deinit,
2948 .check = spoe_check,
2949
2950 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01002951 .attach = spoe_start,
2952 .detach = spoe_stop,
2953 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002954
2955 /* Handle channels activity */
2956 .channel_start_analyze = spoe_start_analyze,
2957 .channel_pre_analyze = spoe_chn_pre_analyze,
2958 .channel_end_analyze = spoe_end_analyze,
2959};
2960
2961
2962static int
2963cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
2964{
2965 const char *err;
2966 int i, err_code = 0;
2967
2968 if ((cfg_scope == NULL && curengine != NULL) ||
2969 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02002970 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002971 goto out;
2972
2973 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
2974 if (!*args[1]) {
2975 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
2976 file, linenum);
2977 err_code |= ERR_ALERT | ERR_ABORT;
2978 goto out;
2979 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01002980 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
2981 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 goto out;
2983 }
2984
2985 err = invalid_char(args[1]);
2986 if (err) {
2987 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
2988 file, linenum, *err, args[0], args[1]);
2989 err_code |= ERR_ALERT | ERR_ABORT;
2990 goto out;
2991 }
2992
2993 if (curagent != NULL) {
2994 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
2995 file, linenum);
2996 err_code |= ERR_ALERT | ERR_ABORT;
2997 goto out;
2998 }
2999 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
3000 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3001 err_code |= ERR_ALERT | ERR_ABORT;
3002 goto out;
3003 }
3004
3005 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003006
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007 curagent->conf.file = strdup(file);
3008 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003009
3010 curagent->timeout.hello = TICK_ETERNITY;
3011 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003012 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003013
3014 curagent->engine_id = NULL;
3015 curagent->var_pfx = NULL;
3016 curagent->var_on_error = NULL;
Christopher Fauletcecd8522017-02-24 22:11:21 +01003017 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003018 curagent->cps_max = 0;
3019 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003020 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003021 curagent->min_applets = 0;
3022 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023
3024 for (i = 0; i < SPOE_EV_EVENTS; ++i)
3025 LIST_INIT(&curagent->messages[i]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003026
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003027 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003028 curagent->applets_act = 0;
3029 curagent->applets_idle = 0;
3030 curagent->sending_rate = 0;
3031
3032 LIST_INIT(&curagent->applets);
3033 LIST_INIT(&curagent->sending_queue);
3034 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003035 }
3036 else if (!strcmp(args[0], "use-backend")) {
3037 if (!*args[1]) {
3038 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3039 file, linenum, args[0]);
3040 err_code |= ERR_ALERT | ERR_FATAL;
3041 goto out;
3042 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003043 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003044 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 free(curagent->b.name);
3046 curagent->b.name = strdup(args[1]);
3047 }
3048 else if (!strcmp(args[0], "messages")) {
3049 int cur_arg = 1;
3050 while (*args[cur_arg]) {
3051 struct spoe_msg_placeholder *mp = NULL;
3052
3053 list_for_each_entry(mp, &curmps, list) {
3054 if (!strcmp(mp->id, args[cur_arg])) {
3055 Alert("parsing [%s:%d]: spoe-message message '%s' already declared.\n",
3056 file, linenum, args[cur_arg]);
3057 err_code |= ERR_ALERT | ERR_FATAL;
3058 goto out;
3059 }
3060 }
3061
3062 if ((mp = calloc(1, sizeof(*mp))) == NULL) {
3063 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3064 err_code |= ERR_ALERT | ERR_ABORT;
3065 goto out;
3066 }
3067 mp->id = strdup(args[cur_arg]);
3068 LIST_ADDQ(&curmps, &mp->list);
3069 cur_arg++;
3070 }
3071 }
3072 else if (!strcmp(args[0], "timeout")) {
3073 unsigned int *tv = NULL;
3074 const char *res;
3075 unsigned timeout;
3076
3077 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003078 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003079 file, linenum);
3080 err_code |= ERR_ALERT | ERR_FATAL;
3081 goto out;
3082 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003083 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3084 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003085 if (!strcmp(args[1], "hello"))
3086 tv = &curagent->timeout.hello;
3087 else if (!strcmp(args[1], "idle"))
3088 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003089 else if (!strcmp(args[1], "processing"))
3090 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003091 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003092 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003093 file, linenum, args[1]);
3094 err_code |= ERR_ALERT | ERR_FATAL;
3095 goto out;
3096 }
3097 if (!*args[2]) {
3098 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3099 file, linenum, args[1]);
3100 err_code |= ERR_ALERT | ERR_FATAL;
3101 goto out;
3102 }
3103 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3104 if (res) {
3105 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3106 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003107 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003108 goto out;
3109 }
3110 *tv = MS_TO_TICKS(timeout);
3111 }
3112 else if (!strcmp(args[0], "option")) {
3113 if (!*args[1]) {
3114 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3115 file, linenum, args[0]);
3116 err_code |= ERR_ALERT | ERR_FATAL;
3117 goto out;
3118 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003119
Christopher Faulet305c6072017-02-23 16:17:53 +01003120 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003121 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003122 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003123 if (kwm == 1)
3124 curagent->flags &= ~SPOE_FL_PIPELINING;
3125 else
3126 curagent->flags |= SPOE_FL_PIPELINING;
3127 goto out;
3128 }
3129 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003130 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003131 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003132 if (kwm == 1)
3133 curagent->flags &= ~SPOE_FL_ASYNC;
3134 else
3135 curagent->flags |= SPOE_FL_ASYNC;
3136 goto out;
3137 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003138 else if (!strcmp(args[1], "send-frag-payload")) {
3139 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3140 goto out;
3141 if (kwm == 1)
3142 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3143 else
3144 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3145 goto out;
3146 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003147
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003148 /* Following options does not support negation */
3149 if (kwm == 1) {
3150 Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3151 file, linenum, args[1]);
3152 err_code |= ERR_ALERT | ERR_FATAL;
3153 goto out;
3154 }
3155
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003156 if (!strcmp(args[1], "var-prefix")) {
3157 char *tmp;
3158
3159 if (!*args[2]) {
3160 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3161 file, linenum, args[0],
3162 args[1]);
3163 err_code |= ERR_ALERT | ERR_FATAL;
3164 goto out;
3165 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003166 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3167 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003168 tmp = args[2];
3169 while (*tmp) {
3170 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3171 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3172 file, linenum, args[0], args[1]);
3173 err_code |= ERR_ALERT | ERR_FATAL;
3174 goto out;
3175 }
3176 tmp++;
3177 }
3178 curagent->var_pfx = strdup(args[2]);
3179 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003180 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003181 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003182 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003183 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3184 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003185 else if (!strcmp(args[1], "set-on-error")) {
3186 char *tmp;
3187
3188 if (!*args[2]) {
3189 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3190 file, linenum, args[0],
3191 args[1]);
3192 err_code |= ERR_ALERT | ERR_FATAL;
3193 goto out;
3194 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003195 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3196 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003197 tmp = args[2];
3198 while (*tmp) {
3199 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3200 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3201 file, linenum, args[0], args[1]);
3202 err_code |= ERR_ALERT | ERR_FATAL;
3203 goto out;
3204 }
3205 tmp++;
3206 }
3207 curagent->var_on_error = strdup(args[2]);
3208 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003209 else {
3210 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3211 file, linenum, args[1]);
3212 err_code |= ERR_ALERT | ERR_FATAL;
3213 goto out;
3214 }
Christopher Faulet48026722016-11-16 15:01:12 +01003215 }
3216 else if (!strcmp(args[0], "maxconnrate")) {
3217 if (!*args[1]) {
3218 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3219 file, linenum, args[0]);
3220 err_code |= ERR_ALERT | ERR_FATAL;
3221 goto out;
3222 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003223 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003224 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003225 curagent->cps_max = atol(args[1]);
3226 }
3227 else if (!strcmp(args[0], "maxerrrate")) {
3228 if (!*args[1]) {
3229 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3230 file, linenum, args[0]);
3231 err_code |= ERR_ALERT | ERR_FATAL;
3232 goto out;
3233 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003234 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003235 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003236 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003237 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003238 else if (!strcmp(args[0], "max-frame-size")) {
3239 if (!*args[1]) {
3240 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3241 file, linenum, args[0]);
3242 err_code |= ERR_ALERT | ERR_FATAL;
3243 goto out;
3244 }
3245 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3246 goto out;
3247 curagent->max_frame_size = atol(args[1]);
3248 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3249 curagent->max_frame_size > MAX_FRAME_SIZE) {
3250 Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3251 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
3252 err_code |= ERR_ALERT | ERR_FATAL;
3253 goto out;
3254 }
3255 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003256 else if (*args[0]) {
3257 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3258 file, linenum, args[0]);
3259 err_code |= ERR_ALERT | ERR_FATAL;
3260 goto out;
3261 }
3262 out:
3263 return err_code;
3264}
3265
3266static int
3267cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3268{
3269 struct spoe_message *msg;
3270 struct spoe_arg *arg;
3271 const char *err;
3272 char *errmsg = NULL;
3273 int err_code = 0;
3274
3275 if ((cfg_scope == NULL && curengine != NULL) ||
3276 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003277 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003278 goto out;
3279
3280 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3281 if (!*args[1]) {
3282 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3283 file, linenum);
3284 err_code |= ERR_ALERT | ERR_ABORT;
3285 goto out;
3286 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003287 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3288 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 goto out;
3290 }
3291
3292 err = invalid_char(args[1]);
3293 if (err) {
3294 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3295 file, linenum, *err, args[0], args[1]);
3296 err_code |= ERR_ALERT | ERR_ABORT;
3297 goto out;
3298 }
3299
3300 list_for_each_entry(msg, &curmsgs, list) {
3301 if (!strcmp(msg->id, args[1])) {
3302 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3303 " name as another one declared at %s:%d.\n",
3304 file, linenum, args[1], msg->conf.file, msg->conf.line);
3305 err_code |= ERR_ALERT | ERR_FATAL;
3306 goto out;
3307 }
3308 }
3309
3310 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3311 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3312 err_code |= ERR_ALERT | ERR_ABORT;
3313 goto out;
3314 }
3315
3316 curmsg->id = strdup(args[1]);
3317 curmsg->id_len = strlen(curmsg->id);
3318 curmsg->event = SPOE_EV_NONE;
3319 curmsg->conf.file = strdup(file);
3320 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003321 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 LIST_INIT(&curmsg->args);
3323 LIST_ADDQ(&curmsgs, &curmsg->list);
3324 }
3325 else if (!strcmp(args[0], "args")) {
3326 int cur_arg = 1;
3327
3328 curproxy->conf.args.ctx = ARGC_SPOE;
3329 curproxy->conf.args.file = file;
3330 curproxy->conf.args.line = linenum;
3331 while (*args[cur_arg]) {
3332 char *delim = strchr(args[cur_arg], '=');
3333 int idx = 0;
3334
3335 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3336 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3337 err_code |= ERR_ALERT | ERR_ABORT;
3338 goto out;
3339 }
3340
3341 if (!delim) {
3342 arg->name = NULL;
3343 arg->name_len = 0;
3344 delim = args[cur_arg];
3345 }
3346 else {
3347 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3348 arg->name_len = delim - args[cur_arg];
3349 delim++;
3350 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003351 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3352 &idx, file, linenum, &errmsg,
3353 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003354 if (arg->expr == NULL) {
3355 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3356 err_code |= ERR_ALERT | ERR_FATAL;
3357 free(arg->name);
3358 free(arg);
3359 goto out;
3360 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003361 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003362 LIST_ADDQ(&curmsg->args, &arg->list);
3363 cur_arg++;
3364 }
3365 curproxy->conf.args.file = NULL;
3366 curproxy->conf.args.line = 0;
3367 }
3368 else if (!strcmp(args[0], "event")) {
3369 if (!*args[1]) {
3370 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003371 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003372 goto out;
3373 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003374 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3375 goto out;
3376
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003377 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3378 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3379 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3380 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3381
3382 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3383 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3384 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3385 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3386 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3387 curmsg->event = SPOE_EV_ON_TCP_RSP;
3388
3389 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3390 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3391 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3392 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3393 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3394 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3395 else {
3396 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3397 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003398 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003399 goto out;
3400 }
3401 }
3402 else if (!*args[0]) {
3403 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3404 file, linenum, args[0]);
3405 err_code |= ERR_ALERT | ERR_FATAL;
3406 goto out;
3407 }
3408 out:
3409 free(errmsg);
3410 return err_code;
3411}
3412
3413/* Return -1 on error, else 0 */
3414static int
3415parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3416 struct flt_conf *fconf, char **err, void *private)
3417{
3418 struct list backup_sections;
3419 struct spoe_config *conf;
3420 struct spoe_message *msg, *msgback;
3421 struct spoe_msg_placeholder *mp, *mpback;
3422 char *file = NULL, *engine = NULL;
3423 int ret, pos = *cur_arg + 1;
3424
3425 conf = calloc(1, sizeof(*conf));
3426 if (conf == NULL) {
3427 memprintf(err, "%s: out of memory", args[*cur_arg]);
3428 goto error;
3429 }
3430 conf->proxy = px;
3431
3432 while (*args[pos]) {
3433 if (!strcmp(args[pos], "config")) {
3434 if (!*args[pos+1]) {
3435 memprintf(err, "'%s' : '%s' option without value",
3436 args[*cur_arg], args[pos]);
3437 goto error;
3438 }
3439 file = args[pos+1];
3440 pos += 2;
3441 }
3442 else if (!strcmp(args[pos], "engine")) {
3443 if (!*args[pos+1]) {
3444 memprintf(err, "'%s' : '%s' option without value",
3445 args[*cur_arg], args[pos]);
3446 goto error;
3447 }
3448 engine = args[pos+1];
3449 pos += 2;
3450 }
3451 else {
3452 memprintf(err, "unknown keyword '%s'", args[pos]);
3453 goto error;
3454 }
3455 }
3456 if (file == NULL) {
3457 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3458 goto error;
3459 }
3460
3461 /* backup sections and register SPOE sections */
3462 LIST_INIT(&backup_sections);
3463 cfg_backup_sections(&backup_sections);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003464 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3465 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003466
3467 /* Parse SPOE filter configuration file */
3468 curengine = engine;
3469 curproxy = px;
3470 curagent = NULL;
3471 curmsg = NULL;
3472 ret = readcfgfile(file);
3473 curproxy = NULL;
3474
3475 /* unregister SPOE sections and restore previous sections */
3476 cfg_unregister_sections();
3477 cfg_restore_sections(&backup_sections);
3478
3479 if (ret == -1) {
3480 memprintf(err, "Could not open configuration file %s : %s",
3481 file, strerror(errno));
3482 goto error;
3483 }
3484 if (ret & (ERR_ABORT|ERR_FATAL)) {
3485 memprintf(err, "Error(s) found in configuration file %s", file);
3486 goto error;
3487 }
3488
3489 /* Check SPOE agent */
3490 if (curagent == NULL) {
3491 memprintf(err, "No SPOE agent found in file %s", file);
3492 goto error;
3493 }
3494 if (curagent->b.name == NULL) {
3495 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3496 curagent->id, curagent->conf.file, curagent->conf.line);
3497 goto error;
3498 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003499 if (curagent->timeout.hello == TICK_ETERNITY ||
3500 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003501 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003502 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3503 " | While not properly invalid, you will certainly encounter various problems\n"
3504 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003505 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003506 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3507 }
3508 if (curagent->var_pfx == NULL) {
3509 char *tmp = curagent->id;
3510
3511 while (*tmp) {
3512 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3513 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3514 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3515 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3516 goto error;
3517 }
3518 tmp++;
3519 }
3520 curagent->var_pfx = strdup(curagent->id);
3521 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003522 if (curagent->engine_id == NULL)
3523 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003524
3525 if (LIST_ISEMPTY(&curmps)) {
3526 Warning("Proxy '%s': No message used by SPOE agent '%s' declared at %s:%d.\n",
3527 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3528 goto finish;
3529 }
3530
3531 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3532 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003533 struct spoe_arg *arg;
3534 unsigned int where;
3535
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003536 if (!strcmp(msg->id, mp->id)) {
3537 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3538 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3539 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3540 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3541 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3542 }
3543 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3544 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3545 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3546 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3547 px->id, msg->conf.file, msg->conf.line);
3548 goto next;
3549 }
3550 if (msg->event == SPOE_EV_NONE) {
3551 Warning("Proxy '%s': Ignore SPOE message without event at %s:%d.\n",
3552 px->id, msg->conf.file, msg->conf.line);
3553 goto next;
3554 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003555
3556 where = 0;
3557 switch (msg->event) {
3558 case SPOE_EV_ON_CLIENT_SESS:
3559 where |= SMP_VAL_FE_CON_ACC;
3560 break;
3561
3562 case SPOE_EV_ON_TCP_REQ_FE:
3563 where |= SMP_VAL_FE_REQ_CNT;
3564 break;
3565
3566 case SPOE_EV_ON_HTTP_REQ_FE:
3567 where |= SMP_VAL_FE_HRQ_HDR;
3568 break;
3569
3570 case SPOE_EV_ON_TCP_REQ_BE:
3571 if (px->cap & PR_CAP_FE)
3572 where |= SMP_VAL_FE_REQ_CNT;
3573 if (px->cap & PR_CAP_BE)
3574 where |= SMP_VAL_BE_REQ_CNT;
3575 break;
3576
3577 case SPOE_EV_ON_HTTP_REQ_BE:
3578 if (px->cap & PR_CAP_FE)
3579 where |= SMP_VAL_FE_HRQ_HDR;
3580 if (px->cap & PR_CAP_BE)
3581 where |= SMP_VAL_BE_HRQ_HDR;
3582 break;
3583
3584 case SPOE_EV_ON_SERVER_SESS:
3585 where |= SMP_VAL_BE_SRV_CON;
3586 break;
3587
3588 case SPOE_EV_ON_TCP_RSP:
3589 if (px->cap & PR_CAP_FE)
3590 where |= SMP_VAL_FE_RES_CNT;
3591 if (px->cap & PR_CAP_BE)
3592 where |= SMP_VAL_BE_RES_CNT;
3593 break;
3594
3595 case SPOE_EV_ON_HTTP_RSP:
3596 if (px->cap & PR_CAP_FE)
3597 where |= SMP_VAL_FE_HRS_HDR;
3598 if (px->cap & PR_CAP_BE)
3599 where |= SMP_VAL_BE_HRS_HDR;
3600 break;
3601
3602 default:
3603 break;
3604 }
3605
3606 list_for_each_entry(arg, &msg->args, list) {
3607 if (!(arg->expr->fetch->val & where)) {
3608 Warning("Proxy '%s': Ignore SPOE message at %s:%d: "
3609 "some args extract information from '%s', "
3610 "none of which is available here ('%s').\n",
3611 px->id, msg->conf.file, msg->conf.line,
3612 sample_ckp_names(arg->expr->fetch->use),
3613 sample_ckp_names(where));
3614 goto next;
3615 }
3616 }
3617
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003618 msg->agent = curagent;
3619 LIST_DEL(&msg->list);
3620 LIST_ADDQ(&curagent->messages[msg->event], &msg->list);
3621 goto next;
3622 }
3623 }
3624 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
3625 curagent->id, mp->id, curagent->conf.file, curagent->conf.line);
3626 goto error;
3627 next:
3628 continue;
3629 }
3630
3631 finish:
3632 conf->agent = curagent;
3633 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3634 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003635 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003636 }
3637 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3638 Warning("Proxy '%s': Ignore unused SPOE messages '%s' declared at %s:%d.\n",
3639 px->id, msg->id, msg->conf.file, msg->conf.line);
3640 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003641 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003642 }
3643
3644 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01003645 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003646 fconf->ops = &spoe_ops;
3647 fconf->conf = conf;
3648 return 0;
3649
3650 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003651 spoe_release_agent(curagent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003652 list_for_each_entry_safe(mp, mpback, &curmps, list) {
3653 LIST_DEL(&mp->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003654 spoe_release_msg_placeholder(mp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003655 }
3656 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3657 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003658 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003659 }
3660 free(conf);
3661 return -1;
3662}
3663
3664
3665/* Declare the filter parser for "spoe" keyword */
3666static struct flt_kw_list flt_kws = { "SPOE", { }, {
3667 { "spoe", parse_spoe_flt, NULL },
3668 { NULL, NULL, NULL },
3669 }
3670};
3671
3672__attribute__((constructor))
3673static void __spoe_init(void)
3674{
3675 flt_register_keywords(&flt_kws);
3676
3677 LIST_INIT(&curmsgs);
3678 LIST_INIT(&curmps);
3679 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003680 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003681}
3682
3683__attribute__((destructor))
3684static void
3685__spoe_deinit(void)
3686{
3687 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01003688 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003689}