blob: b8bb6ce36e8e34e2e6c5c2b5d7e4baa09dc224f5 [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;
1942 if ((SPOE_APPCTX(appctx)->task = task_new()) == NULL)
1943 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 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
1979 LIST_ADDQ(&conf->agent->applets, &SPOE_APPCTX(appctx)->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001980 conf->agent->applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001981
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 Faulet8ef75252017-02-20 22:56:03 +01002177/* Encode SPOE messages for a specific event. Info in <ctx->frag_ctx>, if any,
2178 * are used to handle fragmented content. On success it returns 1. If an error
Christopher Faulet57583e42017-09-04 15:41:09 +02002179 * occurred, -1 is returned. If nothing has been encoded, it returns 0 (this is
2180 * only possible 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 Fauletf51f5fa2017-01-19 10:01:12 +01002183 struct list *messages, int dir)
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 Fauletf51f5fa2017-01-19 10:01:12 +01002193 /* Resume encoding of a SPOE message */
2194 if (ctx->frag_ctx.curmsg != NULL) {
2195 msg = ctx->frag_ctx.curmsg;
2196 goto encode_message;
2197 }
2198
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002199 /* Loop on messages */
Christopher Faulet11610f32017-09-21 10:23:10 +02002200 list_for_each_entry(msg, messages, by_evt) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002201 ctx->frag_ctx.curmsg = msg;
2202 ctx->frag_ctx.curarg = NULL;
2203 ctx->frag_ctx.curoff = UINT_MAX;
2204
2205 encode_message:
Christopher Faulet10e37672017-09-21 16:38:22 +02002206 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002207 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002208 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002209
Christopher Faulet57583e42017-09-04 15:41:09 +02002210 /* nothing has been encoded for an unfragmented payload */
2211 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2212 goto skip;
2213
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002214 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002215 " - encode %s messages - spoe_appctx=%p"
2216 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002217 (int)now.tv_sec, (int)now.tv_usec,
2218 agent->id, __FUNCTION__, s,
2219 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002220 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2221 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002222
Christopher Faulet8ef75252017-02-20 22:56:03 +01002223 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002224 ctx->frag_ctx.curmsg = NULL;
2225 ctx->frag_ctx.curarg = NULL;
2226 ctx->frag_ctx.curoff = 0;
2227 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002228
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002229 return 1;
2230
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002231 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002232 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2233 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2234 return -1;
2235 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002236
2237 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002238 " - encode fragmented messages - spoe_appctx=%p"
2239 " - curmsg=%p - curarg=%p - curoff=%u"
2240 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002241 (int)now.tv_sec, (int)now.tv_usec,
2242 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2243 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002244 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245
Christopher Faulet8ef75252017-02-20 22:56:03 +01002246 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002247 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2248 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2249 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002250
2251 skip:
2252 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2253 " - skip the frame because nothing has been encoded\n",
2254 (int)now.tv_sec, (int)now.tv_usec,
2255 agent->id, __FUNCTION__, s);
2256 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002257}
2258
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259
2260/***************************************************************************
2261 * Functions that handle SPOE actions
2262 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002263/* Helper function to set a variable */
2264static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002266 struct sample *smp)
2267{
2268 struct spoe_config *conf = FLT_CONF(ctx->filter);
2269 struct spoe_agent *agent = conf->agent;
2270 char varname[64];
2271
2272 memset(varname, 0, sizeof(varname));
2273 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2274 scope, agent->var_pfx, len, name);
2275 vars_set_by_name_ifexist(varname, len, smp);
2276}
2277
2278/* Helper function to unset a variable */
2279static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002280spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002281 struct sample *smp)
2282{
2283 struct spoe_config *conf = FLT_CONF(ctx->filter);
2284 struct spoe_agent *agent = conf->agent;
2285 char varname[64];
2286
2287 memset(varname, 0, sizeof(varname));
2288 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2289 scope, agent->var_pfx, len, name);
2290 vars_unset_by_name_ifexist(varname, len, smp);
2291}
2292
2293
Christopher Faulet8ef75252017-02-20 22:56:03 +01002294static inline int
2295spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2296 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002297{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002298 char *str, *scope, *p = *buf;
2299 struct sample smp;
2300 uint64_t sz;
2301 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002302
Christopher Faulet8ef75252017-02-20 22:56:03 +01002303 if (p + 2 >= end)
2304 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002305
Christopher Faulet8ef75252017-02-20 22:56:03 +01002306 /* SET-VAR requires 3 arguments */
2307 if (*p++ != 3)
2308 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002309
Christopher Faulet8ef75252017-02-20 22:56:03 +01002310 switch (*p++) {
2311 case SPOE_SCOPE_PROC: scope = "proc"; break;
2312 case SPOE_SCOPE_SESS: scope = "sess"; break;
2313 case SPOE_SCOPE_TXN : scope = "txn"; break;
2314 case SPOE_SCOPE_REQ : scope = "req"; break;
2315 case SPOE_SCOPE_RES : scope = "res"; break;
2316 default: goto skip;
2317 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002318
Christopher Faulet8ef75252017-02-20 22:56:03 +01002319 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2320 goto skip;
2321 memset(&smp, 0, sizeof(smp));
2322 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324 if (spoe_decode_data(&p, end, &smp) == -1)
2325 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326
Christopher Faulet8ef75252017-02-20 22:56:03 +01002327 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2328 " - set-var '%s.%s.%.*s'\n",
2329 (int)now.tv_sec, (int)now.tv_usec,
2330 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2331 __FUNCTION__, s, scope,
2332 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2333 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002334
Christopher Faulet8ef75252017-02-20 22:56:03 +01002335 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002336
Christopher Faulet8ef75252017-02-20 22:56:03 +01002337 ret = (p - *buf);
2338 *buf = p;
2339 return ret;
2340 skip:
2341 return 0;
2342}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344static inline int
2345spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2346 char **buf, char *end, int dir)
2347{
2348 char *str, *scope, *p = *buf;
2349 struct sample smp;
2350 uint64_t sz;
2351 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002352
Christopher Faulet8ef75252017-02-20 22:56:03 +01002353 if (p + 2 >= end)
2354 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 /* UNSET-VAR requires 2 arguments */
2357 if (*p++ != 2)
2358 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360 switch (*p++) {
2361 case SPOE_SCOPE_PROC: scope = "proc"; break;
2362 case SPOE_SCOPE_SESS: scope = "sess"; break;
2363 case SPOE_SCOPE_TXN : scope = "txn"; break;
2364 case SPOE_SCOPE_REQ : scope = "req"; break;
2365 case SPOE_SCOPE_RES : scope = "res"; break;
2366 default: goto skip;
2367 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368
Christopher Faulet8ef75252017-02-20 22:56:03 +01002369 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2370 goto skip;
2371 memset(&smp, 0, sizeof(smp));
2372 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2375 " - unset-var '%s.%s.%.*s'\n",
2376 (int)now.tv_sec, (int)now.tv_usec,
2377 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2378 __FUNCTION__, s, scope,
2379 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2380 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002381
Christopher Faulet8ef75252017-02-20 22:56:03 +01002382 spoe_unset_var(ctx, scope, str, sz, &smp);
2383
2384 ret = (p - *buf);
2385 *buf = p;
2386 return ret;
2387 skip:
2388 return 0;
2389}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002390
Christopher Faulet8ef75252017-02-20 22:56:03 +01002391/* Process SPOE actions for a specific event. It returns 1 on success. If an
2392 * error occurred, 0 is returned. */
2393static int
2394spoe_process_actions(struct stream *s, struct spoe_context *ctx,
2395 enum spoe_event ev, int dir)
2396{
2397 char *p, *end;
2398 int ret;
2399
2400 p = ctx->buffer->p;
2401 end = p + ctx->buffer->i;
2402
2403 while (p < end) {
2404 enum spoe_action_type type;
2405
2406 type = *p++;
2407 switch (type) {
2408 case SPOE_ACT_T_SET_VAR:
2409 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2410 if (!ret)
2411 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002413
Christopher Faulet8ef75252017-02-20 22:56:03 +01002414 case SPOE_ACT_T_UNSET_VAR:
2415 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2416 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 break;
2419
2420 default:
2421 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422 }
2423 }
2424
2425 return 1;
2426 skip:
2427 return 0;
2428}
2429
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002430/***************************************************************************
2431 * Functions that process SPOE events
2432 **************************************************************************/
2433static inline int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434spoe_start_event_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002435{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002436 /* If a process is already started for this SPOE context, retry
2437 * later. */
2438 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002439 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002440
2441 /* Set the right flag to prevent request and response processing
2442 * in same time. */
2443 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2444 ? SPOE_CTX_FL_REQ_PROCESS
2445 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002446 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002447}
2448
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002449static inline void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002450spoe_stop_event_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002451{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002452 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2453
2454 if (sa) {
2455 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002456 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002457 }
2458
Christopher Fauleta1cda022016-12-21 08:58:06 +01002459 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002460 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002461
Christopher Fauletb067b062017-01-04 16:39:11 +01002462 ctx->status_code = 0;
2463
Christopher Fauleta1cda022016-12-21 08:58:06 +01002464 /* Reset processing timer */
2465 ctx->process_exp = TICK_ETERNITY;
2466
Christopher Faulet8ef75252017-02-20 22:56:03 +01002467 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002468
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002469 ctx->frag_ctx.spoe_appctx = NULL;
2470 ctx->frag_ctx.curmsg = NULL;
2471 ctx->frag_ctx.curarg = NULL;
2472 ctx->frag_ctx.curoff = 0;
2473 ctx->frag_ctx.flags = 0;
2474
Christopher Fauleta1cda022016-12-21 08:58:06 +01002475 if (!LIST_ISEMPTY(&ctx->list)) {
2476 LIST_DEL(&ctx->list);
2477 LIST_INIT(&ctx->list);
2478 }
2479}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002480
2481/* Process a SPOE event. First, this functions will process messages attached to
2482 * this event and send them to an agent in a NOTIFY frame. Then, it will wait a
2483 * ACK frame to process corresponding actions. During all the processing, it
2484 * returns 0 and it returns 1 when the processing is finished. If an error
2485 * occurred, -1 is returned. */
2486static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002487spoe_process_event(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002488 enum spoe_event ev)
2489{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002490 struct spoe_config *conf = FLT_CONF(ctx->filter);
2491 struct spoe_agent *agent = conf->agent;
2492 int dir, ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002493
2494 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2495 " - ctx-state=%s - event=%s\n",
2496 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletf7a30922016-11-10 15:04:51 +01002497 agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002498 spoe_event_str[ev]);
2499
2500 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2501
Christopher Faulet11610f32017-09-21 10:23:10 +02002502 if (LIST_ISEMPTY(&(ctx->events[ev])))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002503 goto out;
2504
2505 if (ctx->state == SPOE_CTX_ST_ERROR)
2506 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002507
2508 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2509 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2510 " - failed to process event '%s': timeout\n",
2511 (int)now.tv_sec, (int)now.tv_usec,
2512 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
Christopher Fauletb067b062017-01-04 16:39:11 +01002513 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002514 goto error;
2515 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002516
2517 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002518 if (agent->eps_max > 0) {
2519 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2520 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2521 " - skip event '%s': max EPS reached\n",
2522 (int)now.tv_sec, (int)now.tv_usec,
2523 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
2524 goto skip;
2525 }
2526 }
2527
Christopher Fauletf7a30922016-11-10 15:04:51 +01002528 if (!tick_isset(ctx->process_exp)) {
2529 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2530 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2531 ctx->process_exp);
2532 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01002533 ret = spoe_start_event_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002534 if (!ret)
2535 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002536
Christopher Faulet8ef75252017-02-20 22:56:03 +01002537 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002538 goto error;
2539
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002540 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002541 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002542 }
2543
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002544 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002545 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002546 goto out;
Christopher Faulet11610f32017-09-21 10:23:10 +02002547 ret = spoe_encode_messages(s, ctx, &(ctx->events[ev]), dir);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002548 if (ret < 0)
2549 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002550 if (!ret)
2551 goto skip;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002552 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2553 }
2554
2555 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2556 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002557 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002558 ret = 0;
2559 goto out;
2560 }
2561
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002562 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2563 ret = 0;
2564 goto out;
2565 }
2566
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002567 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002568 spoe_process_actions(s, ctx, ev, dir);
2569 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002570 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002571 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002572 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002573 }
2574
2575 out:
2576 return ret;
2577
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002578 error:
Christopher Faulet48026722016-11-16 15:01:12 +01002579 if (agent->eps_max > 0)
2580 update_freq_ctr(&agent->err_per_sec, 1);
2581
Christopher Faulet985532d2016-11-16 15:36:19 +01002582 if (agent->var_on_error) {
2583 struct sample smp;
2584
2585 memset(&smp, 0, sizeof(smp));
2586 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletb067b062017-01-04 16:39:11 +01002587 smp.data.u.sint = ctx->status_code;
Christopher Faulet985532d2016-11-16 15:36:19 +01002588 smp.data.type = SMP_T_BOOL;
2589
Christopher Faulet8ef75252017-02-20 22:56:03 +01002590 spoe_set_var(ctx, "txn", agent->var_on_error,
Christopher Faulet985532d2016-11-16 15:36:19 +01002591 strlen(agent->var_on_error), &smp);
2592 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002593 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2594 " - failed to create process event '%s': code=%u\n",
2595 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2596 __FUNCTION__, ctx->strm, spoe_event_str[ev],
2597 ctx->status_code);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002598 send_log(ctx->strm->be, LOG_WARNING,
2599 "SPOE: [%s] failed to process event '%s': code=%u\n",
2600 agent->id, spoe_event_str[ev], ctx->status_code);
Christopher Faulet985532d2016-11-16 15:36:19 +01002601
Christopher Fauletea62c2a2016-11-14 10:54:21 +01002602 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2603 ? SPOE_CTX_ST_READY
Christopher Fauletb067b062017-01-04 16:39:11 +01002604 : SPOE_CTX_ST_NONE);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 ret = 1;
2606 goto end;
2607
2608 skip:
2609 ctx->state = SPOE_CTX_ST_READY;
2610 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002611
Christopher Fauleta1cda022016-12-21 08:58:06 +01002612 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002613 spoe_stop_event_processing(ctx);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002614 return ret;
2615}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002616
2617/***************************************************************************
2618 * Functions that create/destroy SPOE contexts
2619 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002620static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002621spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002622{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002623 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002624 return 1;
2625
Christopher Faulet4596fb72017-01-11 14:05:19 +01002626 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2627 LIST_DEL(&buffer_wait->list);
2628 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002629 }
2630
Christopher Faulet4596fb72017-01-11 14:05:19 +01002631 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002632 return 1;
2633
Christopher Faulet4596fb72017-01-11 14:05:19 +01002634 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635 return 0;
2636}
2637
2638static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002639spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002641 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2642 LIST_DEL(&buffer_wait->list);
2643 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002644 }
2645
2646 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002647 if (*buf != &buf_empty) {
2648 b_free(buf);
2649 offer_buffers(buffer_wait->target,
2650 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002651 }
2652}
2653
Christopher Faulet4596fb72017-01-11 14:05:19 +01002654static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002655spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002656{
2657 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2658 return 1;
2659}
2660
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002661static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002662spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002663{
2664 struct spoe_config *conf = FLT_CONF(filter);
2665 struct spoe_context *ctx;
2666
2667 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2668 if (ctx == NULL) {
2669 return NULL;
2670 }
2671 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002672 ctx->filter = filter;
2673 ctx->state = SPOE_CTX_ST_NONE;
2674 ctx->status_code = SPOE_CTX_ERR_NONE;
2675 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002676 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002677 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002678 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002679 LIST_INIT(&ctx->buffer_wait.list);
2680 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002681 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002682 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002683
Christopher Fauletf7a30922016-11-10 15:04:51 +01002684 ctx->stream_id = 0;
2685 ctx->frame_id = 1;
2686 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002687
2688 return ctx;
2689}
2690
2691static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002692spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693{
2694 if (!ctx)
2695 return;
2696
Christopher Faulet8ef75252017-02-20 22:56:03 +01002697 spoe_stop_event_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002698 pool_free2(pool2_spoe_ctx, ctx);
2699}
2700
2701static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002702spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002703{
2704 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002705 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002706}
2707
2708
2709/***************************************************************************
2710 * Hooks that manage the filter lifecycle (init/check/deinit)
2711 **************************************************************************/
2712/* Signal handler: Do a soft stop, wakeup SPOE applet */
2713static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002714spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002715{
2716 struct proxy *p;
2717
2718 p = proxy;
2719 while (p) {
2720 struct flt_conf *fconf;
2721
2722 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002723 struct spoe_config *conf;
2724 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002725 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002726
Christopher Faulet3b386a32017-02-23 10:17:15 +01002727 if (fconf->id != spoe_filter_id)
2728 continue;
2729
2730 conf = fconf->conf;
2731 agent = conf->agent;
2732
Christopher Faulet42bfa462017-01-04 14:14:19 +01002733 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002734 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002735 }
2736 }
2737 p = p->next;
2738 }
2739}
2740
2741
2742/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2743static int
2744spoe_init(struct proxy *px, struct flt_conf *fconf)
2745{
2746 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002747
2748 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2749 init_new_proxy(&conf->agent_fe);
2750 conf->agent_fe.parent = conf->agent;
2751 conf->agent_fe.last_change = now.tv_sec;
2752 conf->agent_fe.id = conf->agent->id;
2753 conf->agent_fe.cap = PR_CAP_FE;
2754 conf->agent_fe.mode = PR_MODE_TCP;
2755 conf->agent_fe.maxconn = 0;
2756 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2757 conf->agent_fe.conn_retries = CONN_RETRIES;
2758 conf->agent_fe.accept = frontend_accept;
2759 conf->agent_fe.srv = NULL;
2760 conf->agent_fe.timeout.client = TICK_ETERNITY;
2761 conf->agent_fe.default_target = &spoe_applet.obj_type;
2762 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2763
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002764 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002765 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002766 sighandler_registered = 1;
2767 }
2768
2769 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002770}
2771
2772/* Free ressources allocated by the SPOE filter. */
2773static void
2774spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2775{
2776 struct spoe_config *conf = fconf->conf;
2777
2778 if (conf) {
2779 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002780
Christopher Faulet8ef75252017-02-20 22:56:03 +01002781 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002782 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002783 free(conf);
2784 }
2785 fconf->conf = NULL;
2786}
2787
2788/* Check configuration of a SPOE filter for a specified proxy.
2789 * Return 1 on error, else 0. */
2790static int
2791spoe_check(struct proxy *px, struct flt_conf *fconf)
2792{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002793 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002794 struct spoe_config *conf = fconf->conf;
2795 struct proxy *target;
2796
Christopher Faulet7ee86672017-09-19 11:08:28 +02002797 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2798 * are uniq */
2799 list_for_each_entry(f, &px->filter_configs, list) {
2800 struct spoe_config *c = f->conf;
2801
2802 /* This is not an SPOE filter */
2803 if (f->id != spoe_filter_id)
2804 continue;
2805 /* This is the current SPOE filter */
2806 if (f == fconf)
2807 continue;
2808
2809 /* Check engine Id. It should be uniq */
2810 if (!strcmp(conf->id, c->id)) {
2811 Alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2812 px->id, conf->id);
2813 return 1;
2814 }
2815 }
2816
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002817 target = proxy_be_by_name(conf->agent->b.name);
2818 if (target == NULL) {
2819 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2820 " declared at %s:%d.\n",
2821 px->id, conf->agent->b.name, conf->agent->id,
2822 conf->agent->conf.file, conf->agent->conf.line);
2823 return 1;
2824 }
2825 if (target->mode != PR_MODE_TCP) {
2826 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2827 " at %s:%d does not support HTTP mode.\n",
2828 px->id, target->id, conf->agent->id,
2829 conf->agent->conf.file, conf->agent->conf.line);
2830 return 1;
2831 }
2832
2833 free(conf->agent->b.name);
2834 conf->agent->b.name = NULL;
2835 conf->agent->b.be = target;
2836 return 0;
2837}
2838
2839/**************************************************************************
2840 * Hooks attached to a stream
2841 *************************************************************************/
2842/* Called when a filter instance is created and attach to a stream. It creates
2843 * the context that will be used to process this stream. */
2844static int
2845spoe_start(struct stream *s, struct filter *filter)
2846{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002847 struct spoe_config *conf = FLT_CONF(filter);
2848 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002849 struct spoe_context *ctx;
2850
2851 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002852 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002853 __FUNCTION__, s);
2854
Christopher Faulet8ef75252017-02-20 22:56:03 +01002855 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002856 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002857 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2858 " - failed to create SPOE context\n",
2859 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002860 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002862 "SPOE: [%s] failed to create SPOE context\n",
2863 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002864 return 0;
2865 }
2866
2867 ctx->strm = s;
2868 ctx->state = SPOE_CTX_ST_READY;
2869 filter->ctx = ctx;
2870
Christopher Faulet11610f32017-09-21 10:23:10 +02002871 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002872 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2873
Christopher Faulet11610f32017-09-21 10:23:10 +02002874 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002875 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2876
Christopher Faulet11610f32017-09-21 10:23:10 +02002877 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002878 filter->pre_analyzers |= AN_RES_INSPECT;
2879
Christopher Faulet11610f32017-09-21 10:23:10 +02002880 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002881 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2882
Christopher Faulet11610f32017-09-21 10:23:10 +02002883 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002884 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2885
Christopher Faulet11610f32017-09-21 10:23:10 +02002886 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002887 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2888
2889 return 1;
2890}
2891
2892/* Called when a filter instance is detached from a stream. It release the
2893 * attached SPOE context. */
2894static void
2895spoe_stop(struct stream *s, struct filter *filter)
2896{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002897 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2898 (int)now.tv_sec, (int)now.tv_usec,
2899 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2900 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002901 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902}
2903
Christopher Fauletf7a30922016-11-10 15:04:51 +01002904
2905/*
2906 * Called when the stream is woken up because of expired timer.
2907 */
2908static void
2909spoe_check_timeouts(struct stream *s, struct filter *filter)
2910{
2911 struct spoe_context *ctx = filter->ctx;
2912
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002913 if (tick_is_expired(ctx->process_exp, now_ms)) {
2914 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002915 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002916 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002917}
2918
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919/* Called when we are ready to filter data on a channel */
2920static int
2921spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2922{
2923 struct spoe_context *ctx = filter->ctx;
2924 int ret = 1;
2925
2926 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2927 " - ctx-flags=0x%08x\n",
2928 (int)now.tv_sec, (int)now.tv_usec,
2929 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2930 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2931
Christopher Fauletb067b062017-01-04 16:39:11 +01002932 if (ctx->state == SPOE_CTX_ST_NONE)
2933 goto out;
2934
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935 if (!(chn->flags & CF_ISRESP)) {
2936 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2937 chn->analysers |= AN_REQ_INSPECT_FE;
2938 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2939 chn->analysers |= AN_REQ_INSPECT_BE;
2940
2941 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2942 goto out;
2943
2944 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002945 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01002946 if (!ret)
2947 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002948 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
2949 }
2950 else {
2951 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
2952 chn->analysers |= AN_RES_INSPECT;
2953
2954 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
2955 goto out;
2956
Christopher Faulet8ef75252017-02-20 22:56:03 +01002957 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002958 if (!ret) {
2959 channel_dont_read(chn);
2960 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01002961 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002962 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002963 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964 }
2965
2966 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967 return ret;
2968}
2969
2970/* Called before a processing happens on a given channel */
2971static int
2972spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
2973 struct channel *chn, unsigned an_bit)
2974{
2975 struct spoe_context *ctx = filter->ctx;
2976 int ret = 1;
2977
2978 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2979 " - ctx-flags=0x%08x - ana=0x%08x\n",
2980 (int)now.tv_sec, (int)now.tv_usec,
2981 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2982 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2983 ctx->flags, an_bit);
2984
Christopher Fauletb067b062017-01-04 16:39:11 +01002985 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986 goto out;
2987
2988 switch (an_bit) {
2989 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002990 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002991 break;
2992 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002993 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002994 break;
2995 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002996 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997 break;
2998 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002999 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003000 break;
3001 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003002 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003003 break;
3004 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003005 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003006 break;
3007 }
3008
3009 out:
3010 if (!ret) {
3011 channel_dont_read(chn);
3012 channel_dont_close(chn);
3013 }
3014 return ret;
3015}
3016
3017/* Called when the filtering on the channel ends. */
3018static int
3019spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3020{
3021 struct spoe_context *ctx = filter->ctx;
3022
3023 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3024 " - ctx-flags=0x%08x\n",
3025 (int)now.tv_sec, (int)now.tv_usec,
3026 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3027 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3028
3029 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003030 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003031 }
3032
3033 return 1;
3034}
3035
3036/********************************************************************
3037 * Functions that manage the filter initialization
3038 ********************************************************************/
3039struct flt_ops spoe_ops = {
3040 /* Manage SPOE filter, called for each filter declaration */
3041 .init = spoe_init,
3042 .deinit = spoe_deinit,
3043 .check = spoe_check,
3044
3045 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003046 .attach = spoe_start,
3047 .detach = spoe_stop,
3048 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003049
3050 /* Handle channels activity */
3051 .channel_start_analyze = spoe_start_analyze,
3052 .channel_pre_analyze = spoe_chn_pre_analyze,
3053 .channel_end_analyze = spoe_end_analyze,
3054};
3055
3056
3057static int
3058cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3059{
3060 const char *err;
3061 int i, err_code = 0;
3062
3063 if ((cfg_scope == NULL && curengine != NULL) ||
3064 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003065 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003066 goto out;
3067
3068 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3069 if (!*args[1]) {
3070 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3071 file, linenum);
3072 err_code |= ERR_ALERT | ERR_ABORT;
3073 goto out;
3074 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003075 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3076 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003077 goto out;
3078 }
3079
3080 err = invalid_char(args[1]);
3081 if (err) {
3082 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3083 file, linenum, *err, args[0], args[1]);
3084 err_code |= ERR_ALERT | ERR_ABORT;
3085 goto out;
3086 }
3087
3088 if (curagent != NULL) {
3089 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3090 file, linenum);
3091 err_code |= ERR_ALERT | ERR_ABORT;
3092 goto out;
3093 }
3094 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
3095 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3096 err_code |= ERR_ALERT | ERR_ABORT;
3097 goto out;
3098 }
3099
3100 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003101
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003102 curagent->conf.file = strdup(file);
3103 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003104
3105 curagent->timeout.hello = TICK_ETERNITY;
3106 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003107 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003108
3109 curagent->engine_id = NULL;
3110 curagent->var_pfx = NULL;
3111 curagent->var_on_error = NULL;
Christopher Fauletcecd8522017-02-24 22:11:21 +01003112 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003113 curagent->cps_max = 0;
3114 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003115 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003116 curagent->min_applets = 0;
3117 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003118
3119 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003120 LIST_INIT(&curagent->events[i]);
3121 LIST_INIT(&curagent->groups);
3122 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003123
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003124 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003125 curagent->applets_act = 0;
3126 curagent->applets_idle = 0;
3127 curagent->sending_rate = 0;
3128
3129 LIST_INIT(&curagent->applets);
3130 LIST_INIT(&curagent->sending_queue);
3131 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003132 }
3133 else if (!strcmp(args[0], "use-backend")) {
3134 if (!*args[1]) {
3135 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3136 file, linenum, args[0]);
3137 err_code |= ERR_ALERT | ERR_FATAL;
3138 goto out;
3139 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003140 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003141 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142 free(curagent->b.name);
3143 curagent->b.name = strdup(args[1]);
3144 }
3145 else if (!strcmp(args[0], "messages")) {
3146 int cur_arg = 1;
3147 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003148 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003149
Christopher Faulet11610f32017-09-21 10:23:10 +02003150 list_for_each_entry(ph, &curmphs, list) {
3151 if (!strcmp(ph->id, args[cur_arg])) {
3152 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003153 file, linenum, args[cur_arg]);
3154 err_code |= ERR_ALERT | ERR_FATAL;
3155 goto out;
3156 }
3157 }
3158
Christopher Faulet11610f32017-09-21 10:23:10 +02003159 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003160 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3161 err_code |= ERR_ALERT | ERR_ABORT;
3162 goto out;
3163 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003164 ph->id = strdup(args[cur_arg]);
3165 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003166 cur_arg++;
3167 }
3168 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003169 else if (!strcmp(args[0], "groups")) {
3170 int cur_arg = 1;
3171 while (*args[cur_arg]) {
3172 struct spoe_placeholder *ph = NULL;
3173
3174 list_for_each_entry(ph, &curgphs, list) {
3175 if (!strcmp(ph->id, args[cur_arg])) {
3176 Alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3177 file, linenum, args[cur_arg]);
3178 err_code |= ERR_ALERT | ERR_FATAL;
3179 goto out;
3180 }
3181 }
3182
3183 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3184 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3185 err_code |= ERR_ALERT | ERR_ABORT;
3186 goto out;
3187 }
3188 ph->id = strdup(args[cur_arg]);
3189 LIST_ADDQ(&curgphs, &ph->list);
3190 cur_arg++;
3191 }
3192 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003193 else if (!strcmp(args[0], "timeout")) {
3194 unsigned int *tv = NULL;
3195 const char *res;
3196 unsigned timeout;
3197
3198 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003199 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003200 file, linenum);
3201 err_code |= ERR_ALERT | ERR_FATAL;
3202 goto out;
3203 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003204 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3205 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003206 if (!strcmp(args[1], "hello"))
3207 tv = &curagent->timeout.hello;
3208 else if (!strcmp(args[1], "idle"))
3209 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003210 else if (!strcmp(args[1], "processing"))
3211 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003212 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003213 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 file, linenum, args[1]);
3215 err_code |= ERR_ALERT | ERR_FATAL;
3216 goto out;
3217 }
3218 if (!*args[2]) {
3219 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3220 file, linenum, args[1]);
3221 err_code |= ERR_ALERT | ERR_FATAL;
3222 goto out;
3223 }
3224 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3225 if (res) {
3226 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3227 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003228 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 goto out;
3230 }
3231 *tv = MS_TO_TICKS(timeout);
3232 }
3233 else if (!strcmp(args[0], "option")) {
3234 if (!*args[1]) {
3235 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3236 file, linenum, args[0]);
3237 err_code |= ERR_ALERT | ERR_FATAL;
3238 goto out;
3239 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003240
Christopher Faulet305c6072017-02-23 16:17:53 +01003241 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003242 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003243 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003244 if (kwm == 1)
3245 curagent->flags &= ~SPOE_FL_PIPELINING;
3246 else
3247 curagent->flags |= SPOE_FL_PIPELINING;
3248 goto out;
3249 }
3250 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003251 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003252 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003253 if (kwm == 1)
3254 curagent->flags &= ~SPOE_FL_ASYNC;
3255 else
3256 curagent->flags |= SPOE_FL_ASYNC;
3257 goto out;
3258 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003259 else if (!strcmp(args[1], "send-frag-payload")) {
3260 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3261 goto out;
3262 if (kwm == 1)
3263 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3264 else
3265 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3266 goto out;
3267 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003268
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003269 /* Following options does not support negation */
3270 if (kwm == 1) {
3271 Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3272 file, linenum, args[1]);
3273 err_code |= ERR_ALERT | ERR_FATAL;
3274 goto out;
3275 }
3276
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003277 if (!strcmp(args[1], "var-prefix")) {
3278 char *tmp;
3279
3280 if (!*args[2]) {
3281 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3282 file, linenum, args[0],
3283 args[1]);
3284 err_code |= ERR_ALERT | ERR_FATAL;
3285 goto out;
3286 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003287 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3288 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 tmp = args[2];
3290 while (*tmp) {
3291 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3292 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3293 file, linenum, args[0], args[1]);
3294 err_code |= ERR_ALERT | ERR_FATAL;
3295 goto out;
3296 }
3297 tmp++;
3298 }
3299 curagent->var_pfx = strdup(args[2]);
3300 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003301 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003302 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003303 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003304 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3305 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003306 else if (!strcmp(args[1], "set-on-error")) {
3307 char *tmp;
3308
3309 if (!*args[2]) {
3310 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3311 file, linenum, args[0],
3312 args[1]);
3313 err_code |= ERR_ALERT | ERR_FATAL;
3314 goto out;
3315 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003316 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3317 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003318 tmp = args[2];
3319 while (*tmp) {
3320 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3321 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3322 file, linenum, args[0], args[1]);
3323 err_code |= ERR_ALERT | ERR_FATAL;
3324 goto out;
3325 }
3326 tmp++;
3327 }
3328 curagent->var_on_error = strdup(args[2]);
3329 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003330 else {
3331 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3332 file, linenum, args[1]);
3333 err_code |= ERR_ALERT | ERR_FATAL;
3334 goto out;
3335 }
Christopher Faulet48026722016-11-16 15:01:12 +01003336 }
3337 else if (!strcmp(args[0], "maxconnrate")) {
3338 if (!*args[1]) {
3339 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3340 file, linenum, args[0]);
3341 err_code |= ERR_ALERT | ERR_FATAL;
3342 goto out;
3343 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003344 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003345 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003346 curagent->cps_max = atol(args[1]);
3347 }
3348 else if (!strcmp(args[0], "maxerrrate")) {
3349 if (!*args[1]) {
3350 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3351 file, linenum, args[0]);
3352 err_code |= ERR_ALERT | ERR_FATAL;
3353 goto out;
3354 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003355 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003356 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003357 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003358 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003359 else if (!strcmp(args[0], "max-frame-size")) {
3360 if (!*args[1]) {
3361 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3362 file, linenum, args[0]);
3363 err_code |= ERR_ALERT | ERR_FATAL;
3364 goto out;
3365 }
3366 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3367 goto out;
3368 curagent->max_frame_size = atol(args[1]);
3369 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3370 curagent->max_frame_size > MAX_FRAME_SIZE) {
3371 Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3372 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
3373 err_code |= ERR_ALERT | ERR_FATAL;
3374 goto out;
3375 }
3376 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003377 else if (*args[0]) {
3378 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3379 file, linenum, args[0]);
3380 err_code |= ERR_ALERT | ERR_FATAL;
3381 goto out;
3382 }
3383 out:
3384 return err_code;
3385}
Christopher Faulet11610f32017-09-21 10:23:10 +02003386static int
3387cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3388{
3389 struct spoe_group *grp;
3390 const char *err;
3391 int err_code = 0;
3392
3393 if ((cfg_scope == NULL && curengine != NULL) ||
3394 (cfg_scope != NULL && curengine == NULL) ||
3395 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3396 goto out;
3397
3398 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3399 if (!*args[1]) {
3400 Alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3401 file, linenum);
3402 err_code |= ERR_ALERT | ERR_ABORT;
3403 goto out;
3404 }
3405 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3406 err_code |= ERR_ABORT;
3407 goto out;
3408 }
3409
3410 err = invalid_char(args[1]);
3411 if (err) {
3412 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3413 file, linenum, *err, args[0], args[1]);
3414 err_code |= ERR_ALERT | ERR_ABORT;
3415 goto out;
3416 }
3417
3418 list_for_each_entry(grp, &curgrps, list) {
3419 if (!strcmp(grp->id, args[1])) {
3420 Alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3421 " name as another one declared at %s:%d.\n",
3422 file, linenum, args[1], grp->conf.file, grp->conf.line);
3423 err_code |= ERR_ALERT | ERR_FATAL;
3424 goto out;
3425 }
3426 }
3427
3428 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
3429 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3430 err_code |= ERR_ALERT | ERR_ABORT;
3431 goto out;
3432 }
3433
3434 curgrp->id = strdup(args[1]);
3435 curgrp->conf.file = strdup(file);
3436 curgrp->conf.line = linenum;
3437 LIST_INIT(&curgrp->phs);
3438 LIST_INIT(&curgrp->messages);
3439 LIST_ADDQ(&curgrps, &curgrp->list);
3440 }
3441 else if (!strcmp(args[0], "messages")) {
3442 int cur_arg = 1;
3443 while (*args[cur_arg]) {
3444 struct spoe_placeholder *ph = NULL;
3445
3446 list_for_each_entry(ph, &curgrp->phs, list) {
3447 if (!strcmp(ph->id, args[cur_arg])) {
3448 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3449 file, linenum, args[cur_arg]);
3450 err_code |= ERR_ALERT | ERR_FATAL;
3451 goto out;
3452 }
3453 }
3454
3455 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3456 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3457 err_code |= ERR_ALERT | ERR_ABORT;
3458 goto out;
3459 }
3460 ph->id = strdup(args[cur_arg]);
3461 LIST_ADDQ(&curgrp->phs, &ph->list);
3462 cur_arg++;
3463 }
3464 }
3465 else if (*args[0]) {
3466 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3467 file, linenum, args[0]);
3468 err_code |= ERR_ALERT | ERR_FATAL;
3469 goto out;
3470 }
3471 out:
3472 return err_code;
3473}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003474
3475static int
3476cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3477{
3478 struct spoe_message *msg;
3479 struct spoe_arg *arg;
3480 const char *err;
3481 char *errmsg = NULL;
3482 int err_code = 0;
3483
3484 if ((cfg_scope == NULL && curengine != NULL) ||
3485 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003486 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003487 goto out;
3488
3489 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3490 if (!*args[1]) {
3491 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3492 file, linenum);
3493 err_code |= ERR_ALERT | ERR_ABORT;
3494 goto out;
3495 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003496 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3497 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003498 goto out;
3499 }
3500
3501 err = invalid_char(args[1]);
3502 if (err) {
3503 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3504 file, linenum, *err, args[0], args[1]);
3505 err_code |= ERR_ALERT | ERR_ABORT;
3506 goto out;
3507 }
3508
3509 list_for_each_entry(msg, &curmsgs, list) {
3510 if (!strcmp(msg->id, args[1])) {
3511 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3512 " name as another one declared at %s:%d.\n",
3513 file, linenum, args[1], msg->conf.file, msg->conf.line);
3514 err_code |= ERR_ALERT | ERR_FATAL;
3515 goto out;
3516 }
3517 }
3518
3519 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3520 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3521 err_code |= ERR_ALERT | ERR_ABORT;
3522 goto out;
3523 }
3524
3525 curmsg->id = strdup(args[1]);
3526 curmsg->id_len = strlen(curmsg->id);
3527 curmsg->event = SPOE_EV_NONE;
3528 curmsg->conf.file = strdup(file);
3529 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003530 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003531 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003532 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003533 LIST_INIT(&curmsg->by_evt);
3534 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003535 LIST_ADDQ(&curmsgs, &curmsg->list);
3536 }
3537 else if (!strcmp(args[0], "args")) {
3538 int cur_arg = 1;
3539
3540 curproxy->conf.args.ctx = ARGC_SPOE;
3541 curproxy->conf.args.file = file;
3542 curproxy->conf.args.line = linenum;
3543 while (*args[cur_arg]) {
3544 char *delim = strchr(args[cur_arg], '=');
3545 int idx = 0;
3546
3547 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3548 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3549 err_code |= ERR_ALERT | ERR_ABORT;
3550 goto out;
3551 }
3552
3553 if (!delim) {
3554 arg->name = NULL;
3555 arg->name_len = 0;
3556 delim = args[cur_arg];
3557 }
3558 else {
3559 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3560 arg->name_len = delim - args[cur_arg];
3561 delim++;
3562 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003563 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3564 &idx, file, linenum, &errmsg,
3565 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003566 if (arg->expr == NULL) {
3567 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3568 err_code |= ERR_ALERT | ERR_FATAL;
3569 free(arg->name);
3570 free(arg);
3571 goto out;
3572 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003573 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003574 LIST_ADDQ(&curmsg->args, &arg->list);
3575 cur_arg++;
3576 }
3577 curproxy->conf.args.file = NULL;
3578 curproxy->conf.args.line = 0;
3579 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003580 else if (!strcmp(args[0], "acl")) {
3581 err = invalid_char(args[1]);
3582 if (err) {
3583 Alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3584 file, linenum, *err, args[1]);
3585 err_code |= ERR_ALERT | ERR_FATAL;
3586 goto out;
3587 }
3588 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
3589 Alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3590 file, linenum, args[1], errmsg);
3591 err_code |= ERR_ALERT | ERR_FATAL;
3592 goto out;
3593 }
3594 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003595 else if (!strcmp(args[0], "event")) {
3596 if (!*args[1]) {
3597 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003598 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003599 goto out;
3600 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003601 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3602 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003603
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003604 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3605 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3606 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3607 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3608
3609 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3610 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3611 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3612 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3613 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3614 curmsg->event = SPOE_EV_ON_TCP_RSP;
3615
3616 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3617 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3618 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3619 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3620 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3621 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3622 else {
3623 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3624 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003625 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003626 goto out;
3627 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003628
3629 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3630 struct acl_cond *cond;
3631
3632 cond = build_acl_cond(file, linenum, &curmsg->acls,
3633 curproxy, (const char **)args+2,
3634 &errmsg);
3635 if (cond == NULL) {
3636 Alert("parsing [%s:%d] : error detected while "
3637 "parsing an 'event %s' condition : %s.\n",
3638 file, linenum, args[1], errmsg);
3639 err_code |= ERR_ALERT | ERR_FATAL;
3640 goto out;
3641 }
3642 curmsg->cond = cond;
3643 }
3644 else if (*args[2]) {
3645 Alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3646 "or 'unless' followed by a condition but found '%s'.\n",
3647 file, linenum, args[1], args[2]);
3648 err_code |= ERR_ALERT | ERR_FATAL;
3649 goto out;
3650 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003651 }
3652 else if (!*args[0]) {
3653 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3654 file, linenum, args[0]);
3655 err_code |= ERR_ALERT | ERR_FATAL;
3656 goto out;
3657 }
3658 out:
3659 free(errmsg);
3660 return err_code;
3661}
3662
3663/* Return -1 on error, else 0 */
3664static int
3665parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3666 struct flt_conf *fconf, char **err, void *private)
3667{
3668 struct list backup_sections;
3669 struct spoe_config *conf;
3670 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003671 struct spoe_group *grp, *grpback;
3672 struct spoe_placeholder *ph, *phback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003673 char *file = NULL, *engine = NULL;
3674 int ret, pos = *cur_arg + 1;
3675
3676 conf = calloc(1, sizeof(*conf));
3677 if (conf == NULL) {
3678 memprintf(err, "%s: out of memory", args[*cur_arg]);
3679 goto error;
3680 }
3681 conf->proxy = px;
3682
3683 while (*args[pos]) {
3684 if (!strcmp(args[pos], "config")) {
3685 if (!*args[pos+1]) {
3686 memprintf(err, "'%s' : '%s' option without value",
3687 args[*cur_arg], args[pos]);
3688 goto error;
3689 }
3690 file = args[pos+1];
3691 pos += 2;
3692 }
3693 else if (!strcmp(args[pos], "engine")) {
3694 if (!*args[pos+1]) {
3695 memprintf(err, "'%s' : '%s' option without value",
3696 args[*cur_arg], args[pos]);
3697 goto error;
3698 }
3699 engine = args[pos+1];
3700 pos += 2;
3701 }
3702 else {
3703 memprintf(err, "unknown keyword '%s'", args[pos]);
3704 goto error;
3705 }
3706 }
3707 if (file == NULL) {
3708 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3709 goto error;
3710 }
3711
3712 /* backup sections and register SPOE sections */
3713 LIST_INIT(&backup_sections);
3714 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003715 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3716 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003717 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003718
3719 /* Parse SPOE filter configuration file */
3720 curengine = engine;
3721 curproxy = px;
3722 curagent = NULL;
3723 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003724 LIST_INIT(&curmsgs);
3725 LIST_INIT(&curgrps);
3726 LIST_INIT(&curmphs);
3727 LIST_INIT(&curgphs);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003728 ret = readcfgfile(file);
3729 curproxy = NULL;
3730
3731 /* unregister SPOE sections and restore previous sections */
3732 cfg_unregister_sections();
3733 cfg_restore_sections(&backup_sections);
3734
3735 if (ret == -1) {
3736 memprintf(err, "Could not open configuration file %s : %s",
3737 file, strerror(errno));
3738 goto error;
3739 }
3740 if (ret & (ERR_ABORT|ERR_FATAL)) {
3741 memprintf(err, "Error(s) found in configuration file %s", file);
3742 goto error;
3743 }
3744
3745 /* Check SPOE agent */
3746 if (curagent == NULL) {
3747 memprintf(err, "No SPOE agent found in file %s", file);
3748 goto error;
3749 }
3750 if (curagent->b.name == NULL) {
3751 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3752 curagent->id, curagent->conf.file, curagent->conf.line);
3753 goto error;
3754 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003755 if (curagent->timeout.hello == TICK_ETERNITY ||
3756 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003757 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003758 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3759 " | While not properly invalid, you will certainly encounter various problems\n"
3760 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003761 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003762 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3763 }
3764 if (curagent->var_pfx == NULL) {
3765 char *tmp = curagent->id;
3766
3767 while (*tmp) {
3768 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3769 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3770 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3771 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3772 goto error;
3773 }
3774 tmp++;
3775 }
3776 curagent->var_pfx = strdup(curagent->id);
3777 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003778 if (curagent->engine_id == NULL)
3779 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003780
Christopher Faulet11610f32017-09-21 10:23:10 +02003781 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
3782 Warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003783 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3784 goto finish;
3785 }
3786
Christopher Faulet11610f32017-09-21 10:23:10 +02003787 /* Replace placeholders by the corresponding messages for the SPOE
3788 * agent */
3789 list_for_each_entry(ph, &curmphs, list) {
3790 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003791 struct spoe_arg *arg;
3792 unsigned int where;
3793
Christopher Faulet11610f32017-09-21 10:23:10 +02003794 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003795 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3796 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3797 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3798 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3799 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3800 }
3801 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3802 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3803 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3804 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3805 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003806 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003807 }
3808 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003809 Warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3810 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003811 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003812 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003813
3814 where = 0;
3815 switch (msg->event) {
3816 case SPOE_EV_ON_CLIENT_SESS:
3817 where |= SMP_VAL_FE_CON_ACC;
3818 break;
3819
3820 case SPOE_EV_ON_TCP_REQ_FE:
3821 where |= SMP_VAL_FE_REQ_CNT;
3822 break;
3823
3824 case SPOE_EV_ON_HTTP_REQ_FE:
3825 where |= SMP_VAL_FE_HRQ_HDR;
3826 break;
3827
3828 case SPOE_EV_ON_TCP_REQ_BE:
3829 if (px->cap & PR_CAP_FE)
3830 where |= SMP_VAL_FE_REQ_CNT;
3831 if (px->cap & PR_CAP_BE)
3832 where |= SMP_VAL_BE_REQ_CNT;
3833 break;
3834
3835 case SPOE_EV_ON_HTTP_REQ_BE:
3836 if (px->cap & PR_CAP_FE)
3837 where |= SMP_VAL_FE_HRQ_HDR;
3838 if (px->cap & PR_CAP_BE)
3839 where |= SMP_VAL_BE_HRQ_HDR;
3840 break;
3841
3842 case SPOE_EV_ON_SERVER_SESS:
3843 where |= SMP_VAL_BE_SRV_CON;
3844 break;
3845
3846 case SPOE_EV_ON_TCP_RSP:
3847 if (px->cap & PR_CAP_FE)
3848 where |= SMP_VAL_FE_RES_CNT;
3849 if (px->cap & PR_CAP_BE)
3850 where |= SMP_VAL_BE_RES_CNT;
3851 break;
3852
3853 case SPOE_EV_ON_HTTP_RSP:
3854 if (px->cap & PR_CAP_FE)
3855 where |= SMP_VAL_FE_HRS_HDR;
3856 if (px->cap & PR_CAP_BE)
3857 where |= SMP_VAL_BE_HRS_HDR;
3858 break;
3859
3860 default:
3861 break;
3862 }
3863
3864 list_for_each_entry(arg, &msg->args, list) {
3865 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003866 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003867 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003868 "none of which is available here ('%s')",
3869 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003870 sample_ckp_names(arg->expr->fetch->use),
3871 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003872 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003873 }
3874 }
3875
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003876 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003877 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3878 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003879 }
3880 }
3881 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003882 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003883 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003884 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003885 continue;
3886 }
3887
Christopher Faulet11610f32017-09-21 10:23:10 +02003888 /* Replace placeholders by the corresponding groups for the SPOE
3889 * agent */
3890 list_for_each_entry(ph, &curgphs, list) {
3891 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3892 if (!strcmp(grp->id, ph->id)) {
3893 grp->agent = curagent;
3894 LIST_DEL(&grp->list);
3895 LIST_ADDQ(&curagent->groups, &grp->list);
3896 goto next_aph;
3897 }
3898 }
3899 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
3900 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
3901 goto error;
3902 next_aph:
3903 continue;
3904 }
3905
3906 /* Replace placeholders by the corresponding message for each SPOE
3907 * group of the SPOE agent */
3908 list_for_each_entry(grp, &curagent->groups, list) {
3909 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
3910 list_for_each_entry(msg, &curmsgs, list) {
3911 if (!strcmp(msg->id, ph->id)) {
3912 if (msg->group != NULL) {
3913 memprintf(err, "SPOE message '%s' already belongs to "
3914 "the SPOE group '%s' declare at %s:%d",
3915 msg->id, msg->group->id,
3916 msg->group->conf.file,
3917 msg->group->conf.line);
3918 goto error;
3919 }
3920
3921 /* Scope for arguments are not checked for now. We will check
3922 * them only if a rule use the corresponding SPOE group. */
3923 msg->agent = curagent;
3924 msg->group = grp;
3925 LIST_DEL(&ph->list);
3926 LIST_ADDQ(&grp->messages, &msg->by_grp);
3927 goto next_mph_grp;
3928 }
3929 }
3930 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
3931 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
3932 goto error;
3933 next_mph_grp:
3934 continue;
3935 }
3936 }
3937
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003938 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02003939 /* move curmsgs to the agent message list */
3940 curmsgs.n->p = &curagent->messages;
3941 curmsgs.p->n = &curagent->messages;
3942 curagent->messages = curmsgs;
3943 LIST_INIT(&curmsgs);
3944
Christopher Faulet7ee86672017-09-19 11:08:28 +02003945 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003946 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003947 list_for_each_entry_safe(ph, phback, &curmphs, list) {
3948 LIST_DEL(&ph->list);
3949 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003950 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003951 list_for_each_entry_safe(ph, phback, &curgphs, list) {
3952 LIST_DEL(&ph->list);
3953 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003954 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003955 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3956 LIST_DEL(&grp->list);
3957 spoe_release_group(grp);
3958 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003959 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01003960 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003961 fconf->ops = &spoe_ops;
3962 fconf->conf = conf;
3963 return 0;
3964
3965 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003966 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02003967 list_for_each_entry_safe(ph, phback, &curmphs, list) {
3968 LIST_DEL(&ph->list);
3969 spoe_release_placeholder(ph);
3970 }
3971 list_for_each_entry_safe(ph, phback, &curgphs, list) {
3972 LIST_DEL(&ph->list);
3973 spoe_release_placeholder(ph);
3974 }
3975 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3976 LIST_DEL(&grp->list);
3977 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003978 }
3979 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
3980 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01003981 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003982 }
3983 free(conf);
3984 return -1;
3985}
3986
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003987/* Send a SPOE group. TODO */
3988static enum act_return
3989spoe_send_group(struct act_rule *rule, struct proxy *px,
3990 struct session *sess, struct stream *s, int flags)
3991{
3992 struct filter *filter;
3993 struct spoe_agent *agent = NULL;
3994 struct spoe_group *group = NULL;
3995 struct spoe_context *ctx = NULL;
3996 int ret, dir;
3997
3998 list_for_each_entry(filter, &s->strm_flt.filters, list) {
3999 if (filter->config == rule->arg.act.p[0]) {
4000 agent = rule->arg.act.p[2];
4001 group = rule->arg.act.p[3];
4002 ctx = filter->ctx;
4003 break;
4004 }
4005 }
4006 if (agent == NULL || group == NULL || ctx == NULL)
4007 return ACT_RET_ERR;
4008
4009 /* TODO */
4010 return ACT_RET_CONT;
4011}
4012
4013/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4014 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4015 * action should be:
4016 *
4017 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4018 *
4019 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4020 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4021 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4022 * group.
4023 *
4024 * The function returns 1 in success case, otherwise, it returns 0 and err is
4025 * filled.
4026 */
4027static int
4028check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4029{
4030 struct flt_conf *fconf;
4031 struct spoe_config *conf;
4032 struct spoe_agent *agent = NULL;
4033 struct spoe_group *group;
4034 struct spoe_message *msg;
4035 char *engine_id = rule->arg.act.p[0];
4036 char *group_id = rule->arg.act.p[1];
4037 unsigned int where = 0;
4038
4039 switch (rule->from) {
4040 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4041 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4042 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4043 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4044 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4045 default:
4046 memprintf(err,
4047 "internal error, unexpected rule->from=%d, please report this bug!",
4048 rule->from);
4049 goto error;
4050 }
4051
4052 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4053 * <px> */
4054 list_for_each_entry(fconf, &px->filter_configs, list) {
4055 conf = fconf->conf;
4056
4057 /* This is not an SPOE filter */
4058 if (fconf->id != spoe_filter_id)
4059 continue;
4060
4061 /* This is the good engine */
4062 if (!strcmp(conf->id, engine_id)) {
4063 agent = conf->agent;
4064 break;
4065 }
4066 }
4067 if (agent == NULL) {
4068 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4069 engine_id, group_id);
4070 goto error;
4071 }
4072
4073 /* Try to find the right group */
4074 list_for_each_entry(group, &agent->groups, list) {
4075 /* This is the good group */
4076 if (!strcmp(group->id, group_id))
4077 break;
4078 }
4079 if (&group->list == &agent->groups) {
4080 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4081 group_id, engine_id);
4082 goto error;
4083 }
4084
4085 /* Ok, we found the group, we need to check messages and their
4086 * arguments */
4087 list_for_each_entry(msg, &group->messages, by_grp) {
4088 struct spoe_arg *arg;
4089
4090 list_for_each_entry(arg, &msg->args, list) {
4091 if (!(arg->expr->fetch->val & where)) {
4092 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4093 "some args extract information from '%s',"
4094 "none of which is available here ('%s')",
4095 msg->id, group->id, msg->conf.file, msg->conf.line,
4096 sample_ckp_names(arg->expr->fetch->use),
4097 sample_ckp_names(where));
4098 goto error;
4099 }
4100 }
4101 }
4102
4103 free(engine_id);
4104 free(group_id);
4105 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4106 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4107 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4108 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4109 return 1;
4110
4111 error:
4112 free(engine_id);
4113 free(group_id);
4114 return 0;
4115}
4116
4117/* Parse 'send-spoe-group' action following the format:
4118 *
4119 * ... send-spoe-group <engine-id> <group-id>
4120 *
4121 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4122 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4123 * ids are saved and used later, when the rule will be checked.
4124 */
4125static enum act_parse_ret
4126parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4127 struct act_rule *rule, char **err)
4128{
4129 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4130 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4131 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4132 return ACT_RET_PRS_ERR;
4133 }
4134 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4135 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4136
4137 (*orig_arg) += 2;
4138
4139 rule->action = ACT_CUSTOM;
4140 rule->action_ptr = spoe_send_group;
4141 rule->check_ptr = check_send_spoe_group;
4142 return ACT_RET_PRS_OK;
4143}
4144
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004145
4146/* Declare the filter parser for "spoe" keyword */
4147static struct flt_kw_list flt_kws = { "SPOE", { }, {
4148 { "spoe", parse_spoe_flt, NULL },
4149 { NULL, NULL, NULL },
4150 }
4151};
4152
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004153/* Delcate the action parser for "spoe-action" keyword */
4154static struct action_kw_list tcp_req_action_kws = { { }, {
4155 { "send-spoe-group", parse_send_spoe_group },
4156 { /* END */ },
4157 }
4158};
4159static struct action_kw_list tcp_res_action_kws = { { }, {
4160 { "send-spoe-group", parse_send_spoe_group },
4161 { /* END */ },
4162 }
4163};
4164static struct action_kw_list http_req_action_kws = { { }, {
4165 { "send-spoe-group", parse_send_spoe_group },
4166 { /* END */ },
4167 }
4168};
4169static struct action_kw_list http_res_action_kws = { { }, {
4170 { "send-spoe-group", parse_send_spoe_group },
4171 { /* END */ },
4172 }
4173};
4174
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004175__attribute__((constructor))
4176static void __spoe_init(void)
4177{
4178 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004179 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4180 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4181 http_req_keywords_register(&http_req_action_kws);
4182 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004183
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004184 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004185 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004186}
4187
4188__attribute__((destructor))
4189static void
4190__spoe_deinit(void)
4191{
4192 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004193 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004194}