blob: 9543f8fe244becc1160aaa349a128eeea2a40787 [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
Christopher Faulet57583e42017-09-04 15:41:09 +020026#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020027#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020028#include <proto/arg.h>
29#include <proto/backend.h>
30#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010031#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020032#include <proto/frontend.h>
33#include <proto/log.h>
34#include <proto/proto_http.h>
35#include <proto/proxy.h>
36#include <proto/sample.h>
37#include <proto/session.h>
38#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010039#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020040#include <proto/stream.h>
41#include <proto/stream_interface.h>
42#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020043#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020044#include <proto/vars.h>
45
46#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
47#define SPOE_PRINTF(x...) fprintf(x)
48#else
49#define SPOE_PRINTF(x...)
50#endif
51
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010052/* Reserved 4 bytes to the frame size. So a frame and its size can be written
53 * together in a buffer */
54#define MAX_FRAME_SIZE global.tune.bufsize - 4
55
56/* The minimum size for a frame */
57#define MIN_FRAME_SIZE 256
58
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010059/* Reserved for the metadata and the frame type.
60 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010061#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020062
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010063/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010064#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
65
Christopher Faulet3b386a32017-02-23 10:17:15 +010066/* SPOE filter id. Used to identify SPOE filters */
67const char *spoe_filter_id = "SPOE filter";
68
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020069/* Set if the handle on SIGUSR1 is registered */
70static int sighandler_registered = 0;
71
72/* proxy used during the parsing */
73struct proxy *curproxy = NULL;
74
75/* The name of the SPOE engine, used during the parsing */
76char *curengine = NULL;
77
78/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020079/* SPOE agent/group/message used during the parsing */
80struct spoe_agent *curagent = NULL;
81struct spoe_group *curgrp = NULL;
82struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020083
84/* list of SPOE messages and placeholders used during the parsing */
85struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020086struct list curgrps;
87struct list curmphs;
88struct list curgphs;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020089
Christopher Faulet42bfa462017-01-04 14:14:19 +010090/* Pools used to allocate SPOE structs */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020091static struct pool_head *pool2_spoe_ctx = NULL;
Christopher Faulet42bfa462017-01-04 14:14:19 +010092static struct pool_head *pool2_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020093
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020094struct flt_ops spoe_ops;
95
Christopher Faulet8ef75252017-02-20 22:56:03 +010096static int spoe_queue_context(struct spoe_context *ctx);
97static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
98static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020099
100/********************************************************************
101 * helper functions/globals
102 ********************************************************************/
103static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200104spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105{
Christopher Faulet11610f32017-09-21 10:23:10 +0200106 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200108 free(ph->id);
109 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110}
111
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100113spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114{
Christopher Faulet57583e42017-09-04 15:41:09 +0200115 struct spoe_arg *arg, *argback;
116 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200117
118 if (!msg)
119 return;
120 free(msg->id);
121 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200122 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123 release_sample_expr(arg->expr);
124 free(arg->name);
125 LIST_DEL(&arg->list);
126 free(arg);
127 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200128 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
129 LIST_DEL(&acl->list);
130 prune_acl(acl);
131 free(acl);
132 }
133 if (msg->cond) {
134 prune_acl_cond(msg->cond);
135 free(msg->cond);
136 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200137 free(msg);
138}
139
140static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200141spoe_release_group(struct spoe_group *grp)
142{
143 if (!grp)
144 return;
145 free(grp->id);
146 free(grp->conf.file);
147 free(grp);
148}
149
150static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100151spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200152{
Christopher Faulet11610f32017-09-21 10:23:10 +0200153 struct spoe_message *msg, *msgback;
154 struct spoe_group *grp, *grpback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200155
156 if (!agent)
157 return;
158 free(agent->id);
159 free(agent->conf.file);
160 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100161 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100162 free(agent->var_on_error);
Christopher Faulet11610f32017-09-21 10:23:10 +0200163 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
164 LIST_DEL(&msg->list);
165 spoe_release_message(msg);
166 }
167 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
168 LIST_DEL(&grp->list);
169 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200170 }
171 free(agent);
172}
173
174static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100175 [SPOE_FRM_ERR_NONE] = "normal",
176 [SPOE_FRM_ERR_IO] = "I/O error",
177 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
178 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
179 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
180 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
181 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
182 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
183 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
184 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
185 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
186 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100187 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100188 [SPOE_FRM_ERR_RES] = "resource allocation error",
189 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200190};
191
192static const char *spoe_event_str[SPOE_EV_EVENTS] = {
193 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
194 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
195 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
196 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
197 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
198
199 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
200 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
201 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
202};
203
204
205#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
206
207static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100208 [SPOE_CTX_ST_NONE] = "NONE",
209 [SPOE_CTX_ST_READY] = "READY",
210 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
211 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
212 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
213 [SPOE_CTX_ST_DONE] = "DONE",
214 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200215};
216
217static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100218 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
219 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
220 [SPOE_APPCTX_ST_IDLE] = "IDLE",
221 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
222 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
223 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
224 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
225 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
226 [SPOE_APPCTX_ST_EXIT] = "EXIT",
227 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200228};
229
230#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100231
Christopher Faulet8ef75252017-02-20 22:56:03 +0100232/* Used to generates a unique id for an engine. On success, it returns a
233 * allocated string. So it is the caller's reponsibility to release it. If the
234 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100235static char *
236generate_pseudo_uuid()
237{
238 static int init = 0;
239
240 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
241 const char uuid_chr[] = "0123456789ABCDEF-";
242 char *uuid;
243 int i;
244
245 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
246 return NULL;
247
248 if (!init) {
249 srand(now_ms);
250 init = 1;
251 }
252
253 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
254 int r = rand () % 16;
255
256 switch (uuid_fmt[i]) {
257 case 'x' : uuid[i] = uuid_chr[r]; break;
258 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
259 default : uuid[i] = uuid_fmt[i]; break;
260 }
261 }
262 return uuid;
263}
264
Christopher Faulet8ef75252017-02-20 22:56:03 +0100265/* Returns the minimum number of appets alive at a time. This function is used
266 * to know if more applets should be created for an engine. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100267static inline unsigned int
268min_applets_act(struct spoe_agent *agent)
269{
270 unsigned int nbsrv;
271
Christopher Faulet8ef75252017-02-20 22:56:03 +0100272 /* TODO: Add a config parameter to customize this value. Always 0 for
273 * now */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100274 if (agent->min_applets)
275 return agent->min_applets;
276
Christopher Faulet8ef75252017-02-20 22:56:03 +0100277 /* Get the number of active servers for the backend */
278 nbsrv = (agent->b.be->srv_act
279 ? agent->b.be->srv_act
280 : agent->b.be->srv_bck);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100281 return 2*nbsrv;
282}
283
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200284/********************************************************************
285 * Functions that encode/decode SPOE frames
286 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200287/* Helper to get static string length, excluding the terminating null byte */
288#define SLEN(str) (sizeof(str)-1)
289
290/* Predefined key used in HELLO/DISCONNECT frames */
291#define SUPPORTED_VERSIONS_KEY "supported-versions"
292#define VERSION_KEY "version"
293#define MAX_FRAME_SIZE_KEY "max-frame-size"
294#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100295#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100296#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200297#define STATUS_CODE_KEY "status-code"
298#define MSG_KEY "message"
299
300struct spoe_version {
301 char *str;
302 int min;
303 int max;
304};
305
306/* All supported versions */
307static struct spoe_version supported_versions[] = {
308 {"1.0", 1000, 1000},
309 {NULL, 0, 0}
310};
311
312/* Comma-separated list of supported versions */
313#define SUPPORTED_VERSIONS_VAL "1.0"
314
Christopher Faulet8ef75252017-02-20 22:56:03 +0100315/* Convert a string to a SPOE version value. The string must follow the format
316 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
317 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200318static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 const char *p, *end;
322 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200323
Christopher Faulet8ef75252017-02-20 22:56:03 +0100324 p = str;
325 end = str+len;
326 maj = min = 0;
327 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328
Christopher Faulet8ef75252017-02-20 22:56:03 +0100329 /* skip leading spaces */
330 while (p < end && isspace(*p))
331 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 /* parse Major number, until the '.' */
334 while (*p != '.') {
335 if (p >= end || *p < '0' || *p > '9')
336 goto out;
337 maj *= 10;
338 maj += (*p - '0');
339 p++;
340 }
341
342 /* check Major version */
343 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344 goto out;
345
Christopher Faulet8ef75252017-02-20 22:56:03 +0100346 p++; /* skip the '.' */
347 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
348 goto out;
349
350 /* Parse Minor number */
351 while (p < end) {
352 if (*p < '0' || *p > '9')
353 break;
354 min *= 10;
355 min += (*p - '0');
356 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200357 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100358
359 /* check Minor number */
360 if (min > 999)
361 goto out;
362
363 /* skip trailing spaces */
364 while (p < end && isspace(*p))
365 p++;
366 if (p != end)
367 goto out;
368
369 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200370 out:
371 return vsn;
372}
373
Christopher Faulet8ef75252017-02-20 22:56:03 +0100374/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
375 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
376 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200377static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100378spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379{
Christopher Faulet305c6072017-02-23 16:17:53 +0100380 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100381 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100382 char *p, *end;
383 unsigned int flags = SPOE_FRM_FL_FIN;
384 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200385
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 p = frame;
387 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388
Christopher Faulet8ef75252017-02-20 22:56:03 +0100389 /* Set Frame type */
390 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200391
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100392 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100393 memcpy(p, (char *)&flags, 4);
394 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200395
396 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100397 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
399 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
400 * and "capabilities" */
401
402 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 sz = SLEN(SUPPORTED_VERSIONS_KEY);
404 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
405 goto too_big;
406
407 *p++ = SPOE_DATA_T_STR;
408 sz = SLEN(SUPPORTED_VERSIONS_VAL);
409 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
410 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
412 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(MAX_FRAME_SIZE_KEY);
414 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
415 goto too_big;
416
417 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200418 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100419 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200420
421 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100422 sz = SLEN(CAPABILITIES_KEY);
423 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
424 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200425
Christopher Faulet8ef75252017-02-20 22:56:03 +0100426 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100427 chk = get_trash_chunk();
428 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
429 memcpy(chk->str, "pipelining", 10);
430 chk->len += 10;
431 }
432 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
433 if (chk->len) chk->str[chk->len++] = ',';
434 memcpy(chk->str+chk->len, "async", 5);
435 chk->len += 5;
436 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100437 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
438 if (chk->len) chk->str[chk->len++] = ',';
439 memcpy(chk->str+chk->len, "fragmentation", 13);
440 chk->len += 5;
441 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100442 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 goto too_big;
444
445 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100446 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100447 sz = SLEN(ENGINE_ID_KEY);
448 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
449 goto too_big;
450
451 *p++ = SPOE_DATA_T_STR;
452 sz = strlen(agent->engine_id);
453 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
454 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100455 }
456
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 return (p - frame);
458
459 too_big:
460 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
461 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462}
463
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
465 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
466 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100468spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200469{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100470 const char *reason;
471 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100472 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 p = frame;
476 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 /* Set Frame type */
479 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100481 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100482 memcpy(p, (char *)&flags, 4);
483 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484
485 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 *p++ = 0; *p++ = 0;
487
488 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
489 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
491 /* There are 2 mandatory items: "status-code" and "message" */
492
493 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 sz = SLEN(STATUS_CODE_KEY);
495 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
496 goto too_big;
497
498 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200499 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100500 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200501
502 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100503 sz = SLEN(MSG_KEY);
504 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
505 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506
Christopher Faulet8ef75252017-02-20 22:56:03 +0100507 /*Get the message corresponding to the status code */
508 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
509
510 *p++ = SPOE_DATA_T_STR;
511 sz = strlen(reason);
512 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
513 goto too_big;
514
515 return (p - frame);
516
517 too_big:
518 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
519 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520}
521
Christopher Faulet8ef75252017-02-20 22:56:03 +0100522/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
523 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
524 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100526spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100527 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200528{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100529 char *p, *end;
530 unsigned int stream_id, frame_id;
531 unsigned int flags = SPOE_FRM_FL_FIN;
532 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200533
Christopher Faulet8ef75252017-02-20 22:56:03 +0100534 p = frame;
535 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200536
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100537 stream_id = ctx->stream_id;
538 frame_id = ctx->frame_id;
539
540 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
541 /* The fragmentation is not supported by the applet */
542 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
543 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
544 return -1;
545 }
546 flags = ctx->frag_ctx.flags;
547 }
548
549 /* Set Frame type */
550 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
551
552 /* Set flags */
553 memcpy(p, (char *)&flags, 4);
554 p += 4;
555
556 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200557 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100558 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200559 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100560 goto too_big;
561
562 /* Copy encoded messages, if possible */
563 sz = ctx->buffer->i;
564 if (p + sz >= end)
565 goto too_big;
566 memcpy(p, ctx->buffer->p, sz);
567 p += sz;
568
569 return (p - frame);
570
571 too_big:
572 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
573 return 0;
574}
575
576/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
577 * returns the number of encoded bytes in the frame on success, 0 if an encoding
578 * error occurred and -1 if a fatal error occurred. */
579static int
580spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
581 char *frame, size_t size)
582{
583 char *p, *end;
584 unsigned int stream_id, frame_id;
585 unsigned int flags;
586 size_t sz;
587
588 p = frame;
589 end = frame+size;
590
Christopher Faulet8ef75252017-02-20 22:56:03 +0100591 /* <ctx> is null when the stream has aborted the processing of a
592 * fragmented frame. In this case, we must notify the corresponding
593 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100594 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100595 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
597 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
598 }
599 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100600 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100601 stream_id = ctx->stream_id;
602 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100603 }
604
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100606 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100609 memcpy(p, (char *)&flags, 4);
610 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200611
612 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200613 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200615 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200617
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100618 if (ctx == NULL)
619 goto end;
620
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100622 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 if (p + sz >= end)
624 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100625 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100626 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100627
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100628 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 return (p - frame);
630
631 too_big:
632 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
633 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200634}
635
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636/* Decode and process the HELLO frame sent by an agent. It returns the number of
637 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
638 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200641{
Christopher Faulet305c6072017-02-23 16:17:53 +0100642 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
643 char *p, *end;
644 int vsn, max_frame_size;
645 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646
647 p = frame;
648 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649
650 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
652 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 if (size < 7 /* TYPE + METADATA */) {
657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
658 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659 }
660
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100661 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 memcpy((char *)&flags, p, 4);
663 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100665 /* Fragmentation is not supported for HELLO frame */
666 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100668 return -1;
669 }
670
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 if (*p != 0 || *(p+1) != 0) {
673 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
674 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200675 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677
678 /* There are 3 mandatory items: "version", "max-frame-size" and
679 * "capabilities" */
680
681 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100682 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683 while (p < end) {
684 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200685 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687
688 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 ret = spoe_decode_buffer(&p, end, &str, &sz);
690 if (ret == -1 || !sz) {
691 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
692 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695 /* Check "version" K/V item */
696 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 int i, type = *p++;
698
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
701 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
702 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
705 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
706 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 }
708
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 return -1;
713 }
714 for (i = 0; supported_versions[i].str != NULL; ++i) {
715 if (vsn >= supported_versions[i].min &&
716 vsn <= supported_versions[i].max)
717 break;
718 }
719 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 return -1;
722 }
723 }
724 /* Check "max-frame-size" K/V item */
725 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727
728 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
730 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
731 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
732 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
734 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200736 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100737 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
738 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200739 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 if (sz < MIN_FRAME_SIZE ||
741 sz > SPOE_APPCTX(appctx)->max_frame_size) {
742 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200743 return -1;
744 }
745 max_frame_size = sz;
746 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747 /* Check "capabilities" K/V item */
748 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750
751 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
753 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
754 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
757 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
758 return 0;
759 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100760
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 char *delim;
763
764 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
768 str += 10; sz -= 10;
769 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 flags |= SPOE_APPCTX_FL_PIPELINING;
771 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 else if (sz >= 5 && !strncmp(str, "async", 5)) {
773 str += 5; sz -= 5;
774 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775 flags |= SPOE_APPCTX_FL_ASYNC;
776 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
778 str += 13; sz -= 13;
779 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100780 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
781 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100782
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 /* Get the next comma or break */
784 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100785 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 delim++;
787 sz -= (delim - str);
788 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789 }
790 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200791 else {
792 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793 if (spoe_skip_data(&p, end) == -1) {
794 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
795 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797 }
798 }
799
800 /* Final checks */
801 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200803 return -1;
804 }
805 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100806 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200807 return -1;
808 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100809 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
810 flags &= ~SPOE_APPCTX_FL_PIPELINING;
811 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
812 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813
Christopher Faulet42bfa462017-01-04 14:14:19 +0100814 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
815 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
816 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100817
818 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819}
820
821/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
822 * bytes on success, 0 if the frame can be ignored and -1 if an error
823 * occurred. */
824static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100827 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100828 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829
830 p = frame;
831 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832
833 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
835 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100837 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 if (size < 7 /* TYPE + METADATA */) {
840 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
841 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842 }
843
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100844 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100845 memcpy((char *)&flags, p, 4);
846 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 /* Fragmentation is not supported for DISCONNECT frame */
849 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100851 return -1;
852 }
853
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 if (*p != 0 || *(p+1) != 0) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860
861 /* There are 2 mandatory items: "status-code" and "message" */
862
863 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 while (p < end) {
865 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200866 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868
869 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 ret = spoe_decode_buffer(&p, end, &str, &sz);
871 if (ret == -1 || !sz) {
872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
875
876 /* Check "status-code" K/V item */
877 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879
880 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
883 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
884 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200888 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
890 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
894
895 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100896 else if (!memcmp(str, MSG_KEY, sz)) {
897 int type = *p++;
898
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 ret = spoe_decode_buffer(&p, end, &str, &sz);
905 if (ret == -1 || sz > 255) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
910 SPOE_APPCTX(appctx)->reason = str;
911 SPOE_APPCTX(appctx)->rlen = sz;
912#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
914 else {
915 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 if (spoe_skip_data(&p, end) == -1) {
917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
921 }
922
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924}
925
926
Christopher Fauleta1cda022016-12-21 08:58:06 +0100927/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 * success, 0 if the frame can be ignored and -1 if an error occurred. */
929static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938
939 p = frame;
940 end = frame + size;
941 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942
943 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 if (size < 7 /* TYPE + METADATA */) {
950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
951 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 }
953
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 memcpy((char *)&flags, p, 4);
956 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100958 /* Fragmentation is not supported for now */
959 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 return -1;
962 }
963
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200964 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200965 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100967 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200969 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200971 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100973
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100975 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100976 list_for_each_entry((*ctx), &agent->waiting_queue, list) {
977 if ((*ctx)->stream_id == (unsigned int)stream_id &&
978 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100979 goto found;
980 }
981 }
982 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100983 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
984 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100986 goto found;
987 }
988 }
989
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100990 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
991 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
992 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
993
994 /* ABRT bit is set for an unfinished fragmented frame */
995 if (flags & SPOE_FRM_FL_ABRT) {
996 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
997 (*ctx)->frag_ctx.spoe_appctx = NULL;
998 (*ctx)->state = SPOE_CTX_ST_ERROR;
999 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1000 /* Ignore the payload */
1001 goto end;
1002 }
1003 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1004 /* For now, we ignore the ack */
1005 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1006 return 0;
1007 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008
Christopher Fauleta1cda022016-12-21 08:58:06 +01001009 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001010 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1011 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001012 " - stream-id=%u - frame-id=%u\n",
1013 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1014 __FUNCTION__, appctx,
1015 (unsigned int)stream_id, (unsigned int)frame_id);
1016
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001017 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001018 return 0;
1019
1020 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001021 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1022 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001023 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001025 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001026
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001027 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001028 len = (end - p);
1029 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1030 SPOE_APPCTX(appctx)->buffer->i = len;
1031 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001032
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001033 /* Transfer the buffer ownership to the SPOE context */
1034 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1035 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001036
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037 (*ctx)->state = SPOE_CTX_ST_DONE;
1038
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001039 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001041 " - ACK frame received"
1042 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001043 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001044 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1045 (*ctx)->frame_id, flags);
1046 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001047}
1048
Christopher Fauletba7bc162016-11-07 21:07:38 +01001049/* This function is used in cfgparse.c and declared in proto/checks.h. It
1050 * prepare the request to send to agents during a healthcheck. It returns 0 on
1051 * success and -1 if an error occurred. */
1052int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001053spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001054{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 struct appctx appctx;
1056 struct spoe_appctx spoe_appctx;
1057 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1058 size_t sz;
1059 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060
Christopher Faulet42bfa462017-01-04 14:14:19 +01001061 memset(&appctx, 0, sizeof(appctx));
1062 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001063 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001064
1065 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001066 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001067
Christopher Faulet8ef75252017-02-20 22:56:03 +01001068 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1069 end = frame + MAX_FRAME_SIZE;
1070
1071 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1072 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 /* Add "healthcheck" K/V item */
1077 sz = SLEN(HEALTHCHECK_KEY);
1078 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1079 return -1;
1080 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 *len = frame - buf;
1083 sz = htonl(*len - 4);
1084 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001085
Christopher Faulet8ef75252017-02-20 22:56:03 +01001086 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001088 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089 return 0;
1090}
1091
1092/* This function is used in checks.c and declared in proto/checks.h. It decode
1093 * the response received from an agent during a healthcheck. It returns 0 on
1094 * success and -1 if an error occurred. */
1095int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001098 struct appctx appctx;
1099 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100
Christopher Faulet42bfa462017-01-04 14:14:19 +01001101 memset(&appctx, 0, sizeof(appctx));
1102 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001103
Christopher Faulet42bfa462017-01-04 14:14:19 +01001104 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001105 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1108 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109 goto error;
1110 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1112 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001113
1114 return 0;
1115
1116 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1118 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1119 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120 return -1;
1121}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001122
Christopher Fauleta1cda022016-12-21 08:58:06 +01001123/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1124 * the frame can be ignored, 1 to retry later, and the frame legnth on
1125 * success. */
1126static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001128{
1129 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001130 int ret;
1131 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132
1133 if (si_ic(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001134 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001135
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1137 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138 netint = htonl(framesz);
1139 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001140 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141
1142 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001143 if (ret == -1) {
1144 retry:
1145 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001147 }
1148 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149 return -1; /* error */
1150 }
1151 return framesz;
1152}
1153
1154/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1155 * when the frame can be ignored, 1 to retry later and the frame length on
1156 * success. */
1157static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001158spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159{
1160 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001161 int ret;
1162 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001163
1164 if (si_oc(si)->buf == &buf_empty)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001165 goto retry;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166
Willy Tarreau06d80a92017-10-19 14:32:15 +02001167 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168 if (ret > 0) {
1169 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001170 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001172 return -1;
1173 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001174 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 }
1176 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 if (ret == 0) {
1178 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001180 }
1181 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 return -1; /* error */
1183 }
1184 return framesz;
1185}
1186
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001187/********************************************************************
1188 * Functions that manage the SPOE applet
1189 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001190static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001191spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001192{
1193 si_applet_want_get(appctx->owner);
1194 si_applet_want_put(appctx->owner);
1195 appctx_wakeup(appctx);
1196 return 1;
1197}
1198
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001199/* Callback function that catches applet timeouts. If a timeout occurred, we set
1200 * <appctx->st1> flag and the SPOE applet is woken up. */
1201static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001202spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001203{
1204 struct appctx *appctx = task->context;
1205
1206 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1207 if (tick_is_expired(task->expire, now_ms)) {
1208 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001209 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001210 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001211 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001212 return task;
1213}
1214
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001215/* Callback function that releases a SPOE applet. This happens when the
1216 * connection with the agent is closed. */
1217static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001218spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001219{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001220 struct stream_interface *si = appctx->owner;
1221 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1222 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001223 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224
1225 if (spoe_appctx == NULL)
1226 return;
1227
1228 appctx->ctx.spoe.ptr = NULL;
1229 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001230
1231 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1232 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1233 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001234
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 /* Remove applet from the list of running applets */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236 agent->applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1238 LIST_DEL(&spoe_appctx->list);
1239 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001241
Christopher Faulet8ef75252017-02-20 22:56:03 +01001242 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001244 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
1245 agent->applets_idle--;
1246
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001247 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001248 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1249 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001250
1251 si_shutw(si);
1252 si_shutr(si);
1253 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001254 }
1255
Christopher Faulet8ef75252017-02-20 22:56:03 +01001256 /* Destroy the task attached to this applet */
1257 if (spoe_appctx->task) {
1258 task_delete(spoe_appctx->task);
1259 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260 }
1261
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 /* Notify all waiting streams */
1263 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001264 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 Fauletf7e4e7e2016-10-27 22:29:49 +02001268 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001269 }
1270
Christopher Faulet8ef75252017-02-20 22:56:03 +01001271 /* If the applet was processing a fragmented frame, notify the
1272 * corresponding stream. */
1273 if (spoe_appctx->frag_ctx.ctx) {
1274 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001275 ctx->frag_ctx.spoe_appctx = NULL;
1276 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001278 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1279 }
1280
Christopher Faulet8ef75252017-02-20 22:56:03 +01001281 /* Release allocated memory */
1282 spoe_release_buffer(&spoe_appctx->buffer,
1283 &spoe_appctx->buffer_wait);
1284 pool_free2(pool2_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001285
Christopher Fauleta1cda022016-12-21 08:58:06 +01001286 if (!LIST_ISEMPTY(&agent->applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001287 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 /* If this was the last running applet, notify all waiting streams */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001290 list_for_each_entry_safe(ctx, back, &agent->sending_queue, list) {
1291 LIST_DEL(&ctx->list);
1292 LIST_INIT(&ctx->list);
1293 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001294 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001295 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001296 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001297 list_for_each_entry_safe(ctx, back, &agent->waiting_queue, list) {
1298 LIST_DEL(&ctx->list);
1299 LIST_INIT(&ctx->list);
1300 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001302 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1303 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001304
1305 end:
1306 /* Update runtinme agent info */
1307 agent->frame_size = agent->max_frame_size;
1308 list_for_each_entry(spoe_appctx, &agent->applets, list)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001309 agent->frame_size = MIN(spoe_appctx->max_frame_size,
1310 agent->frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311}
1312
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001313static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001314spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001315{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001317 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001318 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001320
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321 if (si->state <= SI_ST_CON) {
1322 si_applet_want_put(si);
1323 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1324 goto stop;
1325 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001326 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001327 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001328 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001329 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001330
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001332 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1333 " - Connection timed out\n",
1334 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1335 __FUNCTION__, appctx);
1336 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001337 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001338 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339
Christopher Faulet42bfa462017-01-04 14:14:19 +01001340 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001341 SPOE_APPCTX(appctx)->task->expire =
1342 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343
Christopher Faulet8ef75252017-02-20 22:56:03 +01001344 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1345 * length. */
1346 buf = trash.str; frame = buf+4;
1347 ret = spoe_prepare_hahello_frame(appctx, frame,
1348 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001349 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001350 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001351
1352 switch (ret) {
1353 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 case 0: /* ignore => an error, cannot be ignored */
1355 goto exit;
1356
1357 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 goto stop;
1359
Christopher Faulet8ef75252017-02-20 22:56:03 +01001360 default:
1361 /* HELLO frame successfully sent, now wait for the
1362 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1364 goto next;
1365 }
1366
1367 next:
1368 return 0;
1369 stop:
1370 return 1;
1371 exit:
1372 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1373 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374}
1375
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001376static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001379 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001380 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381 char *frame;
1382 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001383
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001384
Christopher Fauletb067b062017-01-04 16:39:11 +01001385 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001387 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001388 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389
Christopher Fauleta1cda022016-12-21 08:58:06 +01001390 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1392 " - Connection timed out\n",
1393 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1394 __FUNCTION__, appctx);
1395 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001396 goto exit;
1397 }
1398
Christopher Faulet8ef75252017-02-20 22:56:03 +01001399 frame = trash.str; trash.len = 0;
1400 ret = spoe_recv_frame(appctx, frame,
1401 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 if (ret > 1) {
1403 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1404 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1405 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001407 trash.len = ret + 4;
1408 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001409 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 switch (ret) {
1412 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001413 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001414 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1415 goto next;
1416
1417 case 1: /* retry later */
1418 goto stop;
1419
1420 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001421 /* HELLO handshake is finished, set the idle timeout and
1422 * add the applet in the list of running applets. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001423 agent->applets_idle++;
1424 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001425 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1426 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001427
1428 /* Update runtinme agent info */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001429 agent->frame_size = MIN(SPOE_APPCTX(appctx)->max_frame_size,
1430 agent->frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001432 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001433
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 /* Do not forget to remove processed frame from the output buffer */
1436 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001437 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438
1439 SPOE_APPCTX(appctx)->task->expire =
1440 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001441 return 0;
1442 stop:
1443 return 1;
1444 exit:
1445 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1446 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001447}
1448
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001449
Christopher Fauleta1cda022016-12-21 08:58:06 +01001450static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001451spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001452{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001453 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1454 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001455 char *frame, *buf;
1456 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457
Christopher Faulet8ef75252017-02-20 22:56:03 +01001458 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1459 * length. */
1460 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001461
1462 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1463 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1464 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1465 SPOE_APPCTX(appctx)->max_frame_size);
1466 }
1467 else if (LIST_ISEMPTY(&agent->sending_queue)) {
1468 *skip = 1;
1469 ret = 1;
1470 goto end;
1471 }
1472 else {
1473 ctx = LIST_NEXT(&agent->sending_queue, typeof(ctx), list);
1474 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1475 SPOE_APPCTX(appctx)->max_frame_size);
1476
1477 }
1478
Christopher Faulet8ef75252017-02-20 22:56:03 +01001479 if (ret > 1)
1480 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001481
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 switch (ret) {
1483 case -1: /* error */
1484 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1485 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001486
Christopher Faulet8ef75252017-02-20 22:56:03 +01001487 case 0: /* ignore */
1488 if (ctx == NULL)
1489 goto abort_frag_frame;
1490
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001491 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492 LIST_DEL(&ctx->list);
1493 LIST_INIT(&ctx->list);
1494 ctx->state = SPOE_CTX_ST_ERROR;
1495 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1496 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1497 break;
1498
1499 case 1: /* retry */
1500 *skip = 1;
1501 break;
1502
1503 default:
1504 if (ctx == NULL)
1505 goto abort_frag_frame;
1506
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001507 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 LIST_DEL(&ctx->list);
1509 LIST_INIT(&ctx->list);
1510 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1511 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1512 goto no_frag_frame_sent;
1513 else {
1514 *skip = 1;
1515 goto frag_frame_sent;
1516 }
1517 }
1518 goto end;
1519
1520 frag_frame_sent:
1521 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1522 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1523 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1524 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1525
1526 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1527 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1528 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1529 goto end;
1530
1531 no_frag_frame_sent:
1532 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1533 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1534 LIST_ADDQ(&agent->waiting_queue, &ctx->list);
1535 }
1536 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1537 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1538 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1539 }
1540 else {
1541 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1542 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1543 }
1544 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1545 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1546 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1547
1548 ctx->frag_ctx.spoe_appctx = NULL;
1549 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1550 goto end;
1551
1552 abort_frag_frame:
1553 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1554 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1555 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1556 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1557 goto end;
1558
1559 end:
1560 return ret;
1561}
1562
1563static int
1564spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1565{
1566 struct spoe_context *ctx = NULL;
1567 char *frame;
1568 int ret;
1569
1570 frame = trash.str; trash.len = 0;
1571 ret = spoe_recv_frame(appctx, frame,
1572 SPOE_APPCTX(appctx)->max_frame_size);
1573 if (ret > 1) {
1574 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1575 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1576 goto end;
1577 }
1578 trash.len = ret + 4;
1579 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1580 }
1581 switch (ret) {
1582 case -1: /* error */
1583 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1584 break;
1585
1586 case 0: /* ignore */
1587 break;
1588
1589 case 1: /* retry */
1590 *skip = 1;
1591 break;
1592
1593 default:
1594 LIST_DEL(&ctx->list);
1595 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001596
1597 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1598 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1599 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1600 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1601 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1602 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1603 }
1604 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1605 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1606
Christopher Faulet8ef75252017-02-20 22:56:03 +01001607 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1608 break;
1609 }
1610
1611 /* Do not forget to remove processed frame from the output buffer */
1612 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001613 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001614 end:
1615 return ret;
1616}
1617
1618static int
1619spoe_handle_processing_appctx(struct appctx *appctx)
1620{
1621 struct stream_interface *si = appctx->owner;
1622 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001623 unsigned int fpa = 0;
1624 int ret, skip_sending = 0, skip_receiving = 0;
1625
1626 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1627 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1628 goto exit;
1629 }
1630
1631 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1632 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1633 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1634 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1635 goto next;
1636 }
1637
1638 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001639 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1640 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1641 " - appctx-state=%s\n",
1642 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1643 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001644 skip_sending, skip_receiving,
1645 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001646
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001647 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001648 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001649 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001650 if (skip_receiving)
1651 goto stop;
1652 goto recv_frame;
1653 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001654
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001655 /* send_frame */
1656 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001657 switch (ret) {
1658 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001659 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001660
Christopher Fauleta1cda022016-12-21 08:58:06 +01001661 case 0: /* ignore */
1662 agent->sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001663 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001664 break;
1665
Christopher Fauleta1cda022016-12-21 08:58:06 +01001666 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001667 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001668
Christopher Fauleta1cda022016-12-21 08:58:06 +01001669 default:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001670 agent->sending_rate++;
1671 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001672 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001673 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001674 if (fpa > agent->max_fpa)
1675 goto stop;
1676
1677 recv_frame:
1678 if (skip_receiving)
1679 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001680 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001681 switch (ret) {
1682 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001683 goto next;
1684
1685 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686 fpa++;
1687 break;
1688
1689 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001690 break;
1691
1692 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001693 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001694 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695 }
1696 goto process;
1697
1698 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001699 SPOE_APPCTX(appctx)->task->expire =
1700 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001701 return 0;
1702 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001703 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001705 agent->applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706 }
Christopher Faulet42bfa462017-01-04 14:14:19 +01001707 if (fpa || (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PERSIST)) {
1708 LIST_DEL(&SPOE_APPCTX(appctx)->list);
1709 LIST_ADD(&agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001710 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001711 SPOE_APPCTX(appctx)->task->expire =
1712 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 }
1714 return 1;
1715
1716 exit:
1717 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1718 return 0;
1719}
1720
1721static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001722spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001723{
1724 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001725 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001726 char *frame, *buf;
1727 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001728
Christopher Fauleta1cda022016-12-21 08:58:06 +01001729 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1730 goto exit;
1731
1732 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1733 goto exit;
1734
Christopher Faulet8ef75252017-02-20 22:56:03 +01001735 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1736 * length. */
1737 buf = trash.str; frame = buf+4;
1738 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1739 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001740 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001741 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001742
1743 switch (ret) {
1744 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746 goto exit;
1747
1748 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001749 goto stop;
1750
1751 default:
1752 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1753 " - disconnected by HAProxy (%d): %s\n",
1754 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001755 __FUNCTION__, appctx,
1756 SPOE_APPCTX(appctx)->status_code,
1757 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758
1759 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1760 goto next;
1761 }
1762
1763 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 SPOE_APPCTX(appctx)->task->expire =
1765 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766 return 0;
1767 stop:
1768 return 1;
1769 exit:
1770 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1771 return 0;
1772}
1773
1774static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001775spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001776{
1777 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001778 char *frame;
1779 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780
Christopher Fauletb067b062017-01-04 16:39:11 +01001781 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001784 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785
Christopher Fauletb067b062017-01-04 16:39:11 +01001786 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001787 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001789 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790
Christopher Faulet8ef75252017-02-20 22:56:03 +01001791 frame = trash.str; trash.len = 0;
1792 ret = spoe_recv_frame(appctx, frame,
1793 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001794 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 trash.len = ret + 4;
1796 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797 }
1798
1799 switch (ret) {
1800 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001801 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1802 " - error on frame (%s)\n",
1803 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001804 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001806 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 goto exit;
1808
1809 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001810 goto next;
1811
1812 case 1: /* retry */
1813 goto stop;
1814
1815 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001817 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001819 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001820 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1821 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001822 goto exit;
1823 }
1824
1825 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001826 /* Do not forget to remove processed frame from the output buffer */
1827 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001828 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001829
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 return 0;
1831 stop:
1832 return 1;
1833 exit:
1834 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1835 return 0;
1836}
1837
1838/* I/O Handler processing messages exchanged with the agent */
1839static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001840spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001842 struct stream_interface *si = appctx->owner;
1843 struct spoe_agent *agent;
1844
1845 if (SPOE_APPCTX(appctx) == NULL)
1846 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1849 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001850
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851 switchstate:
1852 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1853 " - appctx-state=%s\n",
1854 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1855 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1856
1857 switch (appctx->st0) {
1858 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001859 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001860 goto out;
1861 goto switchstate;
1862
1863 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865 goto out;
1866 goto switchstate;
1867
1868 case SPOE_APPCTX_ST_IDLE:
1869 if (stopping &&
1870 LIST_ISEMPTY(&agent->sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001871 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 SPOE_APPCTX(appctx)->task->expire =
1873 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001875 goto switchstate;
1876 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001877 agent->applets_idle--;
1878 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1879 /* fall through */
1880
1881 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001882 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1883 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001884 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001885 goto out;
1886 goto switchstate;
1887
1888 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001889 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 goto out;
1891 goto switchstate;
1892
1893 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto out;
1896 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001897
1898 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001899 appctx->st0 = SPOE_APPCTX_ST_END;
1900 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1901
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001902 si_shutw(si);
1903 si_shutr(si);
1904 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001905 /* fall through */
1906
1907 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001908 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001909 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 out:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001911 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1912 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001913 si_oc(si)->flags |= CF_READ_DONTWAIT;
1914 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001915}
1916
1917struct applet spoe_applet = {
1918 .obj_type = OBJ_TYPE_APPLET,
1919 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001920 .fct = spoe_handle_appctx,
1921 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922};
1923
1924/* Create a SPOE applet. On success, the created applet is returned, else
1925 * NULL. */
1926static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001927spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001928{
1929 struct appctx *appctx;
1930 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001931 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932
1933 if ((appctx = appctx_new(&spoe_applet)) == NULL)
1934 goto out_error;
1935
Christopher Faulet42bfa462017-01-04 14:14:19 +01001936 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool2_spoe_appctx);
1937 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938 goto out_free_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001939 memset(appctx->ctx.spoe.ptr, 0, pool2_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001940
Christopher Faulet42bfa462017-01-04 14:14:19 +01001941 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Emeric Brunc60def82017-09-27 14:59:38 +02001942 if ((SPOE_APPCTX(appctx)->task = task_new(MAX_THREADS_MASK)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001943 goto out_free_spoe_appctx;
1944
1945 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001946 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001947 SPOE_APPCTX(appctx)->task->context = appctx;
1948 SPOE_APPCTX(appctx)->agent = conf->agent;
1949 SPOE_APPCTX(appctx)->version = 0;
1950 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1951 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001952 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001953 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1954
1955 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1956 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001957 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001958
1959 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1960 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961
Willy Tarreau5820a362016-12-22 15:59:02 +01001962 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963 if (!sess)
1964 goto out_free_spoe;
1965
Willy Tarreau87787ac2017-08-28 16:22:54 +02001966 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 goto out_free_sess;
1968
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969 stream_set_backend(strm, conf->agent->b.be);
1970
1971 /* applet is waiting for data */
1972 si_applet_cant_get(&strm->si[0]);
1973 appctx_wakeup(appctx);
1974
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001975 strm->do_log = NULL;
1976 strm->res.flags |= CF_READ_DONTWAIT;
1977
Christopher Faulet42bfa462017-01-04 14:14:19 +01001978 LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001979 conf->agent->applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001980
Emeric Brunc60def82017-09-27 14:59:38 +02001981 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001982 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001983 return appctx;
1984
1985 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001986 out_free_sess:
1987 session_free(sess);
1988 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001989 task_free(SPOE_APPCTX(appctx)->task);
1990 out_free_spoe_appctx:
1991 pool_free2(pool2_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992 out_free_appctx:
1993 appctx_free(appctx);
1994 out_error:
1995 return NULL;
1996}
1997
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001999spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002000{
2001 struct spoe_config *conf = FLT_CONF(ctx->filter);
2002 struct spoe_agent *agent = conf->agent;
2003 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002004 struct spoe_appctx *spoe_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002005 unsigned int min_applets;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006
Christopher Fauleta1cda022016-12-21 08:58:06 +01002007 min_applets = min_applets_act(agent);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002008
Christopher Fauleta1cda022016-12-21 08:58:06 +01002009 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002010 if (agent->applets_act >= min_applets &&
2011 agent->applets_idle &&
2012 agent->sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002013 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002014
2015 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002016 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2018 ctx->strm);
2019
Christopher Fauleta1cda022016-12-21 08:58:06 +01002020 /* Do not try to create a new applet if there is no server up for the
2021 * agent's backend. */
2022 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2023 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2024 " - cannot create SPOE appctx: no server up\n",
2025 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2026 __FUNCTION__, ctx->strm);
2027 goto end;
2028 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029
Christopher Fauleta1cda022016-12-21 08:58:06 +01002030 /* Do not try to create a new applet if we have reached the maximum of
2031 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002032 if (agent->cps_max > 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002033 if (!freq_ctr_remain(&agent->conn_per_sec, agent->cps_max, 0)) {
2034 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2035 " - cannot create SPOE appctx: max CPS reached\n",
2036 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2037 __FUNCTION__, ctx->strm);
2038 goto end;
2039 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040 }
2041
Christopher Faulet8ef75252017-02-20 22:56:03 +01002042 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002043 if (appctx == NULL) {
2044 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2045 " - failed to create SPOE appctx\n",
2046 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2047 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002048 send_log(ctx->strm->be, LOG_EMERG,
2049 "SPOE: [%s] failed to create SPOE applet\n",
2050 agent->id);
2051
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002053 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002054 if (agent->applets_act <= min_applets)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002055 SPOE_APPCTX(appctx)->flags |= SPOE_APPCTX_FL_PERSIST;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002056
Christopher Fauleta1cda022016-12-21 08:58:06 +01002057 /* Increase the per-process number of cumulated connections */
2058 if (agent->cps_max > 0)
2059 update_freq_ctr(&agent->conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002060
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 end:
2062 /* The only reason to return an error is when there is no applet */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002063 if (LIST_ISEMPTY(&agent->applets)) {
2064 ctx->status_code = SPOE_CTX_ERR_RES;
2065 return -1;
2066 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002067
Christopher Fauleta1cda022016-12-21 08:58:06 +01002068 /* Add the SPOE context in the sending queue and update all running
2069 * info */
2070 LIST_ADDQ(&agent->sending_queue, &ctx->list);
2071 if (agent->sending_rate)
2072 agent->sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002073
2074 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002075 " - Add stream in sending queue"
2076 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002077 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002078 ctx->strm, agent->applets_act, agent->applets_idle,
2079 agent->sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002080
Christopher Fauleta1cda022016-12-21 08:58:06 +01002081 /* Finally try to wakeup the first IDLE applet found and move it at the
2082 * end of the list. */
Christopher Faulet42bfa462017-01-04 14:14:19 +01002083 list_for_each_entry(spoe_appctx, &agent->applets, list) {
2084 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002085 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002086 spoe_wakeup_appctx(appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002087 LIST_DEL(&spoe_appctx->list);
2088 LIST_ADDQ(&agent->applets, &spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002089 break;
2090 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002091 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002092 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002093}
2094
2095/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002096 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002097 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002098/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2099 * fragmented_content. If the next message can be processed, it returns 0. If
2100 * the message is too big, it returns -1.*/
2101static int
2102spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2103 struct spoe_message *msg, int dir,
2104 char **buf, char *end)
2105{
2106 struct sample *smp;
2107 struct spoe_arg *arg;
2108 int ret;
2109
2110 if (msg->cond) {
2111 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2112 ret = acl_pass(ret);
2113 if (msg->cond->pol == ACL_COND_UNLESS)
2114 ret = !ret;
2115
2116 /* the rule does not match */
2117 if (!ret)
2118 goto next;
2119 }
2120
2121 /* Resume encoding of a SPOE argument */
2122 if (ctx->frag_ctx.curarg != NULL) {
2123 arg = ctx->frag_ctx.curarg;
2124 goto encode_argument;
2125 }
2126
2127 if (ctx->frag_ctx.curoff != UINT_MAX)
2128 goto encode_msg_payload;
2129
2130 /* Check if there is enough space for the message name and the
2131 * number of arguments. It implies <msg->id_len> is encoded on 2
2132 * bytes, at most (< 2288). */
2133 if (*buf + 2 + msg->id_len + 1 > end)
2134 goto too_big;
2135
2136 /* Encode the message name */
2137 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2138 goto too_big;
2139
2140 /* Set the number of arguments for this message */
2141 **buf = msg->nargs;
2142 (*buf)++;
2143
2144 ctx->frag_ctx.curoff = 0;
2145 encode_msg_payload:
2146
2147 /* Loop on arguments */
2148 list_for_each_entry(arg, &msg->args, list) {
2149 ctx->frag_ctx.curarg = arg;
2150 ctx->frag_ctx.curoff = UINT_MAX;
2151
2152 encode_argument:
2153 if (ctx->frag_ctx.curoff != UINT_MAX)
2154 goto encode_arg_value;
2155
2156 /* Encode the arguement name as a string. It can by NULL */
2157 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2158 goto too_big;
2159
2160 ctx->frag_ctx.curoff = 0;
2161 encode_arg_value:
2162
2163 /* Fetch the arguement value */
2164 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2165 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2166 if (ret == -1 || ctx->frag_ctx.curoff)
2167 goto too_big;
2168 }
2169
2170 next:
2171 return 0;
2172
2173 too_big:
2174 return -1;
2175}
2176
Christopher Fauletc718b822017-09-21 16:50:56 +02002177/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2178 * handle fragmented content. On success it returns 1. If an error occurred, -1
2179 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2180 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002181static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002182spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002183 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002184{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002185 struct spoe_config *conf = FLT_CONF(ctx->filter);
2186 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002187 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002188 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002189
Christopher Faulet8ef75252017-02-20 22:56:03 +01002190 p = ctx->buffer->p;
2191 end = p + agent->frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002192
Christopher Fauletc718b822017-09-21 16:50:56 +02002193 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2194 /* Resume encoding of a SPOE message */
2195 if (ctx->frag_ctx.curmsg != NULL) {
2196 msg = ctx->frag_ctx.curmsg;
2197 goto encode_evt_message;
2198 }
2199
2200 list_for_each_entry(msg, messages, by_evt) {
2201 ctx->frag_ctx.curmsg = msg;
2202 ctx->frag_ctx.curarg = NULL;
2203 ctx->frag_ctx.curoff = UINT_MAX;
2204
2205 encode_evt_message:
2206 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2207 goto too_big;
2208 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002209 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002210 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2211 /* Resume encoding of a SPOE message */
2212 if (ctx->frag_ctx.curmsg != NULL) {
2213 msg = ctx->frag_ctx.curmsg;
2214 goto encode_grp_message;
2215 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002216
Christopher Fauletc718b822017-09-21 16:50:56 +02002217 list_for_each_entry(msg, messages, by_grp) {
2218 ctx->frag_ctx.curmsg = msg;
2219 ctx->frag_ctx.curarg = NULL;
2220 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002221
Christopher Fauletc718b822017-09-21 16:50:56 +02002222 encode_grp_message:
2223 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2224 goto too_big;
2225 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002226 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002227 else
2228 goto skip;
2229
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002230
Christopher Faulet57583e42017-09-04 15:41:09 +02002231 /* nothing has been encoded for an unfragmented payload */
2232 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2233 goto skip;
2234
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002236 " - encode %s messages - spoe_appctx=%p"
2237 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002238 (int)now.tv_sec, (int)now.tv_usec,
2239 agent->id, __FUNCTION__, s,
2240 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002241 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2242 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002243
Christopher Faulet8ef75252017-02-20 22:56:03 +01002244 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245 ctx->frag_ctx.curmsg = NULL;
2246 ctx->frag_ctx.curarg = NULL;
2247 ctx->frag_ctx.curoff = 0;
2248 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002249
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002250 return 1;
2251
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002253 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2254 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2255 return -1;
2256 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
2258 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002259 " - encode fragmented messages - spoe_appctx=%p"
2260 " - curmsg=%p - curarg=%p - curoff=%u"
2261 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002262 (int)now.tv_sec, (int)now.tv_usec,
2263 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2264 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266
Christopher Faulet8ef75252017-02-20 22:56:03 +01002267 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002268 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2269 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2270 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002271
2272 skip:
2273 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2274 " - skip the frame because nothing has been encoded\n",
2275 (int)now.tv_sec, (int)now.tv_usec,
2276 agent->id, __FUNCTION__, s);
2277 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002278}
2279
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280
2281/***************************************************************************
2282 * Functions that handle SPOE actions
2283 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002284/* Helper function to set a variable */
2285static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002287 struct sample *smp)
2288{
2289 struct spoe_config *conf = FLT_CONF(ctx->filter);
2290 struct spoe_agent *agent = conf->agent;
2291 char varname[64];
2292
2293 memset(varname, 0, sizeof(varname));
2294 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2295 scope, agent->var_pfx, len, name);
2296 vars_set_by_name_ifexist(varname, len, smp);
2297}
2298
2299/* Helper function to unset a variable */
2300static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002301spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002302 struct sample *smp)
2303{
2304 struct spoe_config *conf = FLT_CONF(ctx->filter);
2305 struct spoe_agent *agent = conf->agent;
2306 char varname[64];
2307
2308 memset(varname, 0, sizeof(varname));
2309 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2310 scope, agent->var_pfx, len, name);
2311 vars_unset_by_name_ifexist(varname, len, smp);
2312}
2313
2314
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315static inline int
2316spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2317 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002318{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002319 char *str, *scope, *p = *buf;
2320 struct sample smp;
2321 uint64_t sz;
2322 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324 if (p + 2 >= end)
2325 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326
Christopher Faulet8ef75252017-02-20 22:56:03 +01002327 /* SET-VAR requires 3 arguments */
2328 if (*p++ != 3)
2329 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002330
Christopher Faulet8ef75252017-02-20 22:56:03 +01002331 switch (*p++) {
2332 case SPOE_SCOPE_PROC: scope = "proc"; break;
2333 case SPOE_SCOPE_SESS: scope = "sess"; break;
2334 case SPOE_SCOPE_TXN : scope = "txn"; break;
2335 case SPOE_SCOPE_REQ : scope = "req"; break;
2336 case SPOE_SCOPE_RES : scope = "res"; break;
2337 default: goto skip;
2338 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002339
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2341 goto skip;
2342 memset(&smp, 0, sizeof(smp));
2343 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344
Christopher Faulet8ef75252017-02-20 22:56:03 +01002345 if (spoe_decode_data(&p, end, &smp) == -1)
2346 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2349 " - set-var '%s.%s.%.*s'\n",
2350 (int)now.tv_sec, (int)now.tv_usec,
2351 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2352 __FUNCTION__, s, scope,
2353 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2354 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358 ret = (p - *buf);
2359 *buf = p;
2360 return ret;
2361 skip:
2362 return 0;
2363}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365static inline int
2366spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2367 char **buf, char *end, int dir)
2368{
2369 char *str, *scope, *p = *buf;
2370 struct sample smp;
2371 uint64_t sz;
2372 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374 if (p + 2 >= end)
2375 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002376
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377 /* UNSET-VAR requires 2 arguments */
2378 if (*p++ != 2)
2379 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 switch (*p++) {
2382 case SPOE_SCOPE_PROC: scope = "proc"; break;
2383 case SPOE_SCOPE_SESS: scope = "sess"; break;
2384 case SPOE_SCOPE_TXN : scope = "txn"; break;
2385 case SPOE_SCOPE_REQ : scope = "req"; break;
2386 case SPOE_SCOPE_RES : scope = "res"; break;
2387 default: goto skip;
2388 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2391 goto skip;
2392 memset(&smp, 0, sizeof(smp));
2393 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2396 " - unset-var '%s.%s.%.*s'\n",
2397 (int)now.tv_sec, (int)now.tv_usec,
2398 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2399 __FUNCTION__, s, scope,
2400 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2401 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 spoe_unset_var(ctx, scope, str, sz, &smp);
2404
2405 ret = (p - *buf);
2406 *buf = p;
2407 return ret;
2408 skip:
2409 return 0;
2410}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Faulet8ef75252017-02-20 22:56:03 +01002412/* Process SPOE actions for a specific event. It returns 1 on success. If an
2413 * error occurred, 0 is returned. */
2414static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002415spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002416{
2417 char *p, *end;
2418 int ret;
2419
2420 p = ctx->buffer->p;
2421 end = p + ctx->buffer->i;
2422
2423 while (p < end) {
2424 enum spoe_action_type type;
2425
2426 type = *p++;
2427 switch (type) {
2428 case SPOE_ACT_T_SET_VAR:
2429 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2430 if (!ret)
2431 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002432 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002433
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434 case SPOE_ACT_T_UNSET_VAR:
2435 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2436 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002438 break;
2439
2440 default:
2441 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442 }
2443 }
2444
2445 return 1;
2446 skip:
2447 return 0;
2448}
2449
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002450/***************************************************************************
2451 * Functions that process SPOE events
2452 **************************************************************************/
2453static inline int
Christopher Faulet58d03682017-09-21 16:57:24 +02002454spoe_start_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002455{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002456 /* If a process is already started for this SPOE context, retry
2457 * later. */
2458 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002459 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002460
2461 /* Set the right flag to prevent request and response processing
2462 * in same time. */
2463 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2464 ? SPOE_CTX_FL_REQ_PROCESS
2465 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002466 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002467}
2468
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002469static inline void
Christopher Faulet58d03682017-09-21 16:57:24 +02002470spoe_stop_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002471{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002472 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2473
2474 if (sa) {
2475 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002476 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002477 }
2478
Christopher Fauleta1cda022016-12-21 08:58:06 +01002479 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002480 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002481
Christopher Fauletb067b062017-01-04 16:39:11 +01002482 ctx->status_code = 0;
2483
Christopher Fauleta1cda022016-12-21 08:58:06 +01002484 /* Reset processing timer */
2485 ctx->process_exp = TICK_ETERNITY;
2486
Christopher Faulet8ef75252017-02-20 22:56:03 +01002487 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002488
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002489 ctx->frag_ctx.spoe_appctx = NULL;
2490 ctx->frag_ctx.curmsg = NULL;
2491 ctx->frag_ctx.curarg = NULL;
2492 ctx->frag_ctx.curoff = 0;
2493 ctx->frag_ctx.flags = 0;
2494
Christopher Fauleta1cda022016-12-21 08:58:06 +01002495 if (!LIST_ISEMPTY(&ctx->list)) {
2496 LIST_DEL(&ctx->list);
2497 LIST_INIT(&ctx->list);
2498 }
2499}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002500
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002501static void
2502spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2503 struct spoe_context *ctx, int dir)
2504{
2505 if (agent->eps_max > 0)
2506 update_freq_ctr(&agent->err_per_sec, 1);
2507
2508 if (agent->var_on_error) {
2509 struct sample smp;
2510
2511 memset(&smp, 0, sizeof(smp));
2512 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2513 smp.data.u.sint = ctx->status_code;
2514 smp.data.type = SMP_T_BOOL;
2515
2516 spoe_set_var(ctx, "txn", agent->var_on_error,
2517 strlen(agent->var_on_error), &smp);
2518 }
2519 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2520 " - failed to process messages: code=%u\n",
2521 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2522 __FUNCTION__, s, ctx->status_code);
2523 send_log(ctx->strm->be, LOG_WARNING,
2524 "SPOE: [%s] failed to process messages: code=%u\n",
2525 agent->id, ctx->status_code);
2526
2527 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2528 ? SPOE_CTX_ST_READY
2529 : SPOE_CTX_ST_NONE);
2530}
2531
Christopher Faulet58d03682017-09-21 16:57:24 +02002532/* Process a list of SPOE messages. First, this functions will process messages
2533 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2534 * to process corresponding actions. During all the processing, it returns 0
2535 * and it returns 1 when the processing is finished. If an error occurred, -1
2536 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002537static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002538spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2539 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002540{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002541 struct spoe_config *conf = FLT_CONF(ctx->filter);
2542 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002543 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002544
2545 if (ctx->state == SPOE_CTX_ST_ERROR)
2546 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002547
2548 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2549 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002550 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002551 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002552 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002553 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002554 goto error;
2555 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002556
2557 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002558 if (agent->eps_max > 0) {
2559 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2560 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002561 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002562 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002563 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564 goto skip;
2565 }
2566 }
2567
Christopher Fauletf7a30922016-11-10 15:04:51 +01002568 if (!tick_isset(ctx->process_exp)) {
2569 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2570 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2571 ctx->process_exp);
2572 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002573 ret = spoe_start_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002574 if (!ret)
2575 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002576
Christopher Faulet8ef75252017-02-20 22:56:03 +01002577 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002578 goto error;
2579
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002580 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002581 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002582 }
2583
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002584 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002585 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002586 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002587 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002588 if (ret < 0)
2589 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002590 if (!ret)
2591 goto skip;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002592 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2593 }
2594
2595 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2596 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002597 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002598 ret = 0;
2599 goto out;
2600 }
2601
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002602 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2603 ret = 0;
2604 goto out;
2605 }
2606
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002607 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002608 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002609 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002610 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002611 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002612 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002613 }
2614
2615 out:
2616 return ret;
2617
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002618 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002619 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002620 ret = 1;
2621 goto end;
2622
2623 skip:
2624 ctx->state = SPOE_CTX_ST_READY;
2625 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002626
Christopher Fauleta1cda022016-12-21 08:58:06 +01002627 end:
Christopher Faulet58d03682017-09-21 16:57:24 +02002628 spoe_stop_processing(ctx);
2629 return ret;
2630}
2631
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002632/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2633 * See spoe_process_message for details. */
2634static int
2635spoe_process_group(struct stream *s, struct spoe_context *ctx,
2636 struct spoe_group *group, int dir)
2637{
2638 int ret;
2639
2640 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2641 " - ctx-state=%s - Process messages for group=%s\n",
2642 (int)now.tv_sec, (int)now.tv_usec,
2643 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2644 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2645 group->id);
2646
2647 if (LIST_ISEMPTY(&group->messages))
2648 return 1;
2649
2650 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2651 return ret;
2652}
2653
Christopher Faulet58d03682017-09-21 16:57:24 +02002654/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2655 * See spoe_process_message for details. */
2656static int
2657spoe_process_event(struct stream *s, struct spoe_context *ctx,
2658 enum spoe_event ev)
2659{
2660 int dir, ret;
2661
2662 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002663 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 (int)now.tv_sec, (int)now.tv_usec,
2665 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2666 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2667 spoe_event_str[ev]);
2668
2669 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2670
2671 if (LIST_ISEMPTY(&(ctx->events[ev])))
2672 return 1;
2673
2674 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002675 return ret;
2676}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002677
2678/***************************************************************************
2679 * Functions that create/destroy SPOE contexts
2680 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002681static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002682spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002683{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002684 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002685 return 1;
2686
Christopher Faulet4596fb72017-01-11 14:05:19 +01002687 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2688 LIST_DEL(&buffer_wait->list);
2689 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002690 }
2691
Christopher Faulet4596fb72017-01-11 14:05:19 +01002692 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002693 return 1;
2694
Christopher Faulet4596fb72017-01-11 14:05:19 +01002695 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002696 return 0;
2697}
2698
2699static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002700spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002701{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002702 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2703 LIST_DEL(&buffer_wait->list);
2704 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002705 }
2706
2707 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002708 if (*buf != &buf_empty) {
2709 b_free(buf);
2710 offer_buffers(buffer_wait->target,
2711 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002712 }
2713}
2714
Christopher Faulet4596fb72017-01-11 14:05:19 +01002715static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002716spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002717{
2718 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2719 return 1;
2720}
2721
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002722static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002723spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002724{
2725 struct spoe_config *conf = FLT_CONF(filter);
2726 struct spoe_context *ctx;
2727
2728 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2729 if (ctx == NULL) {
2730 return NULL;
2731 }
2732 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002733 ctx->filter = filter;
2734 ctx->state = SPOE_CTX_ST_NONE;
2735 ctx->status_code = SPOE_CTX_ERR_NONE;
2736 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002737 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002738 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002739 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002740 LIST_INIT(&ctx->buffer_wait.list);
2741 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002742 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002743 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744
Christopher Fauletf7a30922016-11-10 15:04:51 +01002745 ctx->stream_id = 0;
2746 ctx->frame_id = 1;
2747 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002748
2749 return ctx;
2750}
2751
2752static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002753spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002754{
2755 if (!ctx)
2756 return;
2757
Christopher Faulet58d03682017-09-21 16:57:24 +02002758 spoe_stop_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002759 pool_free2(pool2_spoe_ctx, ctx);
2760}
2761
2762static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002763spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002764{
2765 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002766 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002767}
2768
2769
2770/***************************************************************************
2771 * Hooks that manage the filter lifecycle (init/check/deinit)
2772 **************************************************************************/
2773/* Signal handler: Do a soft stop, wakeup SPOE applet */
2774static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002775spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002776{
2777 struct proxy *p;
2778
2779 p = proxy;
2780 while (p) {
2781 struct flt_conf *fconf;
2782
2783 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002784 struct spoe_config *conf;
2785 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002786 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002787
Christopher Faulet3b386a32017-02-23 10:17:15 +01002788 if (fconf->id != spoe_filter_id)
2789 continue;
2790
2791 conf = fconf->conf;
2792 agent = conf->agent;
2793
Christopher Faulet42bfa462017-01-04 14:14:19 +01002794 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002795 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002796 }
2797 }
2798 p = p->next;
2799 }
2800}
2801
2802
2803/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2804static int
2805spoe_init(struct proxy *px, struct flt_conf *fconf)
2806{
2807 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002808
2809 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2810 init_new_proxy(&conf->agent_fe);
2811 conf->agent_fe.parent = conf->agent;
2812 conf->agent_fe.last_change = now.tv_sec;
2813 conf->agent_fe.id = conf->agent->id;
2814 conf->agent_fe.cap = PR_CAP_FE;
2815 conf->agent_fe.mode = PR_MODE_TCP;
2816 conf->agent_fe.maxconn = 0;
2817 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2818 conf->agent_fe.conn_retries = CONN_RETRIES;
2819 conf->agent_fe.accept = frontend_accept;
2820 conf->agent_fe.srv = NULL;
2821 conf->agent_fe.timeout.client = TICK_ETERNITY;
2822 conf->agent_fe.default_target = &spoe_applet.obj_type;
2823 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2824
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002825 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002826 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002827 sighandler_registered = 1;
2828 }
2829
2830 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002831}
2832
2833/* Free ressources allocated by the SPOE filter. */
2834static void
2835spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2836{
2837 struct spoe_config *conf = fconf->conf;
2838
2839 if (conf) {
2840 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002841
Christopher Faulet8ef75252017-02-20 22:56:03 +01002842 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002843 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002844 free(conf);
2845 }
2846 fconf->conf = NULL;
2847}
2848
2849/* Check configuration of a SPOE filter for a specified proxy.
2850 * Return 1 on error, else 0. */
2851static int
2852spoe_check(struct proxy *px, struct flt_conf *fconf)
2853{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002854 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002855 struct spoe_config *conf = fconf->conf;
2856 struct proxy *target;
2857
Christopher Faulet7ee86672017-09-19 11:08:28 +02002858 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2859 * are uniq */
2860 list_for_each_entry(f, &px->filter_configs, list) {
2861 struct spoe_config *c = f->conf;
2862
2863 /* This is not an SPOE filter */
2864 if (f->id != spoe_filter_id)
2865 continue;
2866 /* This is the current SPOE filter */
2867 if (f == fconf)
2868 continue;
2869
2870 /* Check engine Id. It should be uniq */
2871 if (!strcmp(conf->id, c->id)) {
2872 Alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2873 px->id, conf->id);
2874 return 1;
2875 }
2876 }
2877
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002878 target = proxy_be_by_name(conf->agent->b.name);
2879 if (target == NULL) {
2880 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2881 " declared at %s:%d.\n",
2882 px->id, conf->agent->b.name, conf->agent->id,
2883 conf->agent->conf.file, conf->agent->conf.line);
2884 return 1;
2885 }
2886 if (target->mode != PR_MODE_TCP) {
2887 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2888 " at %s:%d does not support HTTP mode.\n",
2889 px->id, target->id, conf->agent->id,
2890 conf->agent->conf.file, conf->agent->conf.line);
2891 return 1;
2892 }
2893
2894 free(conf->agent->b.name);
2895 conf->agent->b.name = NULL;
2896 conf->agent->b.be = target;
2897 return 0;
2898}
2899
2900/**************************************************************************
2901 * Hooks attached to a stream
2902 *************************************************************************/
2903/* Called when a filter instance is created and attach to a stream. It creates
2904 * the context that will be used to process this stream. */
2905static int
2906spoe_start(struct stream *s, struct filter *filter)
2907{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002908 struct spoe_config *conf = FLT_CONF(filter);
2909 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002910 struct spoe_context *ctx;
2911
2912 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002913 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914 __FUNCTION__, s);
2915
Christopher Faulet8ef75252017-02-20 22:56:03 +01002916 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002917 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002918 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2919 " - failed to create SPOE context\n",
2920 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002921 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002922 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002923 "SPOE: [%s] failed to create SPOE context\n",
2924 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002925 return 0;
2926 }
2927
2928 ctx->strm = s;
2929 ctx->state = SPOE_CTX_ST_READY;
2930 filter->ctx = ctx;
2931
Christopher Faulet11610f32017-09-21 10:23:10 +02002932 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002933 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2934
Christopher Faulet11610f32017-09-21 10:23:10 +02002935 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002936 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2937
Christopher Faulet11610f32017-09-21 10:23:10 +02002938 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002939 filter->pre_analyzers |= AN_RES_INSPECT;
2940
Christopher Faulet11610f32017-09-21 10:23:10 +02002941 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2943
Christopher Faulet11610f32017-09-21 10:23:10 +02002944 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002945 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2946
Christopher Faulet11610f32017-09-21 10:23:10 +02002947 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002948 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2949
2950 return 1;
2951}
2952
2953/* Called when a filter instance is detached from a stream. It release the
2954 * attached SPOE context. */
2955static void
2956spoe_stop(struct stream *s, struct filter *filter)
2957{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002958 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2959 (int)now.tv_sec, (int)now.tv_usec,
2960 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2961 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002962 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002963}
2964
Christopher Fauletf7a30922016-11-10 15:04:51 +01002965
2966/*
2967 * Called when the stream is woken up because of expired timer.
2968 */
2969static void
2970spoe_check_timeouts(struct stream *s, struct filter *filter)
2971{
2972 struct spoe_context *ctx = filter->ctx;
2973
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002974 if (tick_is_expired(ctx->process_exp, now_ms)) {
2975 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002976 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002977 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002978}
2979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980/* Called when we are ready to filter data on a channel */
2981static int
2982spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2983{
2984 struct spoe_context *ctx = filter->ctx;
2985 int ret = 1;
2986
2987 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2988 " - ctx-flags=0x%08x\n",
2989 (int)now.tv_sec, (int)now.tv_usec,
2990 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2991 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2992
Christopher Fauletb067b062017-01-04 16:39:11 +01002993 if (ctx->state == SPOE_CTX_ST_NONE)
2994 goto out;
2995
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002996 if (!(chn->flags & CF_ISRESP)) {
2997 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2998 chn->analysers |= AN_REQ_INSPECT_FE;
2999 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3000 chn->analysers |= AN_REQ_INSPECT_BE;
3001
3002 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3003 goto out;
3004
3005 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003006 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003007 if (!ret)
3008 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003009 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3010 }
3011 else {
3012 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3013 chn->analysers |= AN_RES_INSPECT;
3014
3015 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3016 goto out;
3017
Christopher Faulet8ef75252017-02-20 22:56:03 +01003018 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003019 if (!ret) {
3020 channel_dont_read(chn);
3021 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003022 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003023 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003024 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003025 }
3026
3027 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003028 return ret;
3029}
3030
3031/* Called before a processing happens on a given channel */
3032static int
3033spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3034 struct channel *chn, unsigned an_bit)
3035{
3036 struct spoe_context *ctx = filter->ctx;
3037 int ret = 1;
3038
3039 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3040 " - ctx-flags=0x%08x - ana=0x%08x\n",
3041 (int)now.tv_sec, (int)now.tv_usec,
3042 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3043 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3044 ctx->flags, an_bit);
3045
Christopher Fauletb067b062017-01-04 16:39:11 +01003046 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003047 goto out;
3048
3049 switch (an_bit) {
3050 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003051 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 break;
3053 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003054 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003055 break;
3056 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003057 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003058 break;
3059 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003060 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003061 break;
3062 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003063 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003064 break;
3065 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003066 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003067 break;
3068 }
3069
3070 out:
3071 if (!ret) {
3072 channel_dont_read(chn);
3073 channel_dont_close(chn);
3074 }
3075 return ret;
3076}
3077
3078/* Called when the filtering on the channel ends. */
3079static int
3080spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3081{
3082 struct spoe_context *ctx = filter->ctx;
3083
3084 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3085 " - ctx-flags=0x%08x\n",
3086 (int)now.tv_sec, (int)now.tv_usec,
3087 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3088 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3089
3090 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003091 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003092 }
3093
3094 return 1;
3095}
3096
3097/********************************************************************
3098 * Functions that manage the filter initialization
3099 ********************************************************************/
3100struct flt_ops spoe_ops = {
3101 /* Manage SPOE filter, called for each filter declaration */
3102 .init = spoe_init,
3103 .deinit = spoe_deinit,
3104 .check = spoe_check,
3105
3106 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003107 .attach = spoe_start,
3108 .detach = spoe_stop,
3109 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003110
3111 /* Handle channels activity */
3112 .channel_start_analyze = spoe_start_analyze,
3113 .channel_pre_analyze = spoe_chn_pre_analyze,
3114 .channel_end_analyze = spoe_end_analyze,
3115};
3116
3117
3118static int
3119cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3120{
3121 const char *err;
3122 int i, err_code = 0;
3123
3124 if ((cfg_scope == NULL && curengine != NULL) ||
3125 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003126 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003127 goto out;
3128
3129 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3130 if (!*args[1]) {
3131 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3132 file, linenum);
3133 err_code |= ERR_ALERT | ERR_ABORT;
3134 goto out;
3135 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003136 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3137 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003138 goto out;
3139 }
3140
3141 err = invalid_char(args[1]);
3142 if (err) {
3143 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3144 file, linenum, *err, args[0], args[1]);
3145 err_code |= ERR_ALERT | ERR_ABORT;
3146 goto out;
3147 }
3148
3149 if (curagent != NULL) {
3150 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3151 file, linenum);
3152 err_code |= ERR_ALERT | ERR_ABORT;
3153 goto out;
3154 }
3155 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
3156 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3157 err_code |= ERR_ALERT | ERR_ABORT;
3158 goto out;
3159 }
3160
3161 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003162
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163 curagent->conf.file = strdup(file);
3164 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003165
3166 curagent->timeout.hello = TICK_ETERNITY;
3167 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003168 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003169
3170 curagent->engine_id = NULL;
3171 curagent->var_pfx = NULL;
3172 curagent->var_on_error = NULL;
Christopher Fauletcecd8522017-02-24 22:11:21 +01003173 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003174 curagent->cps_max = 0;
3175 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003176 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003177 curagent->min_applets = 0;
3178 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003179
3180 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003181 LIST_INIT(&curagent->events[i]);
3182 LIST_INIT(&curagent->groups);
3183 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003184
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003185 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003186 curagent->applets_act = 0;
3187 curagent->applets_idle = 0;
3188 curagent->sending_rate = 0;
3189
3190 LIST_INIT(&curagent->applets);
3191 LIST_INIT(&curagent->sending_queue);
3192 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003193 }
3194 else if (!strcmp(args[0], "use-backend")) {
3195 if (!*args[1]) {
3196 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3197 file, linenum, args[0]);
3198 err_code |= ERR_ALERT | ERR_FATAL;
3199 goto out;
3200 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003201 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003202 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003203 free(curagent->b.name);
3204 curagent->b.name = strdup(args[1]);
3205 }
3206 else if (!strcmp(args[0], "messages")) {
3207 int cur_arg = 1;
3208 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003209 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003210
Christopher Faulet11610f32017-09-21 10:23:10 +02003211 list_for_each_entry(ph, &curmphs, list) {
3212 if (!strcmp(ph->id, args[cur_arg])) {
3213 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 file, linenum, args[cur_arg]);
3215 err_code |= ERR_ALERT | ERR_FATAL;
3216 goto out;
3217 }
3218 }
3219
Christopher Faulet11610f32017-09-21 10:23:10 +02003220 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3222 err_code |= ERR_ALERT | ERR_ABORT;
3223 goto out;
3224 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003225 ph->id = strdup(args[cur_arg]);
3226 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227 cur_arg++;
3228 }
3229 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003230 else if (!strcmp(args[0], "groups")) {
3231 int cur_arg = 1;
3232 while (*args[cur_arg]) {
3233 struct spoe_placeholder *ph = NULL;
3234
3235 list_for_each_entry(ph, &curgphs, list) {
3236 if (!strcmp(ph->id, args[cur_arg])) {
3237 Alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3238 file, linenum, args[cur_arg]);
3239 err_code |= ERR_ALERT | ERR_FATAL;
3240 goto out;
3241 }
3242 }
3243
3244 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3245 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3246 err_code |= ERR_ALERT | ERR_ABORT;
3247 goto out;
3248 }
3249 ph->id = strdup(args[cur_arg]);
3250 LIST_ADDQ(&curgphs, &ph->list);
3251 cur_arg++;
3252 }
3253 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003254 else if (!strcmp(args[0], "timeout")) {
3255 unsigned int *tv = NULL;
3256 const char *res;
3257 unsigned timeout;
3258
3259 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003260 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003261 file, linenum);
3262 err_code |= ERR_ALERT | ERR_FATAL;
3263 goto out;
3264 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003265 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3266 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003267 if (!strcmp(args[1], "hello"))
3268 tv = &curagent->timeout.hello;
3269 else if (!strcmp(args[1], "idle"))
3270 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003271 else if (!strcmp(args[1], "processing"))
3272 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003274 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003275 file, linenum, args[1]);
3276 err_code |= ERR_ALERT | ERR_FATAL;
3277 goto out;
3278 }
3279 if (!*args[2]) {
3280 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3281 file, linenum, args[1]);
3282 err_code |= ERR_ALERT | ERR_FATAL;
3283 goto out;
3284 }
3285 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3286 if (res) {
3287 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3288 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003289 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003290 goto out;
3291 }
3292 *tv = MS_TO_TICKS(timeout);
3293 }
3294 else if (!strcmp(args[0], "option")) {
3295 if (!*args[1]) {
3296 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3297 file, linenum, args[0]);
3298 err_code |= ERR_ALERT | ERR_FATAL;
3299 goto out;
3300 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003301
Christopher Faulet305c6072017-02-23 16:17:53 +01003302 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003303 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003304 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003305 if (kwm == 1)
3306 curagent->flags &= ~SPOE_FL_PIPELINING;
3307 else
3308 curagent->flags |= SPOE_FL_PIPELINING;
3309 goto out;
3310 }
3311 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003312 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003313 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003314 if (kwm == 1)
3315 curagent->flags &= ~SPOE_FL_ASYNC;
3316 else
3317 curagent->flags |= SPOE_FL_ASYNC;
3318 goto out;
3319 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003320 else if (!strcmp(args[1], "send-frag-payload")) {
3321 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3322 goto out;
3323 if (kwm == 1)
3324 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3325 else
3326 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3327 goto out;
3328 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003329
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003330 /* Following options does not support negation */
3331 if (kwm == 1) {
3332 Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3333 file, linenum, args[1]);
3334 err_code |= ERR_ALERT | ERR_FATAL;
3335 goto out;
3336 }
3337
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003338 if (!strcmp(args[1], "var-prefix")) {
3339 char *tmp;
3340
3341 if (!*args[2]) {
3342 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3343 file, linenum, args[0],
3344 args[1]);
3345 err_code |= ERR_ALERT | ERR_FATAL;
3346 goto out;
3347 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003348 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3349 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003350 tmp = args[2];
3351 while (*tmp) {
3352 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3353 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3354 file, linenum, args[0], args[1]);
3355 err_code |= ERR_ALERT | ERR_FATAL;
3356 goto out;
3357 }
3358 tmp++;
3359 }
3360 curagent->var_pfx = strdup(args[2]);
3361 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003362 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003363 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003364 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003365 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3366 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003367 else if (!strcmp(args[1], "set-on-error")) {
3368 char *tmp;
3369
3370 if (!*args[2]) {
3371 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3372 file, linenum, args[0],
3373 args[1]);
3374 err_code |= ERR_ALERT | ERR_FATAL;
3375 goto out;
3376 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003377 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3378 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003379 tmp = args[2];
3380 while (*tmp) {
3381 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3382 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3383 file, linenum, args[0], args[1]);
3384 err_code |= ERR_ALERT | ERR_FATAL;
3385 goto out;
3386 }
3387 tmp++;
3388 }
3389 curagent->var_on_error = strdup(args[2]);
3390 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003391 else {
3392 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3393 file, linenum, args[1]);
3394 err_code |= ERR_ALERT | ERR_FATAL;
3395 goto out;
3396 }
Christopher Faulet48026722016-11-16 15:01:12 +01003397 }
3398 else if (!strcmp(args[0], "maxconnrate")) {
3399 if (!*args[1]) {
3400 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3401 file, linenum, args[0]);
3402 err_code |= ERR_ALERT | ERR_FATAL;
3403 goto out;
3404 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003405 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003406 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003407 curagent->cps_max = atol(args[1]);
3408 }
3409 else if (!strcmp(args[0], "maxerrrate")) {
3410 if (!*args[1]) {
3411 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3412 file, linenum, args[0]);
3413 err_code |= ERR_ALERT | ERR_FATAL;
3414 goto out;
3415 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003416 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003417 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003418 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003419 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003420 else if (!strcmp(args[0], "max-frame-size")) {
3421 if (!*args[1]) {
3422 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3423 file, linenum, args[0]);
3424 err_code |= ERR_ALERT | ERR_FATAL;
3425 goto out;
3426 }
3427 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3428 goto out;
3429 curagent->max_frame_size = atol(args[1]);
3430 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3431 curagent->max_frame_size > MAX_FRAME_SIZE) {
3432 Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3433 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
3434 err_code |= ERR_ALERT | ERR_FATAL;
3435 goto out;
3436 }
3437 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003438 else if (*args[0]) {
3439 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3440 file, linenum, args[0]);
3441 err_code |= ERR_ALERT | ERR_FATAL;
3442 goto out;
3443 }
3444 out:
3445 return err_code;
3446}
Christopher Faulet11610f32017-09-21 10:23:10 +02003447static int
3448cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3449{
3450 struct spoe_group *grp;
3451 const char *err;
3452 int err_code = 0;
3453
3454 if ((cfg_scope == NULL && curengine != NULL) ||
3455 (cfg_scope != NULL && curengine == NULL) ||
3456 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3457 goto out;
3458
3459 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3460 if (!*args[1]) {
3461 Alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3462 file, linenum);
3463 err_code |= ERR_ALERT | ERR_ABORT;
3464 goto out;
3465 }
3466 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3467 err_code |= ERR_ABORT;
3468 goto out;
3469 }
3470
3471 err = invalid_char(args[1]);
3472 if (err) {
3473 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3474 file, linenum, *err, args[0], args[1]);
3475 err_code |= ERR_ALERT | ERR_ABORT;
3476 goto out;
3477 }
3478
3479 list_for_each_entry(grp, &curgrps, list) {
3480 if (!strcmp(grp->id, args[1])) {
3481 Alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3482 " name as another one declared at %s:%d.\n",
3483 file, linenum, args[1], grp->conf.file, grp->conf.line);
3484 err_code |= ERR_ALERT | ERR_FATAL;
3485 goto out;
3486 }
3487 }
3488
3489 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
3490 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3491 err_code |= ERR_ALERT | ERR_ABORT;
3492 goto out;
3493 }
3494
3495 curgrp->id = strdup(args[1]);
3496 curgrp->conf.file = strdup(file);
3497 curgrp->conf.line = linenum;
3498 LIST_INIT(&curgrp->phs);
3499 LIST_INIT(&curgrp->messages);
3500 LIST_ADDQ(&curgrps, &curgrp->list);
3501 }
3502 else if (!strcmp(args[0], "messages")) {
3503 int cur_arg = 1;
3504 while (*args[cur_arg]) {
3505 struct spoe_placeholder *ph = NULL;
3506
3507 list_for_each_entry(ph, &curgrp->phs, list) {
3508 if (!strcmp(ph->id, args[cur_arg])) {
3509 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3510 file, linenum, args[cur_arg]);
3511 err_code |= ERR_ALERT | ERR_FATAL;
3512 goto out;
3513 }
3514 }
3515
3516 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3517 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3518 err_code |= ERR_ALERT | ERR_ABORT;
3519 goto out;
3520 }
3521 ph->id = strdup(args[cur_arg]);
3522 LIST_ADDQ(&curgrp->phs, &ph->list);
3523 cur_arg++;
3524 }
3525 }
3526 else if (*args[0]) {
3527 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3528 file, linenum, args[0]);
3529 err_code |= ERR_ALERT | ERR_FATAL;
3530 goto out;
3531 }
3532 out:
3533 return err_code;
3534}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003535
3536static int
3537cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3538{
3539 struct spoe_message *msg;
3540 struct spoe_arg *arg;
3541 const char *err;
3542 char *errmsg = NULL;
3543 int err_code = 0;
3544
3545 if ((cfg_scope == NULL && curengine != NULL) ||
3546 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003547 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003548 goto out;
3549
3550 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3551 if (!*args[1]) {
3552 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3553 file, linenum);
3554 err_code |= ERR_ALERT | ERR_ABORT;
3555 goto out;
3556 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003557 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3558 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003559 goto out;
3560 }
3561
3562 err = invalid_char(args[1]);
3563 if (err) {
3564 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3565 file, linenum, *err, args[0], args[1]);
3566 err_code |= ERR_ALERT | ERR_ABORT;
3567 goto out;
3568 }
3569
3570 list_for_each_entry(msg, &curmsgs, list) {
3571 if (!strcmp(msg->id, args[1])) {
3572 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3573 " name as another one declared at %s:%d.\n",
3574 file, linenum, args[1], msg->conf.file, msg->conf.line);
3575 err_code |= ERR_ALERT | ERR_FATAL;
3576 goto out;
3577 }
3578 }
3579
3580 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3581 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3582 err_code |= ERR_ALERT | ERR_ABORT;
3583 goto out;
3584 }
3585
3586 curmsg->id = strdup(args[1]);
3587 curmsg->id_len = strlen(curmsg->id);
3588 curmsg->event = SPOE_EV_NONE;
3589 curmsg->conf.file = strdup(file);
3590 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003591 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003592 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003593 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003594 LIST_INIT(&curmsg->by_evt);
3595 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003596 LIST_ADDQ(&curmsgs, &curmsg->list);
3597 }
3598 else if (!strcmp(args[0], "args")) {
3599 int cur_arg = 1;
3600
3601 curproxy->conf.args.ctx = ARGC_SPOE;
3602 curproxy->conf.args.file = file;
3603 curproxy->conf.args.line = linenum;
3604 while (*args[cur_arg]) {
3605 char *delim = strchr(args[cur_arg], '=');
3606 int idx = 0;
3607
3608 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3609 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3610 err_code |= ERR_ALERT | ERR_ABORT;
3611 goto out;
3612 }
3613
3614 if (!delim) {
3615 arg->name = NULL;
3616 arg->name_len = 0;
3617 delim = args[cur_arg];
3618 }
3619 else {
3620 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3621 arg->name_len = delim - args[cur_arg];
3622 delim++;
3623 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003624 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3625 &idx, file, linenum, &errmsg,
3626 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003627 if (arg->expr == NULL) {
3628 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3629 err_code |= ERR_ALERT | ERR_FATAL;
3630 free(arg->name);
3631 free(arg);
3632 goto out;
3633 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003634 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003635 LIST_ADDQ(&curmsg->args, &arg->list);
3636 cur_arg++;
3637 }
3638 curproxy->conf.args.file = NULL;
3639 curproxy->conf.args.line = 0;
3640 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003641 else if (!strcmp(args[0], "acl")) {
3642 err = invalid_char(args[1]);
3643 if (err) {
3644 Alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3645 file, linenum, *err, args[1]);
3646 err_code |= ERR_ALERT | ERR_FATAL;
3647 goto out;
3648 }
3649 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
3650 Alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3651 file, linenum, args[1], errmsg);
3652 err_code |= ERR_ALERT | ERR_FATAL;
3653 goto out;
3654 }
3655 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003656 else if (!strcmp(args[0], "event")) {
3657 if (!*args[1]) {
3658 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003659 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003660 goto out;
3661 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003662 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3663 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003664
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003665 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3666 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3667 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3668 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3669
3670 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3671 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3672 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3673 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3674 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3675 curmsg->event = SPOE_EV_ON_TCP_RSP;
3676
3677 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3678 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3679 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3680 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3681 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3682 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3683 else {
3684 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3685 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003686 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003687 goto out;
3688 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003689
3690 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3691 struct acl_cond *cond;
3692
3693 cond = build_acl_cond(file, linenum, &curmsg->acls,
3694 curproxy, (const char **)args+2,
3695 &errmsg);
3696 if (cond == NULL) {
3697 Alert("parsing [%s:%d] : error detected while "
3698 "parsing an 'event %s' condition : %s.\n",
3699 file, linenum, args[1], errmsg);
3700 err_code |= ERR_ALERT | ERR_FATAL;
3701 goto out;
3702 }
3703 curmsg->cond = cond;
3704 }
3705 else if (*args[2]) {
3706 Alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3707 "or 'unless' followed by a condition but found '%s'.\n",
3708 file, linenum, args[1], args[2]);
3709 err_code |= ERR_ALERT | ERR_FATAL;
3710 goto out;
3711 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003712 }
3713 else if (!*args[0]) {
3714 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3715 file, linenum, args[0]);
3716 err_code |= ERR_ALERT | ERR_FATAL;
3717 goto out;
3718 }
3719 out:
3720 free(errmsg);
3721 return err_code;
3722}
3723
3724/* Return -1 on error, else 0 */
3725static int
3726parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3727 struct flt_conf *fconf, char **err, void *private)
3728{
3729 struct list backup_sections;
3730 struct spoe_config *conf;
3731 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003732 struct spoe_group *grp, *grpback;
3733 struct spoe_placeholder *ph, *phback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003734 char *file = NULL, *engine = NULL;
3735 int ret, pos = *cur_arg + 1;
3736
3737 conf = calloc(1, sizeof(*conf));
3738 if (conf == NULL) {
3739 memprintf(err, "%s: out of memory", args[*cur_arg]);
3740 goto error;
3741 }
3742 conf->proxy = px;
3743
3744 while (*args[pos]) {
3745 if (!strcmp(args[pos], "config")) {
3746 if (!*args[pos+1]) {
3747 memprintf(err, "'%s' : '%s' option without value",
3748 args[*cur_arg], args[pos]);
3749 goto error;
3750 }
3751 file = args[pos+1];
3752 pos += 2;
3753 }
3754 else if (!strcmp(args[pos], "engine")) {
3755 if (!*args[pos+1]) {
3756 memprintf(err, "'%s' : '%s' option without value",
3757 args[*cur_arg], args[pos]);
3758 goto error;
3759 }
3760 engine = args[pos+1];
3761 pos += 2;
3762 }
3763 else {
3764 memprintf(err, "unknown keyword '%s'", args[pos]);
3765 goto error;
3766 }
3767 }
3768 if (file == NULL) {
3769 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3770 goto error;
3771 }
3772
3773 /* backup sections and register SPOE sections */
3774 LIST_INIT(&backup_sections);
3775 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003776 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3777 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003778 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003779
3780 /* Parse SPOE filter configuration file */
3781 curengine = engine;
3782 curproxy = px;
3783 curagent = NULL;
3784 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003785 LIST_INIT(&curmsgs);
3786 LIST_INIT(&curgrps);
3787 LIST_INIT(&curmphs);
3788 LIST_INIT(&curgphs);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003789 ret = readcfgfile(file);
3790 curproxy = NULL;
3791
3792 /* unregister SPOE sections and restore previous sections */
3793 cfg_unregister_sections();
3794 cfg_restore_sections(&backup_sections);
3795
3796 if (ret == -1) {
3797 memprintf(err, "Could not open configuration file %s : %s",
3798 file, strerror(errno));
3799 goto error;
3800 }
3801 if (ret & (ERR_ABORT|ERR_FATAL)) {
3802 memprintf(err, "Error(s) found in configuration file %s", file);
3803 goto error;
3804 }
3805
3806 /* Check SPOE agent */
3807 if (curagent == NULL) {
3808 memprintf(err, "No SPOE agent found in file %s", file);
3809 goto error;
3810 }
3811 if (curagent->b.name == NULL) {
3812 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3813 curagent->id, curagent->conf.file, curagent->conf.line);
3814 goto error;
3815 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003816 if (curagent->timeout.hello == TICK_ETERNITY ||
3817 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003818 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003819 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3820 " | While not properly invalid, you will certainly encounter various problems\n"
3821 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003822 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003823 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3824 }
3825 if (curagent->var_pfx == NULL) {
3826 char *tmp = curagent->id;
3827
3828 while (*tmp) {
3829 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3830 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3831 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3832 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3833 goto error;
3834 }
3835 tmp++;
3836 }
3837 curagent->var_pfx = strdup(curagent->id);
3838 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003839 if (curagent->engine_id == NULL)
3840 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003841
Christopher Faulet11610f32017-09-21 10:23:10 +02003842 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
3843 Warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003844 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3845 goto finish;
3846 }
3847
Christopher Faulet11610f32017-09-21 10:23:10 +02003848 /* Replace placeholders by the corresponding messages for the SPOE
3849 * agent */
3850 list_for_each_entry(ph, &curmphs, list) {
3851 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003852 struct spoe_arg *arg;
3853 unsigned int where;
3854
Christopher Faulet11610f32017-09-21 10:23:10 +02003855 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003856 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3857 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3858 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3859 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3860 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3861 }
3862 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3863 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3864 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3865 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3866 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003867 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003868 }
3869 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003870 Warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3871 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003872 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003873 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003874
3875 where = 0;
3876 switch (msg->event) {
3877 case SPOE_EV_ON_CLIENT_SESS:
3878 where |= SMP_VAL_FE_CON_ACC;
3879 break;
3880
3881 case SPOE_EV_ON_TCP_REQ_FE:
3882 where |= SMP_VAL_FE_REQ_CNT;
3883 break;
3884
3885 case SPOE_EV_ON_HTTP_REQ_FE:
3886 where |= SMP_VAL_FE_HRQ_HDR;
3887 break;
3888
3889 case SPOE_EV_ON_TCP_REQ_BE:
3890 if (px->cap & PR_CAP_FE)
3891 where |= SMP_VAL_FE_REQ_CNT;
3892 if (px->cap & PR_CAP_BE)
3893 where |= SMP_VAL_BE_REQ_CNT;
3894 break;
3895
3896 case SPOE_EV_ON_HTTP_REQ_BE:
3897 if (px->cap & PR_CAP_FE)
3898 where |= SMP_VAL_FE_HRQ_HDR;
3899 if (px->cap & PR_CAP_BE)
3900 where |= SMP_VAL_BE_HRQ_HDR;
3901 break;
3902
3903 case SPOE_EV_ON_SERVER_SESS:
3904 where |= SMP_VAL_BE_SRV_CON;
3905 break;
3906
3907 case SPOE_EV_ON_TCP_RSP:
3908 if (px->cap & PR_CAP_FE)
3909 where |= SMP_VAL_FE_RES_CNT;
3910 if (px->cap & PR_CAP_BE)
3911 where |= SMP_VAL_BE_RES_CNT;
3912 break;
3913
3914 case SPOE_EV_ON_HTTP_RSP:
3915 if (px->cap & PR_CAP_FE)
3916 where |= SMP_VAL_FE_HRS_HDR;
3917 if (px->cap & PR_CAP_BE)
3918 where |= SMP_VAL_BE_HRS_HDR;
3919 break;
3920
3921 default:
3922 break;
3923 }
3924
3925 list_for_each_entry(arg, &msg->args, list) {
3926 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003927 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003928 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003929 "none of which is available here ('%s')",
3930 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003931 sample_ckp_names(arg->expr->fetch->use),
3932 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003933 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003934 }
3935 }
3936
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003937 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003938 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3939 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003940 }
3941 }
3942 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003943 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003944 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003945 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003946 continue;
3947 }
3948
Christopher Faulet11610f32017-09-21 10:23:10 +02003949 /* Replace placeholders by the corresponding groups for the SPOE
3950 * agent */
3951 list_for_each_entry(ph, &curgphs, list) {
3952 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3953 if (!strcmp(grp->id, ph->id)) {
3954 grp->agent = curagent;
3955 LIST_DEL(&grp->list);
3956 LIST_ADDQ(&curagent->groups, &grp->list);
3957 goto next_aph;
3958 }
3959 }
3960 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
3961 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
3962 goto error;
3963 next_aph:
3964 continue;
3965 }
3966
3967 /* Replace placeholders by the corresponding message for each SPOE
3968 * group of the SPOE agent */
3969 list_for_each_entry(grp, &curagent->groups, list) {
3970 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
3971 list_for_each_entry(msg, &curmsgs, list) {
3972 if (!strcmp(msg->id, ph->id)) {
3973 if (msg->group != NULL) {
3974 memprintf(err, "SPOE message '%s' already belongs to "
3975 "the SPOE group '%s' declare at %s:%d",
3976 msg->id, msg->group->id,
3977 msg->group->conf.file,
3978 msg->group->conf.line);
3979 goto error;
3980 }
3981
3982 /* Scope for arguments are not checked for now. We will check
3983 * them only if a rule use the corresponding SPOE group. */
3984 msg->agent = curagent;
3985 msg->group = grp;
3986 LIST_DEL(&ph->list);
3987 LIST_ADDQ(&grp->messages, &msg->by_grp);
3988 goto next_mph_grp;
3989 }
3990 }
3991 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
3992 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
3993 goto error;
3994 next_mph_grp:
3995 continue;
3996 }
3997 }
3998
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003999 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004000 /* move curmsgs to the agent message list */
4001 curmsgs.n->p = &curagent->messages;
4002 curmsgs.p->n = &curagent->messages;
4003 curagent->messages = curmsgs;
4004 LIST_INIT(&curmsgs);
4005
Christopher Faulet7ee86672017-09-19 11:08:28 +02004006 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004007 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004008 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4009 LIST_DEL(&ph->list);
4010 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004011 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004012 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4013 LIST_DEL(&ph->list);
4014 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004015 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004016 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4017 LIST_DEL(&grp->list);
4018 spoe_release_group(grp);
4019 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004020 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004021 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004022 fconf->ops = &spoe_ops;
4023 fconf->conf = conf;
4024 return 0;
4025
4026 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004027 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004028 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4029 LIST_DEL(&ph->list);
4030 spoe_release_placeholder(ph);
4031 }
4032 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4033 LIST_DEL(&ph->list);
4034 spoe_release_placeholder(ph);
4035 }
4036 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4037 LIST_DEL(&grp->list);
4038 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004039 }
4040 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4041 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004042 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004043 }
4044 free(conf);
4045 return -1;
4046}
4047
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004048/* Send message of a SPOE group. This is the action_ptr callback of a rule
4049 * associated to a "send-spoe-group" action.
4050 *
4051 * It returns ACT_RET_CONT is processing is finished without error, it returns
4052 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4053 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004054static enum act_return
4055spoe_send_group(struct act_rule *rule, struct proxy *px,
4056 struct session *sess, struct stream *s, int flags)
4057{
4058 struct filter *filter;
4059 struct spoe_agent *agent = NULL;
4060 struct spoe_group *group = NULL;
4061 struct spoe_context *ctx = NULL;
4062 int ret, dir;
4063
4064 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4065 if (filter->config == rule->arg.act.p[0]) {
4066 agent = rule->arg.act.p[2];
4067 group = rule->arg.act.p[3];
4068 ctx = filter->ctx;
4069 break;
4070 }
4071 }
4072 if (agent == NULL || group == NULL || ctx == NULL)
4073 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004074 if (ctx->state == SPOE_CTX_ST_NONE)
4075 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004076
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004077 switch (rule->from) {
4078 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4079 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4080 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4081 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4082 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4083 default:
4084 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4085 " - internal error while execute spoe-send-group\n",
4086 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4087 __FUNCTION__, s);
4088 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4089 agent->id);
4090 return ACT_RET_CONT;
4091 }
4092
4093 ret = spoe_process_group(s, ctx, group, dir);
4094 if (ret == 1)
4095 return ACT_RET_CONT;
4096 else if (ret == 0) {
4097 if (flags & ACT_FLAG_FINAL) {
4098 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4099 " - failed to process group '%s': interrupted by caller\n",
4100 (int)now.tv_sec, (int)now.tv_usec,
4101 agent->id, __FUNCTION__, s, group->id);
4102 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4103 spoe_handle_processing_error(s, agent, ctx, dir);
4104 spoe_stop_processing(ctx);
4105 return ACT_RET_CONT;
4106 }
4107 return ACT_RET_YIELD;
4108 }
4109 else
4110 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004111}
4112
4113/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4114 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4115 * action should be:
4116 *
4117 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4118 *
4119 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4120 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4121 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4122 * group.
4123 *
4124 * The function returns 1 in success case, otherwise, it returns 0 and err is
4125 * filled.
4126 */
4127static int
4128check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4129{
4130 struct flt_conf *fconf;
4131 struct spoe_config *conf;
4132 struct spoe_agent *agent = NULL;
4133 struct spoe_group *group;
4134 struct spoe_message *msg;
4135 char *engine_id = rule->arg.act.p[0];
4136 char *group_id = rule->arg.act.p[1];
4137 unsigned int where = 0;
4138
4139 switch (rule->from) {
4140 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4141 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4142 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4143 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4144 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4145 default:
4146 memprintf(err,
4147 "internal error, unexpected rule->from=%d, please report this bug!",
4148 rule->from);
4149 goto error;
4150 }
4151
4152 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4153 * <px> */
4154 list_for_each_entry(fconf, &px->filter_configs, list) {
4155 conf = fconf->conf;
4156
4157 /* This is not an SPOE filter */
4158 if (fconf->id != spoe_filter_id)
4159 continue;
4160
4161 /* This is the good engine */
4162 if (!strcmp(conf->id, engine_id)) {
4163 agent = conf->agent;
4164 break;
4165 }
4166 }
4167 if (agent == NULL) {
4168 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4169 engine_id, group_id);
4170 goto error;
4171 }
4172
4173 /* Try to find the right group */
4174 list_for_each_entry(group, &agent->groups, list) {
4175 /* This is the good group */
4176 if (!strcmp(group->id, group_id))
4177 break;
4178 }
4179 if (&group->list == &agent->groups) {
4180 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4181 group_id, engine_id);
4182 goto error;
4183 }
4184
4185 /* Ok, we found the group, we need to check messages and their
4186 * arguments */
4187 list_for_each_entry(msg, &group->messages, by_grp) {
4188 struct spoe_arg *arg;
4189
4190 list_for_each_entry(arg, &msg->args, list) {
4191 if (!(arg->expr->fetch->val & where)) {
4192 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4193 "some args extract information from '%s',"
4194 "none of which is available here ('%s')",
4195 msg->id, group->id, msg->conf.file, msg->conf.line,
4196 sample_ckp_names(arg->expr->fetch->use),
4197 sample_ckp_names(where));
4198 goto error;
4199 }
4200 }
4201 }
4202
4203 free(engine_id);
4204 free(group_id);
4205 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4206 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4207 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4208 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4209 return 1;
4210
4211 error:
4212 free(engine_id);
4213 free(group_id);
4214 return 0;
4215}
4216
4217/* Parse 'send-spoe-group' action following the format:
4218 *
4219 * ... send-spoe-group <engine-id> <group-id>
4220 *
4221 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4222 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4223 * ids are saved and used later, when the rule will be checked.
4224 */
4225static enum act_parse_ret
4226parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4227 struct act_rule *rule, char **err)
4228{
4229 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4230 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4231 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4232 return ACT_RET_PRS_ERR;
4233 }
4234 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4235 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4236
4237 (*orig_arg) += 2;
4238
4239 rule->action = ACT_CUSTOM;
4240 rule->action_ptr = spoe_send_group;
4241 rule->check_ptr = check_send_spoe_group;
4242 return ACT_RET_PRS_OK;
4243}
4244
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004245
4246/* Declare the filter parser for "spoe" keyword */
4247static struct flt_kw_list flt_kws = { "SPOE", { }, {
4248 { "spoe", parse_spoe_flt, NULL },
4249 { NULL, NULL, NULL },
4250 }
4251};
4252
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004253/* Delcate the action parser for "spoe-action" keyword */
4254static struct action_kw_list tcp_req_action_kws = { { }, {
4255 { "send-spoe-group", parse_send_spoe_group },
4256 { /* END */ },
4257 }
4258};
4259static struct action_kw_list tcp_res_action_kws = { { }, {
4260 { "send-spoe-group", parse_send_spoe_group },
4261 { /* END */ },
4262 }
4263};
4264static struct action_kw_list http_req_action_kws = { { }, {
4265 { "send-spoe-group", parse_send_spoe_group },
4266 { /* END */ },
4267 }
4268};
4269static struct action_kw_list http_res_action_kws = { { }, {
4270 { "send-spoe-group", parse_send_spoe_group },
4271 { /* END */ },
4272 }
4273};
4274
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004275__attribute__((constructor))
4276static void __spoe_init(void)
4277{
4278 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004279 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4280 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4281 http_req_keywords_register(&http_req_action_kws);
4282 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004283
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004284 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004285 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004286}
4287
4288__attribute__((destructor))
4289static void
4290__spoe_deinit(void)
4291{
4292 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004293 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004294}