blob: d5539c98c2faaeed938586a70c172ae98214f86c [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 Fauletc718b822017-09-21 16:50:56 +02002177/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2178 * handle fragmented content. On success it returns 1. If an error occurred, -1
2179 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2180 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002181static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002182spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002183 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002184{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002185 struct spoe_config *conf = FLT_CONF(ctx->filter);
2186 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002187 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002188 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002189
Christopher Faulet8ef75252017-02-20 22:56:03 +01002190 p = ctx->buffer->p;
2191 end = p + agent->frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002192
Christopher Fauletc718b822017-09-21 16:50:56 +02002193 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2194 /* Resume encoding of a SPOE message */
2195 if (ctx->frag_ctx.curmsg != NULL) {
2196 msg = ctx->frag_ctx.curmsg;
2197 goto encode_evt_message;
2198 }
2199
2200 list_for_each_entry(msg, messages, by_evt) {
2201 ctx->frag_ctx.curmsg = msg;
2202 ctx->frag_ctx.curarg = NULL;
2203 ctx->frag_ctx.curoff = UINT_MAX;
2204
2205 encode_evt_message:
2206 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2207 goto too_big;
2208 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002209 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002210 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2211 /* Resume encoding of a SPOE message */
2212 if (ctx->frag_ctx.curmsg != NULL) {
2213 msg = ctx->frag_ctx.curmsg;
2214 goto encode_grp_message;
2215 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002216
Christopher Fauletc718b822017-09-21 16:50:56 +02002217 list_for_each_entry(msg, messages, by_grp) {
2218 ctx->frag_ctx.curmsg = msg;
2219 ctx->frag_ctx.curarg = NULL;
2220 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002221
Christopher Fauletc718b822017-09-21 16:50:56 +02002222 encode_grp_message:
2223 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2224 goto too_big;
2225 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002226 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002227 else
2228 goto skip;
2229
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002230
Christopher Faulet57583e42017-09-04 15:41:09 +02002231 /* nothing has been encoded for an unfragmented payload */
2232 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2233 goto skip;
2234
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002236 " - encode %s messages - spoe_appctx=%p"
2237 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002238 (int)now.tv_sec, (int)now.tv_usec,
2239 agent->id, __FUNCTION__, s,
2240 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002241 ctx->frag_ctx.spoe_appctx, (agent->frame_size - FRAME_HDR_SIZE),
2242 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002243
Christopher Faulet8ef75252017-02-20 22:56:03 +01002244 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245 ctx->frag_ctx.curmsg = NULL;
2246 ctx->frag_ctx.curarg = NULL;
2247 ctx->frag_ctx.curoff = 0;
2248 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002249
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002250 return 1;
2251
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002252 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002253 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2254 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2255 return -1;
2256 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
2258 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002259 " - encode fragmented messages - spoe_appctx=%p"
2260 " - curmsg=%p - curarg=%p - curoff=%u"
2261 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002262 (int)now.tv_sec, (int)now.tv_usec,
2263 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2264 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265 (agent->frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002266
Christopher Faulet8ef75252017-02-20 22:56:03 +01002267 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002268 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2269 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2270 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002271
2272 skip:
2273 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2274 " - skip the frame because nothing has been encoded\n",
2275 (int)now.tv_sec, (int)now.tv_usec,
2276 agent->id, __FUNCTION__, s);
2277 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002278}
2279
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280
2281/***************************************************************************
2282 * Functions that handle SPOE actions
2283 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002284/* Helper function to set a variable */
2285static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002287 struct sample *smp)
2288{
2289 struct spoe_config *conf = FLT_CONF(ctx->filter);
2290 struct spoe_agent *agent = conf->agent;
2291 char varname[64];
2292
2293 memset(varname, 0, sizeof(varname));
2294 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2295 scope, agent->var_pfx, len, name);
2296 vars_set_by_name_ifexist(varname, len, smp);
2297}
2298
2299/* Helper function to unset a variable */
2300static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002301spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002302 struct sample *smp)
2303{
2304 struct spoe_config *conf = FLT_CONF(ctx->filter);
2305 struct spoe_agent *agent = conf->agent;
2306 char varname[64];
2307
2308 memset(varname, 0, sizeof(varname));
2309 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2310 scope, agent->var_pfx, len, name);
2311 vars_unset_by_name_ifexist(varname, len, smp);
2312}
2313
2314
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315static inline int
2316spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2317 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002318{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002319 char *str, *scope, *p = *buf;
2320 struct sample smp;
2321 uint64_t sz;
2322 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324 if (p + 2 >= end)
2325 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326
Christopher Faulet8ef75252017-02-20 22:56:03 +01002327 /* SET-VAR requires 3 arguments */
2328 if (*p++ != 3)
2329 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002330
Christopher Faulet8ef75252017-02-20 22:56:03 +01002331 switch (*p++) {
2332 case SPOE_SCOPE_PROC: scope = "proc"; break;
2333 case SPOE_SCOPE_SESS: scope = "sess"; break;
2334 case SPOE_SCOPE_TXN : scope = "txn"; break;
2335 case SPOE_SCOPE_REQ : scope = "req"; break;
2336 case SPOE_SCOPE_RES : scope = "res"; break;
2337 default: goto skip;
2338 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002339
Christopher Faulet8ef75252017-02-20 22:56:03 +01002340 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2341 goto skip;
2342 memset(&smp, 0, sizeof(smp));
2343 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344
Christopher Faulet8ef75252017-02-20 22:56:03 +01002345 if (spoe_decode_data(&p, end, &smp) == -1)
2346 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2349 " - set-var '%s.%s.%.*s'\n",
2350 (int)now.tv_sec, (int)now.tv_usec,
2351 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2352 __FUNCTION__, s, scope,
2353 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2354 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358 ret = (p - *buf);
2359 *buf = p;
2360 return ret;
2361 skip:
2362 return 0;
2363}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365static inline int
2366spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2367 char **buf, char *end, int dir)
2368{
2369 char *str, *scope, *p = *buf;
2370 struct sample smp;
2371 uint64_t sz;
2372 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374 if (p + 2 >= end)
2375 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002376
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377 /* UNSET-VAR requires 2 arguments */
2378 if (*p++ != 2)
2379 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 switch (*p++) {
2382 case SPOE_SCOPE_PROC: scope = "proc"; break;
2383 case SPOE_SCOPE_SESS: scope = "sess"; break;
2384 case SPOE_SCOPE_TXN : scope = "txn"; break;
2385 case SPOE_SCOPE_REQ : scope = "req"; break;
2386 case SPOE_SCOPE_RES : scope = "res"; break;
2387 default: goto skip;
2388 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2391 goto skip;
2392 memset(&smp, 0, sizeof(smp));
2393 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2396 " - unset-var '%s.%s.%.*s'\n",
2397 (int)now.tv_sec, (int)now.tv_usec,
2398 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2399 __FUNCTION__, s, scope,
2400 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2401 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 spoe_unset_var(ctx, scope, str, sz, &smp);
2404
2405 ret = (p - *buf);
2406 *buf = p;
2407 return ret;
2408 skip:
2409 return 0;
2410}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002411
Christopher Faulet8ef75252017-02-20 22:56:03 +01002412/* Process SPOE actions for a specific event. It returns 1 on success. If an
2413 * error occurred, 0 is returned. */
2414static int
2415spoe_process_actions(struct stream *s, struct spoe_context *ctx,
2416 enum spoe_event ev, int dir)
2417{
2418 char *p, *end;
2419 int ret;
2420
2421 p = ctx->buffer->p;
2422 end = p + ctx->buffer->i;
2423
2424 while (p < end) {
2425 enum spoe_action_type type;
2426
2427 type = *p++;
2428 switch (type) {
2429 case SPOE_ACT_T_SET_VAR:
2430 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2431 if (!ret)
2432 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002433 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002434
Christopher Faulet8ef75252017-02-20 22:56:03 +01002435 case SPOE_ACT_T_UNSET_VAR:
2436 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2437 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002438 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002439 break;
2440
2441 default:
2442 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002443 }
2444 }
2445
2446 return 1;
2447 skip:
2448 return 0;
2449}
2450
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002451/***************************************************************************
2452 * Functions that process SPOE events
2453 **************************************************************************/
2454static inline int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002455spoe_start_event_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002456{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002457 /* If a process is already started for this SPOE context, retry
2458 * later. */
2459 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002460 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002461
2462 /* Set the right flag to prevent request and response processing
2463 * in same time. */
2464 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2465 ? SPOE_CTX_FL_REQ_PROCESS
2466 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002467 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002468}
2469
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002470static inline void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002471spoe_stop_event_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002472{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002473 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2474
2475 if (sa) {
2476 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002477 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002478 }
2479
Christopher Fauleta1cda022016-12-21 08:58:06 +01002480 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002481 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002482
Christopher Fauletb067b062017-01-04 16:39:11 +01002483 ctx->status_code = 0;
2484
Christopher Fauleta1cda022016-12-21 08:58:06 +01002485 /* Reset processing timer */
2486 ctx->process_exp = TICK_ETERNITY;
2487
Christopher Faulet8ef75252017-02-20 22:56:03 +01002488 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002489
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002490 ctx->frag_ctx.spoe_appctx = NULL;
2491 ctx->frag_ctx.curmsg = NULL;
2492 ctx->frag_ctx.curarg = NULL;
2493 ctx->frag_ctx.curoff = 0;
2494 ctx->frag_ctx.flags = 0;
2495
Christopher Fauleta1cda022016-12-21 08:58:06 +01002496 if (!LIST_ISEMPTY(&ctx->list)) {
2497 LIST_DEL(&ctx->list);
2498 LIST_INIT(&ctx->list);
2499 }
2500}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002501
2502/* Process a SPOE event. First, this functions will process messages attached to
2503 * this event and send them to an agent in a NOTIFY frame. Then, it will wait a
2504 * ACK frame to process corresponding actions. During all the processing, it
2505 * returns 0 and it returns 1 when the processing is finished. If an error
2506 * occurred, -1 is returned. */
2507static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002508spoe_process_event(struct stream *s, struct spoe_context *ctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002509 enum spoe_event ev)
2510{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002511 struct spoe_config *conf = FLT_CONF(ctx->filter);
2512 struct spoe_agent *agent = conf->agent;
2513 int dir, ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002514
2515 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2516 " - ctx-state=%s - event=%s\n",
2517 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletf7a30922016-11-10 15:04:51 +01002518 agent->id, __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002519 spoe_event_str[ev]);
2520
2521 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2522
Christopher Faulet11610f32017-09-21 10:23:10 +02002523 if (LIST_ISEMPTY(&(ctx->events[ev])))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002524 goto out;
2525
2526 if (ctx->state == SPOE_CTX_ST_ERROR)
2527 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002528
2529 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2530 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2531 " - failed to process event '%s': timeout\n",
2532 (int)now.tv_sec, (int)now.tv_usec,
2533 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
Christopher Fauletb067b062017-01-04 16:39:11 +01002534 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002535 goto error;
2536 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002537
2538 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002539 if (agent->eps_max > 0) {
2540 if (!freq_ctr_remain(&agent->err_per_sec, agent->eps_max, 0)) {
2541 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2542 " - skip event '%s': max EPS reached\n",
2543 (int)now.tv_sec, (int)now.tv_usec,
2544 agent->id, __FUNCTION__, s, spoe_event_str[ev]);
2545 goto skip;
2546 }
2547 }
2548
Christopher Fauletf7a30922016-11-10 15:04:51 +01002549 if (!tick_isset(ctx->process_exp)) {
2550 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2551 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2552 ctx->process_exp);
2553 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01002554 ret = spoe_start_event_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002555 if (!ret)
2556 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002557
Christopher Faulet8ef75252017-02-20 22:56:03 +01002558 if (spoe_queue_context(ctx) < 0)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559 goto error;
2560
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002561 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002562 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002563 }
2564
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002565 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002566 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002567 goto out;
Christopher Fauletc718b822017-09-21 16:50:56 +02002568 ret = spoe_encode_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569 if (ret < 0)
2570 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002571 if (!ret)
2572 goto skip;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002573 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2574 }
2575
2576 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2577 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002578 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002579 ret = 0;
2580 goto out;
2581 }
2582
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002583 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2584 ret = 0;
2585 goto out;
2586 }
2587
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002588 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002589 spoe_process_actions(s, ctx, ev, dir);
2590 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002591 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002592 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002593 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002594 }
2595
2596 out:
2597 return ret;
2598
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002599 error:
Christopher Faulet48026722016-11-16 15:01:12 +01002600 if (agent->eps_max > 0)
2601 update_freq_ctr(&agent->err_per_sec, 1);
2602
Christopher Faulet985532d2016-11-16 15:36:19 +01002603 if (agent->var_on_error) {
2604 struct sample smp;
2605
2606 memset(&smp, 0, sizeof(smp));
2607 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletb067b062017-01-04 16:39:11 +01002608 smp.data.u.sint = ctx->status_code;
Christopher Faulet985532d2016-11-16 15:36:19 +01002609 smp.data.type = SMP_T_BOOL;
2610
Christopher Faulet8ef75252017-02-20 22:56:03 +01002611 spoe_set_var(ctx, "txn", agent->var_on_error,
Christopher Faulet985532d2016-11-16 15:36:19 +01002612 strlen(agent->var_on_error), &smp);
2613 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002614 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2615 " - failed to create process event '%s': code=%u\n",
2616 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2617 __FUNCTION__, ctx->strm, spoe_event_str[ev],
2618 ctx->status_code);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002619 send_log(ctx->strm->be, LOG_WARNING,
2620 "SPOE: [%s] failed to process event '%s': code=%u\n",
2621 agent->id, spoe_event_str[ev], ctx->status_code);
Christopher Faulet985532d2016-11-16 15:36:19 +01002622
Christopher Fauletea62c2a2016-11-14 10:54:21 +01002623 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2624 ? SPOE_CTX_ST_READY
Christopher Fauletb067b062017-01-04 16:39:11 +01002625 : SPOE_CTX_ST_NONE);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002626 ret = 1;
2627 goto end;
2628
2629 skip:
2630 ctx->state = SPOE_CTX_ST_READY;
2631 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002632
Christopher Fauleta1cda022016-12-21 08:58:06 +01002633 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +01002634 spoe_stop_event_processing(ctx);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635 return ret;
2636}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002637
2638/***************************************************************************
2639 * Functions that create/destroy SPOE contexts
2640 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002642spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002643{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002644 if (*buf != &buf_empty)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002645 return 1;
2646
Christopher Faulet4596fb72017-01-11 14:05:19 +01002647 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2648 LIST_DEL(&buffer_wait->list);
2649 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002650 }
2651
Christopher Faulet4596fb72017-01-11 14:05:19 +01002652 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002653 return 1;
2654
Christopher Faulet4596fb72017-01-11 14:05:19 +01002655 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002656 return 0;
2657}
2658
2659static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002660spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002662 if (!LIST_ISEMPTY(&buffer_wait->list)) {
2663 LIST_DEL(&buffer_wait->list);
2664 LIST_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665 }
2666
2667 /* Release the buffer if needed */
Christopher Faulet4596fb72017-01-11 14:05:19 +01002668 if (*buf != &buf_empty) {
2669 b_free(buf);
2670 offer_buffers(buffer_wait->target,
2671 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002672 }
2673}
2674
Christopher Faulet4596fb72017-01-11 14:05:19 +01002675static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002676spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002677{
2678 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2679 return 1;
2680}
2681
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002682static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002683spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684{
2685 struct spoe_config *conf = FLT_CONF(filter);
2686 struct spoe_context *ctx;
2687
2688 ctx = pool_alloc_dirty(pool2_spoe_ctx);
2689 if (ctx == NULL) {
2690 return NULL;
2691 }
2692 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002693 ctx->filter = filter;
2694 ctx->state = SPOE_CTX_ST_NONE;
2695 ctx->status_code = SPOE_CTX_ERR_NONE;
2696 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002697 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002698 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002699 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002700 LIST_INIT(&ctx->buffer_wait.list);
2701 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002702 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002704
Christopher Fauletf7a30922016-11-10 15:04:51 +01002705 ctx->stream_id = 0;
2706 ctx->frame_id = 1;
2707 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002708
2709 return ctx;
2710}
2711
2712static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002713spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002714{
2715 if (!ctx)
2716 return;
2717
Christopher Faulet8ef75252017-02-20 22:56:03 +01002718 spoe_stop_event_processing(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002719 pool_free2(pool2_spoe_ctx, ctx);
2720}
2721
2722static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002723spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002724{
2725 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002726 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002727}
2728
2729
2730/***************************************************************************
2731 * Hooks that manage the filter lifecycle (init/check/deinit)
2732 **************************************************************************/
2733/* Signal handler: Do a soft stop, wakeup SPOE applet */
2734static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002735spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002736{
2737 struct proxy *p;
2738
2739 p = proxy;
2740 while (p) {
2741 struct flt_conf *fconf;
2742
2743 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002744 struct spoe_config *conf;
2745 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002746 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002747
Christopher Faulet3b386a32017-02-23 10:17:15 +01002748 if (fconf->id != spoe_filter_id)
2749 continue;
2750
2751 conf = fconf->conf;
2752 agent = conf->agent;
2753
Christopher Faulet42bfa462017-01-04 14:14:19 +01002754 list_for_each_entry(spoe_appctx, &agent->applets, list) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002755 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002756 }
2757 }
2758 p = p->next;
2759 }
2760}
2761
2762
2763/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2764static int
2765spoe_init(struct proxy *px, struct flt_conf *fconf)
2766{
2767 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002768
2769 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2770 init_new_proxy(&conf->agent_fe);
2771 conf->agent_fe.parent = conf->agent;
2772 conf->agent_fe.last_change = now.tv_sec;
2773 conf->agent_fe.id = conf->agent->id;
2774 conf->agent_fe.cap = PR_CAP_FE;
2775 conf->agent_fe.mode = PR_MODE_TCP;
2776 conf->agent_fe.maxconn = 0;
2777 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2778 conf->agent_fe.conn_retries = CONN_RETRIES;
2779 conf->agent_fe.accept = frontend_accept;
2780 conf->agent_fe.srv = NULL;
2781 conf->agent_fe.timeout.client = TICK_ETERNITY;
2782 conf->agent_fe.default_target = &spoe_applet.obj_type;
2783 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2784
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002785 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002786 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002787 sighandler_registered = 1;
2788 }
2789
2790 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002791}
2792
2793/* Free ressources allocated by the SPOE filter. */
2794static void
2795spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2796{
2797 struct spoe_config *conf = fconf->conf;
2798
2799 if (conf) {
2800 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002801
Christopher Faulet8ef75252017-02-20 22:56:03 +01002802 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002803 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002804 free(conf);
2805 }
2806 fconf->conf = NULL;
2807}
2808
2809/* Check configuration of a SPOE filter for a specified proxy.
2810 * Return 1 on error, else 0. */
2811static int
2812spoe_check(struct proxy *px, struct flt_conf *fconf)
2813{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002814 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002815 struct spoe_config *conf = fconf->conf;
2816 struct proxy *target;
2817
Christopher Faulet7ee86672017-09-19 11:08:28 +02002818 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2819 * are uniq */
2820 list_for_each_entry(f, &px->filter_configs, list) {
2821 struct spoe_config *c = f->conf;
2822
2823 /* This is not an SPOE filter */
2824 if (f->id != spoe_filter_id)
2825 continue;
2826 /* This is the current SPOE filter */
2827 if (f == fconf)
2828 continue;
2829
2830 /* Check engine Id. It should be uniq */
2831 if (!strcmp(conf->id, c->id)) {
2832 Alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2833 px->id, conf->id);
2834 return 1;
2835 }
2836 }
2837
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002838 target = proxy_be_by_name(conf->agent->b.name);
2839 if (target == NULL) {
2840 Alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2841 " declared at %s:%d.\n",
2842 px->id, conf->agent->b.name, conf->agent->id,
2843 conf->agent->conf.file, conf->agent->conf.line);
2844 return 1;
2845 }
2846 if (target->mode != PR_MODE_TCP) {
2847 Alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2848 " at %s:%d does not support HTTP mode.\n",
2849 px->id, target->id, conf->agent->id,
2850 conf->agent->conf.file, conf->agent->conf.line);
2851 return 1;
2852 }
2853
2854 free(conf->agent->b.name);
2855 conf->agent->b.name = NULL;
2856 conf->agent->b.be = target;
2857 return 0;
2858}
2859
2860/**************************************************************************
2861 * Hooks attached to a stream
2862 *************************************************************************/
2863/* Called when a filter instance is created and attach to a stream. It creates
2864 * the context that will be used to process this stream. */
2865static int
2866spoe_start(struct stream *s, struct filter *filter)
2867{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002868 struct spoe_config *conf = FLT_CONF(filter);
2869 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002870 struct spoe_context *ctx;
2871
2872 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002873 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002874 __FUNCTION__, s);
2875
Christopher Faulet8ef75252017-02-20 22:56:03 +01002876 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002877 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002878 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2879 " - failed to create SPOE context\n",
2880 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002881 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002882 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002883 "SPOE: [%s] failed to create SPOE context\n",
2884 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002885 return 0;
2886 }
2887
2888 ctx->strm = s;
2889 ctx->state = SPOE_CTX_ST_READY;
2890 filter->ctx = ctx;
2891
Christopher Faulet11610f32017-09-21 10:23:10 +02002892 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002893 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2894
Christopher Faulet11610f32017-09-21 10:23:10 +02002895 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2897
Christopher Faulet11610f32017-09-21 10:23:10 +02002898 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002899 filter->pre_analyzers |= AN_RES_INSPECT;
2900
Christopher Faulet11610f32017-09-21 10:23:10 +02002901 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2903
Christopher Faulet11610f32017-09-21 10:23:10 +02002904 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002905 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2906
Christopher Faulet11610f32017-09-21 10:23:10 +02002907 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2909
2910 return 1;
2911}
2912
2913/* Called when a filter instance is detached from a stream. It release the
2914 * attached SPOE context. */
2915static void
2916spoe_stop(struct stream *s, struct filter *filter)
2917{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002918 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2919 (int)now.tv_sec, (int)now.tv_usec,
2920 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2921 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002922 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002923}
2924
Christopher Fauletf7a30922016-11-10 15:04:51 +01002925
2926/*
2927 * Called when the stream is woken up because of expired timer.
2928 */
2929static void
2930spoe_check_timeouts(struct stream *s, struct filter *filter)
2931{
2932 struct spoe_context *ctx = filter->ctx;
2933
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002934 if (tick_is_expired(ctx->process_exp, now_ms)) {
2935 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002936 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002937 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002938}
2939
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940/* Called when we are ready to filter data on a channel */
2941static int
2942spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2943{
2944 struct spoe_context *ctx = filter->ctx;
2945 int ret = 1;
2946
2947 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2948 " - ctx-flags=0x%08x\n",
2949 (int)now.tv_sec, (int)now.tv_usec,
2950 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2951 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2952
Christopher Fauletb067b062017-01-04 16:39:11 +01002953 if (ctx->state == SPOE_CTX_ST_NONE)
2954 goto out;
2955
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956 if (!(chn->flags & CF_ISRESP)) {
2957 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2958 chn->analysers |= AN_REQ_INSPECT_FE;
2959 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2960 chn->analysers |= AN_REQ_INSPECT_BE;
2961
2962 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2963 goto out;
2964
2965 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002966 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01002967 if (!ret)
2968 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002969 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
2970 }
2971 else {
2972 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
2973 chn->analysers |= AN_RES_INSPECT;
2974
2975 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
2976 goto out;
2977
Christopher Faulet8ef75252017-02-20 22:56:03 +01002978 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002979 if (!ret) {
2980 channel_dont_read(chn);
2981 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01002982 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002983 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002984 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002985 }
2986
2987 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002988 return ret;
2989}
2990
2991/* Called before a processing happens on a given channel */
2992static int
2993spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
2994 struct channel *chn, unsigned an_bit)
2995{
2996 struct spoe_context *ctx = filter->ctx;
2997 int ret = 1;
2998
2999 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3000 " - ctx-flags=0x%08x - ana=0x%08x\n",
3001 (int)now.tv_sec, (int)now.tv_usec,
3002 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3003 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3004 ctx->flags, an_bit);
3005
Christopher Fauletb067b062017-01-04 16:39:11 +01003006 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007 goto out;
3008
3009 switch (an_bit) {
3010 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003011 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003012 break;
3013 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003014 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003015 break;
3016 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003017 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003018 break;
3019 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003020 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021 break;
3022 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003023 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003024 break;
3025 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003026 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003027 break;
3028 }
3029
3030 out:
3031 if (!ret) {
3032 channel_dont_read(chn);
3033 channel_dont_close(chn);
3034 }
3035 return ret;
3036}
3037
3038/* Called when the filtering on the channel ends. */
3039static int
3040spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3041{
3042 struct spoe_context *ctx = filter->ctx;
3043
3044 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3045 " - ctx-flags=0x%08x\n",
3046 (int)now.tv_sec, (int)now.tv_usec,
3047 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3048 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3049
3050 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003051 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 }
3053
3054 return 1;
3055}
3056
3057/********************************************************************
3058 * Functions that manage the filter initialization
3059 ********************************************************************/
3060struct flt_ops spoe_ops = {
3061 /* Manage SPOE filter, called for each filter declaration */
3062 .init = spoe_init,
3063 .deinit = spoe_deinit,
3064 .check = spoe_check,
3065
3066 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003067 .attach = spoe_start,
3068 .detach = spoe_stop,
3069 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003070
3071 /* Handle channels activity */
3072 .channel_start_analyze = spoe_start_analyze,
3073 .channel_pre_analyze = spoe_chn_pre_analyze,
3074 .channel_end_analyze = spoe_end_analyze,
3075};
3076
3077
3078static int
3079cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3080{
3081 const char *err;
3082 int i, err_code = 0;
3083
3084 if ((cfg_scope == NULL && curengine != NULL) ||
3085 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003086 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003087 goto out;
3088
3089 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3090 if (!*args[1]) {
3091 Alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3092 file, linenum);
3093 err_code |= ERR_ALERT | ERR_ABORT;
3094 goto out;
3095 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003096 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3097 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003098 goto out;
3099 }
3100
3101 err = invalid_char(args[1]);
3102 if (err) {
3103 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3104 file, linenum, *err, args[0], args[1]);
3105 err_code |= ERR_ALERT | ERR_ABORT;
3106 goto out;
3107 }
3108
3109 if (curagent != NULL) {
3110 Alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3111 file, linenum);
3112 err_code |= ERR_ALERT | ERR_ABORT;
3113 goto out;
3114 }
3115 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
3116 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3117 err_code |= ERR_ALERT | ERR_ABORT;
3118 goto out;
3119 }
3120
3121 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003123 curagent->conf.file = strdup(file);
3124 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003125
3126 curagent->timeout.hello = TICK_ETERNITY;
3127 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003128 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003129
3130 curagent->engine_id = NULL;
3131 curagent->var_pfx = NULL;
3132 curagent->var_on_error = NULL;
Christopher Fauletcecd8522017-02-24 22:11:21 +01003133 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_ASYNC | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003134 curagent->cps_max = 0;
3135 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003136 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003137 curagent->min_applets = 0;
3138 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139
3140 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003141 LIST_INIT(&curagent->events[i]);
3142 LIST_INIT(&curagent->groups);
3143 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003144
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003145 curagent->frame_size = curagent->max_frame_size;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003146 curagent->applets_act = 0;
3147 curagent->applets_idle = 0;
3148 curagent->sending_rate = 0;
3149
3150 LIST_INIT(&curagent->applets);
3151 LIST_INIT(&curagent->sending_queue);
3152 LIST_INIT(&curagent->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003153 }
3154 else if (!strcmp(args[0], "use-backend")) {
3155 if (!*args[1]) {
3156 Alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3157 file, linenum, args[0]);
3158 err_code |= ERR_ALERT | ERR_FATAL;
3159 goto out;
3160 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003161 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003162 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163 free(curagent->b.name);
3164 curagent->b.name = strdup(args[1]);
3165 }
3166 else if (!strcmp(args[0], "messages")) {
3167 int cur_arg = 1;
3168 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003169 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003170
Christopher Faulet11610f32017-09-21 10:23:10 +02003171 list_for_each_entry(ph, &curmphs, list) {
3172 if (!strcmp(ph->id, args[cur_arg])) {
3173 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003174 file, linenum, args[cur_arg]);
3175 err_code |= ERR_ALERT | ERR_FATAL;
3176 goto out;
3177 }
3178 }
3179
Christopher Faulet11610f32017-09-21 10:23:10 +02003180 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003181 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3182 err_code |= ERR_ALERT | ERR_ABORT;
3183 goto out;
3184 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003185 ph->id = strdup(args[cur_arg]);
3186 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003187 cur_arg++;
3188 }
3189 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003190 else if (!strcmp(args[0], "groups")) {
3191 int cur_arg = 1;
3192 while (*args[cur_arg]) {
3193 struct spoe_placeholder *ph = NULL;
3194
3195 list_for_each_entry(ph, &curgphs, list) {
3196 if (!strcmp(ph->id, args[cur_arg])) {
3197 Alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3198 file, linenum, args[cur_arg]);
3199 err_code |= ERR_ALERT | ERR_FATAL;
3200 goto out;
3201 }
3202 }
3203
3204 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3205 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3206 err_code |= ERR_ALERT | ERR_ABORT;
3207 goto out;
3208 }
3209 ph->id = strdup(args[cur_arg]);
3210 LIST_ADDQ(&curgphs, &ph->list);
3211 cur_arg++;
3212 }
3213 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 else if (!strcmp(args[0], "timeout")) {
3215 unsigned int *tv = NULL;
3216 const char *res;
3217 unsigned timeout;
3218
3219 if (!*args[1]) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003220 Alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 file, linenum);
3222 err_code |= ERR_ALERT | ERR_FATAL;
3223 goto out;
3224 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003225 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3226 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227 if (!strcmp(args[1], "hello"))
3228 tv = &curagent->timeout.hello;
3229 else if (!strcmp(args[1], "idle"))
3230 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003231 else if (!strcmp(args[1], "processing"))
3232 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003233 else {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003234 Alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003235 file, linenum, args[1]);
3236 err_code |= ERR_ALERT | ERR_FATAL;
3237 goto out;
3238 }
3239 if (!*args[2]) {
3240 Alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3241 file, linenum, args[1]);
3242 err_code |= ERR_ALERT | ERR_FATAL;
3243 goto out;
3244 }
3245 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3246 if (res) {
3247 Alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3248 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003249 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 goto out;
3251 }
3252 *tv = MS_TO_TICKS(timeout);
3253 }
3254 else if (!strcmp(args[0], "option")) {
3255 if (!*args[1]) {
3256 Alert("parsing [%s:%d]: '%s' expects an option name.\n",
3257 file, linenum, args[0]);
3258 err_code |= ERR_ALERT | ERR_FATAL;
3259 goto out;
3260 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003261
Christopher Faulet305c6072017-02-23 16:17:53 +01003262 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003263 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003264 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003265 if (kwm == 1)
3266 curagent->flags &= ~SPOE_FL_PIPELINING;
3267 else
3268 curagent->flags |= SPOE_FL_PIPELINING;
3269 goto out;
3270 }
3271 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003272 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003273 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003274 if (kwm == 1)
3275 curagent->flags &= ~SPOE_FL_ASYNC;
3276 else
3277 curagent->flags |= SPOE_FL_ASYNC;
3278 goto out;
3279 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003280 else if (!strcmp(args[1], "send-frag-payload")) {
3281 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3282 goto out;
3283 if (kwm == 1)
3284 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3285 else
3286 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3287 goto out;
3288 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003289
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003290 /* Following options does not support negation */
3291 if (kwm == 1) {
3292 Alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3293 file, linenum, args[1]);
3294 err_code |= ERR_ALERT | ERR_FATAL;
3295 goto out;
3296 }
3297
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003298 if (!strcmp(args[1], "var-prefix")) {
3299 char *tmp;
3300
3301 if (!*args[2]) {
3302 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3303 file, linenum, args[0],
3304 args[1]);
3305 err_code |= ERR_ALERT | ERR_FATAL;
3306 goto out;
3307 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003308 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3309 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003310 tmp = args[2];
3311 while (*tmp) {
3312 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3313 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3314 file, linenum, args[0], args[1]);
3315 err_code |= ERR_ALERT | ERR_FATAL;
3316 goto out;
3317 }
3318 tmp++;
3319 }
3320 curagent->var_pfx = strdup(args[2]);
3321 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003322 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003323 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003324 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003325 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3326 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003327 else if (!strcmp(args[1], "set-on-error")) {
3328 char *tmp;
3329
3330 if (!*args[2]) {
3331 Alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3332 file, linenum, args[0],
3333 args[1]);
3334 err_code |= ERR_ALERT | ERR_FATAL;
3335 goto out;
3336 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003337 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3338 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003339 tmp = args[2];
3340 while (*tmp) {
3341 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3342 Alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3343 file, linenum, args[0], args[1]);
3344 err_code |= ERR_ALERT | ERR_FATAL;
3345 goto out;
3346 }
3347 tmp++;
3348 }
3349 curagent->var_on_error = strdup(args[2]);
3350 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003351 else {
3352 Alert("parsing [%s:%d]: option '%s' is not supported.\n",
3353 file, linenum, args[1]);
3354 err_code |= ERR_ALERT | ERR_FATAL;
3355 goto out;
3356 }
Christopher Faulet48026722016-11-16 15:01:12 +01003357 }
3358 else if (!strcmp(args[0], "maxconnrate")) {
3359 if (!*args[1]) {
3360 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3361 file, linenum, args[0]);
3362 err_code |= ERR_ALERT | ERR_FATAL;
3363 goto out;
3364 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003365 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003366 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003367 curagent->cps_max = atol(args[1]);
3368 }
3369 else if (!strcmp(args[0], "maxerrrate")) {
3370 if (!*args[1]) {
3371 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3372 file, linenum, args[0]);
3373 err_code |= ERR_ALERT | ERR_FATAL;
3374 goto out;
3375 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003376 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003377 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003378 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003380 else if (!strcmp(args[0], "max-frame-size")) {
3381 if (!*args[1]) {
3382 Alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3383 file, linenum, args[0]);
3384 err_code |= ERR_ALERT | ERR_FATAL;
3385 goto out;
3386 }
3387 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3388 goto out;
3389 curagent->max_frame_size = atol(args[1]);
3390 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3391 curagent->max_frame_size > MAX_FRAME_SIZE) {
3392 Alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3393 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
3394 err_code |= ERR_ALERT | ERR_FATAL;
3395 goto out;
3396 }
3397 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 else if (*args[0]) {
3399 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3400 file, linenum, args[0]);
3401 err_code |= ERR_ALERT | ERR_FATAL;
3402 goto out;
3403 }
3404 out:
3405 return err_code;
3406}
Christopher Faulet11610f32017-09-21 10:23:10 +02003407static int
3408cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3409{
3410 struct spoe_group *grp;
3411 const char *err;
3412 int err_code = 0;
3413
3414 if ((cfg_scope == NULL && curengine != NULL) ||
3415 (cfg_scope != NULL && curengine == NULL) ||
3416 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3417 goto out;
3418
3419 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3420 if (!*args[1]) {
3421 Alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3422 file, linenum);
3423 err_code |= ERR_ALERT | ERR_ABORT;
3424 goto out;
3425 }
3426 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3427 err_code |= ERR_ABORT;
3428 goto out;
3429 }
3430
3431 err = invalid_char(args[1]);
3432 if (err) {
3433 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3434 file, linenum, *err, args[0], args[1]);
3435 err_code |= ERR_ALERT | ERR_ABORT;
3436 goto out;
3437 }
3438
3439 list_for_each_entry(grp, &curgrps, list) {
3440 if (!strcmp(grp->id, args[1])) {
3441 Alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3442 " name as another one declared at %s:%d.\n",
3443 file, linenum, args[1], grp->conf.file, grp->conf.line);
3444 err_code |= ERR_ALERT | ERR_FATAL;
3445 goto out;
3446 }
3447 }
3448
3449 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
3450 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3451 err_code |= ERR_ALERT | ERR_ABORT;
3452 goto out;
3453 }
3454
3455 curgrp->id = strdup(args[1]);
3456 curgrp->conf.file = strdup(file);
3457 curgrp->conf.line = linenum;
3458 LIST_INIT(&curgrp->phs);
3459 LIST_INIT(&curgrp->messages);
3460 LIST_ADDQ(&curgrps, &curgrp->list);
3461 }
3462 else if (!strcmp(args[0], "messages")) {
3463 int cur_arg = 1;
3464 while (*args[cur_arg]) {
3465 struct spoe_placeholder *ph = NULL;
3466
3467 list_for_each_entry(ph, &curgrp->phs, list) {
3468 if (!strcmp(ph->id, args[cur_arg])) {
3469 Alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3470 file, linenum, args[cur_arg]);
3471 err_code |= ERR_ALERT | ERR_FATAL;
3472 goto out;
3473 }
3474 }
3475
3476 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
3477 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3478 err_code |= ERR_ALERT | ERR_ABORT;
3479 goto out;
3480 }
3481 ph->id = strdup(args[cur_arg]);
3482 LIST_ADDQ(&curgrp->phs, &ph->list);
3483 cur_arg++;
3484 }
3485 }
3486 else if (*args[0]) {
3487 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3488 file, linenum, args[0]);
3489 err_code |= ERR_ALERT | ERR_FATAL;
3490 goto out;
3491 }
3492 out:
3493 return err_code;
3494}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003495
3496static int
3497cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3498{
3499 struct spoe_message *msg;
3500 struct spoe_arg *arg;
3501 const char *err;
3502 char *errmsg = NULL;
3503 int err_code = 0;
3504
3505 if ((cfg_scope == NULL && curengine != NULL) ||
3506 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003507 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003508 goto out;
3509
3510 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3511 if (!*args[1]) {
3512 Alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3513 file, linenum);
3514 err_code |= ERR_ALERT | ERR_ABORT;
3515 goto out;
3516 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003517 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3518 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003519 goto out;
3520 }
3521
3522 err = invalid_char(args[1]);
3523 if (err) {
3524 Alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3525 file, linenum, *err, args[0], args[1]);
3526 err_code |= ERR_ALERT | ERR_ABORT;
3527 goto out;
3528 }
3529
3530 list_for_each_entry(msg, &curmsgs, list) {
3531 if (!strcmp(msg->id, args[1])) {
3532 Alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3533 " name as another one declared at %s:%d.\n",
3534 file, linenum, args[1], msg->conf.file, msg->conf.line);
3535 err_code |= ERR_ALERT | ERR_FATAL;
3536 goto out;
3537 }
3538 }
3539
3540 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
3541 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3542 err_code |= ERR_ALERT | ERR_ABORT;
3543 goto out;
3544 }
3545
3546 curmsg->id = strdup(args[1]);
3547 curmsg->id_len = strlen(curmsg->id);
3548 curmsg->event = SPOE_EV_NONE;
3549 curmsg->conf.file = strdup(file);
3550 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003551 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003552 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003553 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003554 LIST_INIT(&curmsg->by_evt);
3555 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003556 LIST_ADDQ(&curmsgs, &curmsg->list);
3557 }
3558 else if (!strcmp(args[0], "args")) {
3559 int cur_arg = 1;
3560
3561 curproxy->conf.args.ctx = ARGC_SPOE;
3562 curproxy->conf.args.file = file;
3563 curproxy->conf.args.line = linenum;
3564 while (*args[cur_arg]) {
3565 char *delim = strchr(args[cur_arg], '=');
3566 int idx = 0;
3567
3568 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
3569 Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3570 err_code |= ERR_ALERT | ERR_ABORT;
3571 goto out;
3572 }
3573
3574 if (!delim) {
3575 arg->name = NULL;
3576 arg->name_len = 0;
3577 delim = args[cur_arg];
3578 }
3579 else {
3580 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3581 arg->name_len = delim - args[cur_arg];
3582 delim++;
3583 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003584 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3585 &idx, file, linenum, &errmsg,
3586 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003587 if (arg->expr == NULL) {
3588 Alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
3589 err_code |= ERR_ALERT | ERR_FATAL;
3590 free(arg->name);
3591 free(arg);
3592 goto out;
3593 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003594 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003595 LIST_ADDQ(&curmsg->args, &arg->list);
3596 cur_arg++;
3597 }
3598 curproxy->conf.args.file = NULL;
3599 curproxy->conf.args.line = 0;
3600 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003601 else if (!strcmp(args[0], "acl")) {
3602 err = invalid_char(args[1]);
3603 if (err) {
3604 Alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3605 file, linenum, *err, args[1]);
3606 err_code |= ERR_ALERT | ERR_FATAL;
3607 goto out;
3608 }
3609 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
3610 Alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3611 file, linenum, args[1], errmsg);
3612 err_code |= ERR_ALERT | ERR_FATAL;
3613 goto out;
3614 }
3615 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003616 else if (!strcmp(args[0], "event")) {
3617 if (!*args[1]) {
3618 Alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003619 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003620 goto out;
3621 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003622 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3623 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003624
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003625 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3626 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3627 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3628 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3629
3630 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3631 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3632 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3633 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3634 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3635 curmsg->event = SPOE_EV_ON_TCP_RSP;
3636
3637 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3638 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3639 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3640 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3641 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3642 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3643 else {
3644 Alert("parsing [%s:%d] : unkown event '%s'.\n",
3645 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003646 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003647 goto out;
3648 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003649
3650 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3651 struct acl_cond *cond;
3652
3653 cond = build_acl_cond(file, linenum, &curmsg->acls,
3654 curproxy, (const char **)args+2,
3655 &errmsg);
3656 if (cond == NULL) {
3657 Alert("parsing [%s:%d] : error detected while "
3658 "parsing an 'event %s' condition : %s.\n",
3659 file, linenum, args[1], errmsg);
3660 err_code |= ERR_ALERT | ERR_FATAL;
3661 goto out;
3662 }
3663 curmsg->cond = cond;
3664 }
3665 else if (*args[2]) {
3666 Alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3667 "or 'unless' followed by a condition but found '%s'.\n",
3668 file, linenum, args[1], args[2]);
3669 err_code |= ERR_ALERT | ERR_FATAL;
3670 goto out;
3671 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003672 }
3673 else if (!*args[0]) {
3674 Alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3675 file, linenum, args[0]);
3676 err_code |= ERR_ALERT | ERR_FATAL;
3677 goto out;
3678 }
3679 out:
3680 free(errmsg);
3681 return err_code;
3682}
3683
3684/* Return -1 on error, else 0 */
3685static int
3686parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3687 struct flt_conf *fconf, char **err, void *private)
3688{
3689 struct list backup_sections;
3690 struct spoe_config *conf;
3691 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003692 struct spoe_group *grp, *grpback;
3693 struct spoe_placeholder *ph, *phback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003694 char *file = NULL, *engine = NULL;
3695 int ret, pos = *cur_arg + 1;
3696
3697 conf = calloc(1, sizeof(*conf));
3698 if (conf == NULL) {
3699 memprintf(err, "%s: out of memory", args[*cur_arg]);
3700 goto error;
3701 }
3702 conf->proxy = px;
3703
3704 while (*args[pos]) {
3705 if (!strcmp(args[pos], "config")) {
3706 if (!*args[pos+1]) {
3707 memprintf(err, "'%s' : '%s' option without value",
3708 args[*cur_arg], args[pos]);
3709 goto error;
3710 }
3711 file = args[pos+1];
3712 pos += 2;
3713 }
3714 else if (!strcmp(args[pos], "engine")) {
3715 if (!*args[pos+1]) {
3716 memprintf(err, "'%s' : '%s' option without value",
3717 args[*cur_arg], args[pos]);
3718 goto error;
3719 }
3720 engine = args[pos+1];
3721 pos += 2;
3722 }
3723 else {
3724 memprintf(err, "unknown keyword '%s'", args[pos]);
3725 goto error;
3726 }
3727 }
3728 if (file == NULL) {
3729 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3730 goto error;
3731 }
3732
3733 /* backup sections and register SPOE sections */
3734 LIST_INIT(&backup_sections);
3735 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003736 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3737 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003738 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003739
3740 /* Parse SPOE filter configuration file */
3741 curengine = engine;
3742 curproxy = px;
3743 curagent = NULL;
3744 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003745 LIST_INIT(&curmsgs);
3746 LIST_INIT(&curgrps);
3747 LIST_INIT(&curmphs);
3748 LIST_INIT(&curgphs);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003749 ret = readcfgfile(file);
3750 curproxy = NULL;
3751
3752 /* unregister SPOE sections and restore previous sections */
3753 cfg_unregister_sections();
3754 cfg_restore_sections(&backup_sections);
3755
3756 if (ret == -1) {
3757 memprintf(err, "Could not open configuration file %s : %s",
3758 file, strerror(errno));
3759 goto error;
3760 }
3761 if (ret & (ERR_ABORT|ERR_FATAL)) {
3762 memprintf(err, "Error(s) found in configuration file %s", file);
3763 goto error;
3764 }
3765
3766 /* Check SPOE agent */
3767 if (curagent == NULL) {
3768 memprintf(err, "No SPOE agent found in file %s", file);
3769 goto error;
3770 }
3771 if (curagent->b.name == NULL) {
3772 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3773 curagent->id, curagent->conf.file, curagent->conf.line);
3774 goto error;
3775 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003776 if (curagent->timeout.hello == TICK_ETERNITY ||
3777 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003778 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003779 Warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3780 " | While not properly invalid, you will certainly encounter various problems\n"
3781 " | with such a configuration. To fix this, please ensure that all following\n"
Christopher Faulet03a34492016-11-19 16:47:56 +01003782 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003783 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3784 }
3785 if (curagent->var_pfx == NULL) {
3786 char *tmp = curagent->id;
3787
3788 while (*tmp) {
3789 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3790 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3791 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3792 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3793 goto error;
3794 }
3795 tmp++;
3796 }
3797 curagent->var_pfx = strdup(curagent->id);
3798 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003799 if (curagent->engine_id == NULL)
3800 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003801
Christopher Faulet11610f32017-09-21 10:23:10 +02003802 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
3803 Warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003804 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
3805 goto finish;
3806 }
3807
Christopher Faulet11610f32017-09-21 10:23:10 +02003808 /* Replace placeholders by the corresponding messages for the SPOE
3809 * agent */
3810 list_for_each_entry(ph, &curmphs, list) {
3811 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003812 struct spoe_arg *arg;
3813 unsigned int where;
3814
Christopher Faulet11610f32017-09-21 10:23:10 +02003815 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003816 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3817 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3818 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3819 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3820 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3821 }
3822 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3823 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3824 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
3825 Warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3826 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003827 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003828 }
3829 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003830 Warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3831 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003832 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003833 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003834
3835 where = 0;
3836 switch (msg->event) {
3837 case SPOE_EV_ON_CLIENT_SESS:
3838 where |= SMP_VAL_FE_CON_ACC;
3839 break;
3840
3841 case SPOE_EV_ON_TCP_REQ_FE:
3842 where |= SMP_VAL_FE_REQ_CNT;
3843 break;
3844
3845 case SPOE_EV_ON_HTTP_REQ_FE:
3846 where |= SMP_VAL_FE_HRQ_HDR;
3847 break;
3848
3849 case SPOE_EV_ON_TCP_REQ_BE:
3850 if (px->cap & PR_CAP_FE)
3851 where |= SMP_VAL_FE_REQ_CNT;
3852 if (px->cap & PR_CAP_BE)
3853 where |= SMP_VAL_BE_REQ_CNT;
3854 break;
3855
3856 case SPOE_EV_ON_HTTP_REQ_BE:
3857 if (px->cap & PR_CAP_FE)
3858 where |= SMP_VAL_FE_HRQ_HDR;
3859 if (px->cap & PR_CAP_BE)
3860 where |= SMP_VAL_BE_HRQ_HDR;
3861 break;
3862
3863 case SPOE_EV_ON_SERVER_SESS:
3864 where |= SMP_VAL_BE_SRV_CON;
3865 break;
3866
3867 case SPOE_EV_ON_TCP_RSP:
3868 if (px->cap & PR_CAP_FE)
3869 where |= SMP_VAL_FE_RES_CNT;
3870 if (px->cap & PR_CAP_BE)
3871 where |= SMP_VAL_BE_RES_CNT;
3872 break;
3873
3874 case SPOE_EV_ON_HTTP_RSP:
3875 if (px->cap & PR_CAP_FE)
3876 where |= SMP_VAL_FE_HRS_HDR;
3877 if (px->cap & PR_CAP_BE)
3878 where |= SMP_VAL_BE_HRS_HDR;
3879 break;
3880
3881 default:
3882 break;
3883 }
3884
3885 list_for_each_entry(arg, &msg->args, list) {
3886 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003887 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003888 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003889 "none of which is available here ('%s')",
3890 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003891 sample_ckp_names(arg->expr->fetch->use),
3892 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003893 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003894 }
3895 }
3896
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003897 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003898 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3899 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003900 }
3901 }
3902 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003903 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003904 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003905 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003906 continue;
3907 }
3908
Christopher Faulet11610f32017-09-21 10:23:10 +02003909 /* Replace placeholders by the corresponding groups for the SPOE
3910 * agent */
3911 list_for_each_entry(ph, &curgphs, list) {
3912 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3913 if (!strcmp(grp->id, ph->id)) {
3914 grp->agent = curagent;
3915 LIST_DEL(&grp->list);
3916 LIST_ADDQ(&curagent->groups, &grp->list);
3917 goto next_aph;
3918 }
3919 }
3920 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
3921 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
3922 goto error;
3923 next_aph:
3924 continue;
3925 }
3926
3927 /* Replace placeholders by the corresponding message for each SPOE
3928 * group of the SPOE agent */
3929 list_for_each_entry(grp, &curagent->groups, list) {
3930 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
3931 list_for_each_entry(msg, &curmsgs, list) {
3932 if (!strcmp(msg->id, ph->id)) {
3933 if (msg->group != NULL) {
3934 memprintf(err, "SPOE message '%s' already belongs to "
3935 "the SPOE group '%s' declare at %s:%d",
3936 msg->id, msg->group->id,
3937 msg->group->conf.file,
3938 msg->group->conf.line);
3939 goto error;
3940 }
3941
3942 /* Scope for arguments are not checked for now. We will check
3943 * them only if a rule use the corresponding SPOE group. */
3944 msg->agent = curagent;
3945 msg->group = grp;
3946 LIST_DEL(&ph->list);
3947 LIST_ADDQ(&grp->messages, &msg->by_grp);
3948 goto next_mph_grp;
3949 }
3950 }
3951 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
3952 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
3953 goto error;
3954 next_mph_grp:
3955 continue;
3956 }
3957 }
3958
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003959 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02003960 /* move curmsgs to the agent message list */
3961 curmsgs.n->p = &curagent->messages;
3962 curmsgs.p->n = &curagent->messages;
3963 curagent->messages = curmsgs;
3964 LIST_INIT(&curmsgs);
3965
Christopher Faulet7ee86672017-09-19 11:08:28 +02003966 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003967 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003968 list_for_each_entry_safe(ph, phback, &curmphs, list) {
3969 LIST_DEL(&ph->list);
3970 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003971 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003972 list_for_each_entry_safe(ph, phback, &curgphs, list) {
3973 LIST_DEL(&ph->list);
3974 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003975 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003976 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3977 LIST_DEL(&grp->list);
3978 spoe_release_group(grp);
3979 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003980 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01003981 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003982 fconf->ops = &spoe_ops;
3983 fconf->conf = conf;
3984 return 0;
3985
3986 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003987 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02003988 list_for_each_entry_safe(ph, phback, &curmphs, list) {
3989 LIST_DEL(&ph->list);
3990 spoe_release_placeholder(ph);
3991 }
3992 list_for_each_entry_safe(ph, phback, &curgphs, list) {
3993 LIST_DEL(&ph->list);
3994 spoe_release_placeholder(ph);
3995 }
3996 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3997 LIST_DEL(&grp->list);
3998 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003999 }
4000 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4001 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004002 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004003 }
4004 free(conf);
4005 return -1;
4006}
4007
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004008/* Send a SPOE group. TODO */
4009static enum act_return
4010spoe_send_group(struct act_rule *rule, struct proxy *px,
4011 struct session *sess, struct stream *s, int flags)
4012{
4013 struct filter *filter;
4014 struct spoe_agent *agent = NULL;
4015 struct spoe_group *group = NULL;
4016 struct spoe_context *ctx = NULL;
4017 int ret, dir;
4018
4019 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4020 if (filter->config == rule->arg.act.p[0]) {
4021 agent = rule->arg.act.p[2];
4022 group = rule->arg.act.p[3];
4023 ctx = filter->ctx;
4024 break;
4025 }
4026 }
4027 if (agent == NULL || group == NULL || ctx == NULL)
4028 return ACT_RET_ERR;
4029
4030 /* TODO */
4031 return ACT_RET_CONT;
4032}
4033
4034/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4035 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4036 * action should be:
4037 *
4038 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4039 *
4040 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4041 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4042 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4043 * group.
4044 *
4045 * The function returns 1 in success case, otherwise, it returns 0 and err is
4046 * filled.
4047 */
4048static int
4049check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4050{
4051 struct flt_conf *fconf;
4052 struct spoe_config *conf;
4053 struct spoe_agent *agent = NULL;
4054 struct spoe_group *group;
4055 struct spoe_message *msg;
4056 char *engine_id = rule->arg.act.p[0];
4057 char *group_id = rule->arg.act.p[1];
4058 unsigned int where = 0;
4059
4060 switch (rule->from) {
4061 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4062 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4063 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4064 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4065 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4066 default:
4067 memprintf(err,
4068 "internal error, unexpected rule->from=%d, please report this bug!",
4069 rule->from);
4070 goto error;
4071 }
4072
4073 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4074 * <px> */
4075 list_for_each_entry(fconf, &px->filter_configs, list) {
4076 conf = fconf->conf;
4077
4078 /* This is not an SPOE filter */
4079 if (fconf->id != spoe_filter_id)
4080 continue;
4081
4082 /* This is the good engine */
4083 if (!strcmp(conf->id, engine_id)) {
4084 agent = conf->agent;
4085 break;
4086 }
4087 }
4088 if (agent == NULL) {
4089 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4090 engine_id, group_id);
4091 goto error;
4092 }
4093
4094 /* Try to find the right group */
4095 list_for_each_entry(group, &agent->groups, list) {
4096 /* This is the good group */
4097 if (!strcmp(group->id, group_id))
4098 break;
4099 }
4100 if (&group->list == &agent->groups) {
4101 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4102 group_id, engine_id);
4103 goto error;
4104 }
4105
4106 /* Ok, we found the group, we need to check messages and their
4107 * arguments */
4108 list_for_each_entry(msg, &group->messages, by_grp) {
4109 struct spoe_arg *arg;
4110
4111 list_for_each_entry(arg, &msg->args, list) {
4112 if (!(arg->expr->fetch->val & where)) {
4113 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4114 "some args extract information from '%s',"
4115 "none of which is available here ('%s')",
4116 msg->id, group->id, msg->conf.file, msg->conf.line,
4117 sample_ckp_names(arg->expr->fetch->use),
4118 sample_ckp_names(where));
4119 goto error;
4120 }
4121 }
4122 }
4123
4124 free(engine_id);
4125 free(group_id);
4126 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4127 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4128 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4129 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4130 return 1;
4131
4132 error:
4133 free(engine_id);
4134 free(group_id);
4135 return 0;
4136}
4137
4138/* Parse 'send-spoe-group' action following the format:
4139 *
4140 * ... send-spoe-group <engine-id> <group-id>
4141 *
4142 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4143 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4144 * ids are saved and used later, when the rule will be checked.
4145 */
4146static enum act_parse_ret
4147parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4148 struct act_rule *rule, char **err)
4149{
4150 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4151 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4152 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4153 return ACT_RET_PRS_ERR;
4154 }
4155 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4156 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4157
4158 (*orig_arg) += 2;
4159
4160 rule->action = ACT_CUSTOM;
4161 rule->action_ptr = spoe_send_group;
4162 rule->check_ptr = check_send_spoe_group;
4163 return ACT_RET_PRS_OK;
4164}
4165
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004166
4167/* Declare the filter parser for "spoe" keyword */
4168static struct flt_kw_list flt_kws = { "SPOE", { }, {
4169 { "spoe", parse_spoe_flt, NULL },
4170 { NULL, NULL, NULL },
4171 }
4172};
4173
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004174/* Delcate the action parser for "spoe-action" keyword */
4175static struct action_kw_list tcp_req_action_kws = { { }, {
4176 { "send-spoe-group", parse_send_spoe_group },
4177 { /* END */ },
4178 }
4179};
4180static struct action_kw_list tcp_res_action_kws = { { }, {
4181 { "send-spoe-group", parse_send_spoe_group },
4182 { /* END */ },
4183 }
4184};
4185static struct action_kw_list http_req_action_kws = { { }, {
4186 { "send-spoe-group", parse_send_spoe_group },
4187 { /* END */ },
4188 }
4189};
4190static struct action_kw_list http_res_action_kws = { { }, {
4191 { "send-spoe-group", parse_send_spoe_group },
4192 { /* END */ },
4193 }
4194};
4195
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004196__attribute__((constructor))
4197static void __spoe_init(void)
4198{
4199 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004200 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4201 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4202 http_req_keywords_register(&http_req_action_kws);
4203 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004205 pool2_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004206 pool2_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004207}
4208
4209__attribute__((destructor))
4210static void
4211__spoe_deinit(void)
4212{
4213 pool_destroy2(pool2_spoe_ctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01004214 pool_destroy2(pool2_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004215}