blob: 760e26c603564e7447d10da7fdb9859893604b6b [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)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010049#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020050#else
51#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010052#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020053#endif
54
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010055/* Reserved 4 bytes to the frame size. So a frame and its size can be written
56 * together in a buffer */
57#define MAX_FRAME_SIZE global.tune.bufsize - 4
58
59/* The minimum size for a frame */
60#define MIN_FRAME_SIZE 256
61
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Reserved for the metadata and the frame type.
63 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010064#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020065
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010066/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010067#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
68
Christopher Faulet3b386a32017-02-23 10:17:15 +010069/* SPOE filter id. Used to identify SPOE filters */
70const char *spoe_filter_id = "SPOE filter";
71
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020072/* Set if the handle on SIGUSR1 is registered */
73static int sighandler_registered = 0;
74
75/* proxy used during the parsing */
76struct proxy *curproxy = NULL;
77
78/* The name of the SPOE engine, used during the parsing */
79char *curengine = NULL;
80
81/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020082/* SPOE agent/group/message used during the parsing */
83struct spoe_agent *curagent = NULL;
84struct spoe_group *curgrp = NULL;
85struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020086
87/* list of SPOE messages and placeholders used during the parsing */
88struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020089struct list curgrps;
90struct list curmphs;
91struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010092struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020093
Christopher Faulet42bfa462017-01-04 14:14:19 +010094/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +010095static struct pool_head *pool_head_spoe_ctx = NULL;
96static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020097
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020098struct flt_ops spoe_ops;
99
Christopher Faulet8ef75252017-02-20 22:56:03 +0100100static int spoe_queue_context(struct spoe_context *ctx);
101static int spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
102static void spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200103
104/********************************************************************
105 * helper functions/globals
106 ********************************************************************/
107static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200108spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109{
Christopher Faulet11610f32017-09-21 10:23:10 +0200110 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200111 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200112 free(ph->id);
113 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114}
115
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100117spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118{
Christopher Faulet57583e42017-09-04 15:41:09 +0200119 struct spoe_arg *arg, *argback;
120 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121
122 if (!msg)
123 return;
124 free(msg->id);
125 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127 release_sample_expr(arg->expr);
128 free(arg->name);
129 LIST_DEL(&arg->list);
130 free(arg);
131 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200132 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
133 LIST_DEL(&acl->list);
134 prune_acl(acl);
135 free(acl);
136 }
137 if (msg->cond) {
138 prune_acl_cond(msg->cond);
139 free(msg->cond);
140 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200141 free(msg);
142}
143
144static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200145spoe_release_group(struct spoe_group *grp)
146{
147 if (!grp)
148 return;
149 free(grp->id);
150 free(grp->conf.file);
151 free(grp);
152}
153
154static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100155spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200156{
Christopher Faulet11610f32017-09-21 10:23:10 +0200157 struct spoe_message *msg, *msgback;
158 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200159 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200160
161 if (!agent)
162 return;
163 free(agent->id);
164 free(agent->conf.file);
165 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100166 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100167 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100168 free(agent->var_t_process);
169 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200170 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
171 LIST_DEL(&msg->list);
172 spoe_release_message(msg);
173 }
174 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
175 LIST_DEL(&grp->list);
176 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200177 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200178 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100179 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200180 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200181 free(agent);
182}
183
184static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100185 [SPOE_FRM_ERR_NONE] = "normal",
186 [SPOE_FRM_ERR_IO] = "I/O error",
187 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
188 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
189 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
190 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
191 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
192 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
193 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
194 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
195 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
196 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100197 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100198 [SPOE_FRM_ERR_RES] = "resource allocation error",
199 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200200};
201
202static const char *spoe_event_str[SPOE_EV_EVENTS] = {
203 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
204 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
205 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
206 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
207 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
208
209 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
210 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
211 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
212};
213
214
215#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
216
217static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100218 [SPOE_CTX_ST_NONE] = "NONE",
219 [SPOE_CTX_ST_READY] = "READY",
220 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
221 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
222 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
223 [SPOE_CTX_ST_DONE] = "DONE",
224 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200225};
226
227static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100228 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
229 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
230 [SPOE_APPCTX_ST_IDLE] = "IDLE",
231 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
232 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
233 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
234 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
235 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
236 [SPOE_APPCTX_ST_EXIT] = "EXIT",
237 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200238};
239
240#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100241
Christopher Faulet8ef75252017-02-20 22:56:03 +0100242/* Used to generates a unique id for an engine. On success, it returns a
243 * allocated string. So it is the caller's reponsibility to release it. If the
244 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100245static char *
246generate_pseudo_uuid()
247{
248 static int init = 0;
249
250 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
251 const char uuid_chr[] = "0123456789ABCDEF-";
252 char *uuid;
253 int i;
254
255 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
256 return NULL;
257
258 if (!init) {
259 srand(now_ms);
260 init = 1;
261 }
262
263 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
264 int r = rand () % 16;
265
266 switch (uuid_fmt[i]) {
267 case 'x' : uuid[i] = uuid_chr[r]; break;
268 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
269 default : uuid[i] = uuid_fmt[i]; break;
270 }
271 }
272 return uuid;
273}
274
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100275
276static inline void
277spoe_update_stat_time(struct timeval *tv, long *t)
278{
279 if (*t == -1)
280 *t = tv_ms_elapsed(tv, &now);
281 else
282 *t += tv_ms_elapsed(tv, &now);
283 tv_zero(tv);
284}
285
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200286/********************************************************************
287 * Functions that encode/decode SPOE frames
288 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200289/* Helper to get static string length, excluding the terminating null byte */
290#define SLEN(str) (sizeof(str)-1)
291
292/* Predefined key used in HELLO/DISCONNECT frames */
293#define SUPPORTED_VERSIONS_KEY "supported-versions"
294#define VERSION_KEY "version"
295#define MAX_FRAME_SIZE_KEY "max-frame-size"
296#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100297#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100298#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200299#define STATUS_CODE_KEY "status-code"
300#define MSG_KEY "message"
301
302struct spoe_version {
303 char *str;
304 int min;
305 int max;
306};
307
308/* All supported versions */
309static struct spoe_version supported_versions[] = {
310 {"1.0", 1000, 1000},
311 {NULL, 0, 0}
312};
313
314/* Comma-separated list of supported versions */
315#define SUPPORTED_VERSIONS_VAL "1.0"
316
Christopher Faulet8ef75252017-02-20 22:56:03 +0100317/* Convert a string to a SPOE version value. The string must follow the format
318 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
319 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200322{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100323 const char *p, *end;
324 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200325
Christopher Faulet8ef75252017-02-20 22:56:03 +0100326 p = str;
327 end = str+len;
328 maj = min = 0;
329 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200330
Christopher Faulet8ef75252017-02-20 22:56:03 +0100331 /* skip leading spaces */
332 while (p < end && isspace(*p))
333 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334
Christopher Faulet8ef75252017-02-20 22:56:03 +0100335 /* parse Major number, until the '.' */
336 while (*p != '.') {
337 if (p >= end || *p < '0' || *p > '9')
338 goto out;
339 maj *= 10;
340 maj += (*p - '0');
341 p++;
342 }
343
344 /* check Major version */
345 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346 goto out;
347
Christopher Faulet8ef75252017-02-20 22:56:03 +0100348 p++; /* skip the '.' */
349 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
350 goto out;
351
352 /* Parse Minor number */
353 while (p < end) {
354 if (*p < '0' || *p > '9')
355 break;
356 min *= 10;
357 min += (*p - '0');
358 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200359 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100360
361 /* check Minor number */
362 if (min > 999)
363 goto out;
364
365 /* skip trailing spaces */
366 while (p < end && isspace(*p))
367 p++;
368 if (p != end)
369 goto out;
370
371 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372 out:
373 return vsn;
374}
375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
377 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
378 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100380spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200381{
Christopher Faulet305c6072017-02-23 16:17:53 +0100382 struct chunk *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100383 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384 char *p, *end;
385 unsigned int flags = SPOE_FRM_FL_FIN;
386 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200387
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 p = frame;
389 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200390
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 /* Set Frame type */
392 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200393
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100394 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100395 memcpy(p, (char *)&flags, 4);
396 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
398 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
401 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
402 * and "capabilities" */
403
404 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100405 sz = SLEN(SUPPORTED_VERSIONS_KEY);
406 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
407 goto too_big;
408
409 *p++ = SPOE_DATA_T_STR;
410 sz = SLEN(SUPPORTED_VERSIONS_VAL);
411 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
412 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
414 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 sz = SLEN(MAX_FRAME_SIZE_KEY);
416 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
417 goto too_big;
418
419 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200420 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100421 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200422
423 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100424 sz = SLEN(CAPABILITIES_KEY);
425 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
426 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200427
Christopher Faulet8ef75252017-02-20 22:56:03 +0100428 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100429 chk = get_trash_chunk();
430 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
431 memcpy(chk->str, "pipelining", 10);
432 chk->len += 10;
433 }
434 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
435 if (chk->len) chk->str[chk->len++] = ',';
436 memcpy(chk->str+chk->len, "async", 5);
437 chk->len += 5;
438 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100439 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
440 if (chk->len) chk->str[chk->len++] = ',';
441 memcpy(chk->str+chk->len, "fragmentation", 13);
442 chk->len += 5;
443 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100444 if (spoe_encode_buffer(chk->str, chk->len, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 goto too_big;
446
447 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100448 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100449 sz = SLEN(ENGINE_ID_KEY);
450 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
451 goto too_big;
452
453 *p++ = SPOE_DATA_T_STR;
454 sz = strlen(agent->engine_id);
455 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
456 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100457 }
458
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 return (p - frame);
460
461 too_big:
462 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
463 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200464}
465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
467 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
468 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200469static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100470spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100472 const char *reason;
473 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100474 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200476
Christopher Faulet8ef75252017-02-20 22:56:03 +0100477 p = frame;
478 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 /* Set Frame type */
481 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100483 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 memcpy(p, (char *)&flags, 4);
485 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486
487 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 *p++ = 0; *p++ = 0;
489
490 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
491 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200492
493 /* There are 2 mandatory items: "status-code" and "message" */
494
495 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100496 sz = SLEN(STATUS_CODE_KEY);
497 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
498 goto too_big;
499
500 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200501 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100502 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200503
504 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100505 sz = SLEN(MSG_KEY);
506 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
507 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200508
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509 /*Get the message corresponding to the status code */
510 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
511
512 *p++ = SPOE_DATA_T_STR;
513 sz = strlen(reason);
514 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
515 goto too_big;
516
517 return (p - frame);
518
519 too_big:
520 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
521 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522}
523
Christopher Faulet8ef75252017-02-20 22:56:03 +0100524/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
525 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
526 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200527static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100528spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100529 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200530{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100531 char *p, *end;
532 unsigned int stream_id, frame_id;
533 unsigned int flags = SPOE_FRM_FL_FIN;
534 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200535
Christopher Faulet8ef75252017-02-20 22:56:03 +0100536 p = frame;
537 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200538
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100539 stream_id = ctx->stream_id;
540 frame_id = ctx->frame_id;
541
542 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
543 /* The fragmentation is not supported by the applet */
544 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
545 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
546 return -1;
547 }
548 flags = ctx->frag_ctx.flags;
549 }
550
551 /* Set Frame type */
552 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
553
554 /* Set flags */
555 memcpy(p, (char *)&flags, 4);
556 p += 4;
557
558 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200559 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100560 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200561 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100562 goto too_big;
563
564 /* Copy encoded messages, if possible */
565 sz = ctx->buffer->i;
566 if (p + sz >= end)
567 goto too_big;
568 memcpy(p, ctx->buffer->p, sz);
569 p += sz;
570
571 return (p - frame);
572
573 too_big:
574 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
575 return 0;
576}
577
578/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
579 * returns the number of encoded bytes in the frame on success, 0 if an encoding
580 * error occurred and -1 if a fatal error occurred. */
581static int
582spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
583 char *frame, size_t size)
584{
585 char *p, *end;
586 unsigned int stream_id, frame_id;
587 unsigned int flags;
588 size_t sz;
589
590 p = frame;
591 end = frame+size;
592
Christopher Faulet8ef75252017-02-20 22:56:03 +0100593 /* <ctx> is null when the stream has aborted the processing of a
594 * fragmented frame. In this case, we must notify the corresponding
595 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100597 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100598 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
599 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
600 }
601 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100602 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100603 stream_id = ctx->stream_id;
604 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100605 }
606
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100608 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100609
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100610 /* Set flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100611 memcpy(p, (char *)&flags, 4);
612 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200613
614 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200615 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200617 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200619
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100620 if (ctx == NULL)
621 goto end;
622
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 /* Copy encoded messages, if possible */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100624 sz = ctx->buffer->i;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625 if (p + sz >= end)
626 goto too_big;
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100627 memcpy(p, ctx->buffer->p, sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100629
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100630 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631 return (p - frame);
632
633 too_big:
634 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
635 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200636}
637
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638/* Decode and process the HELLO frame sent by an agent. It returns the number of
639 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
640 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200641static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643{
Christopher Faulet305c6072017-02-23 16:17:53 +0100644 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
645 char *p, *end;
646 int vsn, max_frame_size;
647 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648
649 p = frame;
650 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200651
652 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
654 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200657
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658 if (size < 7 /* TYPE + METADATA */) {
659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
660 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661 }
662
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100663 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 memcpy((char *)&flags, p, 4);
665 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100667 /* Fragmentation is not supported for HELLO frame */
668 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100670 return -1;
671 }
672
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200673 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100674 if (*p != 0 || *(p+1) != 0) {
675 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
676 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200679
680 /* There are 3 mandatory items: "version", "max-frame-size" and
681 * "capabilities" */
682
683 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100684 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100685 while (p < end) {
686 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200687 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689
690 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 ret = spoe_decode_buffer(&p, end, &str, &sz);
692 if (ret == -1 || !sz) {
693 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
694 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 /* Check "version" K/V item */
698 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 int i, type = *p++;
700
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
703 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
704 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
707 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
708 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709 }
710
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714 return -1;
715 }
716 for (i = 0; supported_versions[i].str != NULL; ++i) {
717 if (vsn >= supported_versions[i].min &&
718 vsn <= supported_versions[i].max)
719 break;
720 }
721 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100722 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200723 return -1;
724 }
725 }
726 /* Check "max-frame-size" K/V item */
727 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729
730 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200731 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
732 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
733 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
734 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
736 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200737 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200738 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
740 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200741 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 if (sz < MIN_FRAME_SIZE ||
743 sz > SPOE_APPCTX(appctx)->max_frame_size) {
744 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200745 return -1;
746 }
747 max_frame_size = sz;
748 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100749 /* Check "capabilities" K/V item */
750 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752
753 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
755 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
756 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100758 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
759 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
760 return 0;
761 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762
Christopher Faulet8ef75252017-02-20 22:56:03 +0100763 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764 char *delim;
765
766 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100768
Christopher Faulet8ef75252017-02-20 22:56:03 +0100769 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
770 str += 10; sz -= 10;
771 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772 flags |= SPOE_APPCTX_FL_PIPELINING;
773 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100774 else if (sz >= 5 && !strncmp(str, "async", 5)) {
775 str += 5; sz -= 5;
776 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777 flags |= SPOE_APPCTX_FL_ASYNC;
778 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
780 str += 13; sz -= 13;
781 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100782 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
783 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784
Christopher Faulet8ef75252017-02-20 22:56:03 +0100785 /* Get the next comma or break */
786 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100787 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 delim++;
789 sz -= (delim - str);
790 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100791 }
792 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200793 else {
794 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 if (spoe_skip_data(&p, end) == -1) {
796 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
797 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200798 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200799 }
800 }
801
802 /* Final checks */
803 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100804 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200805 return -1;
806 }
807 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200809 return -1;
810 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100811 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
812 flags &= ~SPOE_APPCTX_FL_PIPELINING;
813 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
814 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815
Christopher Faulet42bfa462017-01-04 14:14:19 +0100816 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
817 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
818 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100819
820 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200821}
822
823/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
824 * bytes on success, 0 if the frame can be ignored and -1 if an error
825 * occurred. */
826static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100827spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100830 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831
832 p = frame;
833 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834
835 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
837 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200840
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 if (size < 7 /* TYPE + METADATA */) {
842 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
843 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200844 }
845
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100846 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100847 memcpy((char *)&flags, p, 4);
848 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100850 /* Fragmentation is not supported for DISCONNECT frame */
851 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100853 return -1;
854 }
855
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 if (*p != 0 || *(p+1) != 0) {
858 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
859 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200862
863 /* There are 2 mandatory items: "status-code" and "message" */
864
865 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 while (p < end) {
867 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200868 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100869 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200870
871 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 ret = spoe_decode_buffer(&p, end, &str, &sz);
873 if (ret == -1 || !sz) {
874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
875 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 }
877
878 /* Check "status-code" K/V item */
879 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100880 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881
882 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
884 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
885 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
886 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
888 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200890 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
892 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200895 }
896
897 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 else if (!memcmp(str, MSG_KEY, sz)) {
899 int type = *p++;
900
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200901 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
903 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
904 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906 ret = spoe_decode_buffer(&p, end, &str, &sz);
907 if (ret == -1 || sz > 255) {
908 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
909 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200910 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
912 SPOE_APPCTX(appctx)->reason = str;
913 SPOE_APPCTX(appctx)->rlen = sz;
914#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
916 else {
917 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918 if (spoe_skip_data(&p, end) == -1) {
919 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
920 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200922 }
923 }
924
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926}
927
928
Christopher Fauleta1cda022016-12-21 08:58:06 +0100929/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930 * success, 0 if the frame can be ignored and -1 if an error occurred. */
931static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200934{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100939 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940
941 p = frame;
942 end = frame + size;
943 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944
945 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100946 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
947 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200950
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 if (size < 7 /* TYPE + METADATA */) {
952 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
953 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200954 }
955
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100956 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 memcpy((char *)&flags, p, 4);
958 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200959
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100960 /* Fragmentation is not supported for now */
961 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100963 return -1;
964 }
965
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200967 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100969 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200971 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200973 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100975
Christopher Faulet8ef75252017-02-20 22:56:03 +0100976 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100977 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200978 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100979 if ((*ctx)->stream_id == (unsigned int)stream_id &&
980 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100981 goto found;
982 }
983 }
984 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100985 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
986 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100987 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100988 goto found;
989 }
990 }
991
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100992 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
993 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
994 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
995
996 /* ABRT bit is set for an unfinished fragmented frame */
997 if (flags & SPOE_FRM_FL_ABRT) {
998 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100999 (*ctx)->state = SPOE_CTX_ST_ERROR;
1000 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1001 /* Ignore the payload */
1002 goto end;
1003 }
1004 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1005 /* For now, we ignore the ack */
1006 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1007 return 0;
1008 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001009
Christopher Fauleta1cda022016-12-21 08:58:06 +01001010 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001011 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1012 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001013 " - stream-id=%u - frame-id=%u\n",
1014 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1015 __FUNCTION__, appctx,
1016 (unsigned int)stream_id, (unsigned int)frame_id);
1017
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001018 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001019 return 0;
1020
1021 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001022 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1023 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001024 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001025 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001026 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001027
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001028 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 len = (end - p);
1030 memcpy(SPOE_APPCTX(appctx)->buffer->p, p, len);
1031 SPOE_APPCTX(appctx)->buffer->i = len;
1032 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001033
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001034 /* Transfer the buffer ownership to the SPOE context */
1035 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
1036 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001037
Christopher Faulet8ef75252017-02-20 22:56:03 +01001038 (*ctx)->state = SPOE_CTX_ST_DONE;
1039
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001040 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 " - ACK frame received"
1043 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001044 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1046 (*ctx)->frame_id, flags);
1047 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001048}
1049
Christopher Fauletba7bc162016-11-07 21:07:38 +01001050/* This function is used in cfgparse.c and declared in proto/checks.h. It
1051 * prepare the request to send to agents during a healthcheck. It returns 0 on
1052 * success and -1 if an error occurred. */
1053int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001055{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 struct appctx appctx;
1057 struct spoe_appctx spoe_appctx;
1058 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1059 size_t sz;
1060 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001061
Christopher Faulet42bfa462017-01-04 14:14:19 +01001062 memset(&appctx, 0, sizeof(appctx));
1063 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001064 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001065
1066 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001067 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet8ef75252017-02-20 22:56:03 +01001069 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1070 end = frame + MAX_FRAME_SIZE;
1071
1072 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1073 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001076
Christopher Faulet8ef75252017-02-20 22:56:03 +01001077 /* Add "healthcheck" K/V item */
1078 sz = SLEN(HEALTHCHECK_KEY);
1079 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1080 return -1;
1081 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 *len = frame - buf;
1084 sz = htonl(*len - 4);
1085 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001089 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090 return 0;
1091}
1092
1093/* This function is used in checks.c and declared in proto/checks.h. It decode
1094 * the response received from an agent during a healthcheck. It returns 0 on
1095 * success and -1 if an error occurred. */
1096int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001098{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001099 struct appctx appctx;
1100 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001101
Christopher Faulet42bfa462017-01-04 14:14:19 +01001102 memset(&appctx, 0, sizeof(appctx));
1103 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104
Christopher Faulet42bfa462017-01-04 14:14:19 +01001105 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001106 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001107
Christopher Faulet8ef75252017-02-20 22:56:03 +01001108 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1109 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001110 goto error;
1111 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001112 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1113 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
1115 return 0;
1116
1117 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1119 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1120 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121 return -1;
1122}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001123
Christopher Fauleta1cda022016-12-21 08:58:06 +01001124/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1125 * the frame can be ignored, 1 to retry later, and the frame legnth on
1126 * success. */
1127static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001128spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129{
1130 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001131 int ret;
1132 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133
Christopher Faulet8ef75252017-02-20 22:56:03 +01001134 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1135 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136 netint = htonl(framesz);
1137 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001138 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139 if (ret <= 0) {
Christopher Fauletd5216d42018-02-01 08:45:22 +01001140 if ((ret == -3 && si_ic(si)->buf == &buf_empty) || ret == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 si_applet_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001143 }
1144 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145 return -1; /* error */
1146 }
1147 return framesz;
1148}
1149
1150/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1151 * when the frame can be ignored, 1 to retry later and the frame length on
1152 * success. */
1153static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001154spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155{
1156 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 int ret;
1158 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159
Willy Tarreau06d80a92017-10-19 14:32:15 +02001160 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 if (ret > 0) {
1162 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001163 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001165 return -1;
1166 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001167 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168 }
1169 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001172 }
1173 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174 return -1; /* error */
1175 }
1176 return framesz;
1177}
1178
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001179/********************************************************************
1180 * Functions that manage the SPOE applet
1181 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001182static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001183spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001184{
1185 si_applet_want_get(appctx->owner);
1186 si_applet_want_put(appctx->owner);
1187 appctx_wakeup(appctx);
1188 return 1;
1189}
1190
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001191/* Callback function that catches applet timeouts. If a timeout occurred, we set
1192 * <appctx->st1> flag and the SPOE applet is woken up. */
1193static struct task *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001194spoe_process_appctx(struct task * task)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001195{
1196 struct appctx *appctx = task->context;
1197
1198 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1199 if (tick_is_expired(task->expire, now_ms)) {
1200 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001201 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001203 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001204 return task;
1205}
1206
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207/* Callback function that releases a SPOE applet. This happens when the
1208 * connection with the agent is closed. */
1209static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001212 struct stream_interface *si = appctx->owner;
1213 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1214 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001215 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001216
1217 if (spoe_appctx == NULL)
1218 return;
1219
1220 appctx->ctx.spoe.ptr = NULL;
1221 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001222
1223 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1224 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1225 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001226
Christopher Faulet8ef75252017-02-20 22:56:03 +01001227 /* Remove applet from the list of running applets */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001228 SPOE_DEBUG_STMT(agent->rt[tid].applets_act--);
1229 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1231 LIST_DEL(&spoe_appctx->list);
1232 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001233 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001234 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001235
Christopher Faulet8ef75252017-02-20 22:56:03 +01001236 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001237 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001238 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1239 eb32_delete(&spoe_appctx->node);
1240 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1241 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001242
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1245 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001246
1247 si_shutw(si);
1248 si_shutr(si);
1249 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001250 }
1251
Christopher Faulet8ef75252017-02-20 22:56:03 +01001252 /* Destroy the task attached to this applet */
1253 if (spoe_appctx->task) {
1254 task_delete(spoe_appctx->task);
1255 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001256 }
1257
Christopher Faulet8ef75252017-02-20 22:56:03 +01001258 /* Notify all waiting streams */
1259 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260 LIST_DEL(&ctx->list);
1261 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001262 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001263 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001264 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001265 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001266 }
1267
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 /* If the applet was processing a fragmented frame, notify the
1269 * corresponding stream. */
1270 if (spoe_appctx->frag_ctx.ctx) {
1271 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001272 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001273 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001275 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1276 }
1277
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 /* Release allocated memory */
1279 spoe_release_buffer(&spoe_appctx->buffer,
1280 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001281 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001282
Christopher Faulet24289f22017-09-25 14:48:02 +02001283 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001284 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001285
Christopher Faulet8ef75252017-02-20 22:56:03 +01001286 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001287 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001288 LIST_DEL(&ctx->list);
1289 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001290 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001291 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001292 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001293 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001294 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001295 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001296 LIST_DEL(&ctx->list);
1297 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001298 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001301 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1302 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001303
1304 end:
1305 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001306 agent->rt[tid].frame_size = agent->max_frame_size;
1307 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1308 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001309}
1310
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001313{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001315 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001316 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001318
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 if (si->state <= SI_ST_CON) {
1320 si_applet_want_put(si);
1321 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1322 goto stop;
1323 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001324 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001325 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001326 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001327 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001328
Christopher Fauleta1cda022016-12-21 08:58:06 +01001329 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001330 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1331 " - Connection timed out\n",
1332 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1333 __FUNCTION__, appctx);
1334 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001336 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337
Christopher Faulet42bfa462017-01-04 14:14:19 +01001338 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001339 SPOE_APPCTX(appctx)->task->expire =
1340 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1343 * length. */
1344 buf = trash.str; frame = buf+4;
1345 ret = spoe_prepare_hahello_frame(appctx, frame,
1346 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001348 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001349
1350 switch (ret) {
1351 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352 case 0: /* ignore => an error, cannot be ignored */
1353 goto exit;
1354
1355 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001356 goto stop;
1357
Christopher Faulet8ef75252017-02-20 22:56:03 +01001358 default:
1359 /* HELLO frame successfully sent, now wait for the
1360 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001361 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1362 goto next;
1363 }
1364
1365 next:
1366 return 0;
1367 stop:
1368 return 1;
1369 exit:
1370 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1371 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001372}
1373
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001376{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001377 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001378 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001379 char *frame;
1380 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001381
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001382
Christopher Fauletb067b062017-01-04 16:39:11 +01001383 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001384 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001385 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001386 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001387
Christopher Fauleta1cda022016-12-21 08:58:06 +01001388 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1390 " - Connection timed out\n",
1391 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1392 __FUNCTION__, appctx);
1393 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 goto exit;
1395 }
1396
Christopher Faulet8ef75252017-02-20 22:56:03 +01001397 frame = trash.str; trash.len = 0;
1398 ret = spoe_recv_frame(appctx, frame,
1399 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400 if (ret > 1) {
1401 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1402 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1403 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001404 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001405 trash.len = ret + 4;
1406 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001407 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001408
Christopher Fauleta1cda022016-12-21 08:58:06 +01001409 switch (ret) {
1410 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001412 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1413 goto next;
1414
1415 case 1: /* retry later */
1416 goto stop;
1417
1418 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001419 /* HELLO handshake is finished, set the idle timeout and
1420 * add the applet in the list of running applets. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001421 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001422 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001423 SPOE_APPCTX(appctx)->node.key = 0;
1424 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001425
1426 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001427 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001429 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001430
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001432 /* Do not forget to remove processed frame from the output buffer */
1433 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001434 co_skip(si_oc(si), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435
1436 SPOE_APPCTX(appctx)->task->expire =
1437 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001438 return 0;
1439 stop:
1440 return 1;
1441 exit:
1442 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1443 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001444}
1445
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001446
Christopher Fauleta1cda022016-12-21 08:58:06 +01001447static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001448spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001449{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001450 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1451 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001452 char *frame, *buf;
1453 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001454
Christopher Faulet8ef75252017-02-20 22:56:03 +01001455 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1456 * length. */
1457 buf = trash.str; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001458
1459 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1460 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1461 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1462 SPOE_APPCTX(appctx)->max_frame_size);
1463 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001464 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001465 *skip = 1;
1466 ret = 1;
1467 goto end;
1468 }
1469 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001470 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001471 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1472 SPOE_APPCTX(appctx)->max_frame_size);
1473
1474 }
1475
Christopher Faulet8ef75252017-02-20 22:56:03 +01001476 if (ret > 1)
1477 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001478
Christopher Faulet8ef75252017-02-20 22:56:03 +01001479 switch (ret) {
1480 case -1: /* error */
1481 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1482 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001483
Christopher Faulet8ef75252017-02-20 22:56:03 +01001484 case 0: /* ignore */
1485 if (ctx == NULL)
1486 goto abort_frag_frame;
1487
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001488 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001489 LIST_DEL(&ctx->list);
1490 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001491 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001492 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001493 ctx->state = SPOE_CTX_ST_ERROR;
1494 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1495 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001496 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001497 break;
1498
1499 case 1: /* retry */
1500 *skip = 1;
1501 break;
1502
1503 default:
1504 if (ctx == NULL)
1505 goto abort_frag_frame;
1506
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001507 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 LIST_DEL(&ctx->list);
1509 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001510 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001511 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001512 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1513 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1514 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001515 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001516 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001517 }
1518 goto end;
1519
1520 frag_frame_sent:
1521 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001522 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1524 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1525 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001526 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1527 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1528 goto end;
1529
1530 no_frag_frame_sent:
1531 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1532 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001533 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 }
1535 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1536 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1537 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1538 }
1539 else {
1540 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001541 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001542 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1543 }
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001544 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1546 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1547 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001548 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1551 goto end;
1552
1553 abort_frag_frame:
1554 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1555 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1556 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1557 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1558 goto end;
1559
1560 end:
1561 return ret;
1562}
1563
1564static int
1565spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1566{
1567 struct spoe_context *ctx = NULL;
1568 char *frame;
1569 int ret;
1570
1571 frame = trash.str; trash.len = 0;
1572 ret = spoe_recv_frame(appctx, frame,
1573 SPOE_APPCTX(appctx)->max_frame_size);
1574 if (ret > 1) {
1575 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1576 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001577 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001578 goto end;
1579 }
1580 trash.len = ret + 4;
1581 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1582 }
1583 switch (ret) {
1584 case -1: /* error */
1585 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1586 break;
1587
1588 case 0: /* ignore */
1589 break;
1590
1591 case 1: /* retry */
1592 *skip = 1;
1593 break;
1594
1595 default:
1596 LIST_DEL(&ctx->list);
1597 LIST_INIT(&ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001598 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1599 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001600 if (ctx->spoe_appctx) {
1601 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001602 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001603 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001604 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1605 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1606 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1607 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1608 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1609 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1610 }
1611 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1612 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001613 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1614 break;
1615 }
1616
1617 /* Do not forget to remove processed frame from the output buffer */
1618 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001619 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001620 end:
1621 return ret;
1622}
1623
1624static int
1625spoe_handle_processing_appctx(struct appctx *appctx)
1626{
1627 struct stream_interface *si = appctx->owner;
1628 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001629 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001630
1631 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1632 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1633 goto exit;
1634 }
1635
1636 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1637 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1638 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1639 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1640 goto next;
1641 }
1642
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001643 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001644 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001645 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001646 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1647 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001648 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001649
Christopher Faulet8f82b202018-01-24 16:23:03 +01001650 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1651 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001652
Christopher Faulet8f82b202018-01-24 16:23:03 +01001653 /* receiving_frame loop */
1654 while (!skip_receiving) {
1655 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1656 switch (ret) {
1657 case -1: /* error */
1658 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001659
Christopher Faulet8f82b202018-01-24 16:23:03 +01001660 case 0: /* ignore */
1661 active_r = 1;
1662 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001663
Christopher Faulet8f82b202018-01-24 16:23:03 +01001664 case 1: /* retry */
1665 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001666
Christopher Faulet8f82b202018-01-24 16:23:03 +01001667 default:
1668 active_r = 1;
1669 break;
1670 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001671 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001672
Christopher Faulet8f82b202018-01-24 16:23:03 +01001673 /* send_frame loop */
1674 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1675 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1676 switch (ret) {
1677 case -1: /* error */
1678 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679
Christopher Faulet8f82b202018-01-24 16:23:03 +01001680 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001681 if (SPOE_APPCTX(appctx)->node.key)
1682 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001683 active_s++;
1684 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001685
Christopher Faulet8f82b202018-01-24 16:23:03 +01001686 case 1: /* retry */
1687 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001690 if (SPOE_APPCTX(appctx)->node.key)
1691 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001692 active_s++;
1693 break;
1694 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001695 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001698 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1699 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1700 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001701
Christopher Faulet8f82b202018-01-24 16:23:03 +01001702 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001703 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle++);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001705 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706 }
1707 return 1;
1708
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 next:
1710 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1711 return 0;
1712
Christopher Fauleta1cda022016-12-21 08:58:06 +01001713 exit:
1714 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1715 return 0;
1716}
1717
1718static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001719spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001720{
1721 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001722 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001723 char *frame, *buf;
1724 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001725
Christopher Fauleta1cda022016-12-21 08:58:06 +01001726 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1727 goto exit;
1728
1729 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1730 goto exit;
1731
Christopher Faulet8ef75252017-02-20 22:56:03 +01001732 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1733 * length. */
1734 buf = trash.str; frame = buf+4;
1735 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1736 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001737 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001738 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739
1740 switch (ret) {
1741 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001742 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001743 goto exit;
1744
1745 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746 goto stop;
1747
1748 default:
1749 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1750 " - disconnected by HAProxy (%d): %s\n",
1751 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001752 __FUNCTION__, appctx,
1753 SPOE_APPCTX(appctx)->status_code,
1754 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001755
1756 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1757 goto next;
1758 }
1759
1760 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001761 SPOE_APPCTX(appctx)->task->expire =
1762 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763 return 0;
1764 stop:
1765 return 1;
1766 exit:
1767 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1768 return 0;
1769}
1770
1771static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773{
1774 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001775 char *frame;
1776 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001777
Christopher Fauletb067b062017-01-04 16:39:11 +01001778 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001781 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782
Christopher Fauletb067b062017-01-04 16:39:11 +01001783 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001784 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001786 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787
Christopher Faulet8ef75252017-02-20 22:56:03 +01001788 frame = trash.str; trash.len = 0;
1789 ret = spoe_recv_frame(appctx, frame,
1790 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 if (ret > 1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001792 trash.len = ret + 4;
1793 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001794 }
1795
1796 switch (ret) {
1797 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1799 " - error on frame (%s)\n",
1800 (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 Fauleta1cda022016-12-21 08:58:06 +01001802 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001803 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 goto exit;
1805
1806 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 goto next;
1808
1809 case 1: /* retry */
1810 goto stop;
1811
1812 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001816 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001817 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1818 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819 goto exit;
1820 }
1821
1822 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001823 /* Do not forget to remove processed frame from the output buffer */
1824 if (trash.len)
Willy Tarreau06d80a92017-10-19 14:32:15 +02001825 co_skip(si_oc(appctx->owner), trash.len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001826
Christopher Fauleta1cda022016-12-21 08:58:06 +01001827 return 0;
1828 stop:
1829 return 1;
1830 exit:
1831 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1832 return 0;
1833}
1834
1835/* I/O Handler processing messages exchanged with the agent */
1836static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001839 struct stream_interface *si = appctx->owner;
1840 struct spoe_agent *agent;
1841
1842 if (SPOE_APPCTX(appctx) == NULL)
1843 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001844
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1846 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001847
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848 switchstate:
1849 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1850 " - appctx-state=%s\n",
1851 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1852 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1853
1854 switch (appctx->st0) {
1855 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001857 goto out;
1858 goto switchstate;
1859
1860 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001861 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001862 goto out;
1863 goto switchstate;
1864
1865 case SPOE_APPCTX_ST_IDLE:
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001866 SPOE_DEBUG_STMT(agent->rt[tid].applets_idle--);
1867 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001869 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001870 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 SPOE_APPCTX(appctx)->task->expire =
1872 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001874 goto switchstate;
1875 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1877 /* fall through */
1878
1879 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001880 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1881 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001882 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 goto out;
1884 goto switchstate;
1885
1886 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001887 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888 goto out;
1889 goto switchstate;
1890
1891 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001892 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001893 goto out;
1894 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001895
1896 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001897 appctx->st0 = SPOE_APPCTX_ST_END;
1898 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1899
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001900 si_shutw(si);
1901 si_shutr(si);
1902 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001903 /* fall through */
1904
1905 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001906 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001907 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001908 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001909 if (stopping)
1910 spoe_wakeup_appctx(appctx);
1911
Christopher Faulet42bfa462017-01-04 14:14:19 +01001912 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1913 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914 si_oc(si)->flags |= CF_READ_DONTWAIT;
1915 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001916}
1917
1918struct applet spoe_applet = {
1919 .obj_type = OBJ_TYPE_APPLET,
1920 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001921 .fct = spoe_handle_appctx,
1922 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001923};
1924
1925/* Create a SPOE applet. On success, the created applet is returned, else
1926 * NULL. */
1927static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001928spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001929{
1930 struct appctx *appctx;
1931 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933
Emeric Brun1138fd02017-06-19 12:38:55 +02001934 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001935 goto out_error;
1936
Willy Tarreaubafbe012017-11-24 17:34:44 +01001937 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001938 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001940 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001941
Christopher Faulet42bfa462017-01-04 14:14:19 +01001942 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001943 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001944 goto out_free_spoe_appctx;
1945
1946 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001947 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001948 SPOE_APPCTX(appctx)->task->context = appctx;
1949 SPOE_APPCTX(appctx)->agent = conf->agent;
1950 SPOE_APPCTX(appctx)->version = 0;
1951 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1952 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001953 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001954 SPOE_APPCTX(appctx)->buffer = &buf_empty;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001955 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001956
1957 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1958 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001959 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001960
1961 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1962 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963
Willy Tarreau5820a362016-12-22 15:59:02 +01001964 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965 if (!sess)
1966 goto out_free_spoe;
1967
Willy Tarreau87787ac2017-08-28 16:22:54 +02001968 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969 goto out_free_sess;
1970
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971 stream_set_backend(strm, conf->agent->b.be);
1972
1973 /* applet is waiting for data */
1974 si_applet_cant_get(&strm->si[0]);
1975 appctx_wakeup(appctx);
1976
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977 strm->do_log = NULL;
1978 strm->res.flags |= CF_READ_DONTWAIT;
1979
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001980 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001981 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001982 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001983 SPOE_DEBUG_STMT(conf->agent->rt[tid].applets_act++);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001984
Emeric Brunc60def82017-09-27 14:59:38 +02001985 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001986 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001987 return appctx;
1988
1989 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990 out_free_sess:
1991 session_free(sess);
1992 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01001993 task_free(SPOE_APPCTX(appctx)->task);
1994 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001995 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 out_free_appctx:
1997 appctx_free(appctx);
1998 out_error:
1999 return NULL;
2000}
2001
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002003spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004{
2005 struct spoe_config *conf = FLT_CONF(ctx->filter);
2006 struct spoe_agent *agent = conf->agent;
2007 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002008 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009
Christopher Fauleta1cda022016-12-21 08:58:06 +01002010 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002011 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002012 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002013 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002014
2015 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002016 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2018 ctx->strm);
2019
Christopher Fauleta1cda022016-12-21 08:58:06 +01002020 /* Do not try to create a new applet if there is no server up for the
2021 * agent's backend. */
2022 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2023 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2024 " - cannot create SPOE appctx: no server up\n",
2025 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2026 __FUNCTION__, ctx->strm);
2027 goto end;
2028 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029
Christopher Fauleta1cda022016-12-21 08:58:06 +01002030 /* Do not try to create a new applet if we have reached the maximum of
2031 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002032 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002033 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002034 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2035 " - cannot create SPOE appctx: max CPS reached\n",
2036 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2037 __FUNCTION__, ctx->strm);
2038 goto end;
2039 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040 }
2041
Christopher Faulet8ef75252017-02-20 22:56:03 +01002042 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002043 if (appctx == NULL) {
2044 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2045 " - failed to create SPOE appctx\n",
2046 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2047 __FUNCTION__, ctx->strm);
Christopher Faulet72bcc472017-01-04 16:39:41 +01002048 send_log(ctx->strm->be, LOG_EMERG,
2049 "SPOE: [%s] failed to create SPOE applet\n",
2050 agent->id);
2051
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002053 }
2054
Christopher Fauleta1cda022016-12-21 08:58:06 +01002055 /* Increase the per-process number of cumulated connections */
2056 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002057 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002058
Christopher Fauleta1cda022016-12-21 08:58:06 +01002059 end:
2060 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002061 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002062 ctx->status_code = SPOE_CTX_ERR_RES;
2063 return -1;
2064 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002065
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002066 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002067 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002068 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2069 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002070
2071 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002072 " - Add stream in sending queue"
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002073 " - applets_act=%u - applets_idle=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet24289f22017-09-25 14:48:02 +02002075 ctx->strm, agent->rt[tid].applets_act, agent->rt[tid].applets_idle,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002076 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002077
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002078 /* Finally try to wakeup an IDLE applet. */
2079 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2080 struct eb32_node *node;
2081
2082 node = eb32_first(&agent->rt[tid].idle_applets);
2083 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2084 if (node && spoe_appctx) {
2085 eb32_delete(&spoe_appctx->node);
2086 spoe_appctx->node.key++;
2087 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2088 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002089 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002090 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002091 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002092}
2093
2094/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002095 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002096 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002097/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2098 * fragmented_content. If the next message can be processed, it returns 0. If
2099 * the message is too big, it returns -1.*/
2100static int
2101spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2102 struct spoe_message *msg, int dir,
2103 char **buf, char *end)
2104{
2105 struct sample *smp;
2106 struct spoe_arg *arg;
2107 int ret;
2108
2109 if (msg->cond) {
2110 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2111 ret = acl_pass(ret);
2112 if (msg->cond->pol == ACL_COND_UNLESS)
2113 ret = !ret;
2114
2115 /* the rule does not match */
2116 if (!ret)
2117 goto next;
2118 }
2119
2120 /* Resume encoding of a SPOE argument */
2121 if (ctx->frag_ctx.curarg != NULL) {
2122 arg = ctx->frag_ctx.curarg;
2123 goto encode_argument;
2124 }
2125
2126 if (ctx->frag_ctx.curoff != UINT_MAX)
2127 goto encode_msg_payload;
2128
2129 /* Check if there is enough space for the message name and the
2130 * number of arguments. It implies <msg->id_len> is encoded on 2
2131 * bytes, at most (< 2288). */
2132 if (*buf + 2 + msg->id_len + 1 > end)
2133 goto too_big;
2134
2135 /* Encode the message name */
2136 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2137 goto too_big;
2138
2139 /* Set the number of arguments for this message */
2140 **buf = msg->nargs;
2141 (*buf)++;
2142
2143 ctx->frag_ctx.curoff = 0;
2144 encode_msg_payload:
2145
2146 /* Loop on arguments */
2147 list_for_each_entry(arg, &msg->args, list) {
2148 ctx->frag_ctx.curarg = arg;
2149 ctx->frag_ctx.curoff = UINT_MAX;
2150
2151 encode_argument:
2152 if (ctx->frag_ctx.curoff != UINT_MAX)
2153 goto encode_arg_value;
2154
2155 /* Encode the arguement name as a string. It can by NULL */
2156 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2157 goto too_big;
2158
2159 ctx->frag_ctx.curoff = 0;
2160 encode_arg_value:
2161
2162 /* Fetch the arguement value */
2163 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2164 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2165 if (ret == -1 || ctx->frag_ctx.curoff)
2166 goto too_big;
2167 }
2168
2169 next:
2170 return 0;
2171
2172 too_big:
2173 return -1;
2174}
2175
Christopher Fauletc718b822017-09-21 16:50:56 +02002176/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2177 * handle fragmented content. On success it returns 1. If an error occurred, -1
2178 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2179 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002180static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002181spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002182 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002183{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002184 struct spoe_config *conf = FLT_CONF(ctx->filter);
2185 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002186 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002187 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002188
Christopher Faulet8ef75252017-02-20 22:56:03 +01002189 p = ctx->buffer->p;
Christopher Faulet24289f22017-09-25 14:48:02 +02002190 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002191
Christopher Fauletc718b822017-09-21 16:50:56 +02002192 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2193 /* Resume encoding of a SPOE message */
2194 if (ctx->frag_ctx.curmsg != NULL) {
2195 msg = ctx->frag_ctx.curmsg;
2196 goto encode_evt_message;
2197 }
2198
2199 list_for_each_entry(msg, messages, by_evt) {
2200 ctx->frag_ctx.curmsg = msg;
2201 ctx->frag_ctx.curarg = NULL;
2202 ctx->frag_ctx.curoff = UINT_MAX;
2203
2204 encode_evt_message:
2205 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2206 goto too_big;
2207 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002208 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002209 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2210 /* Resume encoding of a SPOE message */
2211 if (ctx->frag_ctx.curmsg != NULL) {
2212 msg = ctx->frag_ctx.curmsg;
2213 goto encode_grp_message;
2214 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002215
Christopher Fauletc718b822017-09-21 16:50:56 +02002216 list_for_each_entry(msg, messages, by_grp) {
2217 ctx->frag_ctx.curmsg = msg;
2218 ctx->frag_ctx.curarg = NULL;
2219 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002220
Christopher Fauletc718b822017-09-21 16:50:56 +02002221 encode_grp_message:
2222 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2223 goto too_big;
2224 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002225 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002226 else
2227 goto skip;
2228
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002229
Christopher Faulet57583e42017-09-04 15:41:09 +02002230 /* nothing has been encoded for an unfragmented payload */
2231 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == ctx->buffer->p)
2232 goto skip;
2233
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002234 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002235 " - encode %s messages - spoe_appctx=%p"
2236 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002237 (int)now.tv_sec, (int)now.tv_usec,
2238 agent->id, __FUNCTION__, s,
2239 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002240 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet8ef75252017-02-20 22:56:03 +01002241 p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002242
Christopher Faulet8ef75252017-02-20 22:56:03 +01002243 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002244 ctx->frag_ctx.curmsg = NULL;
2245 ctx->frag_ctx.curarg = NULL;
2246 ctx->frag_ctx.curoff = 0;
2247 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002248
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002249 return 1;
2250
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002251 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002252 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2253 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2254 return -1;
2255 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002256
2257 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002258 " - encode fragmented messages - spoe_appctx=%p"
2259 " - curmsg=%p - curarg=%p - curoff=%u"
2260 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002261 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002262 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Christopher Faulet24289f22017-09-25 14:48:02 +02002264 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - ctx->buffer->p);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002265
Christopher Faulet8ef75252017-02-20 22:56:03 +01002266 ctx->buffer->i = p - ctx->buffer->p;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2268 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2269 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002270
2271 skip:
2272 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2273 " - skip the frame because nothing has been encoded\n",
2274 (int)now.tv_sec, (int)now.tv_usec,
2275 agent->id, __FUNCTION__, s);
2276 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002277}
2278
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002279
2280/***************************************************************************
2281 * Functions that handle SPOE actions
2282 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002283/* Helper function to set a variable */
2284static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002285spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002286 struct sample *smp)
2287{
2288 struct spoe_config *conf = FLT_CONF(ctx->filter);
2289 struct spoe_agent *agent = conf->agent;
2290 char varname[64];
2291
2292 memset(varname, 0, sizeof(varname));
2293 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2294 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002295 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2296 vars_set_by_name(varname, len, smp);
2297 else
2298 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002299}
2300
2301/* Helper function to unset a variable */
2302static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002303spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002304 struct sample *smp)
2305{
2306 struct spoe_config *conf = FLT_CONF(ctx->filter);
2307 struct spoe_agent *agent = conf->agent;
2308 char varname[64];
2309
2310 memset(varname, 0, sizeof(varname));
2311 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2312 scope, agent->var_pfx, len, name);
2313 vars_unset_by_name_ifexist(varname, len, smp);
2314}
2315
2316
Christopher Faulet8ef75252017-02-20 22:56:03 +01002317static inline int
2318spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2319 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002321 char *str, *scope, *p = *buf;
2322 struct sample smp;
2323 uint64_t sz;
2324 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002325
Christopher Faulet8ef75252017-02-20 22:56:03 +01002326 if (p + 2 >= end)
2327 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002328
Christopher Faulet8ef75252017-02-20 22:56:03 +01002329 /* SET-VAR requires 3 arguments */
2330 if (*p++ != 3)
2331 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002332
Christopher Faulet8ef75252017-02-20 22:56:03 +01002333 switch (*p++) {
2334 case SPOE_SCOPE_PROC: scope = "proc"; break;
2335 case SPOE_SCOPE_SESS: scope = "sess"; break;
2336 case SPOE_SCOPE_TXN : scope = "txn"; break;
2337 case SPOE_SCOPE_REQ : scope = "req"; break;
2338 case SPOE_SCOPE_RES : scope = "res"; break;
2339 default: goto skip;
2340 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002341
Christopher Faulet8ef75252017-02-20 22:56:03 +01002342 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2343 goto skip;
2344 memset(&smp, 0, sizeof(smp));
2345 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002346
Christopher Faulet8ef75252017-02-20 22:56:03 +01002347 if (spoe_decode_data(&p, end, &smp) == -1)
2348 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002349
Christopher Faulet8ef75252017-02-20 22:56:03 +01002350 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2351 " - set-var '%s.%s.%.*s'\n",
2352 (int)now.tv_sec, (int)now.tv_usec,
2353 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2354 __FUNCTION__, s, scope,
2355 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2356 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002357
Christopher Faulet8ef75252017-02-20 22:56:03 +01002358 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360 ret = (p - *buf);
2361 *buf = p;
2362 return ret;
2363 skip:
2364 return 0;
2365}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002366
Christopher Faulet8ef75252017-02-20 22:56:03 +01002367static inline int
2368spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2369 char **buf, char *end, int dir)
2370{
2371 char *str, *scope, *p = *buf;
2372 struct sample smp;
2373 uint64_t sz;
2374 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 if (p + 2 >= end)
2377 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378
Christopher Faulet8ef75252017-02-20 22:56:03 +01002379 /* UNSET-VAR requires 2 arguments */
2380 if (*p++ != 2)
2381 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002382
Christopher Faulet8ef75252017-02-20 22:56:03 +01002383 switch (*p++) {
2384 case SPOE_SCOPE_PROC: scope = "proc"; break;
2385 case SPOE_SCOPE_SESS: scope = "sess"; break;
2386 case SPOE_SCOPE_TXN : scope = "txn"; break;
2387 case SPOE_SCOPE_REQ : scope = "req"; break;
2388 case SPOE_SCOPE_RES : scope = "res"; break;
2389 default: goto skip;
2390 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002391
Christopher Faulet8ef75252017-02-20 22:56:03 +01002392 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2393 goto skip;
2394 memset(&smp, 0, sizeof(smp));
2395 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2398 " - unset-var '%s.%s.%.*s'\n",
2399 (int)now.tv_sec, (int)now.tv_usec,
2400 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2401 __FUNCTION__, s, scope,
2402 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2403 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002404
Christopher Faulet8ef75252017-02-20 22:56:03 +01002405 spoe_unset_var(ctx, scope, str, sz, &smp);
2406
2407 ret = (p - *buf);
2408 *buf = p;
2409 return ret;
2410 skip:
2411 return 0;
2412}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002413
Christopher Faulet8ef75252017-02-20 22:56:03 +01002414/* Process SPOE actions for a specific event. It returns 1 on success. If an
2415 * error occurred, 0 is returned. */
2416static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002417spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418{
2419 char *p, *end;
2420 int ret;
2421
2422 p = ctx->buffer->p;
2423 end = p + ctx->buffer->i;
2424
2425 while (p < end) {
2426 enum spoe_action_type type;
2427
2428 type = *p++;
2429 switch (type) {
2430 case SPOE_ACT_T_SET_VAR:
2431 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2432 if (!ret)
2433 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002434 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002435
Christopher Faulet8ef75252017-02-20 22:56:03 +01002436 case SPOE_ACT_T_UNSET_VAR:
2437 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2438 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002439 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002440 break;
2441
2442 default:
2443 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002444 }
2445 }
2446
2447 return 1;
2448 skip:
2449 return 0;
2450}
2451
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002452/***************************************************************************
2453 * Functions that process SPOE events
2454 **************************************************************************/
2455static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002456spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002457{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002458 /* If a process is already started for this SPOE context, retry
2459 * later. */
2460 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002461 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002462
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002463 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002464 ctx->stats.tv_start = now;
2465 ctx->stats.tv_request = now;
2466 ctx->stats.t_request = -1;
2467 ctx->stats.t_queue = -1;
2468 ctx->stats.t_waiting = -1;
2469 ctx->stats.t_response = -1;
2470 ctx->stats.t_process = -1;
2471
2472 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002473
Christopher Fauleta1cda022016-12-21 08:58:06 +01002474 /* Set the right flag to prevent request and response processing
2475 * in same time. */
2476 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2477 ? SPOE_CTX_FL_REQ_PROCESS
2478 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002479 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002480}
2481
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002482static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002483spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002484{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002485 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002486
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002487 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2488 return;
2489
Christopher Faulet879dca92018-03-23 11:53:24 +01002490 if (sa) {
2491 if (sa->frag_ctx.ctx == ctx) {
2492 sa->frag_ctx.ctx = NULL;
2493 spoe_wakeup_appctx(sa->owner);
2494 }
2495 else
2496 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002497 }
2498
Christopher Fauleta1cda022016-12-21 08:58:06 +01002499 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002500 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002501 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002502
2503 /* Reset processing timer */
2504 ctx->process_exp = TICK_ETERNITY;
2505
Christopher Faulet8ef75252017-02-20 22:56:03 +01002506 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002507
Christopher Fauletfce747b2018-01-24 15:59:32 +01002508 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002509 ctx->frag_ctx.curmsg = NULL;
2510 ctx->frag_ctx.curarg = NULL;
2511 ctx->frag_ctx.curoff = 0;
2512 ctx->frag_ctx.flags = 0;
2513
Christopher Fauleta1cda022016-12-21 08:58:06 +01002514 if (!LIST_ISEMPTY(&ctx->list)) {
2515 LIST_DEL(&ctx->list);
2516 LIST_INIT(&ctx->list);
2517 }
2518}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002519
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002520static void
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002521spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2522 struct spoe_context *ctx, int dir)
2523{
2524 if (!tv_iszero(&ctx->stats.tv_start)) {
2525 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2526 ctx->stats.t_total += ctx->stats.t_process;
2527 tv_zero(&ctx->stats.tv_request);
2528 tv_zero(&ctx->stats.tv_queue);
2529 tv_zero(&ctx->stats.tv_wait);
2530 tv_zero(&ctx->stats.tv_response);
2531 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01002532
2533 if (agent->var_t_process) {
2534 struct sample smp;
2535
2536 memset(&smp, 0, sizeof(smp));
2537 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2538 smp.data.u.sint = ctx->stats.t_process;
2539 smp.data.type = SMP_T_SINT;
2540
2541 spoe_set_var(ctx, "txn", agent->var_t_process,
2542 strlen(agent->var_t_process), &smp);
2543 }
2544
2545 if (agent->var_t_total) {
2546 struct sample smp;
2547
2548 memset(&smp, 0, sizeof(smp));
2549 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2550 smp.data.u.sint = ctx->stats.t_total;
2551 smp.data.type = SMP_T_SINT;
2552
2553 spoe_set_var(ctx, "txn", agent->var_t_total,
2554 strlen(agent->var_t_total), &smp);
2555 }
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002556}
2557
2558static void
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002559spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2560 struct spoe_context *ctx, int dir)
2561{
2562 if (agent->eps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002563 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002564
2565 if (agent->var_on_error) {
2566 struct sample smp;
2567
2568 memset(&smp, 0, sizeof(smp));
2569 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2570 smp.data.u.sint = ctx->status_code;
2571 smp.data.type = SMP_T_BOOL;
2572
2573 spoe_set_var(ctx, "txn", agent->var_on_error,
2574 strlen(agent->var_on_error), &smp);
2575 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002576
2577 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2578 ? SPOE_CTX_ST_READY
2579 : SPOE_CTX_ST_NONE);
2580}
2581
Christopher Faulet58d03682017-09-21 16:57:24 +02002582/* Process a list of SPOE messages. First, this functions will process messages
2583 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2584 * to process corresponding actions. During all the processing, it returns 0
2585 * and it returns 1 when the processing is finished. If an error occurred, -1
2586 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002587static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002588spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2589 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002590{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002591 struct spoe_config *conf = FLT_CONF(ctx->filter);
2592 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002593 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002594
2595 if (ctx->state == SPOE_CTX_ST_ERROR)
2596 goto error;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002597
2598 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2599 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002600 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002601 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002602 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002603 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002604 goto error;
2605 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002606
2607 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002608 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002609 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002611 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002612 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002613 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002614 goto skip;
2615 }
2616 }
2617
Christopher Fauletf7a30922016-11-10 15:04:51 +01002618 if (!tick_isset(ctx->process_exp)) {
2619 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2620 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2621 ctx->process_exp);
2622 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002623 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002624 if (!ret)
2625 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002626
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002627 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002628 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002629 }
2630
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002631 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002632 if (!tv_iszero(&ctx->stats.tv_request))
2633 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002634 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002635 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002636 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002637 if (ret < 0)
2638 goto error;
Christopher Faulet57583e42017-09-04 15:41:09 +02002639 if (!ret)
2640 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002641 if (spoe_queue_context(ctx) < 0)
2642 goto error;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002643 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2644 }
2645
2646 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002647 if (ctx->spoe_appctx)
2648 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002649 ret = 0;
2650 goto out;
2651 }
2652
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002653 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2654 ret = 0;
2655 goto out;
2656 }
2657
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002658 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002659 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002660 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002661 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002662 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002663 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002664 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002665 }
2666
2667 out:
2668 return ret;
2669
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002670 error:
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002671 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002672 ret = 1;
2673 goto end;
2674
2675 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002676 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002677 ctx->state = SPOE_CTX_ST_READY;
2678 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002679
Christopher Fauleta1cda022016-12-21 08:58:06 +01002680 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002681 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002682 spoe_stop_processing(agent, ctx);
Christopher Faulet58d03682017-09-21 16:57:24 +02002683 return ret;
2684}
2685
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002686/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2687 * See spoe_process_message for details. */
2688static int
2689spoe_process_group(struct stream *s, struct spoe_context *ctx,
2690 struct spoe_group *group, int dir)
2691{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002692 struct spoe_config *conf = FLT_CONF(ctx->filter);
2693 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002694 int ret;
2695
2696 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2697 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002698 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002699 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2700 group->id);
2701
2702 if (LIST_ISEMPTY(&group->messages))
2703 return 1;
2704
2705 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002706 if (ret && ctx->stats.t_process != -1) {
2707 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2708 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2709 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2710 __FUNCTION__, s, s->uniq_id, ctx->status_code,
2711 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2712 ctx->stats.t_response, ctx->stats.t_process);
2713 send_log(s->be, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2714 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2715 agent->id, group->id, s->uniq_id, ctx->status_code,
2716 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2717 ctx->stats.t_response, ctx->stats.t_process);
2718 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002719 return ret;
2720}
2721
Christopher Faulet58d03682017-09-21 16:57:24 +02002722/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2723 * See spoe_process_message for details. */
2724static int
2725spoe_process_event(struct stream *s, struct spoe_context *ctx,
2726 enum spoe_event ev)
2727{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002728 struct spoe_config *conf = FLT_CONF(ctx->filter);
2729 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002730 int dir, ret;
2731
2732 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002733 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002734 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002735 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2736 spoe_event_str[ev]);
2737
2738 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2739
2740 if (LIST_ISEMPTY(&(ctx->events[ev])))
2741 return 1;
2742
2743 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002744 if (ret && ctx->stats.t_process != -1) {
2745 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2746 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2747 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2748 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2749 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2750 ctx->stats.t_response, ctx->stats.t_process);
2751 send_log(s->be, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2752 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld\n",
2753 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2754 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2755 ctx->stats.t_response, ctx->stats.t_process);
2756 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002757 return ret;
2758}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002759
2760/***************************************************************************
2761 * Functions that create/destroy SPOE contexts
2762 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002763static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002764spoe_acquire_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002765{
Christopher Faulet600d37e2017-11-10 11:54:58 +01002766 if ((*buf)->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002767 return 1;
2768
Christopher Faulet4596fb72017-01-11 14:05:19 +01002769 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002770 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002771 LIST_DEL(&buffer_wait->list);
2772 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002773 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002774 }
2775
Christopher Faulet4596fb72017-01-11 14:05:19 +01002776 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002777 return 1;
2778
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002779 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002780 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002781 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002782 return 0;
2783}
2784
2785static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002786spoe_release_buffer(struct buffer **buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002787{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002788 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002789 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002790 LIST_DEL(&buffer_wait->list);
2791 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002792 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002793 }
2794
2795 /* Release the buffer if needed */
Christopher Faulet600d37e2017-11-10 11:54:58 +01002796 if ((*buf)->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002797 b_free(buf);
2798 offer_buffers(buffer_wait->target,
2799 tasks_run_queue + applets_active_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002800 }
2801}
2802
Christopher Faulet4596fb72017-01-11 14:05:19 +01002803static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002804spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002805{
2806 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2807 return 1;
2808}
2809
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002810static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002811spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002812{
2813 struct spoe_config *conf = FLT_CONF(filter);
2814 struct spoe_context *ctx;
2815
Willy Tarreaubafbe012017-11-24 17:34:44 +01002816 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002817 if (ctx == NULL) {
2818 return NULL;
2819 }
2820 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002821 ctx->filter = filter;
2822 ctx->state = SPOE_CTX_ST_NONE;
2823 ctx->status_code = SPOE_CTX_ERR_NONE;
2824 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002825 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002826 ctx->groups = &conf->agent->groups;
Christopher Fauletb067b062017-01-04 16:39:11 +01002827 ctx->buffer = &buf_empty;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002828 LIST_INIT(&ctx->buffer_wait.list);
2829 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002830 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002831 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002832
Christopher Fauletf7a30922016-11-10 15:04:51 +01002833 ctx->stream_id = 0;
2834 ctx->frame_id = 1;
2835 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002836
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002837 tv_zero(&ctx->stats.tv_start);
2838 tv_zero(&ctx->stats.tv_request);
2839 tv_zero(&ctx->stats.tv_queue);
2840 tv_zero(&ctx->stats.tv_wait);
2841 tv_zero(&ctx->stats.tv_response);
2842 ctx->stats.t_request = -1;
2843 ctx->stats.t_queue = -1;
2844 ctx->stats.t_waiting = -1;
2845 ctx->stats.t_response = -1;
2846 ctx->stats.t_process = -1;
2847 ctx->stats.t_total = 0;
2848
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002849 ctx->strm = s;
2850 ctx->state = SPOE_CTX_ST_READY;
2851 filter->ctx = ctx;
2852
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002853 return ctx;
2854}
2855
2856static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002857spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002858{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002859 struct spoe_config *conf = FLT_CONF(filter);
2860 struct spoe_context *ctx = filter->ctx;
2861
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002862 if (!ctx)
2863 return;
2864
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002865 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002866 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002867 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002868}
2869
2870static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002871spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002872{
2873 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002874 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002875
2876 tv_zero(&ctx->stats.tv_start);
2877 tv_zero(&ctx->stats.tv_request);
2878 tv_zero(&ctx->stats.tv_queue);
2879 tv_zero(&ctx->stats.tv_wait);
2880 tv_zero(&ctx->stats.tv_response);
2881 ctx->stats.t_request = -1;
2882 ctx->stats.t_queue = -1;
2883 ctx->stats.t_waiting = -1;
2884 ctx->stats.t_response = -1;
2885 ctx->stats.t_process = -1;
2886 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002887}
2888
2889
2890/***************************************************************************
2891 * Hooks that manage the filter lifecycle (init/check/deinit)
2892 **************************************************************************/
2893/* Signal handler: Do a soft stop, wakeup SPOE applet */
2894static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002895spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002896{
2897 struct proxy *p;
2898
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002899 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002900 while (p) {
2901 struct flt_conf *fconf;
2902
2903 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002904 struct spoe_config *conf;
2905 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002906 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002907 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002908
Christopher Faulet3b386a32017-02-23 10:17:15 +01002909 if (fconf->id != spoe_filter_id)
2910 continue;
2911
2912 conf = fconf->conf;
2913 agent = conf->agent;
2914
Christopher Faulet24289f22017-09-25 14:48:02 +02002915 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002916 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002917 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2918 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002919 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002920 }
2921 }
2922 p = p->next;
2923 }
2924}
2925
2926
2927/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2928static int
2929spoe_init(struct proxy *px, struct flt_conf *fconf)
2930{
2931 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002932
2933 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
2934 init_new_proxy(&conf->agent_fe);
2935 conf->agent_fe.parent = conf->agent;
2936 conf->agent_fe.last_change = now.tv_sec;
2937 conf->agent_fe.id = conf->agent->id;
2938 conf->agent_fe.cap = PR_CAP_FE;
2939 conf->agent_fe.mode = PR_MODE_TCP;
2940 conf->agent_fe.maxconn = 0;
2941 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2942 conf->agent_fe.conn_retries = CONN_RETRIES;
2943 conf->agent_fe.accept = frontend_accept;
2944 conf->agent_fe.srv = NULL;
2945 conf->agent_fe.timeout.client = TICK_ETERNITY;
2946 conf->agent_fe.default_target = &spoe_applet.obj_type;
2947 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2948
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002949 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002950 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002951 sighandler_registered = 1;
2952 }
2953
2954 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002955}
2956
2957/* Free ressources allocated by the SPOE filter. */
2958static void
2959spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2960{
2961 struct spoe_config *conf = fconf->conf;
2962
2963 if (conf) {
2964 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002965
Christopher Faulet8ef75252017-02-20 22:56:03 +01002966 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002967 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002968 free(conf);
2969 }
2970 fconf->conf = NULL;
2971}
2972
2973/* Check configuration of a SPOE filter for a specified proxy.
2974 * Return 1 on error, else 0. */
2975static int
2976spoe_check(struct proxy *px, struct flt_conf *fconf)
2977{
Christopher Faulet7ee86672017-09-19 11:08:28 +02002978 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002979 struct spoe_config *conf = fconf->conf;
2980 struct proxy *target;
2981
Christopher Faulet7ee86672017-09-19 11:08:28 +02002982 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
2983 * are uniq */
2984 list_for_each_entry(f, &px->filter_configs, list) {
2985 struct spoe_config *c = f->conf;
2986
2987 /* This is not an SPOE filter */
2988 if (f->id != spoe_filter_id)
2989 continue;
2990 /* This is the current SPOE filter */
2991 if (f == fconf)
2992 continue;
2993
2994 /* Check engine Id. It should be uniq */
2995 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01002996 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
2997 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002998 return 1;
2999 }
3000 }
3001
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003002 target = proxy_be_by_name(conf->agent->b.name);
3003 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003004 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3005 " declared at %s:%d.\n",
3006 px->id, conf->agent->b.name, conf->agent->id,
3007 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003008 return 1;
3009 }
3010 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003011 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3012 " at %s:%d does not support HTTP mode.\n",
3013 px->id, target->id, conf->agent->id,
3014 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003015 return 1;
3016 }
3017
3018 free(conf->agent->b.name);
3019 conf->agent->b.name = NULL;
3020 conf->agent->b.be = target;
3021 return 0;
3022}
3023
3024/**************************************************************************
3025 * Hooks attached to a stream
3026 *************************************************************************/
3027/* Called when a filter instance is created and attach to a stream. It creates
3028 * the context that will be used to process this stream. */
3029static int
3030spoe_start(struct stream *s, struct filter *filter)
3031{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003032 struct spoe_config *conf = FLT_CONF(filter);
3033 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003034 struct spoe_context *ctx;
3035
3036 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003037 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038 __FUNCTION__, s);
3039
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003040 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003041 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3042 " - failed to create SPOE context\n",
3043 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003044 __FUNCTION__, s);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 send_log(s->be, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003046 "SPOE: [%s] failed to create SPOE context\n",
3047 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048 return 0;
3049 }
3050
Christopher Faulet11610f32017-09-21 10:23:10 +02003051 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3053
Christopher Faulet11610f32017-09-21 10:23:10 +02003054 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003055 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3056
Christopher Faulet11610f32017-09-21 10:23:10 +02003057 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003058 filter->pre_analyzers |= AN_RES_INSPECT;
3059
Christopher Faulet11610f32017-09-21 10:23:10 +02003060 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003061 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3062
Christopher Faulet11610f32017-09-21 10:23:10 +02003063 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003064 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3065
Christopher Faulet11610f32017-09-21 10:23:10 +02003066 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003067 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3068
3069 return 1;
3070}
3071
3072/* Called when a filter instance is detached from a stream. It release the
3073 * attached SPOE context. */
3074static void
3075spoe_stop(struct stream *s, struct filter *filter)
3076{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003077 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3078 (int)now.tv_sec, (int)now.tv_usec,
3079 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3080 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003081 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003082}
3083
Christopher Fauletf7a30922016-11-10 15:04:51 +01003084
3085/*
3086 * Called when the stream is woken up because of expired timer.
3087 */
3088static void
3089spoe_check_timeouts(struct stream *s, struct filter *filter)
3090{
3091 struct spoe_context *ctx = filter->ctx;
3092
Christopher Fauletac580602018-03-20 16:09:20 +01003093 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003094 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003095}
3096
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003097/* Called when we are ready to filter data on a channel */
3098static int
3099spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3100{
3101 struct spoe_context *ctx = filter->ctx;
3102 int ret = 1;
3103
3104 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3105 " - ctx-flags=0x%08x\n",
3106 (int)now.tv_sec, (int)now.tv_usec,
3107 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3108 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3109
Christopher Fauletb067b062017-01-04 16:39:11 +01003110 if (ctx->state == SPOE_CTX_ST_NONE)
3111 goto out;
3112
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003113 if (!(chn->flags & CF_ISRESP)) {
3114 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3115 chn->analysers |= AN_REQ_INSPECT_FE;
3116 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3117 chn->analysers |= AN_REQ_INSPECT_BE;
3118
3119 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3120 goto out;
3121
3122 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003123 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003124 if (!ret)
3125 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003126 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3127 }
3128 else {
3129 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3130 chn->analysers |= AN_RES_INSPECT;
3131
3132 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3133 goto out;
3134
Christopher Faulet8ef75252017-02-20 22:56:03 +01003135 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003136 if (!ret) {
3137 channel_dont_read(chn);
3138 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003139 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003140 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003141 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142 }
3143
3144 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003145 return ret;
3146}
3147
3148/* Called before a processing happens on a given channel */
3149static int
3150spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3151 struct channel *chn, unsigned an_bit)
3152{
3153 struct spoe_context *ctx = filter->ctx;
3154 int ret = 1;
3155
3156 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3157 " - ctx-flags=0x%08x - ana=0x%08x\n",
3158 (int)now.tv_sec, (int)now.tv_usec,
3159 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3160 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3161 ctx->flags, an_bit);
3162
Christopher Fauletb067b062017-01-04 16:39:11 +01003163 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003164 goto out;
3165
3166 switch (an_bit) {
3167 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003168 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169 break;
3170 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003171 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003172 break;
3173 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003174 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003175 break;
3176 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003177 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003178 break;
3179 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003180 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003181 break;
3182 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003183 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003184 break;
3185 }
3186
3187 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003188 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003189 channel_dont_read(chn);
3190 channel_dont_close(chn);
3191 }
3192 return ret;
3193}
3194
3195/* Called when the filtering on the channel ends. */
3196static int
3197spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3198{
3199 struct spoe_context *ctx = filter->ctx;
3200
3201 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3202 " - ctx-flags=0x%08x\n",
3203 (int)now.tv_sec, (int)now.tv_usec,
3204 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3205 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3206
3207 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003208 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003209 }
3210
3211 return 1;
3212}
3213
3214/********************************************************************
3215 * Functions that manage the filter initialization
3216 ********************************************************************/
3217struct flt_ops spoe_ops = {
3218 /* Manage SPOE filter, called for each filter declaration */
3219 .init = spoe_init,
3220 .deinit = spoe_deinit,
3221 .check = spoe_check,
3222
3223 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003224 .attach = spoe_start,
3225 .detach = spoe_stop,
3226 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227
3228 /* Handle channels activity */
3229 .channel_start_analyze = spoe_start_analyze,
3230 .channel_pre_analyze = spoe_chn_pre_analyze,
3231 .channel_end_analyze = spoe_end_analyze,
3232};
3233
3234
3235static int
3236cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3237{
3238 const char *err;
3239 int i, err_code = 0;
3240
3241 if ((cfg_scope == NULL && curengine != NULL) ||
3242 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003243 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003244 goto out;
3245
3246 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3247 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003248 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3249 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 err_code |= ERR_ALERT | ERR_ABORT;
3251 goto out;
3252 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003253 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3254 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003255 goto out;
3256 }
3257
3258 err = invalid_char(args[1]);
3259 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003260 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3261 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003262 err_code |= ERR_ALERT | ERR_ABORT;
3263 goto out;
3264 }
3265
3266 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003267 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3268 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003269 err_code |= ERR_ALERT | ERR_ABORT;
3270 goto out;
3271 }
3272 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003273 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003274 err_code |= ERR_ALERT | ERR_ABORT;
3275 goto out;
3276 }
3277
3278 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003279
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003280 curagent->conf.file = strdup(file);
3281 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003282
3283 curagent->timeout.hello = TICK_ETERNITY;
3284 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003285 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003286
3287 curagent->engine_id = NULL;
3288 curagent->var_pfx = NULL;
3289 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003290 curagent->var_t_process = NULL;
3291 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003292 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3293 if (global.nbthread == 1)
3294 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003295 curagent->cps_max = 0;
3296 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003297 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003298 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299
3300 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003301 LIST_INIT(&curagent->events[i]);
3302 LIST_INIT(&curagent->groups);
3303 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003304
Christopher Faulet24289f22017-09-25 14:48:02 +02003305 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003306 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003307 err_code |= ERR_ALERT | ERR_ABORT;
3308 goto out;
3309 }
3310 for (i = 0; i < global.nbthread; ++i) {
3311 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003312 SPOE_DEBUG_STMT(curagent->rt[i].applets_act = 0);
3313 SPOE_DEBUG_STMT(curagent->rt[i].applets_idle = 0);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003314 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003315 LIST_INIT(&curagent->rt[i].applets);
3316 LIST_INIT(&curagent->rt[i].sending_queue);
3317 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003318 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003319 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003320 }
3321 else if (!strcmp(args[0], "use-backend")) {
3322 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003323 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3324 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 err_code |= ERR_ALERT | ERR_FATAL;
3326 goto out;
3327 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003328 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003329 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003330 free(curagent->b.name);
3331 curagent->b.name = strdup(args[1]);
3332 }
3333 else if (!strcmp(args[0], "messages")) {
3334 int cur_arg = 1;
3335 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003336 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003337
Christopher Faulet11610f32017-09-21 10:23:10 +02003338 list_for_each_entry(ph, &curmphs, list) {
3339 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003340 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3341 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003342 err_code |= ERR_ALERT | ERR_FATAL;
3343 goto out;
3344 }
3345 }
3346
Christopher Faulet11610f32017-09-21 10:23:10 +02003347 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003348 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003349 err_code |= ERR_ALERT | ERR_ABORT;
3350 goto out;
3351 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003352 ph->id = strdup(args[cur_arg]);
3353 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003354 cur_arg++;
3355 }
3356 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003357 else if (!strcmp(args[0], "groups")) {
3358 int cur_arg = 1;
3359 while (*args[cur_arg]) {
3360 struct spoe_placeholder *ph = NULL;
3361
3362 list_for_each_entry(ph, &curgphs, list) {
3363 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003364 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3365 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003366 err_code |= ERR_ALERT | ERR_FATAL;
3367 goto out;
3368 }
3369 }
3370
3371 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003372 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003373 err_code |= ERR_ALERT | ERR_ABORT;
3374 goto out;
3375 }
3376 ph->id = strdup(args[cur_arg]);
3377 LIST_ADDQ(&curgphs, &ph->list);
3378 cur_arg++;
3379 }
3380 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 else if (!strcmp(args[0], "timeout")) {
3382 unsigned int *tv = NULL;
3383 const char *res;
3384 unsigned timeout;
3385
3386 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003387 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3388 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 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 Fauletf7e4e7e2016-10-27 22:29:49 +02003394 if (!strcmp(args[1], "hello"))
3395 tv = &curagent->timeout.hello;
3396 else if (!strcmp(args[1], "idle"))
3397 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003398 else if (!strcmp(args[1], "processing"))
3399 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003400 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003401 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3402 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003403 err_code |= ERR_ALERT | ERR_FATAL;
3404 goto out;
3405 }
3406 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003407 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3408 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003409 err_code |= ERR_ALERT | ERR_FATAL;
3410 goto out;
3411 }
3412 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3413 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003414 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3415 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003416 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417 goto out;
3418 }
3419 *tv = MS_TO_TICKS(timeout);
3420 }
3421 else if (!strcmp(args[0], "option")) {
3422 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003423 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3424 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003425 err_code |= ERR_ALERT | ERR_FATAL;
3426 goto out;
3427 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003428
Christopher Faulet305c6072017-02-23 16:17:53 +01003429 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003430 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003431 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003432 if (kwm == 1)
3433 curagent->flags &= ~SPOE_FL_PIPELINING;
3434 else
3435 curagent->flags |= SPOE_FL_PIPELINING;
3436 goto out;
3437 }
3438 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003439 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003440 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003441 if (kwm == 1)
3442 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003443 else {
3444 if (global.nbthread == 1)
3445 curagent->flags |= SPOE_FL_ASYNC;
3446 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003447 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3448 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003449 err_code |= ERR_WARN;
3450 }
3451 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003452 goto out;
3453 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003454 else if (!strcmp(args[1], "send-frag-payload")) {
3455 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3456 goto out;
3457 if (kwm == 1)
3458 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3459 else
3460 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3461 goto out;
3462 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003463
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003464 /* Following options does not support negation */
3465 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003466 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3467 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003468 err_code |= ERR_ALERT | ERR_FATAL;
3469 goto out;
3470 }
3471
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003472 if (!strcmp(args[1], "var-prefix")) {
3473 char *tmp;
3474
3475 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003476 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3477 file, linenum, args[0],
3478 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003479 err_code |= ERR_ALERT | ERR_FATAL;
3480 goto out;
3481 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003482 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3483 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003484 tmp = args[2];
3485 while (*tmp) {
3486 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003487 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3488 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003489 err_code |= ERR_ALERT | ERR_FATAL;
3490 goto out;
3491 }
3492 tmp++;
3493 }
3494 curagent->var_pfx = strdup(args[2]);
3495 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003496 else if (!strcmp(args[1], "force-set-var")) {
3497 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3498 goto out;
3499 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3500 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003501 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003502 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003503 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003504 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3505 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003506 else if (!strcmp(args[1], "set-on-error")) {
3507 char *tmp;
3508
3509 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003510 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3511 file, linenum, args[0],
3512 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003513 err_code |= ERR_ALERT | ERR_FATAL;
3514 goto out;
3515 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003516 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3517 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003518 tmp = args[2];
3519 while (*tmp) {
3520 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003521 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3522 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003523 err_code |= ERR_ALERT | ERR_FATAL;
3524 goto out;
3525 }
3526 tmp++;
3527 }
3528 curagent->var_on_error = strdup(args[2]);
3529 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003530 else if (!strcmp(args[1], "set-process-time")) {
3531 char *tmp;
3532
3533 if (!*args[2]) {
3534 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3535 file, linenum, args[0],
3536 args[1]);
3537 err_code |= ERR_ALERT | ERR_FATAL;
3538 goto out;
3539 }
3540 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3541 goto out;
3542 tmp = args[2];
3543 while (*tmp) {
3544 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3545 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3546 file, linenum, args[0], args[1]);
3547 err_code |= ERR_ALERT | ERR_FATAL;
3548 goto out;
3549 }
3550 tmp++;
3551 }
3552 curagent->var_t_process = strdup(args[2]);
3553 }
3554 else if (!strcmp(args[1], "set-total-time")) {
3555 char *tmp;
3556
3557 if (!*args[2]) {
3558 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3559 file, linenum, args[0],
3560 args[1]);
3561 err_code |= ERR_ALERT | ERR_FATAL;
3562 goto out;
3563 }
3564 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3565 goto out;
3566 tmp = args[2];
3567 while (*tmp) {
3568 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
3569 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z_-.] chars.\n",
3570 file, linenum, args[0], args[1]);
3571 err_code |= ERR_ALERT | ERR_FATAL;
3572 goto out;
3573 }
3574 tmp++;
3575 }
3576 curagent->var_t_total = strdup(args[2]);
3577 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003578 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003579 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3580 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003581 err_code |= ERR_ALERT | ERR_FATAL;
3582 goto out;
3583 }
Christopher Faulet48026722016-11-16 15:01:12 +01003584 }
3585 else if (!strcmp(args[0], "maxconnrate")) {
3586 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003587 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3588 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003589 err_code |= ERR_ALERT | ERR_FATAL;
3590 goto out;
3591 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003592 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003593 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003594 curagent->cps_max = atol(args[1]);
3595 }
3596 else if (!strcmp(args[0], "maxerrrate")) {
3597 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003598 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3599 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003600 err_code |= ERR_ALERT | ERR_FATAL;
3601 goto out;
3602 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003603 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003604 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003605 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003606 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003607 else if (!strcmp(args[0], "max-frame-size")) {
3608 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003609 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3610 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003611 err_code |= ERR_ALERT | ERR_FATAL;
3612 goto out;
3613 }
3614 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3615 goto out;
3616 curagent->max_frame_size = atol(args[1]);
3617 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3618 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003619 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3620 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003621 err_code |= ERR_ALERT | ERR_FATAL;
3622 goto out;
3623 }
3624 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003625 else if (!strcmp(args[0], "max-waiting-frames")) {
3626 if (!*args[1]) {
3627 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3628 file, linenum, args[0]);
3629 err_code |= ERR_ALERT | ERR_FATAL;
3630 goto out;
3631 }
3632 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3633 goto out;
3634 curagent->max_fpa = atol(args[1]);
3635 if (curagent->max_fpa < 1) {
3636 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3637 file, linenum, args[0]);
3638 err_code |= ERR_ALERT | ERR_FATAL;
3639 goto out;
3640 }
3641 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003642 else if (!strcmp(args[0], "register-var-names")) {
3643 int cur_arg;
3644
3645 if (!*args[1]) {
3646 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3647 file, linenum, args[0]);
3648 err_code |= ERR_ALERT | ERR_FATAL;
3649 goto out;
3650 }
3651 cur_arg = 1;
3652 while (*args[cur_arg]) {
3653 struct spoe_var_placeholder *vph;
3654
3655 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3656 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3657 err_code |= ERR_ALERT | ERR_ABORT;
3658 goto out;
3659 }
3660 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3661 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3662 err_code |= ERR_ALERT | ERR_ABORT;
3663 goto out;
3664 }
3665 LIST_ADDQ(&curvars, &vph->list);
3666 cur_arg++;
3667 }
3668 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003669 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003670 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3671 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003672 err_code |= ERR_ALERT | ERR_FATAL;
3673 goto out;
3674 }
3675 out:
3676 return err_code;
3677}
Christopher Faulet11610f32017-09-21 10:23:10 +02003678static int
3679cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3680{
3681 struct spoe_group *grp;
3682 const char *err;
3683 int err_code = 0;
3684
3685 if ((cfg_scope == NULL && curengine != NULL) ||
3686 (cfg_scope != NULL && curengine == NULL) ||
3687 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3688 goto out;
3689
3690 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3691 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003692 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3693 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003694 err_code |= ERR_ALERT | ERR_ABORT;
3695 goto out;
3696 }
3697 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3698 err_code |= ERR_ABORT;
3699 goto out;
3700 }
3701
3702 err = invalid_char(args[1]);
3703 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003704 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3705 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003706 err_code |= ERR_ALERT | ERR_ABORT;
3707 goto out;
3708 }
3709
3710 list_for_each_entry(grp, &curgrps, list) {
3711 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003712 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3713 " name as another one declared at %s:%d.\n",
3714 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003715 err_code |= ERR_ALERT | ERR_FATAL;
3716 goto out;
3717 }
3718 }
3719
3720 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003721 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003722 err_code |= ERR_ALERT | ERR_ABORT;
3723 goto out;
3724 }
3725
3726 curgrp->id = strdup(args[1]);
3727 curgrp->conf.file = strdup(file);
3728 curgrp->conf.line = linenum;
3729 LIST_INIT(&curgrp->phs);
3730 LIST_INIT(&curgrp->messages);
3731 LIST_ADDQ(&curgrps, &curgrp->list);
3732 }
3733 else if (!strcmp(args[0], "messages")) {
3734 int cur_arg = 1;
3735 while (*args[cur_arg]) {
3736 struct spoe_placeholder *ph = NULL;
3737
3738 list_for_each_entry(ph, &curgrp->phs, list) {
3739 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003740 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3741 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003742 err_code |= ERR_ALERT | ERR_FATAL;
3743 goto out;
3744 }
3745 }
3746
3747 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003748 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003749 err_code |= ERR_ALERT | ERR_ABORT;
3750 goto out;
3751 }
3752 ph->id = strdup(args[cur_arg]);
3753 LIST_ADDQ(&curgrp->phs, &ph->list);
3754 cur_arg++;
3755 }
3756 }
3757 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003758 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3759 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003760 err_code |= ERR_ALERT | ERR_FATAL;
3761 goto out;
3762 }
3763 out:
3764 return err_code;
3765}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003766
3767static int
3768cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3769{
3770 struct spoe_message *msg;
3771 struct spoe_arg *arg;
3772 const char *err;
3773 char *errmsg = NULL;
3774 int err_code = 0;
3775
3776 if ((cfg_scope == NULL && curengine != NULL) ||
3777 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003778 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003779 goto out;
3780
3781 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3782 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003783 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3784 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003785 err_code |= ERR_ALERT | ERR_ABORT;
3786 goto out;
3787 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003788 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3789 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003790 goto out;
3791 }
3792
3793 err = invalid_char(args[1]);
3794 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003795 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3796 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003797 err_code |= ERR_ALERT | ERR_ABORT;
3798 goto out;
3799 }
3800
3801 list_for_each_entry(msg, &curmsgs, list) {
3802 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003803 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3804 " name as another one declared at %s:%d.\n",
3805 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003806 err_code |= ERR_ALERT | ERR_FATAL;
3807 goto out;
3808 }
3809 }
3810
3811 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003812 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003813 err_code |= ERR_ALERT | ERR_ABORT;
3814 goto out;
3815 }
3816
3817 curmsg->id = strdup(args[1]);
3818 curmsg->id_len = strlen(curmsg->id);
3819 curmsg->event = SPOE_EV_NONE;
3820 curmsg->conf.file = strdup(file);
3821 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003822 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003823 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003824 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003825 LIST_INIT(&curmsg->by_evt);
3826 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003827 LIST_ADDQ(&curmsgs, &curmsg->list);
3828 }
3829 else if (!strcmp(args[0], "args")) {
3830 int cur_arg = 1;
3831
3832 curproxy->conf.args.ctx = ARGC_SPOE;
3833 curproxy->conf.args.file = file;
3834 curproxy->conf.args.line = linenum;
3835 while (*args[cur_arg]) {
3836 char *delim = strchr(args[cur_arg], '=');
3837 int idx = 0;
3838
3839 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003840 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003841 err_code |= ERR_ALERT | ERR_ABORT;
3842 goto out;
3843 }
3844
3845 if (!delim) {
3846 arg->name = NULL;
3847 arg->name_len = 0;
3848 delim = args[cur_arg];
3849 }
3850 else {
3851 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3852 arg->name_len = delim - args[cur_arg];
3853 delim++;
3854 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003855 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3856 &idx, file, linenum, &errmsg,
3857 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003858 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003859 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003860 err_code |= ERR_ALERT | ERR_FATAL;
3861 free(arg->name);
3862 free(arg);
3863 goto out;
3864 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003865 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003866 LIST_ADDQ(&curmsg->args, &arg->list);
3867 cur_arg++;
3868 }
3869 curproxy->conf.args.file = NULL;
3870 curproxy->conf.args.line = 0;
3871 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003872 else if (!strcmp(args[0], "acl")) {
3873 err = invalid_char(args[1]);
3874 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003875 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3876 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003877 err_code |= ERR_ALERT | ERR_FATAL;
3878 goto out;
3879 }
3880 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003881 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3882 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003883 err_code |= ERR_ALERT | ERR_FATAL;
3884 goto out;
3885 }
3886 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003887 else if (!strcmp(args[0], "event")) {
3888 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003889 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003890 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003891 goto out;
3892 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003893 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3894 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003895
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003896 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3897 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3898 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3899 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3900
3901 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3902 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3903 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3904 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3905 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3906 curmsg->event = SPOE_EV_ON_TCP_RSP;
3907
3908 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3909 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3910 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3911 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3912 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3913 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3914 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003915 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3916 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003917 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918 goto out;
3919 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003920
3921 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3922 struct acl_cond *cond;
3923
3924 cond = build_acl_cond(file, linenum, &curmsg->acls,
3925 curproxy, (const char **)args+2,
3926 &errmsg);
3927 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003928 ha_alert("parsing [%s:%d] : error detected while "
3929 "parsing an 'event %s' condition : %s.\n",
3930 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003931 err_code |= ERR_ALERT | ERR_FATAL;
3932 goto out;
3933 }
3934 curmsg->cond = cond;
3935 }
3936 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003937 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
3938 "or 'unless' followed by a condition but found '%s'.\n",
3939 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003940 err_code |= ERR_ALERT | ERR_FATAL;
3941 goto out;
3942 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003943 }
3944 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003945 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
3946 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003947 err_code |= ERR_ALERT | ERR_FATAL;
3948 goto out;
3949 }
3950 out:
3951 free(errmsg);
3952 return err_code;
3953}
3954
3955/* Return -1 on error, else 0 */
3956static int
3957parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
3958 struct flt_conf *fconf, char **err, void *private)
3959{
3960 struct list backup_sections;
3961 struct spoe_config *conf;
3962 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02003963 struct spoe_group *grp, *grpback;
3964 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003965 struct spoe_var_placeholder *vph, *vphback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003966 char *file = NULL, *engine = NULL;
3967 int ret, pos = *cur_arg + 1;
3968
Christopher Faulet84c844e2018-03-23 14:37:14 +01003969 LIST_INIT(&curmsgs);
3970 LIST_INIT(&curgrps);
3971 LIST_INIT(&curmphs);
3972 LIST_INIT(&curgphs);
3973 LIST_INIT(&curvars);
3974
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003975 conf = calloc(1, sizeof(*conf));
3976 if (conf == NULL) {
3977 memprintf(err, "%s: out of memory", args[*cur_arg]);
3978 goto error;
3979 }
3980 conf->proxy = px;
3981
3982 while (*args[pos]) {
3983 if (!strcmp(args[pos], "config")) {
3984 if (!*args[pos+1]) {
3985 memprintf(err, "'%s' : '%s' option without value",
3986 args[*cur_arg], args[pos]);
3987 goto error;
3988 }
3989 file = args[pos+1];
3990 pos += 2;
3991 }
3992 else if (!strcmp(args[pos], "engine")) {
3993 if (!*args[pos+1]) {
3994 memprintf(err, "'%s' : '%s' option without value",
3995 args[*cur_arg], args[pos]);
3996 goto error;
3997 }
3998 engine = args[pos+1];
3999 pos += 2;
4000 }
4001 else {
4002 memprintf(err, "unknown keyword '%s'", args[pos]);
4003 goto error;
4004 }
4005 }
4006 if (file == NULL) {
4007 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4008 goto error;
4009 }
4010
4011 /* backup sections and register SPOE sections */
4012 LIST_INIT(&backup_sections);
4013 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004014 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4015 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004016 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004017
4018 /* Parse SPOE filter configuration file */
4019 curengine = engine;
4020 curproxy = px;
4021 curagent = NULL;
4022 curmsg = NULL;
4023 ret = readcfgfile(file);
4024 curproxy = NULL;
4025
4026 /* unregister SPOE sections and restore previous sections */
4027 cfg_unregister_sections();
4028 cfg_restore_sections(&backup_sections);
4029
4030 if (ret == -1) {
4031 memprintf(err, "Could not open configuration file %s : %s",
4032 file, strerror(errno));
4033 goto error;
4034 }
4035 if (ret & (ERR_ABORT|ERR_FATAL)) {
4036 memprintf(err, "Error(s) found in configuration file %s", file);
4037 goto error;
4038 }
4039
4040 /* Check SPOE agent */
4041 if (curagent == NULL) {
4042 memprintf(err, "No SPOE agent found in file %s", file);
4043 goto error;
4044 }
4045 if (curagent->b.name == NULL) {
4046 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4047 curagent->id, curagent->conf.file, curagent->conf.line);
4048 goto error;
4049 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004050 if (curagent->timeout.hello == TICK_ETERNITY ||
4051 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004052 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004053 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4054 " | While not properly invalid, you will certainly encounter various problems\n"
4055 " | with such a configuration. To fix this, please ensure that all following\n"
4056 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4057 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004058 }
4059 if (curagent->var_pfx == NULL) {
4060 char *tmp = curagent->id;
4061
4062 while (*tmp) {
4063 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4064 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4065 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4066 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4067 goto error;
4068 }
4069 tmp++;
4070 }
4071 curagent->var_pfx = strdup(curagent->id);
4072 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004073 if (curagent->engine_id == NULL)
4074 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004075
Christopher Fauletb7426d12018-03-21 14:12:17 +01004076 if (curagent->var_on_error) {
4077 struct arg arg;
4078
4079 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4080 curagent->var_pfx, curagent->var_on_error);
4081
4082 arg.type = ARGT_STR;
4083 arg.data.str.str = trash.str;
4084 arg.data.str.len = trash.len;
4085 if (!vars_check_arg(&arg, err)) {
4086 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4087 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4088 goto error;
4089 }
4090 }
4091
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004092 if (curagent->var_t_process) {
4093 struct arg arg;
4094
4095 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4096 curagent->var_pfx, curagent->var_t_process);
4097
4098 arg.type = ARGT_STR;
4099 arg.data.str.str = trash.str;
4100 arg.data.str.len = trash.len;
4101 if (!vars_check_arg(&arg, err)) {
4102 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4103 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4104 goto error;
4105 }
4106 }
4107
4108 if (curagent->var_t_total) {
4109 struct arg arg;
4110
4111 trash.len = snprintf(trash.str, trash.size, "txn.%s.%s",
4112 curagent->var_pfx, curagent->var_t_total);
4113
4114 arg.type = ARGT_STR;
4115 arg.data.str.str = trash.str;
4116 arg.data.str.len = trash.len;
4117 if (!vars_check_arg(&arg, err)) {
4118 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4119 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4120 goto error;
4121 }
4122 }
4123
Christopher Faulet11610f32017-09-21 10:23:10 +02004124 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004125 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4126 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004127 goto finish;
4128 }
4129
Christopher Faulet11610f32017-09-21 10:23:10 +02004130 /* Replace placeholders by the corresponding messages for the SPOE
4131 * agent */
4132 list_for_each_entry(ph, &curmphs, list) {
4133 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004134 struct spoe_arg *arg;
4135 unsigned int where;
4136
Christopher Faulet11610f32017-09-21 10:23:10 +02004137 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004138 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4139 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4140 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4141 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4142 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4143 }
4144 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4145 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4146 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004147 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4148 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004149 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004150 }
4151 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004152 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4153 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004154 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004155 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004156
4157 where = 0;
4158 switch (msg->event) {
4159 case SPOE_EV_ON_CLIENT_SESS:
4160 where |= SMP_VAL_FE_CON_ACC;
4161 break;
4162
4163 case SPOE_EV_ON_TCP_REQ_FE:
4164 where |= SMP_VAL_FE_REQ_CNT;
4165 break;
4166
4167 case SPOE_EV_ON_HTTP_REQ_FE:
4168 where |= SMP_VAL_FE_HRQ_HDR;
4169 break;
4170
4171 case SPOE_EV_ON_TCP_REQ_BE:
4172 if (px->cap & PR_CAP_FE)
4173 where |= SMP_VAL_FE_REQ_CNT;
4174 if (px->cap & PR_CAP_BE)
4175 where |= SMP_VAL_BE_REQ_CNT;
4176 break;
4177
4178 case SPOE_EV_ON_HTTP_REQ_BE:
4179 if (px->cap & PR_CAP_FE)
4180 where |= SMP_VAL_FE_HRQ_HDR;
4181 if (px->cap & PR_CAP_BE)
4182 where |= SMP_VAL_BE_HRQ_HDR;
4183 break;
4184
4185 case SPOE_EV_ON_SERVER_SESS:
4186 where |= SMP_VAL_BE_SRV_CON;
4187 break;
4188
4189 case SPOE_EV_ON_TCP_RSP:
4190 if (px->cap & PR_CAP_FE)
4191 where |= SMP_VAL_FE_RES_CNT;
4192 if (px->cap & PR_CAP_BE)
4193 where |= SMP_VAL_BE_RES_CNT;
4194 break;
4195
4196 case SPOE_EV_ON_HTTP_RSP:
4197 if (px->cap & PR_CAP_FE)
4198 where |= SMP_VAL_FE_HRS_HDR;
4199 if (px->cap & PR_CAP_BE)
4200 where |= SMP_VAL_BE_HRS_HDR;
4201 break;
4202
4203 default:
4204 break;
4205 }
4206
4207 list_for_each_entry(arg, &msg->args, list) {
4208 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004209 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004210 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004211 "none of which is available here ('%s')",
4212 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004213 sample_ckp_names(arg->expr->fetch->use),
4214 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004215 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004216 }
4217 }
4218
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004219 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004220 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4221 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004222 }
4223 }
4224 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004225 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004226 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004227 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004228 continue;
4229 }
4230
Christopher Faulet11610f32017-09-21 10:23:10 +02004231 /* Replace placeholders by the corresponding groups for the SPOE
4232 * agent */
4233 list_for_each_entry(ph, &curgphs, list) {
4234 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4235 if (!strcmp(grp->id, ph->id)) {
4236 grp->agent = curagent;
4237 LIST_DEL(&grp->list);
4238 LIST_ADDQ(&curagent->groups, &grp->list);
4239 goto next_aph;
4240 }
4241 }
4242 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4243 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4244 goto error;
4245 next_aph:
4246 continue;
4247 }
4248
4249 /* Replace placeholders by the corresponding message for each SPOE
4250 * group of the SPOE agent */
4251 list_for_each_entry(grp, &curagent->groups, list) {
4252 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4253 list_for_each_entry(msg, &curmsgs, list) {
4254 if (!strcmp(msg->id, ph->id)) {
4255 if (msg->group != NULL) {
4256 memprintf(err, "SPOE message '%s' already belongs to "
4257 "the SPOE group '%s' declare at %s:%d",
4258 msg->id, msg->group->id,
4259 msg->group->conf.file,
4260 msg->group->conf.line);
4261 goto error;
4262 }
4263
4264 /* Scope for arguments are not checked for now. We will check
4265 * them only if a rule use the corresponding SPOE group. */
4266 msg->agent = curagent;
4267 msg->group = grp;
4268 LIST_DEL(&ph->list);
4269 LIST_ADDQ(&grp->messages, &msg->by_grp);
4270 goto next_mph_grp;
4271 }
4272 }
4273 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4274 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4275 goto error;
4276 next_mph_grp:
4277 continue;
4278 }
4279 }
4280
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004281 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004282 /* move curmsgs to the agent message list */
4283 curmsgs.n->p = &curagent->messages;
4284 curmsgs.p->n = &curagent->messages;
4285 curagent->messages = curmsgs;
4286 LIST_INIT(&curmsgs);
4287
Christopher Faulet7ee86672017-09-19 11:08:28 +02004288 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004289 conf->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004290 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4291 LIST_DEL(&ph->list);
4292 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004293 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004294 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4295 LIST_DEL(&ph->list);
4296 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004297 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004298 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4299 struct arg arg;
4300
4301 trash.len = snprintf(trash.str, trash.size, "proc.%s.%s",
4302 curagent->var_pfx, vph->name);
4303
4304 arg.type = ARGT_STR;
4305 arg.data.str.str = trash.str;
4306 arg.data.str.len = trash.len;
4307 if (!vars_check_arg(&arg, err)) {
4308 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4309 curagent->id, curagent->var_pfx, vph->name, *err);
4310 goto error;
4311 }
4312
4313 LIST_DEL(&vph->list);
4314 free(vph->name);
4315 free(vph);
4316 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004317 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4318 LIST_DEL(&grp->list);
4319 spoe_release_group(grp);
4320 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004321 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004322 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004323 fconf->ops = &spoe_ops;
4324 fconf->conf = conf;
4325 return 0;
4326
4327 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004328 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004329 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4330 LIST_DEL(&ph->list);
4331 spoe_release_placeholder(ph);
4332 }
4333 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4334 LIST_DEL(&ph->list);
4335 spoe_release_placeholder(ph);
4336 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004337 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4338 LIST_DEL(&vph->list);
4339 free(vph->name);
4340 free(vph);
4341 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004342 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4343 LIST_DEL(&grp->list);
4344 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004345 }
4346 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4347 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004348 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004349 }
4350 free(conf);
4351 return -1;
4352}
4353
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004354/* Send message of a SPOE group. This is the action_ptr callback of a rule
4355 * associated to a "send-spoe-group" action.
4356 *
4357 * It returns ACT_RET_CONT is processing is finished without error, it returns
4358 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4359 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004360static enum act_return
4361spoe_send_group(struct act_rule *rule, struct proxy *px,
4362 struct session *sess, struct stream *s, int flags)
4363{
4364 struct filter *filter;
4365 struct spoe_agent *agent = NULL;
4366 struct spoe_group *group = NULL;
4367 struct spoe_context *ctx = NULL;
4368 int ret, dir;
4369
4370 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4371 if (filter->config == rule->arg.act.p[0]) {
4372 agent = rule->arg.act.p[2];
4373 group = rule->arg.act.p[3];
4374 ctx = filter->ctx;
4375 break;
4376 }
4377 }
4378 if (agent == NULL || group == NULL || ctx == NULL)
4379 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004380 if (ctx->state == SPOE_CTX_ST_NONE)
4381 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004382
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004383 switch (rule->from) {
4384 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4385 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4386 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4387 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4388 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4389 default:
4390 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4391 " - internal error while execute spoe-send-group\n",
4392 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4393 __FUNCTION__, s);
4394 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4395 agent->id);
4396 return ACT_RET_CONT;
4397 }
4398
4399 ret = spoe_process_group(s, ctx, group, dir);
4400 if (ret == 1)
4401 return ACT_RET_CONT;
4402 else if (ret == 0) {
4403 if (flags & ACT_FLAG_FINAL) {
4404 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4405 " - failed to process group '%s': interrupted by caller\n",
4406 (int)now.tv_sec, (int)now.tv_usec,
4407 agent->id, __FUNCTION__, s, group->id);
4408 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
4409 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004410 spoe_stop_processing(agent, ctx);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004411 return ACT_RET_CONT;
4412 }
4413 return ACT_RET_YIELD;
4414 }
4415 else
4416 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004417}
4418
4419/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4420 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4421 * action should be:
4422 *
4423 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4424 *
4425 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4426 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4427 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4428 * group.
4429 *
4430 * The function returns 1 in success case, otherwise, it returns 0 and err is
4431 * filled.
4432 */
4433static int
4434check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4435{
4436 struct flt_conf *fconf;
4437 struct spoe_config *conf;
4438 struct spoe_agent *agent = NULL;
4439 struct spoe_group *group;
4440 struct spoe_message *msg;
4441 char *engine_id = rule->arg.act.p[0];
4442 char *group_id = rule->arg.act.p[1];
4443 unsigned int where = 0;
4444
4445 switch (rule->from) {
4446 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4447 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4448 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4449 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4450 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4451 default:
4452 memprintf(err,
4453 "internal error, unexpected rule->from=%d, please report this bug!",
4454 rule->from);
4455 goto error;
4456 }
4457
4458 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4459 * <px> */
4460 list_for_each_entry(fconf, &px->filter_configs, list) {
4461 conf = fconf->conf;
4462
4463 /* This is not an SPOE filter */
4464 if (fconf->id != spoe_filter_id)
4465 continue;
4466
4467 /* This is the good engine */
4468 if (!strcmp(conf->id, engine_id)) {
4469 agent = conf->agent;
4470 break;
4471 }
4472 }
4473 if (agent == NULL) {
4474 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4475 engine_id, group_id);
4476 goto error;
4477 }
4478
4479 /* Try to find the right group */
4480 list_for_each_entry(group, &agent->groups, list) {
4481 /* This is the good group */
4482 if (!strcmp(group->id, group_id))
4483 break;
4484 }
4485 if (&group->list == &agent->groups) {
4486 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4487 group_id, engine_id);
4488 goto error;
4489 }
4490
4491 /* Ok, we found the group, we need to check messages and their
4492 * arguments */
4493 list_for_each_entry(msg, &group->messages, by_grp) {
4494 struct spoe_arg *arg;
4495
4496 list_for_each_entry(arg, &msg->args, list) {
4497 if (!(arg->expr->fetch->val & where)) {
4498 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4499 "some args extract information from '%s',"
4500 "none of which is available here ('%s')",
4501 msg->id, group->id, msg->conf.file, msg->conf.line,
4502 sample_ckp_names(arg->expr->fetch->use),
4503 sample_ckp_names(where));
4504 goto error;
4505 }
4506 }
4507 }
4508
4509 free(engine_id);
4510 free(group_id);
4511 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4512 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4513 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4514 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4515 return 1;
4516
4517 error:
4518 free(engine_id);
4519 free(group_id);
4520 return 0;
4521}
4522
4523/* Parse 'send-spoe-group' action following the format:
4524 *
4525 * ... send-spoe-group <engine-id> <group-id>
4526 *
4527 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4528 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4529 * ids are saved and used later, when the rule will be checked.
4530 */
4531static enum act_parse_ret
4532parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4533 struct act_rule *rule, char **err)
4534{
4535 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4536 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4537 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4538 return ACT_RET_PRS_ERR;
4539 }
4540 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4541 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4542
4543 (*orig_arg) += 2;
4544
4545 rule->action = ACT_CUSTOM;
4546 rule->action_ptr = spoe_send_group;
4547 rule->check_ptr = check_send_spoe_group;
4548 return ACT_RET_PRS_OK;
4549}
4550
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004551
4552/* Declare the filter parser for "spoe" keyword */
4553static struct flt_kw_list flt_kws = { "SPOE", { }, {
4554 { "spoe", parse_spoe_flt, NULL },
4555 { NULL, NULL, NULL },
4556 }
4557};
4558
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004559/* Delcate the action parser for "spoe-action" keyword */
4560static struct action_kw_list tcp_req_action_kws = { { }, {
4561 { "send-spoe-group", parse_send_spoe_group },
4562 { /* END */ },
4563 }
4564};
4565static struct action_kw_list tcp_res_action_kws = { { }, {
4566 { "send-spoe-group", parse_send_spoe_group },
4567 { /* END */ },
4568 }
4569};
4570static struct action_kw_list http_req_action_kws = { { }, {
4571 { "send-spoe-group", parse_send_spoe_group },
4572 { /* END */ },
4573 }
4574};
4575static struct action_kw_list http_res_action_kws = { { }, {
4576 { "send-spoe-group", parse_send_spoe_group },
4577 { /* END */ },
4578 }
4579};
4580
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004581__attribute__((constructor))
4582static void __spoe_init(void)
4583{
4584 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004585 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4586 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4587 http_req_keywords_register(&http_req_action_kws);
4588 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004589
Willy Tarreaubafbe012017-11-24 17:34:44 +01004590 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4591 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004592}
4593
4594__attribute__((destructor))
4595static void
4596__spoe_deinit(void)
4597{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004598 pool_destroy(pool_head_spoe_ctx);
4599 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004600}