blob: 0c221f3f1ddd7f371a4349bdf9e19ce8372a97e8 [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>
Emeric Bruna1dd2432017-06-21 15:42:52 +020021#include <common/hathreads.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020022
23#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020024#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010025#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020026
Christopher Faulet57583e42017-09-04 15:41:09 +020027#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020028#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020029#include <proto/arg.h>
30#include <proto/backend.h>
31#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010032#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020033#include <proto/frontend.h>
34#include <proto/log.h>
35#include <proto/proto_http.h>
36#include <proto/proxy.h>
37#include <proto/sample.h>
38#include <proto/session.h>
39#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010040#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/stream.h>
42#include <proto/stream_interface.h>
43#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020044#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#include <proto/vars.h>
46
47#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
48#define SPOE_PRINTF(x...) fprintf(x)
49#else
50#define SPOE_PRINTF(x...)
51#endif
52
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010053/* Reserved 4 bytes to the frame size. So a frame and its size can be written
54 * together in a buffer */
55#define MAX_FRAME_SIZE global.tune.bufsize - 4
56
57/* The minimum size for a frame */
58#define MIN_FRAME_SIZE 256
59
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010060/* Reserved for the metadata and the frame type.
61 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010062#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020063
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010065#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
66
Christopher Faulet3b386a32017-02-23 10:17:15 +010067/* SPOE filter id. Used to identify SPOE filters */
68const char *spoe_filter_id = "SPOE filter";
69
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020070/* Set if the handle on SIGUSR1 is registered */
71static int sighandler_registered = 0;
72
73/* proxy used during the parsing */
74struct proxy *curproxy = NULL;
75
76/* The name of the SPOE engine, used during the parsing */
77char *curengine = NULL;
78
79/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020080/* SPOE agent/group/message used during the parsing */
81struct spoe_agent *curagent = NULL;
82struct spoe_group *curgrp = NULL;
83struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020084
85/* list of SPOE messages and placeholders used during the parsing */
86struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020087struct list curgrps;
88struct list curmphs;
89struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010090struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020091
Christopher Faulet42bfa462017-01-04 14:14:19 +010092/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +010093static struct pool_head *pool_head_spoe_ctx = NULL;
94static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020096struct flt_ops spoe_ops;
97
Christopher Faulet8ef75252017-02-20 22:56:03 +010098static int spoe_queue_context(struct spoe_context *ctx);
99static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
100static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200101
102/********************************************************************
103 * helper functions/globals
104 ********************************************************************/
105static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200106spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107{
Christopher Faulet11610f32017-09-21 10:23:10 +0200108 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200110 free(ph->id);
111 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112}
113
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100115spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet57583e42017-09-04 15:41:09 +0200117 struct spoe_arg *arg, *argback;
118 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119
120 if (!msg)
121 return;
122 free(msg->id);
123 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200124 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125 release_sample_expr(arg->expr);
126 free(arg->name);
127 LIST_DEL(&arg->list);
128 free(arg);
129 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200130 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
131 LIST_DEL(&acl->list);
132 prune_acl(acl);
133 free(acl);
134 }
135 if (msg->cond) {
136 prune_acl_cond(msg->cond);
137 free(msg->cond);
138 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200139 free(msg);
140}
141
142static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200143spoe_release_group(struct spoe_group *grp)
144{
145 if (!grp)
146 return;
147 free(grp->id);
148 free(grp->conf.file);
149 free(grp);
150}
151
152static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100153spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200154{
Christopher Faulet11610f32017-09-21 10:23:10 +0200155 struct spoe_message *msg, *msgback;
156 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200157 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200158
159 if (!agent)
160 return;
161 free(agent->id);
162 free(agent->conf.file);
163 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100164 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100165 free(agent->var_on_error);
Christopher Faulet11610f32017-09-21 10:23:10 +0200166 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
167 LIST_DEL(&msg->list);
168 spoe_release_message(msg);
169 }
170 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
171 LIST_DEL(&grp->list);
172 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200173 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200174 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100175 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200176 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200177 free(agent);
178}
179
180static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100181 [SPOE_FRM_ERR_NONE] = "normal",
182 [SPOE_FRM_ERR_IO] = "I/O error",
183 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
184 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
185 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
186 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
187 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
188 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
189 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
190 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
191 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
192 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100193 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100194 [SPOE_FRM_ERR_RES] = "resource allocation error",
195 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200196};
197
198static const char *spoe_event_str[SPOE_EV_EVENTS] = {
199 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
200 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
201 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
202 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
203 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
204
205 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
206 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
207 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
208};
209
210
211#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
212
213static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100214 [SPOE_CTX_ST_NONE] = "NONE",
215 [SPOE_CTX_ST_READY] = "READY",
216 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
217 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
218 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
219 [SPOE_CTX_ST_DONE] = "DONE",
220 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200221};
222
223static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100224 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
225 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
226 [SPOE_APPCTX_ST_IDLE] = "IDLE",
227 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
228 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
229 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
230 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
231 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
232 [SPOE_APPCTX_ST_EXIT] = "EXIT",
233 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200234};
235
236#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100237
Christopher Faulet8ef75252017-02-20 22:56:03 +0100238/* Used to generates a unique id for an engine. On success, it returns a
239 * allocated string. So it is the caller's reponsibility to release it. If the
240 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100241static char *
242generate_pseudo_uuid()
243{
244 static int init = 0;
245
246 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
247 const char uuid_chr[] = "0123456789ABCDEF-";
248 char *uuid;
249 int i;
250
251 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
252 return NULL;
253
254 if (!init) {
255 srand(now_ms);
256 init = 1;
257 }
258
259 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
260 int r = rand () % 16;
261
262 switch (uuid_fmt[i]) {
263 case 'x' : uuid[i] = uuid_chr[r]; break;
264 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
265 default : uuid[i] = uuid_fmt[i]; break;
266 }
267 }
268 return uuid;
269}
270
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200271/********************************************************************
272 * Functions that encode/decode SPOE frames
273 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200274/* Helper to get static string length, excluding the terminating null byte */
275#define SLEN(str) (sizeof(str)-1)
276
277/* Predefined key used in HELLO/DISCONNECT frames */
278#define SUPPORTED_VERSIONS_KEY "supported-versions"
279#define VERSION_KEY "version"
280#define MAX_FRAME_SIZE_KEY "max-frame-size"
281#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100282#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100283#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200284#define STATUS_CODE_KEY "status-code"
285#define MSG_KEY "message"
286
287struct spoe_version {
288 char *str;
289 int min;
290 int max;
291};
292
293/* All supported versions */
294static struct spoe_version supported_versions[] = {
295 {"1.0", 1000, 1000},
296 {NULL, 0, 0}
297};
298
299/* Comma-separated list of supported versions */
300#define SUPPORTED_VERSIONS_VAL "1.0"
301
Christopher Faulet8ef75252017-02-20 22:56:03 +0100302/* Convert a string to a SPOE version value. The string must follow the format
303 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
304 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308 const char *p, *end;
309 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 p = str;
312 end = str+len;
313 maj = min = 0;
314 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200315
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 /* skip leading spaces */
317 while (p < end && isspace(*p))
318 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200319
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320 /* parse Major number, until the '.' */
321 while (*p != '.') {
322 if (p >= end || *p < '0' || *p > '9')
323 goto out;
324 maj *= 10;
325 maj += (*p - '0');
326 p++;
327 }
328
329 /* check Major version */
330 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200331 goto out;
332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p++; /* skip the '.' */
334 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
335 goto out;
336
337 /* Parse Minor number */
338 while (p < end) {
339 if (*p < '0' || *p > '9')
340 break;
341 min *= 10;
342 min += (*p - '0');
343 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100345
346 /* check Minor number */
347 if (min > 999)
348 goto out;
349
350 /* skip trailing spaces */
351 while (p < end && isspace(*p))
352 p++;
353 if (p != end)
354 goto out;
355
356 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200357 out:
358 return vsn;
359}
360
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
362 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
363 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100365spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366{
Christopher Faulet305c6072017-02-23 16:17:53 +0100367 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100368 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100369 char *p, *end;
370 unsigned int flags = SPOE_FRM_FL_FIN;
371 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372
Christopher Faulet8ef75252017-02-20 22:56:03 +0100373 p = frame;
374 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 /* Set Frame type */
377 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100379 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100380 memcpy(p, (char *)&flags, 4);
381 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200382
383 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200385
386 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
387 * and "capabilities" */
388
389 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100390 sz = SLEN(SUPPORTED_VERSIONS_KEY);
391 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
392 goto too_big;
393
394 *p++ = SPOE_DATA_T_STR;
395 sz = SLEN(SUPPORTED_VERSIONS_VAL);
396 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
397 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
399 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 sz = SLEN(MAX_FRAME_SIZE_KEY);
401 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
402 goto too_big;
403
404 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200405 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200407
408 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100409 sz = SLEN(CAPABILITIES_KEY);
410 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
411 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200412
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100414 chk = get_trash_chunk();
415 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
416 memcpy(chk->str, "pipelining", 10);
417 chk->len += 10;
418 }
419 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
420 if (chk->len) chk->str[chk->len++] = ',';
421 memcpy(chk->str+chk->len, "async", 5);
422 chk->len += 5;
423 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100424 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
425 if (chk->len) chk->str[chk->len++] = ',';
426 memcpy(chk->str+chk->len, "fragmentation", 13);
427 chk->len += 5;
428 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100429 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100430 goto too_big;
431
432 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100433 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 sz = SLEN(ENGINE_ID_KEY);
435 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
436 goto too_big;
437
438 *p++ = SPOE_DATA_T_STR;
439 sz = strlen(agent->engine_id);
440 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
441 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100442 }
443
Christopher Faulet8ef75252017-02-20 22:56:03 +0100444 return (p - frame);
445
446 too_big:
447 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
448 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200449}
450
Christopher Faulet8ef75252017-02-20 22:56:03 +0100451/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
452 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
453 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200454static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200456{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 const char *reason;
458 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100459 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100460 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200461
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 p = frame;
463 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200464
Christopher Faulet8ef75252017-02-20 22:56:03 +0100465 /* Set Frame type */
466 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100468 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 memcpy(p, (char *)&flags, 4);
470 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471
472 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 *p++ = 0; *p++ = 0;
474
475 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
476 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477
478 /* There are 2 mandatory items: "status-code" and "message" */
479
480 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100481 sz = SLEN(STATUS_CODE_KEY);
482 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
483 goto too_big;
484
485 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200486 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488
489 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 sz = SLEN(MSG_KEY);
491 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
492 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 /*Get the message corresponding to the status code */
495 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
496
497 *p++ = SPOE_DATA_T_STR;
498 sz = strlen(reason);
499 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
500 goto too_big;
501
502 return (p - frame);
503
504 too_big:
505 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
506 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200507}
508
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
510 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
511 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100514 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 char *p, *end;
517 unsigned int stream_id, frame_id;
518 unsigned int flags = SPOE_FRM_FL_FIN;
519 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 p = frame;
522 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200523
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100524 stream_id = ctx->stream_id;
525 frame_id = ctx->frame_id;
526
527 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
528 /* The fragmentation is not supported by the applet */
529 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
530 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
531 return -1;
532 }
533 flags = ctx->frag_ctx.flags;
534 }
535
536 /* Set Frame type */
537 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
538
539 /* Set flags */
540 memcpy(p, (char *)&flags, 4);
541 p += 4;
542
543 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200544 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100545 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200546 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100547 goto too_big;
548
549 /* Copy encoded messages, if possible */
550 sz = ctx->buffer->i;
551 if (p + sz >= end)
552 goto too_big;
553 memcpy(p, ctx->buffer->p, sz);
554 p += sz;
555
556 return (p - frame);
557
558 too_big:
559 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
560 return 0;
561}
562
563/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
564 * returns the number of encoded bytes in the frame on success, 0 if an encoding
565 * error occurred and -1 if a fatal error occurred. */
566static int
567spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
568 char *frame, size_t size)
569{
570 char *p, *end;
571 unsigned int stream_id, frame_id;
572 unsigned int flags;
573 size_t sz;
574
575 p = frame;
576 end = frame+size;
577
Christopher Faulet8ef75252017-02-20 22:56:03 +0100578 /* <ctx> is null when the stream has aborted the processing of a
579 * fragmented frame. In this case, we must notify the corresponding
580 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100581 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100582 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100583 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
584 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
585 }
586 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100587 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100588 stream_id = ctx->stream_id;
589 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100590 }
591
Christopher Faulet8ef75252017-02-20 22:56:03 +0100592 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100593 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100594
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100595 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596 memcpy(p, (char *)&flags, 4);
597 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200598
599 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200600 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100601 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200602 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200604
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100605 if (ctx == NULL)
606 goto end;
607
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 if (p + sz >= end)
611 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100614
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100615 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 return (p - frame);
617
618 too_big:
619 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
620 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200621}
622
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623/* Decode and process the HELLO frame sent by an agent. It returns the number of
624 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
625 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628{
Christopher Faulet305c6072017-02-23 16:17:53 +0100629 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
630 char *p, *end;
631 int vsn, max_frame_size;
632 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100633
634 p = frame;
635 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200636
637 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
639 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200640 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200642
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 if (size < 7 /* TYPE + METADATA */) {
644 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
645 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646 }
647
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100648 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649 memcpy((char *)&flags, p, 4);
650 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200651
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100652 /* Fragmentation is not supported for HELLO frame */
653 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100655 return -1;
656 }
657
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200658 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 if (*p != 0 || *(p+1) != 0) {
660 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
661 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100663 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664
665 /* There are 3 mandatory items: "version", "max-frame-size" and
666 * "capabilities" */
667
668 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100669 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100670 while (p < end) {
671 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200672 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200674
675 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 ret = spoe_decode_buffer(&p, end, &str, &sz);
677 if (ret == -1 || !sz) {
678 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
679 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682 /* Check "version" K/V item */
683 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 int i, type = *p++;
685
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
688 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
689 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
692 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
693 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 }
695
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 return -1;
700 }
701 for (i = 0; supported_versions[i].str != NULL; ++i) {
702 if (vsn >= supported_versions[i].min &&
703 vsn <= supported_versions[i].max)
704 break;
705 }
706 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100707 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200708 return -1;
709 }
710 }
711 /* Check "max-frame-size" K/V item */
712 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714
715 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
717 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
718 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
719 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
721 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200723 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100724 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
725 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 if (sz < MIN_FRAME_SIZE ||
728 sz > SPOE_APPCTX(appctx)->max_frame_size) {
729 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 return -1;
731 }
732 max_frame_size = sz;
733 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100734 /* Check "capabilities" K/V item */
735 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100736 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100737
738 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
740 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
741 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
744 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
745 return 0;
746 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747
Christopher Faulet8ef75252017-02-20 22:56:03 +0100748 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100749 char *delim;
750
751 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100753
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
755 str += 10; sz -= 10;
756 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757 flags |= SPOE_APPCTX_FL_PIPELINING;
758 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 else if (sz >= 5 && !strncmp(str, "async", 5)) {
760 str += 5; sz -= 5;
761 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 flags |= SPOE_APPCTX_FL_ASYNC;
763 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100764 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
765 str += 13; sz -= 13;
766 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100767 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
768 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 /* Get the next comma or break */
771 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100773 delim++;
774 sz -= (delim - str);
775 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776 }
777 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200778 else {
779 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100780 if (spoe_skip_data(&p, end) == -1) {
781 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
782 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200783 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200784 }
785 }
786
787 /* Final checks */
788 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100789 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200790 return -1;
791 }
792 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200794 return -1;
795 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100796 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
797 flags &= ~SPOE_APPCTX_FL_PIPELINING;
798 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
799 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800
Christopher Faulet42bfa462017-01-04 14:14:19 +0100801 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
802 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
803 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100804
805 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200806}
807
808/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
809 * bytes on success, 0 if the frame can be ignored and -1 if an error
810 * occurred. */
811static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100812spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100814 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100815 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816
817 p = frame;
818 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819
820 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
822 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200823 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826 if (size < 7 /* TYPE + METADATA */) {
827 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
828 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200829 }
830
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100831 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 memcpy((char *)&flags, p, 4);
833 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100835 /* Fragmentation is not supported for DISCONNECT frame */
836 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100837 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100838 return -1;
839 }
840
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 if (*p != 0 || *(p+1) != 0) {
843 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
844 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
848 /* There are 2 mandatory items: "status-code" and "message" */
849
850 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 while (p < end) {
852 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200853 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855
856 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 ret = spoe_decode_buffer(&p, end, &str, &sz);
858 if (ret == -1 || !sz) {
859 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
860 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200861 }
862
863 /* Check "status-code" K/V item */
864 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200866
867 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
869 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
870 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
871 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200875 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
877 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200880 }
881
882 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 else if (!memcmp(str, MSG_KEY, sz)) {
884 int type = *p++;
885
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
888 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
889 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 ret = spoe_decode_buffer(&p, end, &str, &sz);
892 if (ret == -1 || sz > 255) {
893 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
894 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200895 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100896#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
897 SPOE_APPCTX(appctx)->reason = str;
898 SPOE_APPCTX(appctx)->rlen = sz;
899#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900 }
901 else {
902 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100903 if (spoe_skip_data(&p, end) == -1) {
904 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
905 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907 }
908 }
909
Christopher Faulet8ef75252017-02-20 22:56:03 +0100910 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200911}
912
913
Christopher Fauleta1cda022016-12-21 08:58:06 +0100914/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 * success, 0 if the frame can be ignored and -1 if an error occurred. */
916static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100918 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100920 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100922 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100924 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925
926 p = frame;
927 end = frame + size;
928 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929
930 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
932 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200935
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 if (size < 7 /* TYPE + METADATA */) {
937 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
938 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939 }
940
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100941 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 memcpy((char *)&flags, p, 4);
943 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100945 /* Fragmentation is not supported for now */
946 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100948 return -1;
949 }
950
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200952 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100954 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200956 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200958 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100960
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100962 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200963 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100964 if ((*ctx)->stream_id == (unsigned int)stream_id &&
965 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100966 goto found;
967 }
968 }
969 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100970 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
971 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100973 goto found;
974 }
975 }
976
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100977 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
978 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
979 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
980
981 /* ABRT bit is set for an unfinished fragmented frame */
982 if (flags & SPOE_FRM_FL_ABRT) {
983 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
984 (*ctx)->frag_ctx.spoe_appctx = NULL;
985 (*ctx)->state = SPOE_CTX_ST_ERROR;
986 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
987 /* Ignore the payload */
988 goto end;
989 }
990 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
991 /* For now, we ignore the ack */
992 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
993 return 0;
994 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100995
Christopher Fauleta1cda022016-12-21 08:58:06 +0100996 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100997 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
998 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100999 " - stream-id=%u - frame-id=%u\n",
1000 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1001 __FUNCTION__, appctx,
1002 (unsigned int)stream_id, (unsigned int)frame_id);
1003
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001004 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001005 return 0;
1006
1007 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001008 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1009 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001010 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001011 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001012 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001013
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001014 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001015 len = (end - p);
1016 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1017 SPOE_APPCTX(appctx)->buffer->i = len;
1018 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001019
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001020 /* Transfer the buffer ownership to the SPOE context */
1021 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1022 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001023
Christopher Faulet8ef75252017-02-20 22:56:03 +01001024 (*ctx)->state = SPOE_CTX_ST_DONE;
1025
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001026 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001027 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001028 " - ACK frame received"
1029 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001030 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001031 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1032 (*ctx)->frame_id, flags);
1033 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001034}
1035
Christopher Fauletba7bc162016-11-07 21:07:38 +01001036/* This function is used in cfgparse.c and declared in proto/checks.h. It
1037 * prepare the request to send to agents during a healthcheck. It returns 0 on
1038 * success and -1 if an error occurred. */
1039int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001040spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001041{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 struct appctx appctx;
1043 struct spoe_appctx spoe_appctx;
1044 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1045 size_t sz;
1046 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001047
Christopher Faulet42bfa462017-01-04 14:14:19 +01001048 memset(&appctx, 0, sizeof(appctx));
1049 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001050 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001051
1052 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001053 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001054
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1056 end = frame + MAX_FRAME_SIZE;
1057
1058 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1059 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 /* Add "healthcheck" K/V item */
1064 sz = SLEN(HEALTHCHECK_KEY);
1065 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1066 return -1;
1067 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet8ef75252017-02-20 22:56:03 +01001069 *len = frame - buf;
1070 sz = htonl(*len - 4);
1071 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001072
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001076 return 0;
1077}
1078
1079/* This function is used in checks.c and declared in proto/checks.h. It decode
1080 * the response received from an agent during a healthcheck. It returns 0 on
1081 * success and -1 if an error occurred. */
1082int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001085 struct appctx appctx;
1086 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087
Christopher Faulet42bfa462017-01-04 14:14:19 +01001088 memset(&appctx, 0, sizeof(appctx));
1089 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090
Christopher Faulet42bfa462017-01-04 14:14:19 +01001091 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001092 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1095 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096 goto error;
1097 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001098 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1099 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100
1101 return 0;
1102
1103 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1105 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1106 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107 return -1;
1108}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001109
Christopher Fauleta1cda022016-12-21 08:58:06 +01001110/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1111 * the frame can be ignored, 1 to retry later, and the frame legnth on
1112 * success. */
1113static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001114spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001115{
1116 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 int ret;
1118 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001119
Christopher Faulet8ef75252017-02-20 22:56:03 +01001120 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1121 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001122 netint = htonl(framesz);
1123 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001124 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001125 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001126 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127 retry:
1128 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001130 }
1131 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132 return -1; /* error */
1133 }
1134 return framesz;
1135}
1136
1137/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1138 * when the frame can be ignored, 1 to retry later and the frame length on
1139 * success. */
1140static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142{
1143 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001144 int ret;
1145 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146
Willy Tarreau06d80a92017-10-19 14:32:15 +02001147 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001148 if (ret > 0) {
1149 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001150 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001151 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return -1;
1153 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001154 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155 }
1156 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 if (ret == 0) {
1158 retry:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001160 }
1161 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162 return -1; /* error */
1163 }
1164 return framesz;
1165}
1166
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001167/********************************************************************
1168 * Functions that manage the SPOE applet
1169 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001170static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001171spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001172{
1173 si_applet_want_get(appctx->owner);
1174 si_applet_want_put(appctx->owner);
1175 appctx_wakeup(appctx);
1176 return 1;
1177}
1178
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001179/* Callback function that catches applet timeouts. If a timeout occurred, we set
1180 * <appctx->st1> flag and the SPOE applet is woken up. */
1181static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001183{
1184 struct appctx *appctx = task->context;
1185
1186 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1187 if (tick_is_expired(task->expire, now_ms)) {
1188 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001189 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001190 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001191 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001192 return task;
1193}
1194
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001195/* Callback function that releases a SPOE applet. This happens when the
1196 * connection with the agent is closed. */
1197static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001198spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001199{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001200 struct stream_interface *si = appctx->owner;
1201 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1202 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001203 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001204
1205 if (spoe_appctx == NULL)
1206 return;
1207
1208 appctx->ctx.spoe.ptr = NULL;
1209 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001210
1211 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1212 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1213 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215 /* Remove applet from the list of running applets */
Christopher Faulet24289f22017-09-25 14:48:02 +02001216 agent->rt[tid].applets_act--;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1218 LIST_DEL(&spoe_appctx->list);
1219 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001220 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221
Christopher Faulet8ef75252017-02-20 22:56:03 +01001222 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001223 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001224 if (appctx->st0 == SPOE_APPCTX_ST_IDLE)
Christopher Faulet24289f22017-09-25 14:48:02 +02001225 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001226
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001227 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001228 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1229 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001230
1231 si_shutw(si);
1232 si_shutr(si);
1233 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001234 }
1235
Christopher Faulet8ef75252017-02-20 22:56:03 +01001236 /* Destroy the task attached to this applet */
1237 if (spoe_appctx->task) {
1238 task_delete(spoe_appctx->task);
1239 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240 }
1241
Christopher Faulet8ef75252017-02-20 22:56:03 +01001242 /* Notify all waiting streams */
1243 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001244 LIST_DEL(&ctx->list);
1245 LIST_INIT(&ctx->list);
1246 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001247 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001248 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249 }
1250
Christopher Faulet8ef75252017-02-20 22:56:03 +01001251 /* If the applet was processing a fragmented frame, notify the
1252 * corresponding stream. */
1253 if (spoe_appctx->frag_ctx.ctx) {
1254 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001255 ctx->frag_ctx.spoe_appctx = NULL;
1256 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001257 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001258 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1259 }
1260
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 /* Release allocated memory */
1262 spoe_release_buffer(&spoe_appctx->buffer,
1263 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001264 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001265
Christopher Faulet24289f22017-09-25 14:48:02 +02001266 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001267 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001268
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001270 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001271 LIST_DEL(&ctx->list);
1272 LIST_INIT(&ctx->list);
1273 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001275 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001276 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001277 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001278 LIST_DEL(&ctx->list);
1279 LIST_INIT(&ctx->list);
1280 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001281 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001282 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1283 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001284
1285 end:
1286 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001287 agent->rt[tid].frame_size = agent->max_frame_size;
1288 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1289 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001290}
1291
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001292static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001294{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001295 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001296 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001297 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001298 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001299
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300 if (si->state <= SI_ST_CON) {
1301 si_applet_want_put(si);
1302 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1303 goto stop;
1304 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001305 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001306 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001308 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001309
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1312 " - Connection timed out\n",
1313 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1314 __FUNCTION__, appctx);
1315 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001317 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001318
Christopher Faulet42bfa462017-01-04 14:14:19 +01001319 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001320 SPOE_APPCTX(appctx)->task->expire =
1321 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1324 * length. */
1325 buf = trash.str; frame = buf+4;
1326 ret = spoe_prepare_hahello_frame(appctx, frame,
1327 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001328 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001329 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001330
1331 switch (ret) {
1332 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001333 case 0: /* ignore => an error, cannot be ignored */
1334 goto exit;
1335
1336 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001337 goto stop;
1338
Christopher Faulet8ef75252017-02-20 22:56:03 +01001339 default:
1340 /* HELLO frame successfully sent, now wait for the
1341 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1343 goto next;
1344 }
1345
1346 next:
1347 return 0;
1348 stop:
1349 return 1;
1350 exit:
1351 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1352 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001353}
1354
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001355static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001356spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001357{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001359 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001360 char *frame;
1361 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001362
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001363
Christopher Fauletb067b062017-01-04 16:39:11 +01001364 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001367 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001368
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1371 " - Connection timed out\n",
1372 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1373 __FUNCTION__, appctx);
1374 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001375 goto exit;
1376 }
1377
Christopher Faulet8ef75252017-02-20 22:56:03 +01001378 frame = trash.str; trash.len = 0;
1379 ret = spoe_recv_frame(appctx, frame,
1380 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001381 if (ret > 1) {
1382 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1383 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1384 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001385 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001386 trash.len = ret + 4;
1387 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389
Christopher Fauleta1cda022016-12-21 08:58:06 +01001390 switch (ret) {
1391 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001392 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001393 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1394 goto next;
1395
1396 case 1: /* retry later */
1397 goto stop;
1398
1399 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001400 /* HELLO handshake is finished, set the idle timeout and
1401 * add the applet in the list of running applets. */
Christopher Faulet24289f22017-09-25 14:48:02 +02001402 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001403 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001404 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001405 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001406 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001407 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001408
1409 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001410 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001412 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413
Christopher Fauleta1cda022016-12-21 08:58:06 +01001414 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001415 /* Do not forget to remove processed frame from the output buffer */
1416 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001417 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001418
1419 SPOE_APPCTX(appctx)->task->expire =
1420 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001421 return 0;
1422 stop:
1423 return 1;
1424 exit:
1425 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1426 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001427}
1428
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001429
Christopher Fauleta1cda022016-12-21 08:58:06 +01001430static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001431spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001432{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001433 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1434 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 char *frame, *buf;
1436 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001437
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1439 * length. */
1440 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001441
1442 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1443 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1444 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1445 SPOE_APPCTX(appctx)->max_frame_size);
1446 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001447 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001448 *skip = 1;
1449 ret = 1;
1450 goto end;
1451 }
1452 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001453 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001454 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1455 SPOE_APPCTX(appctx)->max_frame_size);
1456
1457 }
1458
Christopher Faulet8ef75252017-02-20 22:56:03 +01001459 if (ret > 1)
1460 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001461
Christopher Faulet8ef75252017-02-20 22:56:03 +01001462 switch (ret) {
1463 case -1: /* error */
1464 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1465 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001466
Christopher Faulet8ef75252017-02-20 22:56:03 +01001467 case 0: /* ignore */
1468 if (ctx == NULL)
1469 goto abort_frag_frame;
1470
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001471 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001472 LIST_DEL(&ctx->list);
1473 LIST_INIT(&ctx->list);
1474 ctx->state = SPOE_CTX_ST_ERROR;
1475 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1476 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1477 break;
1478
1479 case 1: /* retry */
1480 *skip = 1;
1481 break;
1482
1483 default:
1484 if (ctx == NULL)
1485 goto abort_frag_frame;
1486
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001487 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001488 LIST_DEL(&ctx->list);
1489 LIST_INIT(&ctx->list);
1490 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1491 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1492 goto no_frag_frame_sent;
1493 else {
1494 *skip = 1;
1495 goto frag_frame_sent;
1496 }
1497 }
1498 goto end;
1499
1500 frag_frame_sent:
1501 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
1502 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1503 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1504 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
1505
1506 ctx->frag_ctx.spoe_appctx = SPOE_APPCTX(appctx);
1507 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1508 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1509 goto end;
1510
1511 no_frag_frame_sent:
1512 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1513 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001514 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 }
1516 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1517 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1518 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1519 }
1520 else {
1521 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
1522 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1523 }
1524 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1525 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1526 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1527
1528 ctx->frag_ctx.spoe_appctx = NULL;
1529 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1530 goto end;
1531
1532 abort_frag_frame:
1533 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1534 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1535 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1536 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1537 goto end;
1538
1539 end:
1540 return ret;
1541}
1542
1543static int
1544spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1545{
1546 struct spoe_context *ctx = NULL;
1547 char *frame;
1548 int ret;
1549
1550 frame = trash.str; trash.len = 0;
1551 ret = spoe_recv_frame(appctx, frame,
1552 SPOE_APPCTX(appctx)->max_frame_size);
1553 if (ret > 1) {
1554 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1555 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1556 goto end;
1557 }
1558 trash.len = ret + 4;
1559 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1560 }
1561 switch (ret) {
1562 case -1: /* error */
1563 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1564 break;
1565
1566 case 0: /* ignore */
1567 break;
1568
1569 case 1: /* retry */
1570 *skip = 1;
1571 break;
1572
1573 default:
1574 LIST_DEL(&ctx->list);
1575 LIST_INIT(&ctx->list);
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001576
1577 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1578 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1579 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1580 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1581 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1582 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1583 }
1584 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1585 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1586
Christopher Faulet8ef75252017-02-20 22:56:03 +01001587 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1588 break;
1589 }
1590
1591 /* Do not forget to remove processed frame from the output buffer */
1592 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001593 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001594 end:
1595 return ret;
1596}
1597
1598static int
1599spoe_handle_processing_appctx(struct appctx *appctx)
1600{
1601 struct stream_interface *si = appctx->owner;
1602 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001603 unsigned int fpa = 0;
1604 int ret, skip_sending = 0, skip_receiving = 0;
1605
1606 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1607 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1608 goto exit;
1609 }
1610
1611 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1612 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1613 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1614 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1615 goto next;
1616 }
1617
1618 process:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001619 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1620 " - process: fpa=%u/%u - skip_sending=%d - skip_receiving=%d"
1621 " - appctx-state=%s\n",
1622 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1623 __FUNCTION__, appctx, fpa, agent->max_fpa,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001624 skip_sending, skip_receiving,
1625 spoe_appctx_state_str[appctx->st0]);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001626
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001627 if (fpa > agent->max_fpa)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001628 goto stop;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001629 else if (skip_sending || appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001630 if (skip_receiving)
1631 goto stop;
1632 goto recv_frame;
1633 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001634
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001635 /* send_frame */
1636 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001637 switch (ret) {
1638 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001639 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001640
Christopher Fauleta1cda022016-12-21 08:58:06 +01001641 case 0: /* ignore */
Christopher Faulet24289f22017-09-25 14:48:02 +02001642 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001643 fpa++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001644 break;
1645
Christopher Fauleta1cda022016-12-21 08:58:06 +01001646 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001647 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001648
Christopher Fauleta1cda022016-12-21 08:58:06 +01001649 default:
Christopher Faulet24289f22017-09-25 14:48:02 +02001650 agent->rt[tid].sending_rate++;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001651 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001653 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001654 if (fpa > agent->max_fpa)
1655 goto stop;
1656
1657 recv_frame:
1658 if (skip_receiving)
1659 goto process;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001660 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001661 switch (ret) {
1662 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001663 goto next;
1664
1665 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001666 fpa++;
1667 break;
1668
1669 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001670 break;
1671
1672 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001673 fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001674 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001675 }
1676 goto process;
1677
1678 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001679 SPOE_APPCTX(appctx)->task->expire =
1680 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001681 return 0;
1682 stop:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001683 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001684 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Faulet24289f22017-09-25 14:48:02 +02001685 agent->rt[tid].applets_idle++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001686 }
Christopher Faulet42097792018-01-24 15:49:45 +01001687 if (fpa) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001688 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001689 LIST_DEL(&SPOE_APPCTX(appctx)->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02001690 LIST_ADD(&agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001691 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001692 if (fpa)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001693 SPOE_APPCTX(appctx)->task->expire =
1694 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695 }
1696 return 1;
1697
1698 exit:
1699 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1700 return 0;
1701}
1702
1703static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001704spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001705{
1706 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001707 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001708 char *frame, *buf;
1709 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001710
Christopher Fauleta1cda022016-12-21 08:58:06 +01001711 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1712 goto exit;
1713
1714 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1715 goto exit;
1716
Christopher Faulet8ef75252017-02-20 22:56:03 +01001717 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1718 * length. */
1719 buf = trash.str; frame = buf+4;
1720 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1721 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001722 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001723 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001724
1725 switch (ret) {
1726 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001727 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001728 goto exit;
1729
1730 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731 goto stop;
1732
1733 default:
1734 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1735 " - disconnected by HAProxy (%d): %s\n",
1736 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001737 __FUNCTION__, appctx,
1738 SPOE_APPCTX(appctx)->status_code,
1739 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001740
1741 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1742 goto next;
1743 }
1744
1745 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001746 SPOE_APPCTX(appctx)->task->expire =
1747 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748 return 0;
1749 stop:
1750 return 1;
1751 exit:
1752 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1753 return 0;
1754}
1755
1756static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001757spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001758{
1759 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001760 char *frame;
1761 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762
Christopher Fauletb067b062017-01-04 16:39:11 +01001763 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001766 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767
Christopher Fauletb067b062017-01-04 16:39:11 +01001768 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001769 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001771 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772
Christopher Faulet8ef75252017-02-20 22:56:03 +01001773 frame = trash.str; trash.len = 0;
1774 ret = spoe_recv_frame(appctx, frame,
1775 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001776 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001777 trash.len = ret + 4;
1778 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001779 }
1780
1781 switch (ret) {
1782 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1784 " - error on frame (%s)\n",
1785 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001786 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001788 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 goto exit;
1790
1791 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792 goto next;
1793
1794 case 1: /* retry */
1795 goto stop;
1796
1797 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001800 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001801 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1803 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 goto exit;
1805 }
1806
1807 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808 /* Do not forget to remove processed frame from the output buffer */
1809 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001810 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812 return 0;
1813 stop:
1814 return 1;
1815 exit:
1816 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1817 return 0;
1818}
1819
1820/* I/O Handler processing messages exchanged with the agent */
1821static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001822spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 struct stream_interface *si = appctx->owner;
1825 struct spoe_agent *agent;
1826
1827 if (SPOE_APPCTX(appctx) == NULL)
1828 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829
Christopher Faulet8ef75252017-02-20 22:56:03 +01001830 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1831 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001832
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 switchstate:
1834 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1835 " - appctx-state=%s\n",
1836 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1837 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1838
1839 switch (appctx->st0) {
1840 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001841 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842 goto out;
1843 goto switchstate;
1844
1845 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001846 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847 goto out;
1848 goto switchstate;
1849
1850 case SPOE_APPCTX_ST_IDLE:
1851 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001852 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001853 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001854 SPOE_APPCTX(appctx)->task->expire =
1855 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001856 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001857 goto switchstate;
1858 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001859 agent->rt[tid].applets_idle--;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001860 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1861 /* fall through */
1862
1863 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001864 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1865 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 goto out;
1868 goto switchstate;
1869
1870 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001872 goto out;
1873 goto switchstate;
1874
1875 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001876 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001877 goto out;
1878 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001879
1880 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001881 appctx->st0 = SPOE_APPCTX_ST_END;
1882 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1883
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001884 si_shutw(si);
1885 si_shutr(si);
1886 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001887 /* fall through */
1888
1889 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001890 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001891 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001893 if (stopping)
1894 spoe_wakeup_appctx(appctx);
1895
Christopher Faulet42bfa462017-01-04 14:14:19 +01001896 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1897 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001898 si_oc(si)->flags |= CF_READ_DONTWAIT;
1899 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001900}
1901
1902struct applet spoe_applet = {
1903 .obj_type = OBJ_TYPE_APPLET,
1904 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001905 .fct = spoe_handle_appctx,
1906 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001907};
1908
1909/* Create a SPOE applet. On success, the created applet is returned, else
1910 * NULL. */
1911static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001912spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001913{
1914 struct appctx *appctx;
1915 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001917
Emeric Brun1138fd02017-06-19 12:38:55 +02001918 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001919 goto out_error;
1920
Willy Tarreaubafbe012017-11-24 17:34:44 +01001921 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001922 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001923 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001924 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001925
Christopher Faulet42bfa462017-01-04 14:14:19 +01001926 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001927 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001928 goto out_free_spoe_appctx;
1929
1930 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001931 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001932 SPOE_APPCTX(appctx)->task->context = appctx;
1933 SPOE_APPCTX(appctx)->agent = conf->agent;
1934 SPOE_APPCTX(appctx)->version = 0;
1935 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1936 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001937 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001938 SPOE_APPCTX(appctx)->buffer = &buf_empty;
1939
1940 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1941 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001942 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001943
1944 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1945 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001946
Willy Tarreau5820a362016-12-22 15:59:02 +01001947 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948 if (!sess)
1949 goto out_free_spoe;
1950
Willy Tarreau87787ac2017-08-28 16:22:54 +02001951 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001952 goto out_free_sess;
1953
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001954 stream_set_backend(strm, conf->agent->b.be);
1955
1956 /* applet is waiting for data */
1957 si_applet_cant_get(&strm->si[0]);
1958 appctx_wakeup(appctx);
1959
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001960 strm->do_log = NULL;
1961 strm->res.flags |= CF_READ_DONTWAIT;
1962
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001963 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001964 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001965 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001966 conf->agent->rt[tid].applets_act++;
Emeric Brun5f77fef2017-05-29 15:26:51 +02001967
Emeric Brunc60def82017-09-27 14:59:38 +02001968 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001969 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001970 return appctx;
1971
1972 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001973 out_free_sess:
1974 session_free(sess);
1975 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001976 task_free(SPOE_APPCTX(appctx)->task);
1977 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001978 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001979 out_free_appctx:
1980 appctx_free(appctx);
1981 out_error:
1982 return NULL;
1983}
1984
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001985static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001986spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987{
1988 struct spoe_config *conf = FLT_CONF(ctx->filter);
1989 struct spoe_agent *agent = conf->agent;
1990 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001991 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992
Christopher Fauleta1cda022016-12-21 08:58:06 +01001993 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulet42097792018-01-24 15:49:45 +01001994 if (agent->rt[tid].applets_idle &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001995 agent->rt[tid].sending_rate)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001996 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997
1998 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01001999 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002000 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2001 ctx->strm);
2002
Christopher Fauleta1cda022016-12-21 08:58:06 +01002003 /* Do not try to create a new applet if there is no server up for the
2004 * agent's backend. */
2005 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2006 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2007 " - cannot create SPOE appctx: no server up\n",
2008 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2009 __FUNCTION__, ctx->strm);
2010 goto end;
2011 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012
Christopher Fauleta1cda022016-12-21 08:58:06 +01002013 /* Do not try to create a new applet if we have reached the maximum of
2014 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002015 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002016 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002017 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2018 " - cannot create SPOE appctx: max CPS reached\n",
2019 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2020 __FUNCTION__, ctx->strm);
2021 goto end;
2022 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023 }
2024
Christopher Faulet8ef75252017-02-20 22:56:03 +01002025 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002026 if (appctx == NULL) {
2027 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2028 " - failed to create SPOE appctx\n",
2029 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2030 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002031 send_log(ctx->strm->be, LOG_EMERG,
2032 "SPOE: [%s] failed to create SPOE applet\n",
2033 agent->id);
2034
Christopher Fauleta1cda022016-12-21 08:58:06 +01002035 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036 }
2037
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 /* Increase the per-process number of cumulated connections */
2039 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002040 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 end:
2043 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002044 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002045 ctx->status_code = SPOE_CTX_ERR_RES;
2046 return -1;
2047 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002048
Christopher Fauleta1cda022016-12-21 08:58:06 +01002049 /* Add the SPOE context in the sending queue and update all running
2050 * info */
Christopher Faulet24289f22017-09-25 14:48:02 +02002051 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
2052 if (agent->rt[tid].sending_rate)
2053 agent->rt[tid].sending_rate--;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002054
2055 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002056 " - Add stream in sending queue"
2057 " - applets_act=%u - applets_idle=%u - sending_rate=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002059 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
2060 agent->rt[tid].sending_rate);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002061
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 /* Finally try to wakeup the first IDLE applet found and move it at the
2063 * end of the list. */
Christopher Faulet24289f22017-09-25 14:48:02 +02002064 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list) {
Christopher Faulet42bfa462017-01-04 14:14:19 +01002065 appctx = spoe_appctx->owner;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002066 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002067 spoe_wakeup_appctx(appctx);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002068 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002069 LIST_DEL(&spoe_appctx->list);
Christopher Faulet24289f22017-09-25 14:48:02 +02002070 LIST_ADDQ(&agent->rt[tid].applets, &spoe_appctx->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002071 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002072 break;
2073 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002075 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002076}
2077
2078/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002079 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002081/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2082 * fragmented_content. If the next message can be processed, it returns 0. If
2083 * the message is too big, it returns -1.*/
2084static int
2085spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2086 struct spoe_message *msg, int dir,
2087 char **buf, char *end)
2088{
2089 struct sample *smp;
2090 struct spoe_arg *arg;
2091 int ret;
2092
2093 if (msg->cond) {
2094 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2095 ret = acl_pass(ret);
2096 if (msg->cond->pol == ACL_COND_UNLESS)
2097 ret = !ret;
2098
2099 /* the rule does not match */
2100 if (!ret)
2101 goto next;
2102 }
2103
2104 /* Resume encoding of a SPOE argument */
2105 if (ctx->frag_ctx.curarg != NULL) {
2106 arg = ctx->frag_ctx.curarg;
2107 goto encode_argument;
2108 }
2109
2110 if (ctx->frag_ctx.curoff != UINT_MAX)
2111 goto encode_msg_payload;
2112
2113 /* Check if there is enough space for the message name and the
2114 * number of arguments. It implies <msg->id_len> is encoded on 2
2115 * bytes, at most (< 2288). */
2116 if (*buf + 2 + msg->id_len + 1 > end)
2117 goto too_big;
2118
2119 /* Encode the message name */
2120 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2121 goto too_big;
2122
2123 /* Set the number of arguments for this message */
2124 **buf = msg->nargs;
2125 (*buf)++;
2126
2127 ctx->frag_ctx.curoff = 0;
2128 encode_msg_payload:
2129
2130 /* Loop on arguments */
2131 list_for_each_entry(arg, &msg->args, list) {
2132 ctx->frag_ctx.curarg = arg;
2133 ctx->frag_ctx.curoff = UINT_MAX;
2134
2135 encode_argument:
2136 if (ctx->frag_ctx.curoff != UINT_MAX)
2137 goto encode_arg_value;
2138
2139 /* Encode the arguement name as a string. It can by NULL */
2140 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2141 goto too_big;
2142
2143 ctx->frag_ctx.curoff = 0;
2144 encode_arg_value:
2145
2146 /* Fetch the arguement value */
2147 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2148 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2149 if (ret == -1 || ctx->frag_ctx.curoff)
2150 goto too_big;
2151 }
2152
2153 next:
2154 return 0;
2155
2156 too_big:
2157 return -1;
2158}
2159
Christopher Fauletc718b822017-09-21 16:50:56 +02002160/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2161 * handle fragmented content. On success it returns 1. If an error occurred, -1
2162 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2163 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002164static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002165spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002166 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002167{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002168 struct spoe_config *conf = FLT_CONF(ctx->filter);
2169 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002170 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002171 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002172
Christopher Faulet8ef75252017-02-20 22:56:03 +01002173 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002174 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002175
Christopher Fauletc718b822017-09-21 16:50:56 +02002176 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2177 /* Resume encoding of a SPOE message */
2178 if (ctx->frag_ctx.curmsg != NULL) {
2179 msg = ctx->frag_ctx.curmsg;
2180 goto encode_evt_message;
2181 }
2182
2183 list_for_each_entry(msg, messages, by_evt) {
2184 ctx->frag_ctx.curmsg = msg;
2185 ctx->frag_ctx.curarg = NULL;
2186 ctx->frag_ctx.curoff = UINT_MAX;
2187
2188 encode_evt_message:
2189 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2190 goto too_big;
2191 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002192 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002193 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2194 /* Resume encoding of a SPOE message */
2195 if (ctx->frag_ctx.curmsg != NULL) {
2196 msg = ctx->frag_ctx.curmsg;
2197 goto encode_grp_message;
2198 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002199
Christopher Fauletc718b822017-09-21 16:50:56 +02002200 list_for_each_entry(msg, messages, by_grp) {
2201 ctx->frag_ctx.curmsg = msg;
2202 ctx->frag_ctx.curarg = NULL;
2203 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002204
Christopher Fauletc718b822017-09-21 16:50:56 +02002205 encode_grp_message:
2206 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2207 goto too_big;
2208 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002209 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002210 else
2211 goto skip;
2212
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002213
Christopher Faulet57583e42017-09-04 15:41:09 +02002214 /* nothing has been encoded for an unfragmented payload */
2215 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2216 goto skip;
2217
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002218 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002219 " - encode %s messages - spoe_appctx=%p"
2220 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002221 (int)now.tv_sec, (int)now.tv_usec,
2222 agent->id, __FUNCTION__, s,
2223 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Faulet24289f22017-09-25 14:48:02 +02002224 ctx->frag_ctx.spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002225 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002226
Christopher Faulet8ef75252017-02-20 22:56:03 +01002227 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002228 ctx->frag_ctx.curmsg = NULL;
2229 ctx->frag_ctx.curarg = NULL;
2230 ctx->frag_ctx.curoff = 0;
2231 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002232
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002233 return 1;
2234
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002236 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2237 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2238 return -1;
2239 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002240
2241 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002242 " - encode fragmented messages - spoe_appctx=%p"
2243 " - curmsg=%p - curarg=%p - curoff=%u"
2244 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245 (int)now.tv_sec, (int)now.tv_usec,
2246 agent->id, __FUNCTION__, s, ctx->frag_ctx.spoe_appctx,
2247 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002248 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249
Christopher Faulet8ef75252017-02-20 22:56:03 +01002250 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002251 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2252 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2253 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002254
2255 skip:
2256 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2257 " - skip the frame because nothing has been encoded\n",
2258 (int)now.tv_sec, (int)now.tv_usec,
2259 agent->id, __FUNCTION__, s);
2260 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002261}
2262
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263
2264/***************************************************************************
2265 * Functions that handle SPOE actions
2266 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002267/* Helper function to set a variable */
2268static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002269spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002270 struct sample *smp)
2271{
2272 struct spoe_config *conf = FLT_CONF(ctx->filter);
2273 struct spoe_agent *agent = conf->agent;
2274 char varname[64];
2275
2276 memset(varname, 0, sizeof(varname));
2277 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2278 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002279 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2280 vars_set_by_name(varname, len, smp);
2281 else
2282 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002283}
2284
2285/* Helper function to unset a variable */
2286static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002287spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002288 struct sample *smp)
2289{
2290 struct spoe_config *conf = FLT_CONF(ctx->filter);
2291 struct spoe_agent *agent = conf->agent;
2292 char varname[64];
2293
2294 memset(varname, 0, sizeof(varname));
2295 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2296 scope, agent->var_pfx, len, name);
2297 vars_unset_by_name_ifexist(varname, len, smp);
2298}
2299
2300
Christopher Faulet8ef75252017-02-20 22:56:03 +01002301static inline int
2302spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2303 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002304{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002305 char *str, *scope, *p = *buf;
2306 struct sample smp;
2307 uint64_t sz;
2308 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002309
Christopher Faulet8ef75252017-02-20 22:56:03 +01002310 if (p + 2 >= end)
2311 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002312
Christopher Faulet8ef75252017-02-20 22:56:03 +01002313 /* SET-VAR requires 3 arguments */
2314 if (*p++ != 3)
2315 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002316
Christopher Faulet8ef75252017-02-20 22:56:03 +01002317 switch (*p++) {
2318 case SPOE_SCOPE_PROC: scope = "proc"; break;
2319 case SPOE_SCOPE_SESS: scope = "sess"; break;
2320 case SPOE_SCOPE_TXN : scope = "txn"; break;
2321 case SPOE_SCOPE_REQ : scope = "req"; break;
2322 case SPOE_SCOPE_RES : scope = "res"; break;
2323 default: goto skip;
2324 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002325
Christopher Faulet8ef75252017-02-20 22:56:03 +01002326 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2327 goto skip;
2328 memset(&smp, 0, sizeof(smp));
2329 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002330
Christopher Faulet8ef75252017-02-20 22:56:03 +01002331 if (spoe_decode_data(&p, end, &smp) == -1)
2332 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002333
Christopher Faulet8ef75252017-02-20 22:56:03 +01002334 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2335 " - set-var '%s.%s.%.*s'\n",
2336 (int)now.tv_sec, (int)now.tv_usec,
2337 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2338 __FUNCTION__, s, scope,
2339 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2340 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002341
Christopher Faulet8ef75252017-02-20 22:56:03 +01002342 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002343
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344 ret = (p - *buf);
2345 *buf = p;
2346 return ret;
2347 skip:
2348 return 0;
2349}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002350
Christopher Faulet8ef75252017-02-20 22:56:03 +01002351static inline int
2352spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2353 char **buf, char *end, int dir)
2354{
2355 char *str, *scope, *p = *buf;
2356 struct sample smp;
2357 uint64_t sz;
2358 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360 if (p + 2 >= end)
2361 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002362
Christopher Faulet8ef75252017-02-20 22:56:03 +01002363 /* UNSET-VAR requires 2 arguments */
2364 if (*p++ != 2)
2365 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002366
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367 switch (*p++) {
2368 case SPOE_SCOPE_PROC: scope = "proc"; break;
2369 case SPOE_SCOPE_SESS: scope = "sess"; break;
2370 case SPOE_SCOPE_TXN : scope = "txn"; break;
2371 case SPOE_SCOPE_REQ : scope = "req"; break;
2372 case SPOE_SCOPE_RES : scope = "res"; break;
2373 default: goto skip;
2374 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2377 goto skip;
2378 memset(&smp, 0, sizeof(smp));
2379 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2382 " - unset-var '%s.%s.%.*s'\n",
2383 (int)now.tv_sec, (int)now.tv_usec,
2384 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2385 __FUNCTION__, s, scope,
2386 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2387 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002388
Christopher Faulet8ef75252017-02-20 22:56:03 +01002389 spoe_unset_var(ctx, scope, str, sz, &smp);
2390
2391 ret = (p - *buf);
2392 *buf = p;
2393 return ret;
2394 skip:
2395 return 0;
2396}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002397
Christopher Faulet8ef75252017-02-20 22:56:03 +01002398/* Process SPOE actions for a specific event. It returns 1 on success. If an
2399 * error occurred, 0 is returned. */
2400static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002401spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002402{
2403 char *p, *end;
2404 int ret;
2405
2406 p = ctx->buffer->p;
2407 end = p + ctx->buffer->i;
2408
2409 while (p < end) {
2410 enum spoe_action_type type;
2411
2412 type = *p++;
2413 switch (type) {
2414 case SPOE_ACT_T_SET_VAR:
2415 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2416 if (!ret)
2417 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002419
Christopher Faulet8ef75252017-02-20 22:56:03 +01002420 case SPOE_ACT_T_UNSET_VAR:
2421 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2422 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002423 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002424 break;
2425
2426 default:
2427 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428 }
2429 }
2430
2431 return 1;
2432 skip:
2433 return 0;
2434}
2435
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002436/***************************************************************************
2437 * Functions that process SPOE events
2438 **************************************************************************/
2439static inline int
Christopher Faulet58d03682017-09-21 16:57:24 +02002440spoe_start_processing(struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002441{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002442 /* If a process is already started for this SPOE context, retry
2443 * later. */
2444 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002445 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002446
2447 /* Set the right flag to prevent request and response processing
2448 * in same time. */
2449 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2450 ? SPOE_CTX_FL_REQ_PROCESS
2451 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002452 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002453}
2454
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002455static inline void
Christopher Faulet58d03682017-09-21 16:57:24 +02002456spoe_stop_processing(struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002457{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002458 struct spoe_appctx *sa = ctx->frag_ctx.spoe_appctx;
2459
2460 if (sa) {
2461 sa->frag_ctx.ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002462 spoe_wakeup_appctx(sa->owner);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002463 }
2464
Christopher Fauleta1cda022016-12-21 08:58:06 +01002465 /* Reset the flag to allow next processing */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002466 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002467
Christopher Fauletb067b062017-01-04 16:39:11 +01002468 ctx->status_code = 0;
2469
Christopher Fauleta1cda022016-12-21 08:58:06 +01002470 /* Reset processing timer */
2471 ctx->process_exp = TICK_ETERNITY;
2472
Christopher Faulet8ef75252017-02-20 22:56:03 +01002473 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002474
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002475 ctx->frag_ctx.spoe_appctx = NULL;
2476 ctx->frag_ctx.curmsg = NULL;
2477 ctx->frag_ctx.curarg = NULL;
2478 ctx->frag_ctx.curoff = 0;
2479 ctx->frag_ctx.flags = 0;
2480
Christopher Fauleta1cda022016-12-21 08:58:06 +01002481 if (!LIST_ISEMPTY(&ctx->list)) {
2482 LIST_DEL(&ctx->list);
2483 LIST_INIT(&ctx->list);
2484 }
2485}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002486
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002487static void
2488spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2489 struct spoe_context *ctx, int dir)
2490{
2491 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002492 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002493
2494 if (agent->var_on_error) {
2495 struct sample smp;
2496
2497 memset(&smp, 0, sizeof(smp));
2498 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2499 smp.data.u.sint = ctx->status_code;
2500 smp.data.type = SMP_T_BOOL;
2501
2502 spoe_set_var(ctx, "txn", agent->var_on_error,
2503 strlen(agent->var_on_error), &smp);
2504 }
2505 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2506 " - failed to process messages: code=%u\n",
2507 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2508 __FUNCTION__, s, ctx->status_code);
2509 send_log(ctx->strm->be, LOG_WARNING,
2510 "SPOE: [%s] failed to process messages: code=%u\n",
2511 agent->id, ctx->status_code);
2512
2513 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2514 ? SPOE_CTX_ST_READY
2515 : SPOE_CTX_ST_NONE);
2516}
2517
Christopher Faulet58d03682017-09-21 16:57:24 +02002518/* Process a list of SPOE messages. First, this functions will process messages
2519 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2520 * to process corresponding actions. During all the processing, it returns 0
2521 * and it returns 1 when the processing is finished. If an error occurred, -1
2522 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002523static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002524spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2525 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002526{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002527 struct spoe_config *conf = FLT_CONF(ctx->filter);
2528 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002529 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002530
2531 if (ctx->state == SPOE_CTX_ST_ERROR)
2532 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002533
2534 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2535 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002536 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002537 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002538 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002539 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002540 goto error;
2541 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002542
2543 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002544 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002545 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002546 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002547 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002548 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002549 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002550 goto skip;
2551 }
2552 }
2553
Christopher Fauletf7a30922016-11-10 15:04:51 +01002554 if (!tick_isset(ctx->process_exp)) {
2555 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2556 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2557 ctx->process_exp);
2558 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002559 ret = spoe_start_processing(ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002560 if (!ret)
2561 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002562
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002563 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002565 }
2566
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002567 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002568 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002570 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002571 if (ret < 0)
2572 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002573 if (!ret)
2574 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002575 if (spoe_queue_context(ctx) < 0)
2576 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002577 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2578 }
2579
2580 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
2581 if (ctx->frag_ctx.spoe_appctx)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002582 spoe_wakeup_appctx(ctx->frag_ctx.spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002583 ret = 0;
2584 goto out;
2585 }
2586
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002587 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2588 ret = 0;
2589 goto out;
2590 }
2591
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002592 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002593 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002594 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002595 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002596 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002597 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002598 }
2599
2600 out:
2601 return ret;
2602
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002603 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002604 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002605 ret = 1;
2606 goto end;
2607
2608 skip:
2609 ctx->state = SPOE_CTX_ST_READY;
2610 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002611
Christopher Fauleta1cda022016-12-21 08:58:06 +01002612 end:
Christopher Faulet58d03682017-09-21 16:57:24 +02002613 spoe_stop_processing(ctx);
2614 return ret;
2615}
2616
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002617/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2618 * See spoe_process_message for details. */
2619static int
2620spoe_process_group(struct stream *s, struct spoe_context *ctx,
2621 struct spoe_group *group, int dir)
2622{
2623 int ret;
2624
2625 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2626 " - ctx-state=%s - Process messages for group=%s\n",
2627 (int)now.tv_sec, (int)now.tv_usec,
2628 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2629 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2630 group->id);
2631
2632 if (LIST_ISEMPTY(&group->messages))
2633 return 1;
2634
2635 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
2636 return ret;
2637}
2638
Christopher Faulet58d03682017-09-21 16:57:24 +02002639/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2640 * See spoe_process_message for details. */
2641static int
2642spoe_process_event(struct stream *s, struct spoe_context *ctx,
2643 enum spoe_event ev)
2644{
2645 int dir, ret;
2646
2647 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002648 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Faulet58d03682017-09-21 16:57:24 +02002649 (int)now.tv_sec, (int)now.tv_usec,
2650 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2651 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2652 spoe_event_str[ev]);
2653
2654 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2655
2656 if (LIST_ISEMPTY(&(ctx->events[ev])))
2657 return 1;
2658
2659 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002660 return ret;
2661}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002662
2663/***************************************************************************
2664 * Functions that create/destroy SPOE contexts
2665 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002666static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002667spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002668{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002669 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002670 return 1;
2671
Christopher Faulet4596fb72017-01-11 14:05:19 +01002672 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002673 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002674 LIST_DEL(&buffer_wait->list);
2675 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002676 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002677 }
2678
Christopher Faulet4596fb72017-01-11 14:05:19 +01002679 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002680 return 1;
2681
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002682 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002683 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002684 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002685 return 0;
2686}
2687
2688static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002689spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002690{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002691 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002692 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002693 LIST_DEL(&buffer_wait->list);
2694 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002695 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002696 }
2697
2698 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002699 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002700 b_free(buf);
2701 offer_buffers(buffer_wait->target,
2702 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 }
2704}
2705
Christopher Faulet4596fb72017-01-11 14:05:19 +01002706static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002707spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002708{
2709 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2710 return 1;
2711}
2712
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002713static struct spoe_context *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002714spoe_create_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002715{
2716 struct spoe_config *conf = FLT_CONF(filter);
2717 struct spoe_context *ctx;
2718
Willy Tarreaubafbe012017-11-24 17:34:44 +01002719 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002720 if (ctx == NULL) {
2721 return NULL;
2722 }
2723 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002724 ctx->filter = filter;
2725 ctx->state = SPOE_CTX_ST_NONE;
2726 ctx->status_code = SPOE_CTX_ERR_NONE;
2727 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002728 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002729 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002730 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002731 LIST_INIT(&ctx->buffer_wait.list);
2732 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002733 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002734 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002735
Christopher Fauletf7a30922016-11-10 15:04:51 +01002736 ctx->stream_id = 0;
2737 ctx->frame_id = 1;
2738 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002739
2740 return ctx;
2741}
2742
2743static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002744spoe_destroy_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002745{
2746 if (!ctx)
2747 return;
2748
Christopher Faulet58d03682017-09-21 16:57:24 +02002749 spoe_stop_processing(ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002750 pool_free(pool_head_spoe_ctx, ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002751}
2752
2753static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002754spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002755{
2756 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002757 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002758}
2759
2760
2761/***************************************************************************
2762 * Hooks that manage the filter lifecycle (init/check/deinit)
2763 **************************************************************************/
2764/* Signal handler: Do a soft stop, wakeup SPOE applet */
2765static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002766spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002767{
2768 struct proxy *p;
2769
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002770 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002771 while (p) {
2772 struct flt_conf *fconf;
2773
2774 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002775 struct spoe_config *conf;
2776 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002777 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002778 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002779
Christopher Faulet3b386a32017-02-23 10:17:15 +01002780 if (fconf->id != spoe_filter_id)
2781 continue;
2782
2783 conf = fconf->conf;
2784 agent = conf->agent;
2785
Christopher Faulet24289f22017-09-25 14:48:02 +02002786 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002787 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002788 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2789 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002790 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002791 }
2792 }
2793 p = p->next;
2794 }
2795}
2796
2797
2798/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2799static int
2800spoe_init(struct proxy *px, struct flt_conf *fconf)
2801{
2802 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002803
2804 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2805 init_new_proxy(&conf->agent_fe);
2806 conf->agent_fe.parent = conf->agent;
2807 conf->agent_fe.last_change = now.tv_sec;
2808 conf->agent_fe.id = conf->agent->id;
2809 conf->agent_fe.cap = PR_CAP_FE;
2810 conf->agent_fe.mode = PR_MODE_TCP;
2811 conf->agent_fe.maxconn = 0;
2812 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2813 conf->agent_fe.conn_retries = CONN_RETRIES;
2814 conf->agent_fe.accept = frontend_accept;
2815 conf->agent_fe.srv = NULL;
2816 conf->agent_fe.timeout.client = TICK_ETERNITY;
2817 conf->agent_fe.default_target = &spoe_applet.obj_type;
2818 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2819
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002820 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002821 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002822 sighandler_registered = 1;
2823 }
2824
2825 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002826}
2827
2828/* Free ressources allocated by the SPOE filter. */
2829static void
2830spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2831{
2832 struct spoe_config *conf = fconf->conf;
2833
2834 if (conf) {
2835 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002836
Christopher Faulet8ef75252017-02-20 22:56:03 +01002837 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002838 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002839 free(conf);
2840 }
2841 fconf->conf = NULL;
2842}
2843
2844/* Check configuration of a SPOE filter for a specified proxy.
2845 * Return 1 on error, else 0. */
2846static int
2847spoe_check(struct proxy *px, struct flt_conf *fconf)
2848{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002849 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002850 struct spoe_config *conf = fconf->conf;
2851 struct proxy *target;
2852
Christopher Faulet7ee86672017-09-19 11:08:28 +02002853 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2854 * are uniq */
2855 list_for_each_entry(f, &px->filter_configs, list) {
2856 struct spoe_config *c = f->conf;
2857
2858 /* This is not an SPOE filter */
2859 if (f->id != spoe_filter_id)
2860 continue;
2861 /* This is the current SPOE filter */
2862 if (f == fconf)
2863 continue;
2864
2865 /* Check engine Id. It should be uniq */
2866 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002867 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2868 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002869 return 1;
2870 }
2871 }
2872
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002873 target = proxy_be_by_name(conf->agent->b.name);
2874 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002875 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
2876 " declared at %s:%d.\n",
2877 px->id, conf->agent->b.name, conf->agent->id,
2878 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002879 return 1;
2880 }
2881 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002882 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
2883 " at %s:%d does not support HTTP mode.\n",
2884 px->id, target->id, conf->agent->id,
2885 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002886 return 1;
2887 }
2888
2889 free(conf->agent->b.name);
2890 conf->agent->b.name = NULL;
2891 conf->agent->b.be = target;
2892 return 0;
2893}
2894
2895/**************************************************************************
2896 * Hooks attached to a stream
2897 *************************************************************************/
2898/* Called when a filter instance is created and attach to a stream. It creates
2899 * the context that will be used to process this stream. */
2900static int
2901spoe_start(struct stream *s, struct filter *filter)
2902{
Christopher Faulet72bcc472017-01-04 16:39:41 +01002903 struct spoe_config *conf = FLT_CONF(filter);
2904 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002905 struct spoe_context *ctx;
2906
2907 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01002908 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002909 __FUNCTION__, s);
2910
Christopher Faulet8ef75252017-02-20 22:56:03 +01002911 ctx = spoe_create_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002912 if (ctx == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01002913 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2914 " - failed to create SPOE context\n",
2915 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02002916 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002917 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002918 "SPOE: [%s] failed to create SPOE context\n",
2919 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002920 return 0;
2921 }
2922
2923 ctx->strm = s;
2924 ctx->state = SPOE_CTX_ST_READY;
2925 filter->ctx = ctx;
2926
Christopher Faulet11610f32017-09-21 10:23:10 +02002927 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002928 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
2929
Christopher Faulet11610f32017-09-21 10:23:10 +02002930 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002931 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
2932
Christopher Faulet11610f32017-09-21 10:23:10 +02002933 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002934 filter->pre_analyzers |= AN_RES_INSPECT;
2935
Christopher Faulet11610f32017-09-21 10:23:10 +02002936 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002937 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
2938
Christopher Faulet11610f32017-09-21 10:23:10 +02002939 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
2941
Christopher Faulet11610f32017-09-21 10:23:10 +02002942 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002943 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
2944
2945 return 1;
2946}
2947
2948/* Called when a filter instance is detached from a stream. It release the
2949 * attached SPOE context. */
2950static void
2951spoe_stop(struct stream *s, struct filter *filter)
2952{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002953 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
2954 (int)now.tv_sec, (int)now.tv_usec,
2955 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2956 __FUNCTION__, s);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002957 spoe_destroy_context(filter->ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002958}
2959
Christopher Fauletf7a30922016-11-10 15:04:51 +01002960
2961/*
2962 * Called when the stream is woken up because of expired timer.
2963 */
2964static void
2965spoe_check_timeouts(struct stream *s, struct filter *filter)
2966{
2967 struct spoe_context *ctx = filter->ctx;
2968
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002969 if (tick_is_expired(ctx->process_exp, now_ms)) {
2970 s->pending_events |= TASK_WOKEN_MSG;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002971 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002972 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01002973}
2974
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002975/* Called when we are ready to filter data on a channel */
2976static int
2977spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
2978{
2979 struct spoe_context *ctx = filter->ctx;
2980 int ret = 1;
2981
2982 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
2983 " - ctx-flags=0x%08x\n",
2984 (int)now.tv_sec, (int)now.tv_usec,
2985 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
2986 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
2987
Christopher Fauletb067b062017-01-04 16:39:11 +01002988 if (ctx->state == SPOE_CTX_ST_NONE)
2989 goto out;
2990
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002991 if (!(chn->flags & CF_ISRESP)) {
2992 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
2993 chn->analysers |= AN_REQ_INSPECT_FE;
2994 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
2995 chn->analysers |= AN_REQ_INSPECT_BE;
2996
2997 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
2998 goto out;
2999
3000 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003001 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003002 if (!ret)
3003 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003004 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3005 }
3006 else {
3007 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3008 chn->analysers |= AN_RES_INSPECT;
3009
3010 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3011 goto out;
3012
Christopher Faulet8ef75252017-02-20 22:56:03 +01003013 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003014 if (!ret) {
3015 channel_dont_read(chn);
3016 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003017 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003018 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003019 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003020 }
3021
3022 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023 return ret;
3024}
3025
3026/* Called before a processing happens on a given channel */
3027static int
3028spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3029 struct channel *chn, unsigned an_bit)
3030{
3031 struct spoe_context *ctx = filter->ctx;
3032 int ret = 1;
3033
3034 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3035 " - ctx-flags=0x%08x - ana=0x%08x\n",
3036 (int)now.tv_sec, (int)now.tv_usec,
3037 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3038 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3039 ctx->flags, an_bit);
3040
Christopher Fauletb067b062017-01-04 16:39:11 +01003041 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042 goto out;
3043
3044 switch (an_bit) {
3045 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003046 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003047 break;
3048 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003049 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003050 break;
3051 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003052 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003053 break;
3054 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003055 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056 break;
3057 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003058 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003059 break;
3060 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003061 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003062 break;
3063 }
3064
3065 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003066 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003067 channel_dont_read(chn);
3068 channel_dont_close(chn);
3069 }
3070 return ret;
3071}
3072
3073/* Called when the filtering on the channel ends. */
3074static int
3075spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3076{
3077 struct spoe_context *ctx = filter->ctx;
3078
3079 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3080 " - ctx-flags=0x%08x\n",
3081 (int)now.tv_sec, (int)now.tv_usec,
3082 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3083 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3084
3085 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003086 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003087 }
3088
3089 return 1;
3090}
3091
3092/********************************************************************
3093 * Functions that manage the filter initialization
3094 ********************************************************************/
3095struct flt_ops spoe_ops = {
3096 /* Manage SPOE filter, called for each filter declaration */
3097 .init = spoe_init,
3098 .deinit = spoe_deinit,
3099 .check = spoe_check,
3100
3101 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003102 .attach = spoe_start,
3103 .detach = spoe_stop,
3104 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003105
3106 /* Handle channels activity */
3107 .channel_start_analyze = spoe_start_analyze,
3108 .channel_pre_analyze = spoe_chn_pre_analyze,
3109 .channel_end_analyze = spoe_end_analyze,
3110};
3111
3112
3113static int
3114cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3115{
3116 const char *err;
3117 int i, err_code = 0;
3118
3119 if ((cfg_scope == NULL && curengine != NULL) ||
3120 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003121 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003122 goto out;
3123
3124 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3125 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003126 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3127 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003128 err_code |= ERR_ALERT | ERR_ABORT;
3129 goto out;
3130 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003131 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3132 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003133 goto out;
3134 }
3135
3136 err = invalid_char(args[1]);
3137 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003138 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3139 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003140 err_code |= ERR_ALERT | ERR_ABORT;
3141 goto out;
3142 }
3143
3144 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003145 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3146 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003147 err_code |= ERR_ALERT | ERR_ABORT;
3148 goto out;
3149 }
3150 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003151 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003152 err_code |= ERR_ALERT | ERR_ABORT;
3153 goto out;
3154 }
3155
3156 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003157
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003158 curagent->conf.file = strdup(file);
3159 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003160
3161 curagent->timeout.hello = TICK_ETERNITY;
3162 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003163 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003164
3165 curagent->engine_id = NULL;
3166 curagent->var_pfx = NULL;
3167 curagent->var_on_error = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003168 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3169 if (global.nbthread == 1)
3170 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003171 curagent->cps_max = 0;
3172 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003173 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003174 curagent->max_fpa = 100;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003175
3176 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003177 LIST_INIT(&curagent->events[i]);
3178 LIST_INIT(&curagent->groups);
3179 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003180
Christopher Faulet24289f22017-09-25 14:48:02 +02003181 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003182 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003183 err_code |= ERR_ALERT | ERR_ABORT;
3184 goto out;
3185 }
3186 for (i = 0; i < global.nbthread; ++i) {
3187 curagent->rt[i].frame_size = curagent->max_frame_size;
3188 curagent->rt[i].applets_act = 0;
3189 curagent->rt[i].applets_idle = 0;
3190 curagent->rt[i].sending_rate = 0;
3191 LIST_INIT(&curagent->rt[i].applets);
3192 LIST_INIT(&curagent->rt[i].sending_queue);
3193 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003194 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003195 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003196 }
3197 else if (!strcmp(args[0], "use-backend")) {
3198 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003199 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3200 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003201 err_code |= ERR_ALERT | ERR_FATAL;
3202 goto out;
3203 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003204 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003205 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003206 free(curagent->b.name);
3207 curagent->b.name = strdup(args[1]);
3208 }
3209 else if (!strcmp(args[0], "messages")) {
3210 int cur_arg = 1;
3211 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003212 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003213
Christopher Faulet11610f32017-09-21 10:23:10 +02003214 list_for_each_entry(ph, &curmphs, list) {
3215 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003216 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3217 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003218 err_code |= ERR_ALERT | ERR_FATAL;
3219 goto out;
3220 }
3221 }
3222
Christopher Faulet11610f32017-09-21 10:23:10 +02003223 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003224 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003225 err_code |= ERR_ALERT | ERR_ABORT;
3226 goto out;
3227 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003228 ph->id = strdup(args[cur_arg]);
3229 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230 cur_arg++;
3231 }
3232 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003233 else if (!strcmp(args[0], "groups")) {
3234 int cur_arg = 1;
3235 while (*args[cur_arg]) {
3236 struct spoe_placeholder *ph = NULL;
3237
3238 list_for_each_entry(ph, &curgphs, list) {
3239 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003240 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3241 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003242 err_code |= ERR_ALERT | ERR_FATAL;
3243 goto out;
3244 }
3245 }
3246
3247 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003248 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003249 err_code |= ERR_ALERT | ERR_ABORT;
3250 goto out;
3251 }
3252 ph->id = strdup(args[cur_arg]);
3253 LIST_ADDQ(&curgphs, &ph->list);
3254 cur_arg++;
3255 }
3256 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003257 else if (!strcmp(args[0], "timeout")) {
3258 unsigned int *tv = NULL;
3259 const char *res;
3260 unsigned timeout;
3261
3262 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003263 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3264 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003265 err_code |= ERR_ALERT | ERR_FATAL;
3266 goto out;
3267 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003268 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3269 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003270 if (!strcmp(args[1], "hello"))
3271 tv = &curagent->timeout.hello;
3272 else if (!strcmp(args[1], "idle"))
3273 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003274 else if (!strcmp(args[1], "processing"))
3275 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003277 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3278 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 err_code |= ERR_ALERT | ERR_FATAL;
3280 goto out;
3281 }
3282 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003283 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3284 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003285 err_code |= ERR_ALERT | ERR_FATAL;
3286 goto out;
3287 }
3288 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3289 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003290 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3291 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003292 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003293 goto out;
3294 }
3295 *tv = MS_TO_TICKS(timeout);
3296 }
3297 else if (!strcmp(args[0], "option")) {
3298 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003299 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3300 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003301 err_code |= ERR_ALERT | ERR_FATAL;
3302 goto out;
3303 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003304
Christopher Faulet305c6072017-02-23 16:17:53 +01003305 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003306 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003307 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003308 if (kwm == 1)
3309 curagent->flags &= ~SPOE_FL_PIPELINING;
3310 else
3311 curagent->flags |= SPOE_FL_PIPELINING;
3312 goto out;
3313 }
3314 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003315 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003316 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003317 if (kwm == 1)
3318 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003319 else {
3320 if (global.nbthread == 1)
3321 curagent->flags |= SPOE_FL_ASYNC;
3322 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003323 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3324 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003325 err_code |= ERR_WARN;
3326 }
3327 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003328 goto out;
3329 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003330 else if (!strcmp(args[1], "send-frag-payload")) {
3331 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3332 goto out;
3333 if (kwm == 1)
3334 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3335 else
3336 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3337 goto out;
3338 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003339
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003340 /* Following options does not support negation */
3341 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003342 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3343 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003344 err_code |= ERR_ALERT | ERR_FATAL;
3345 goto out;
3346 }
3347
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003348 if (!strcmp(args[1], "var-prefix")) {
3349 char *tmp;
3350
3351 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003352 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3353 file, linenum, args[0],
3354 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003355 err_code |= ERR_ALERT | ERR_FATAL;
3356 goto out;
3357 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003358 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3359 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003360 tmp = args[2];
3361 while (*tmp) {
3362 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003363 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3364 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003365 err_code |= ERR_ALERT | ERR_FATAL;
3366 goto out;
3367 }
3368 tmp++;
3369 }
3370 curagent->var_pfx = strdup(args[2]);
3371 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003372 else if (!strcmp(args[1], "force-set-var")) {
3373 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3374 goto out;
3375 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3376 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003377 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003378 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003379 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003380 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3381 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003382 else if (!strcmp(args[1], "set-on-error")) {
3383 char *tmp;
3384
3385 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003386 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3387 file, linenum, args[0],
3388 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003389 err_code |= ERR_ALERT | ERR_FATAL;
3390 goto out;
3391 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003392 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3393 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003394 tmp = args[2];
3395 while (*tmp) {
3396 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003397 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3398 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003399 err_code |= ERR_ALERT | ERR_FATAL;
3400 goto out;
3401 }
3402 tmp++;
3403 }
3404 curagent->var_on_error = strdup(args[2]);
3405 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003406 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003407 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3408 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003409 err_code |= ERR_ALERT | ERR_FATAL;
3410 goto out;
3411 }
Christopher Faulet48026722016-11-16 15:01:12 +01003412 }
3413 else if (!strcmp(args[0], "maxconnrate")) {
3414 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3416 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003417 err_code |= ERR_ALERT | ERR_FATAL;
3418 goto out;
3419 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003420 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003421 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003422 curagent->cps_max = atol(args[1]);
3423 }
3424 else if (!strcmp(args[0], "maxerrrate")) {
3425 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003426 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3427 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003428 err_code |= ERR_ALERT | ERR_FATAL;
3429 goto out;
3430 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003431 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003432 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003433 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003435 else if (!strcmp(args[0], "max-frame-size")) {
3436 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003437 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3438 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003439 err_code |= ERR_ALERT | ERR_FATAL;
3440 goto out;
3441 }
3442 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3443 goto out;
3444 curagent->max_frame_size = atol(args[1]);
3445 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3446 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003447 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3448 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003449 err_code |= ERR_ALERT | ERR_FATAL;
3450 goto out;
3451 }
3452 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003453 else if (!strcmp(args[0], "register-var-names")) {
3454 int cur_arg;
3455
3456 if (!*args[1]) {
3457 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3458 file, linenum, args[0]);
3459 err_code |= ERR_ALERT | ERR_FATAL;
3460 goto out;
3461 }
3462 cur_arg = 1;
3463 while (*args[cur_arg]) {
3464 struct spoe_var_placeholder *vph;
3465
3466 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3467 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3468 err_code |= ERR_ALERT | ERR_ABORT;
3469 goto out;
3470 }
3471 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3472 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3473 err_code |= ERR_ALERT | ERR_ABORT;
3474 goto out;
3475 }
3476 LIST_ADDQ(&curvars, &vph->list);
3477 cur_arg++;
3478 }
3479 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003480 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003481 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3482 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003483 err_code |= ERR_ALERT | ERR_FATAL;
3484 goto out;
3485 }
3486 out:
3487 return err_code;
3488}
Christopher Faulet11610f32017-09-21 10:23:10 +02003489static int
3490cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3491{
3492 struct spoe_group *grp;
3493 const char *err;
3494 int err_code = 0;
3495
3496 if ((cfg_scope == NULL && curengine != NULL) ||
3497 (cfg_scope != NULL && curengine == NULL) ||
3498 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3499 goto out;
3500
3501 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3502 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003503 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3504 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003505 err_code |= ERR_ALERT | ERR_ABORT;
3506 goto out;
3507 }
3508 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3509 err_code |= ERR_ABORT;
3510 goto out;
3511 }
3512
3513 err = invalid_char(args[1]);
3514 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003515 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3516 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003517 err_code |= ERR_ALERT | ERR_ABORT;
3518 goto out;
3519 }
3520
3521 list_for_each_entry(grp, &curgrps, list) {
3522 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003523 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3524 " name as another one declared at %s:%d.\n",
3525 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003526 err_code |= ERR_ALERT | ERR_FATAL;
3527 goto out;
3528 }
3529 }
3530
3531 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003532 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003533 err_code |= ERR_ALERT | ERR_ABORT;
3534 goto out;
3535 }
3536
3537 curgrp->id = strdup(args[1]);
3538 curgrp->conf.file = strdup(file);
3539 curgrp->conf.line = linenum;
3540 LIST_INIT(&curgrp->phs);
3541 LIST_INIT(&curgrp->messages);
3542 LIST_ADDQ(&curgrps, &curgrp->list);
3543 }
3544 else if (!strcmp(args[0], "messages")) {
3545 int cur_arg = 1;
3546 while (*args[cur_arg]) {
3547 struct spoe_placeholder *ph = NULL;
3548
3549 list_for_each_entry(ph, &curgrp->phs, list) {
3550 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003551 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3552 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003553 err_code |= ERR_ALERT | ERR_FATAL;
3554 goto out;
3555 }
3556 }
3557
3558 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003559 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003560 err_code |= ERR_ALERT | ERR_ABORT;
3561 goto out;
3562 }
3563 ph->id = strdup(args[cur_arg]);
3564 LIST_ADDQ(&curgrp->phs, &ph->list);
3565 cur_arg++;
3566 }
3567 }
3568 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003569 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3570 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003571 err_code |= ERR_ALERT | ERR_FATAL;
3572 goto out;
3573 }
3574 out:
3575 return err_code;
3576}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003577
3578static int
3579cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3580{
3581 struct spoe_message *msg;
3582 struct spoe_arg *arg;
3583 const char *err;
3584 char *errmsg = NULL;
3585 int err_code = 0;
3586
3587 if ((cfg_scope == NULL && curengine != NULL) ||
3588 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003589 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003590 goto out;
3591
3592 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3593 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003594 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3595 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003596 err_code |= ERR_ALERT | ERR_ABORT;
3597 goto out;
3598 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003599 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3600 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003601 goto out;
3602 }
3603
3604 err = invalid_char(args[1]);
3605 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003606 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3607 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003608 err_code |= ERR_ALERT | ERR_ABORT;
3609 goto out;
3610 }
3611
3612 list_for_each_entry(msg, &curmsgs, list) {
3613 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003614 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3615 " name as another one declared at %s:%d.\n",
3616 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003617 err_code |= ERR_ALERT | ERR_FATAL;
3618 goto out;
3619 }
3620 }
3621
3622 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003623 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003624 err_code |= ERR_ALERT | ERR_ABORT;
3625 goto out;
3626 }
3627
3628 curmsg->id = strdup(args[1]);
3629 curmsg->id_len = strlen(curmsg->id);
3630 curmsg->event = SPOE_EV_NONE;
3631 curmsg->conf.file = strdup(file);
3632 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003633 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003634 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003635 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003636 LIST_INIT(&curmsg->by_evt);
3637 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003638 LIST_ADDQ(&curmsgs, &curmsg->list);
3639 }
3640 else if (!strcmp(args[0], "args")) {
3641 int cur_arg = 1;
3642
3643 curproxy->conf.args.ctx = ARGC_SPOE;
3644 curproxy->conf.args.file = file;
3645 curproxy->conf.args.line = linenum;
3646 while (*args[cur_arg]) {
3647 char *delim = strchr(args[cur_arg], '=');
3648 int idx = 0;
3649
3650 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003651 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003652 err_code |= ERR_ALERT | ERR_ABORT;
3653 goto out;
3654 }
3655
3656 if (!delim) {
3657 arg->name = NULL;
3658 arg->name_len = 0;
3659 delim = args[cur_arg];
3660 }
3661 else {
3662 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3663 arg->name_len = delim - args[cur_arg];
3664 delim++;
3665 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003666 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3667 &idx, file, linenum, &errmsg,
3668 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003669 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003670 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003671 err_code |= ERR_ALERT | ERR_FATAL;
3672 free(arg->name);
3673 free(arg);
3674 goto out;
3675 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003676 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003677 LIST_ADDQ(&curmsg->args, &arg->list);
3678 cur_arg++;
3679 }
3680 curproxy->conf.args.file = NULL;
3681 curproxy->conf.args.line = 0;
3682 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003683 else if (!strcmp(args[0], "acl")) {
3684 err = invalid_char(args[1]);
3685 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003686 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3687 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003688 err_code |= ERR_ALERT | ERR_FATAL;
3689 goto out;
3690 }
3691 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003692 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3693 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003694 err_code |= ERR_ALERT | ERR_FATAL;
3695 goto out;
3696 }
3697 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003698 else if (!strcmp(args[0], "event")) {
3699 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003700 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003701 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003702 goto out;
3703 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003704 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3705 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003706
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003707 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3708 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3709 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3710 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3711
3712 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3713 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3714 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3715 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3716 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3717 curmsg->event = SPOE_EV_ON_TCP_RSP;
3718
3719 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3720 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3721 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3722 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3723 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3724 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3725 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003726 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3727 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003728 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003729 goto out;
3730 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003731
3732 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3733 struct acl_cond *cond;
3734
3735 cond = build_acl_cond(file, linenum, &curmsg->acls,
3736 curproxy, (const char **)args+2,
3737 &errmsg);
3738 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003739 ha_alert("parsing [%s:%d] : error detected while "
3740 "parsing an 'event %s' condition : %s.\n",
3741 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003742 err_code |= ERR_ALERT | ERR_FATAL;
3743 goto out;
3744 }
3745 curmsg->cond = cond;
3746 }
3747 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003748 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3749 "or 'unless' followed by a condition but found '%s'.\n",
3750 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003751 err_code |= ERR_ALERT | ERR_FATAL;
3752 goto out;
3753 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003754 }
3755 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003756 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3757 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003758 err_code |= ERR_ALERT | ERR_FATAL;
3759 goto out;
3760 }
3761 out:
3762 free(errmsg);
3763 return err_code;
3764}
3765
3766/* Return -1 on error, else 0 */
3767static int
3768parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3769 struct flt_conf *fconf, char **err, void *private)
3770{
3771 struct list backup_sections;
3772 struct spoe_config *conf;
3773 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003774 struct spoe_group *grp, *grpback;
3775 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003776 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003777 char *file = NULL, *engine = NULL;
3778 int ret, pos = *cur_arg + 1;
3779
3780 conf = calloc(1, sizeof(*conf));
3781 if (conf == NULL) {
3782 memprintf(err, "%s: out of memory", args[*cur_arg]);
3783 goto error;
3784 }
3785 conf->proxy = px;
3786
3787 while (*args[pos]) {
3788 if (!strcmp(args[pos], "config")) {
3789 if (!*args[pos+1]) {
3790 memprintf(err, "'%s' : '%s' option without value",
3791 args[*cur_arg], args[pos]);
3792 goto error;
3793 }
3794 file = args[pos+1];
3795 pos += 2;
3796 }
3797 else if (!strcmp(args[pos], "engine")) {
3798 if (!*args[pos+1]) {
3799 memprintf(err, "'%s' : '%s' option without value",
3800 args[*cur_arg], args[pos]);
3801 goto error;
3802 }
3803 engine = args[pos+1];
3804 pos += 2;
3805 }
3806 else {
3807 memprintf(err, "unknown keyword '%s'", args[pos]);
3808 goto error;
3809 }
3810 }
3811 if (file == NULL) {
3812 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
3813 goto error;
3814 }
3815
3816 /* backup sections and register SPOE sections */
3817 LIST_INIT(&backup_sections);
3818 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02003819 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
3820 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02003821 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003822
3823 /* Parse SPOE filter configuration file */
3824 curengine = engine;
3825 curproxy = px;
3826 curagent = NULL;
3827 curmsg = NULL;
Christopher Faulet11610f32017-09-21 10:23:10 +02003828 LIST_INIT(&curmsgs);
3829 LIST_INIT(&curgrps);
3830 LIST_INIT(&curmphs);
3831 LIST_INIT(&curgphs);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003832 LIST_INIT(&curvars);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003833 ret = readcfgfile(file);
3834 curproxy = NULL;
3835
3836 /* unregister SPOE sections and restore previous sections */
3837 cfg_unregister_sections();
3838 cfg_restore_sections(&backup_sections);
3839
3840 if (ret == -1) {
3841 memprintf(err, "Could not open configuration file %s : %s",
3842 file, strerror(errno));
3843 goto error;
3844 }
3845 if (ret & (ERR_ABORT|ERR_FATAL)) {
3846 memprintf(err, "Error(s) found in configuration file %s", file);
3847 goto error;
3848 }
3849
3850 /* Check SPOE agent */
3851 if (curagent == NULL) {
3852 memprintf(err, "No SPOE agent found in file %s", file);
3853 goto error;
3854 }
3855 if (curagent->b.name == NULL) {
3856 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
3857 curagent->id, curagent->conf.file, curagent->conf.line);
3858 goto error;
3859 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01003860 if (curagent->timeout.hello == TICK_ETERNITY ||
3861 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01003862 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003863 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
3864 " | While not properly invalid, you will certainly encounter various problems\n"
3865 " | with such a configuration. To fix this, please ensure that all following\n"
3866 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
3867 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003868 }
3869 if (curagent->var_pfx == NULL) {
3870 char *tmp = curagent->id;
3871
3872 while (*tmp) {
3873 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3874 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
3875 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
3876 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
3877 goto error;
3878 }
3879 tmp++;
3880 }
3881 curagent->var_pfx = strdup(curagent->id);
3882 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01003883 if (curagent->engine_id == NULL)
3884 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003885
Christopher Faulet11610f32017-09-21 10:23:10 +02003886 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003887 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
3888 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003889 goto finish;
3890 }
3891
Christopher Faulet11610f32017-09-21 10:23:10 +02003892 /* Replace placeholders by the corresponding messages for the SPOE
3893 * agent */
3894 list_for_each_entry(ph, &curmphs, list) {
3895 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01003896 struct spoe_arg *arg;
3897 unsigned int where;
3898
Christopher Faulet11610f32017-09-21 10:23:10 +02003899 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003900 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
3901 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
3902 msg->event = SPOE_EV_ON_TCP_REQ_FE;
3903 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
3904 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
3905 }
3906 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
3907 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
3908 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003909 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
3910 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003911 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003912 }
3913 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003914 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
3915 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003916 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003917 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01003918
3919 where = 0;
3920 switch (msg->event) {
3921 case SPOE_EV_ON_CLIENT_SESS:
3922 where |= SMP_VAL_FE_CON_ACC;
3923 break;
3924
3925 case SPOE_EV_ON_TCP_REQ_FE:
3926 where |= SMP_VAL_FE_REQ_CNT;
3927 break;
3928
3929 case SPOE_EV_ON_HTTP_REQ_FE:
3930 where |= SMP_VAL_FE_HRQ_HDR;
3931 break;
3932
3933 case SPOE_EV_ON_TCP_REQ_BE:
3934 if (px->cap & PR_CAP_FE)
3935 where |= SMP_VAL_FE_REQ_CNT;
3936 if (px->cap & PR_CAP_BE)
3937 where |= SMP_VAL_BE_REQ_CNT;
3938 break;
3939
3940 case SPOE_EV_ON_HTTP_REQ_BE:
3941 if (px->cap & PR_CAP_FE)
3942 where |= SMP_VAL_FE_HRQ_HDR;
3943 if (px->cap & PR_CAP_BE)
3944 where |= SMP_VAL_BE_HRQ_HDR;
3945 break;
3946
3947 case SPOE_EV_ON_SERVER_SESS:
3948 where |= SMP_VAL_BE_SRV_CON;
3949 break;
3950
3951 case SPOE_EV_ON_TCP_RSP:
3952 if (px->cap & PR_CAP_FE)
3953 where |= SMP_VAL_FE_RES_CNT;
3954 if (px->cap & PR_CAP_BE)
3955 where |= SMP_VAL_BE_RES_CNT;
3956 break;
3957
3958 case SPOE_EV_ON_HTTP_RSP:
3959 if (px->cap & PR_CAP_FE)
3960 where |= SMP_VAL_FE_HRS_HDR;
3961 if (px->cap & PR_CAP_BE)
3962 where |= SMP_VAL_BE_HRS_HDR;
3963 break;
3964
3965 default:
3966 break;
3967 }
3968
3969 list_for_each_entry(arg, &msg->args, list) {
3970 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003971 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01003972 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003973 "none of which is available here ('%s')",
3974 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01003975 sample_ckp_names(arg->expr->fetch->use),
3976 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02003977 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01003978 }
3979 }
3980
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02003982 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
3983 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003984 }
3985 }
3986 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02003987 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003988 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02003989 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003990 continue;
3991 }
3992
Christopher Faulet11610f32017-09-21 10:23:10 +02003993 /* Replace placeholders by the corresponding groups for the SPOE
3994 * agent */
3995 list_for_each_entry(ph, &curgphs, list) {
3996 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
3997 if (!strcmp(grp->id, ph->id)) {
3998 grp->agent = curagent;
3999 LIST_DEL(&grp->list);
4000 LIST_ADDQ(&curagent->groups, &grp->list);
4001 goto next_aph;
4002 }
4003 }
4004 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4005 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4006 goto error;
4007 next_aph:
4008 continue;
4009 }
4010
4011 /* Replace placeholders by the corresponding message for each SPOE
4012 * group of the SPOE agent */
4013 list_for_each_entry(grp, &curagent->groups, list) {
4014 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4015 list_for_each_entry(msg, &curmsgs, list) {
4016 if (!strcmp(msg->id, ph->id)) {
4017 if (msg->group != NULL) {
4018 memprintf(err, "SPOE message '%s' already belongs to "
4019 "the SPOE group '%s' declare at %s:%d",
4020 msg->id, msg->group->id,
4021 msg->group->conf.file,
4022 msg->group->conf.line);
4023 goto error;
4024 }
4025
4026 /* Scope for arguments are not checked for now. We will check
4027 * them only if a rule use the corresponding SPOE group. */
4028 msg->agent = curagent;
4029 msg->group = grp;
4030 LIST_DEL(&ph->list);
4031 LIST_ADDQ(&grp->messages, &msg->by_grp);
4032 goto next_mph_grp;
4033 }
4034 }
4035 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4036 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4037 goto error;
4038 next_mph_grp:
4039 continue;
4040 }
4041 }
4042
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004043 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004044 /* move curmsgs to the agent message list */
4045 curmsgs.n->p = &curagent->messages;
4046 curmsgs.p->n = &curagent->messages;
4047 curagent->messages = curmsgs;
4048 LIST_INIT(&curmsgs);
4049
Christopher Faulet7ee86672017-09-19 11:08:28 +02004050 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004051 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004052 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4053 LIST_DEL(&ph->list);
4054 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004056 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4057 LIST_DEL(&ph->list);
4058 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004059 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004060 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4061 struct arg arg;
4062
4063 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4064 curagent->var_pfx, vph->name);
4065
4066 arg.type = ARGT_STR;
4067 arg.data.str.str = trash.str;
4068 arg.data.str.len = trash.len;
4069 if (!vars_check_arg(&arg, err)) {
4070 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4071 curagent->id, curagent->var_pfx, vph->name, *err);
4072 goto error;
4073 }
4074
4075 LIST_DEL(&vph->list);
4076 free(vph->name);
4077 free(vph);
4078 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004079 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4080 LIST_DEL(&grp->list);
4081 spoe_release_group(grp);
4082 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004083 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004084 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004085 fconf->ops = &spoe_ops;
4086 fconf->conf = conf;
4087 return 0;
4088
4089 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004090 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004091 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4092 LIST_DEL(&ph->list);
4093 spoe_release_placeholder(ph);
4094 }
4095 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4096 LIST_DEL(&ph->list);
4097 spoe_release_placeholder(ph);
4098 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004099 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4100 LIST_DEL(&vph->list);
4101 free(vph->name);
4102 free(vph);
4103 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004104 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4105 LIST_DEL(&grp->list);
4106 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004107 }
4108 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4109 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004110 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004111 }
4112 free(conf);
4113 return -1;
4114}
4115
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004116/* Send message of a SPOE group. This is the action_ptr callback of a rule
4117 * associated to a "send-spoe-group" action.
4118 *
4119 * It returns ACT_RET_CONT is processing is finished without error, it returns
4120 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4121 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004122static enum act_return
4123spoe_send_group(struct act_rule *rule, struct proxy *px,
4124 struct session *sess, struct stream *s, int flags)
4125{
4126 struct filter *filter;
4127 struct spoe_agent *agent = NULL;
4128 struct spoe_group *group = NULL;
4129 struct spoe_context *ctx = NULL;
4130 int ret, dir;
4131
4132 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4133 if (filter->config == rule->arg.act.p[0]) {
4134 agent = rule->arg.act.p[2];
4135 group = rule->arg.act.p[3];
4136 ctx = filter->ctx;
4137 break;
4138 }
4139 }
4140 if (agent == NULL || group == NULL || ctx == NULL)
4141 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004142 if (ctx->state == SPOE_CTX_ST_NONE)
4143 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004144
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004145 switch (rule->from) {
4146 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4147 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4148 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4149 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4150 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4151 default:
4152 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4153 " - internal error while execute spoe-send-group\n",
4154 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4155 __FUNCTION__, s);
4156 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4157 agent->id);
4158 return ACT_RET_CONT;
4159 }
4160
4161 ret = spoe_process_group(s, ctx, group, dir);
4162 if (ret == 1)
4163 return ACT_RET_CONT;
4164 else if (ret == 0) {
4165 if (flags & ACT_FLAG_FINAL) {
4166 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4167 " - failed to process group '%s': interrupted by caller\n",
4168 (int)now.tv_sec, (int)now.tv_usec,
4169 agent->id, __FUNCTION__, s, group->id);
4170 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4171 spoe_handle_processing_error(s, agent, ctx, dir);
4172 spoe_stop_processing(ctx);
4173 return ACT_RET_CONT;
4174 }
4175 return ACT_RET_YIELD;
4176 }
4177 else
4178 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004179}
4180
4181/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4182 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4183 * action should be:
4184 *
4185 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4186 *
4187 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4188 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4189 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4190 * group.
4191 *
4192 * The function returns 1 in success case, otherwise, it returns 0 and err is
4193 * filled.
4194 */
4195static int
4196check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4197{
4198 struct flt_conf *fconf;
4199 struct spoe_config *conf;
4200 struct spoe_agent *agent = NULL;
4201 struct spoe_group *group;
4202 struct spoe_message *msg;
4203 char *engine_id = rule->arg.act.p[0];
4204 char *group_id = rule->arg.act.p[1];
4205 unsigned int where = 0;
4206
4207 switch (rule->from) {
4208 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4209 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4210 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4211 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4212 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4213 default:
4214 memprintf(err,
4215 "internal error, unexpected rule->from=%d, please report this bug!",
4216 rule->from);
4217 goto error;
4218 }
4219
4220 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4221 * <px> */
4222 list_for_each_entry(fconf, &px->filter_configs, list) {
4223 conf = fconf->conf;
4224
4225 /* This is not an SPOE filter */
4226 if (fconf->id != spoe_filter_id)
4227 continue;
4228
4229 /* This is the good engine */
4230 if (!strcmp(conf->id, engine_id)) {
4231 agent = conf->agent;
4232 break;
4233 }
4234 }
4235 if (agent == NULL) {
4236 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4237 engine_id, group_id);
4238 goto error;
4239 }
4240
4241 /* Try to find the right group */
4242 list_for_each_entry(group, &agent->groups, list) {
4243 /* This is the good group */
4244 if (!strcmp(group->id, group_id))
4245 break;
4246 }
4247 if (&group->list == &agent->groups) {
4248 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4249 group_id, engine_id);
4250 goto error;
4251 }
4252
4253 /* Ok, we found the group, we need to check messages and their
4254 * arguments */
4255 list_for_each_entry(msg, &group->messages, by_grp) {
4256 struct spoe_arg *arg;
4257
4258 list_for_each_entry(arg, &msg->args, list) {
4259 if (!(arg->expr->fetch->val & where)) {
4260 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4261 "some args extract information from '%s',"
4262 "none of which is available here ('%s')",
4263 msg->id, group->id, msg->conf.file, msg->conf.line,
4264 sample_ckp_names(arg->expr->fetch->use),
4265 sample_ckp_names(where));
4266 goto error;
4267 }
4268 }
4269 }
4270
4271 free(engine_id);
4272 free(group_id);
4273 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4274 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4275 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4276 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4277 return 1;
4278
4279 error:
4280 free(engine_id);
4281 free(group_id);
4282 return 0;
4283}
4284
4285/* Parse 'send-spoe-group' action following the format:
4286 *
4287 * ... send-spoe-group <engine-id> <group-id>
4288 *
4289 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4290 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4291 * ids are saved and used later, when the rule will be checked.
4292 */
4293static enum act_parse_ret
4294parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4295 struct act_rule *rule, char **err)
4296{
4297 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4298 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4299 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4300 return ACT_RET_PRS_ERR;
4301 }
4302 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4303 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4304
4305 (*orig_arg) += 2;
4306
4307 rule->action = ACT_CUSTOM;
4308 rule->action_ptr = spoe_send_group;
4309 rule->check_ptr = check_send_spoe_group;
4310 return ACT_RET_PRS_OK;
4311}
4312
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004313
4314/* Declare the filter parser for "spoe" keyword */
4315static struct flt_kw_list flt_kws = { "SPOE", { }, {
4316 { "spoe", parse_spoe_flt, NULL },
4317 { NULL, NULL, NULL },
4318 }
4319};
4320
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004321/* Delcate the action parser for "spoe-action" keyword */
4322static struct action_kw_list tcp_req_action_kws = { { }, {
4323 { "send-spoe-group", parse_send_spoe_group },
4324 { /* END */ },
4325 }
4326};
4327static struct action_kw_list tcp_res_action_kws = { { }, {
4328 { "send-spoe-group", parse_send_spoe_group },
4329 { /* END */ },
4330 }
4331};
4332static struct action_kw_list http_req_action_kws = { { }, {
4333 { "send-spoe-group", parse_send_spoe_group },
4334 { /* END */ },
4335 }
4336};
4337static struct action_kw_list http_res_action_kws = { { }, {
4338 { "send-spoe-group", parse_send_spoe_group },
4339 { /* END */ },
4340 }
4341};
4342
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004343__attribute__((constructor))
4344static void __spoe_init(void)
4345{
4346 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004347 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4348 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4349 http_req_keywords_register(&http_req_action_kws);
4350 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004351
Willy Tarreaubafbe012017-11-24 17:34:44 +01004352 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4353 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004354}
4355
4356__attribute__((destructor))
4357static void
4358__spoe_deinit(void)
4359{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004360 pool_destroy(pool_head_spoe_ctx);
4361 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004362}