blob: bd351be18059935e3a8f240fdcc9a7f7b47d9091 [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>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/hathreads.h>
20#include <common/initcall.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020021#include <common/memory.h>
22#include <common/time.h>
23
24#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020025#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010026#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020027
Christopher Faulet57583e42017-09-04 15:41:09 +020028#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020029#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020030#include <proto/arg.h>
31#include <proto/backend.h>
32#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010033#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020034#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020035#include <proto/http_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020036#include <proto/log.h>
37#include <proto/proto_http.h>
38#include <proto/proxy.h>
39#include <proto/sample.h>
40#include <proto/session.h>
41#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010042#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020043#include <proto/stream.h>
44#include <proto/stream_interface.h>
45#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020046#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020047#include <proto/vars.h>
48
49#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
50#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010051#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020052#else
53#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010054#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020055#endif
56
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010057/* Reserved 4 bytes to the frame size. So a frame and its size can be written
58 * together in a buffer */
59#define MAX_FRAME_SIZE global.tune.bufsize - 4
60
61/* The minimum size for a frame */
62#define MIN_FRAME_SIZE 256
63
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Reserved for the metadata and the frame type.
65 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010066#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020067
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010068/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010069#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
70
Christopher Faulet3b386a32017-02-23 10:17:15 +010071/* SPOE filter id. Used to identify SPOE filters */
72const char *spoe_filter_id = "SPOE filter";
73
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020074/* Set if the handle on SIGUSR1 is registered */
75static int sighandler_registered = 0;
76
77/* proxy used during the parsing */
78struct proxy *curproxy = NULL;
79
80/* The name of the SPOE engine, used during the parsing */
81char *curengine = NULL;
82
83/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020084/* SPOE agent/group/message used during the parsing */
85struct spoe_agent *curagent = NULL;
86struct spoe_group *curgrp = NULL;
87struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
89/* list of SPOE messages and placeholders used during the parsing */
90struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020091struct list curgrps;
92struct list curmphs;
93struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010094struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
Christopher Faulet7250b8f2018-03-26 17:19:01 +020096/* list of log servers used during the parsing */
97struct list curlogsrvs;
98
Christopher Faulet0e0f0852018-03-26 17:20:36 +020099/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
100int curpxopts;
101int curpxopts2;
102
Christopher Faulet42bfa462017-01-04 14:14:19 +0100103/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100104DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
105DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107struct flt_ops spoe_ops;
108
Christopher Faulet8ef75252017-02-20 22:56:03 +0100109static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200110static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
111static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112
113/********************************************************************
114 * helper functions/globals
115 ********************************************************************/
116static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200117spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118{
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200121 free(ph->id);
122 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123}
124
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100126spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127{
Christopher Faulet57583e42017-09-04 15:41:09 +0200128 struct spoe_arg *arg, *argback;
129 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200130
131 if (!msg)
132 return;
133 free(msg->id);
134 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200135 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200136 release_sample_expr(arg->expr);
137 free(arg->name);
138 LIST_DEL(&arg->list);
139 free(arg);
140 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200141 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
142 LIST_DEL(&acl->list);
143 prune_acl(acl);
144 free(acl);
145 }
146 if (msg->cond) {
147 prune_acl_cond(msg->cond);
148 free(msg->cond);
149 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200150 free(msg);
151}
152
153static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200154spoe_release_group(struct spoe_group *grp)
155{
156 if (!grp)
157 return;
158 free(grp->id);
159 free(grp->conf.file);
160 free(grp);
161}
162
163static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100164spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200165{
Christopher Faulet11610f32017-09-21 10:23:10 +0200166 struct spoe_message *msg, *msgback;
167 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200168 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200169
170 if (!agent)
171 return;
172 free(agent->id);
173 free(agent->conf.file);
174 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100175 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100176 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100177 free(agent->var_t_process);
178 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200179 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
180 LIST_DEL(&msg->list);
181 spoe_release_message(msg);
182 }
183 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
184 LIST_DEL(&grp->list);
185 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200186 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100188 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200189 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200190 free(agent);
191}
192
193static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100194 [SPOE_FRM_ERR_NONE] = "normal",
195 [SPOE_FRM_ERR_IO] = "I/O error",
196 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
197 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
198 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
199 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
200 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
201 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
202 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
203 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
204 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
205 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100206 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100207 [SPOE_FRM_ERR_RES] = "resource allocation error",
208 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200209};
210
211static const char *spoe_event_str[SPOE_EV_EVENTS] = {
212 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
213 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
214 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
215 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
216 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
217
218 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
219 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
220 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
221};
222
223
224#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
225
226static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100227 [SPOE_CTX_ST_NONE] = "NONE",
228 [SPOE_CTX_ST_READY] = "READY",
229 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
230 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
231 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
232 [SPOE_CTX_ST_DONE] = "DONE",
233 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200234};
235
236static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100237 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
238 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
239 [SPOE_APPCTX_ST_IDLE] = "IDLE",
240 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
241 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
242 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
243 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
244 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
245 [SPOE_APPCTX_ST_EXIT] = "EXIT",
246 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200247};
248
249#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100250
Christopher Faulet8ef75252017-02-20 22:56:03 +0100251/* Used to generates a unique id for an engine. On success, it returns a
252 * allocated string. So it is the caller's reponsibility to release it. If the
253 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100254static char *
255generate_pseudo_uuid()
256{
257 static int init = 0;
258
259 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
260 const char uuid_chr[] = "0123456789ABCDEF-";
261 char *uuid;
262 int i;
263
264 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
265 return NULL;
266
267 if (!init) {
268 srand(now_ms);
269 init = 1;
270 }
271
272 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
273 int r = rand () % 16;
274
275 switch (uuid_fmt[i]) {
276 case 'x' : uuid[i] = uuid_chr[r]; break;
277 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
278 default : uuid[i] = uuid_fmt[i]; break;
279 }
280 }
281 return uuid;
282}
283
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100284
285static inline void
286spoe_update_stat_time(struct timeval *tv, long *t)
287{
288 if (*t == -1)
289 *t = tv_ms_elapsed(tv, &now);
290 else
291 *t += tv_ms_elapsed(tv, &now);
292 tv_zero(tv);
293}
294
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200295/********************************************************************
296 * Functions that encode/decode SPOE frames
297 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200298/* Helper to get static string length, excluding the terminating null byte */
299#define SLEN(str) (sizeof(str)-1)
300
301/* Predefined key used in HELLO/DISCONNECT frames */
302#define SUPPORTED_VERSIONS_KEY "supported-versions"
303#define VERSION_KEY "version"
304#define MAX_FRAME_SIZE_KEY "max-frame-size"
305#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100306#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100307#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308#define STATUS_CODE_KEY "status-code"
309#define MSG_KEY "message"
310
311struct spoe_version {
312 char *str;
313 int min;
314 int max;
315};
316
317/* All supported versions */
318static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200319 /* 1.0 is now unsupported because of a bug about frame's flags*/
320 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200321 {NULL, 0, 0}
322};
323
324/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200325#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200326
Christopher Faulet8ef75252017-02-20 22:56:03 +0100327/* Convert a string to a SPOE version value. The string must follow the format
328 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
329 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200330static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100331spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 const char *p, *end;
334 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200335
Christopher Faulet8ef75252017-02-20 22:56:03 +0100336 p = str;
337 end = str+len;
338 maj = min = 0;
339 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200340
Christopher Faulet8ef75252017-02-20 22:56:03 +0100341 /* skip leading spaces */
342 while (p < end && isspace(*p))
343 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344
Christopher Faulet8ef75252017-02-20 22:56:03 +0100345 /* parse Major number, until the '.' */
346 while (*p != '.') {
347 if (p >= end || *p < '0' || *p > '9')
348 goto out;
349 maj *= 10;
350 maj += (*p - '0');
351 p++;
352 }
353
354 /* check Major version */
355 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200356 goto out;
357
Christopher Faulet8ef75252017-02-20 22:56:03 +0100358 p++; /* skip the '.' */
359 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
360 goto out;
361
362 /* Parse Minor number */
363 while (p < end) {
364 if (*p < '0' || *p > '9')
365 break;
366 min *= 10;
367 min += (*p - '0');
368 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200369 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370
371 /* check Minor number */
372 if (min > 999)
373 goto out;
374
375 /* skip trailing spaces */
376 while (p < end && isspace(*p))
377 p++;
378 if (p != end)
379 goto out;
380
381 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200382 out:
383 return vsn;
384}
385
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800387 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200389static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100390spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200391{
Willy Tarreau83061a82018-07-13 11:56:34 +0200392 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100393 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 char *p, *end;
395 unsigned int flags = SPOE_FRM_FL_FIN;
396 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
Christopher Faulet8ef75252017-02-20 22:56:03 +0100398 p = frame;
399 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
Christopher Faulet8ef75252017-02-20 22:56:03 +0100401 /* Set Frame type */
402 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200403
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100404 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200405 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 memcpy(p, (char *)&flags, 4);
407 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
409 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
412 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
413 * and "capabilities" */
414
415 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100416 sz = SLEN(SUPPORTED_VERSIONS_KEY);
417 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
418 goto too_big;
419
420 *p++ = SPOE_DATA_T_STR;
421 sz = SLEN(SUPPORTED_VERSIONS_VAL);
422 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
423 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200424
425 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100426 sz = SLEN(MAX_FRAME_SIZE_KEY);
427 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
428 goto too_big;
429
430 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200431 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200433
434 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100435 sz = SLEN(CAPABILITIES_KEY);
436 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
437 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200438
Christopher Faulet8ef75252017-02-20 22:56:03 +0100439 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100440 chk = get_trash_chunk();
441 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200442 memcpy(chk->area, "pipelining", 10);
443 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100444 }
445 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200446 if (chk->data) chk->area[chk->data++] = ',';
447 memcpy(chk->area+chk->data, "async", 5);
448 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100449 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100450 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200451 if (chk->data) chk->area[chk->data++] = ',';
452 memcpy(chk->area+chk->data, "fragmentation", 13);
453 chk->data += 5;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100454 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200455 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456 goto too_big;
457
458 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100459 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100460 sz = SLEN(ENGINE_ID_KEY);
461 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
462 goto too_big;
463
464 *p++ = SPOE_DATA_T_STR;
465 sz = strlen(agent->engine_id);
466 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
467 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100468 }
469
Christopher Faulet8ef75252017-02-20 22:56:03 +0100470 return (p - frame);
471
472 too_big:
473 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
474 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200475}
476
Christopher Faulet8ef75252017-02-20 22:56:03 +0100477/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
478 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
479 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100481spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 const char *reason;
484 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100485 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200487
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 p = frame;
489 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 /* Set Frame type */
492 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100494 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200495 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100496 memcpy(p, (char *)&flags, 4);
497 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200498
499 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100500 *p++ = 0; *p++ = 0;
501
502 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
503 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200504
505 /* There are 2 mandatory items: "status-code" and "message" */
506
507 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100508 sz = SLEN(STATUS_CODE_KEY);
509 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
510 goto too_big;
511
512 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200513 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515
516 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100517 sz = SLEN(MSG_KEY);
518 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
519 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 /*Get the message corresponding to the status code */
522 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
523
524 *p++ = SPOE_DATA_T_STR;
525 sz = strlen(reason);
526 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
527 goto too_big;
528
529 return (p - frame);
530
531 too_big:
532 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
533 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200534}
535
Christopher Faulet8ef75252017-02-20 22:56:03 +0100536/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
537 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
538 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200539static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100540spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100541 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200542{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100543 char *p, *end;
544 unsigned int stream_id, frame_id;
545 unsigned int flags = SPOE_FRM_FL_FIN;
546 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200547
Christopher Faulet8ef75252017-02-20 22:56:03 +0100548 p = frame;
549 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200550
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100551 stream_id = ctx->stream_id;
552 frame_id = ctx->frame_id;
553
554 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
555 /* The fragmentation is not supported by the applet */
556 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
557 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
558 return -1;
559 }
560 flags = ctx->frag_ctx.flags;
561 }
562
563 /* Set Frame type */
564 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
565
566 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200567 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100568 memcpy(p, (char *)&flags, 4);
569 p += 4;
570
571 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200572 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100573 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200574 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100575 goto too_big;
576
577 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200578 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100579 if (p + sz >= end)
580 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200581 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100582 p += sz;
583
584 return (p - frame);
585
586 too_big:
587 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
588 return 0;
589}
590
591/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
592 * returns the number of encoded bytes in the frame on success, 0 if an encoding
593 * error occurred and -1 if a fatal error occurred. */
594static int
595spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
596 char *frame, size_t size)
597{
598 char *p, *end;
599 unsigned int stream_id, frame_id;
600 unsigned int flags;
601 size_t sz;
602
603 p = frame;
604 end = frame+size;
605
Christopher Faulet8ef75252017-02-20 22:56:03 +0100606 /* <ctx> is null when the stream has aborted the processing of a
607 * fragmented frame. In this case, we must notify the corresponding
608 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100609 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100610 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100611 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
612 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
613 }
614 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100615 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100616 stream_id = ctx->stream_id;
617 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100618 }
619
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100621 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100623 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200624 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625 memcpy(p, (char *)&flags, 4);
626 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200627
628 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200629 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200631 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100634 if (ctx == NULL)
635 goto end;
636
Christopher Faulet8ef75252017-02-20 22:56:03 +0100637 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200638 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639 if (p + sz >= end)
640 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200641 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100643
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100644 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 return (p - frame);
646
647 too_big:
648 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
649 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200650}
651
Christopher Faulet8ef75252017-02-20 22:56:03 +0100652/* Decode and process the HELLO frame sent by an agent. It returns the number of
653 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
654 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200657{
Christopher Faulet305c6072017-02-23 16:17:53 +0100658 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
659 char *p, *end;
660 int vsn, max_frame_size;
661 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662
663 p = frame;
664 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200665
666 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
668 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100670 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 if (size < 7 /* TYPE + METADATA */) {
673 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
674 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200675 }
676
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100677 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200679 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100680 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200681
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100682 /* Fragmentation is not supported for HELLO frame */
683 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100685 return -1;
686 }
687
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 if (*p != 0 || *(p+1) != 0) {
690 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
691 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694
695 /* There are 3 mandatory items: "version", "max-frame-size" and
696 * "capabilities" */
697
698 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100699 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 while (p < end) {
701 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200702 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704
705 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 ret = spoe_decode_buffer(&p, end, &str, &sz);
707 if (ret == -1 || !sz) {
708 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
709 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 /* Check "version" K/V item */
713 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100714 int i, type = *p++;
715
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100717 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
718 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
719 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200720 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100721 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
722 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
723 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 }
725
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 return -1;
730 }
731 for (i = 0; supported_versions[i].str != NULL; ++i) {
732 if (vsn >= supported_versions[i].min &&
733 vsn <= supported_versions[i].max)
734 break;
735 }
736 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100737 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200738 return -1;
739 }
740 }
741 /* Check "max-frame-size" K/V item */
742 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744
745 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200746 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
747 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
748 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
749 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100750 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
751 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200752 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200753 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
755 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200756 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100757 if (sz < MIN_FRAME_SIZE ||
758 sz > SPOE_APPCTX(appctx)->max_frame_size) {
759 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200760 return -1;
761 }
762 max_frame_size = sz;
763 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764 /* Check "capabilities" K/V item */
765 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100767
768 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100769 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
770 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
771 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100773 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
774 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
775 return 0;
776 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779 char *delim;
780
781 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100782 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100783
Christopher Faulet8ef75252017-02-20 22:56:03 +0100784 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
785 str += 10; sz -= 10;
786 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100787 flags |= SPOE_APPCTX_FL_PIPELINING;
788 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100789 else if (sz >= 5 && !strncmp(str, "async", 5)) {
790 str += 5; sz -= 5;
791 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100792 flags |= SPOE_APPCTX_FL_ASYNC;
793 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100794 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
795 str += 13; sz -= 13;
796 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100797 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
798 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100799
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 /* Get the next comma or break */
801 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100802 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100803 delim++;
804 sz -= (delim - str);
805 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100806 }
807 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200808 else {
809 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100810 if (spoe_skip_data(&p, end) == -1) {
811 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
812 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200814 }
815 }
816
817 /* Final checks */
818 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100819 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820 return -1;
821 }
822 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200824 return -1;
825 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100826 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
827 flags &= ~SPOE_APPCTX_FL_PIPELINING;
828 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
829 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830
Christopher Faulet42bfa462017-01-04 14:14:19 +0100831 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
832 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
833 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834
835 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836}
837
838/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
839 * bytes on success, 0 if the frame can be ignored and -1 if an error
840 * occurred. */
841static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200843{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100845 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846
847 p = frame;
848 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849
850 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
852 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 if (size < 7 /* TYPE + METADATA */) {
857 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
858 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859 }
860
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100861 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200863 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100866 /* Fragmentation is not supported for DISCONNECT frame */
867 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100869 return -1;
870 }
871
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200872 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 if (*p != 0 || *(p+1) != 0) {
874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
875 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100877 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878
879 /* There are 2 mandatory items: "status-code" and "message" */
880
881 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 while (p < end) {
883 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200884 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886
887 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100888 ret = spoe_decode_buffer(&p, end, &str, &sz);
889 if (ret == -1 || !sz) {
890 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
891 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892 }
893
894 /* Check "status-code" K/V item */
895 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100896 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897
898 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
900 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
901 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
902 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100903 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
904 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200906 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
908 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200909 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100910 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200911 }
912
913 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100914 else if (!memcmp(str, MSG_KEY, sz)) {
915 int type = *p++;
916
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
919 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
920 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100922 ret = spoe_decode_buffer(&p, end, &str, &sz);
923 if (ret == -1 || sz > 255) {
924 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
925 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100927#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
928 SPOE_APPCTX(appctx)->reason = str;
929 SPOE_APPCTX(appctx)->rlen = sz;
930#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200931 }
932 else {
933 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 if (spoe_skip_data(&p, end) == -1) {
935 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
936 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200938 }
939 }
940
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942}
943
944
Christopher Fauleta1cda022016-12-21 08:58:06 +0100945/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 * success, 0 if the frame can be ignored and -1 if an error occurred. */
947static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100948spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100949 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200950{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100951 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100953 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100955 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956
957 p = frame;
958 end = frame + size;
959 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200960
961 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
963 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200964 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 if (size < 7 /* TYPE + METADATA */) {
968 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
969 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970 }
971
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100972 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200974 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200976
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100977 /* Fragmentation is not supported for now */
978 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100979 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100980 return -1;
981 }
982
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200983 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200984 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100986 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100987 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200988 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100989 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200990 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100991 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100992
Christopher Faulet8ef75252017-02-20 22:56:03 +0100993 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100994 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200995 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100996 if ((*ctx)->stream_id == (unsigned int)stream_id &&
997 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100998 goto found;
999 }
1000 }
1001 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001002 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1003 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001004 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001005 goto found;
1006 }
1007 }
1008
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001009 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1010 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1011 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1012
1013 /* ABRT bit is set for an unfinished fragmented frame */
1014 if (flags & SPOE_FRM_FL_ABRT) {
1015 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001016 (*ctx)->state = SPOE_CTX_ST_ERROR;
1017 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1018 /* Ignore the payload */
1019 goto end;
1020 }
1021 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1022 /* For now, we ignore the ack */
1023 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1024 return 0;
1025 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001026
Christopher Fauleta1cda022016-12-21 08:58:06 +01001027 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001028 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1029 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001030 " - stream-id=%u - frame-id=%u\n",
1031 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1032 __FUNCTION__, appctx,
1033 (unsigned int)stream_id, (unsigned int)frame_id);
1034
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001035 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001036 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1037 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001038 return 0;
1039
1040 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001041 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1042 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001043 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001045 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001046
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001047 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001048 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001049 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1050 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001051 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001052
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001053 /* Transfer the buffer ownership to the SPOE context */
1054 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001055 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001056
Christopher Faulet8ef75252017-02-20 22:56:03 +01001057 (*ctx)->state = SPOE_CTX_ST_DONE;
1058
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001059 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001060 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061 " - ACK frame received"
1062 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001063 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1065 (*ctx)->frame_id, flags);
1066 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001067}
1068
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069/* This function is used in cfgparse.c and declared in proto/checks.h. It
1070 * prepare the request to send to agents during a healthcheck. It returns 0 on
1071 * success and -1 if an error occurred. */
1072int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001073spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075 struct appctx appctx;
1076 struct spoe_appctx spoe_appctx;
1077 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1078 size_t sz;
1079 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080
Christopher Faulet42bfa462017-01-04 14:14:19 +01001081 memset(&appctx, 0, sizeof(appctx));
1082 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001084
1085 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001086 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087
Christopher Faulet8ef75252017-02-20 22:56:03 +01001088 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1089 end = frame + MAX_FRAME_SIZE;
1090
1091 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1092 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096 /* Add "healthcheck" K/V item */
1097 sz = SLEN(HEALTHCHECK_KEY);
1098 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1099 return -1;
1100 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001101
Christopher Faulet8ef75252017-02-20 22:56:03 +01001102 *len = frame - buf;
1103 sz = htonl(*len - 4);
1104 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105
Christopher Faulet8ef75252017-02-20 22:56:03 +01001106 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001108 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109 return 0;
1110}
1111
1112/* This function is used in checks.c and declared in proto/checks.h. It decode
1113 * the response received from an agent during a healthcheck. It returns 0 on
1114 * success and -1 if an error occurred. */
1115int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001116spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001118 struct appctx appctx;
1119 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120
Christopher Faulet42bfa462017-01-04 14:14:19 +01001121 memset(&appctx, 0, sizeof(appctx));
1122 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001123
Christopher Faulet42bfa462017-01-04 14:14:19 +01001124 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001125 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001126
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1128 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001129 goto error;
1130 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001131 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1132 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001133
1134 return 0;
1135
1136 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001137 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1138 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1139 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001140 return -1;
1141}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001142
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1144 * the frame can be ignored, 1 to retry later, and the frame legnth on
1145 * success. */
1146static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001147spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001148{
1149 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 int ret;
1151 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152
Christopher Faulet8ef75252017-02-20 22:56:03 +01001153 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1154 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155 netint = htonl(framesz);
1156 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001157 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001159 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001160 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001162 }
1163 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164 return -1; /* error */
1165 }
1166 return framesz;
1167}
1168
1169/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1170 * when the frame can be ignored, 1 to retry later and the frame length on
1171 * success. */
1172static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001173spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174{
1175 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001176 int ret;
1177 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178
Willy Tarreau06d80a92017-10-19 14:32:15 +02001179 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180 if (ret > 0) {
1181 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001182 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001183 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001184 return -1;
1185 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001186 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001187 }
1188 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001189 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001190 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001191 }
1192 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001193 return -1; /* error */
1194 }
1195 return framesz;
1196}
1197
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001198/********************************************************************
1199 * Functions that manage the SPOE applet
1200 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001201static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001202spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001203{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001204 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001205 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001206 appctx_wakeup(appctx);
1207 return 1;
1208}
1209
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001210/* Callback function that catches applet timeouts. If a timeout occurred, we set
1211 * <appctx->st1> flag and the SPOE applet is woken up. */
1212static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001213spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001215 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216
1217 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1218 if (tick_is_expired(task->expire, now_ms)) {
1219 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001220 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001222 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001223 return task;
1224}
1225
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001226/* Callback function that releases a SPOE applet. This happens when the
1227 * connection with the agent is closed. */
1228static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001229spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001230{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001231 struct stream_interface *si = appctx->owner;
1232 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1233 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001234 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235
1236 if (spoe_appctx == NULL)
1237 return;
1238
1239 appctx->ctx.spoe.ptr = NULL;
1240 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001241
1242 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1243 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1244 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001245
Christopher Faulet8ef75252017-02-20 22:56:03 +01001246 /* Remove applet from the list of running applets */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001247 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001248 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001249 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1250 LIST_DEL(&spoe_appctx->list);
1251 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001252 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001253 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001254
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001256 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001257 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1258 eb32_delete(&spoe_appctx->node);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001259 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001260 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001261
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001262 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001263 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1264 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001265
1266 si_shutw(si);
1267 si_shutr(si);
1268 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001269 }
1270
Christopher Faulet8ef75252017-02-20 22:56:03 +01001271 /* Destroy the task attached to this applet */
1272 if (spoe_appctx->task) {
1273 task_delete(spoe_appctx->task);
1274 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001275 }
1276
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 /* Notify all waiting streams */
1278 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001279 LIST_DEL(&ctx->list);
1280 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001281 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001282 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001283 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001284 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001285 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001286 }
1287
Christopher Faulet8ef75252017-02-20 22:56:03 +01001288 /* If the applet was processing a fragmented frame, notify the
1289 * corresponding stream. */
1290 if (spoe_appctx->frag_ctx.ctx) {
1291 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001292 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001293 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001294 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001295 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1296 }
1297
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298 /* Release allocated memory */
1299 spoe_release_buffer(&spoe_appctx->buffer,
1300 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001301 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001302
Christopher Faulet24289f22017-09-25 14:48:02 +02001303 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001304 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001305
Christopher Faulet8ef75252017-02-20 22:56:03 +01001306 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001307 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001308 LIST_DEL(&ctx->list);
1309 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001310 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001311 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001313 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001315 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001316 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 LIST_DEL(&ctx->list);
1318 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001319 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001320 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001322 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001323 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1324 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001325
1326 end:
1327 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001328 agent->rt[tid].frame_size = agent->max_frame_size;
1329 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1330 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001331}
1332
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001333static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001334spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001335{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001336 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001337 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001338 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001339 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001340
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 if (si->state <= SI_ST_CON) {
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001342 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1344 goto stop;
1345 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001346 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001347 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001348 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001349 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001350
Christopher Fauleta1cda022016-12-21 08:58:06 +01001351 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001352 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1353 " - Connection timed out\n",
1354 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1355 __FUNCTION__, appctx);
1356 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001357 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001358 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001359
Christopher Faulet42bfa462017-01-04 14:14:19 +01001360 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 SPOE_APPCTX(appctx)->task->expire =
1362 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1365 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001366 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001367 ret = spoe_prepare_hahello_frame(appctx, frame,
1368 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001371
1372 switch (ret) {
1373 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001374 case 0: /* ignore => an error, cannot be ignored */
1375 goto exit;
1376
1377 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 goto stop;
1379
Christopher Faulet8ef75252017-02-20 22:56:03 +01001380 default:
1381 /* HELLO frame successfully sent, now wait for the
1382 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1384 goto next;
1385 }
1386
1387 next:
1388 return 0;
1389 stop:
1390 return 1;
1391 exit:
1392 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1393 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001394}
1395
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001396static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001397spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001399 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001400 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 char *frame;
1402 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001404
Christopher Fauletb067b062017-01-04 16:39:11 +01001405 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001406 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001407 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001408 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001409
Christopher Fauleta1cda022016-12-21 08:58:06 +01001410 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1412 " - Connection timed out\n",
1413 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1414 __FUNCTION__, appctx);
1415 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 goto exit;
1417 }
1418
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001419 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001420 ret = spoe_recv_frame(appctx, frame,
1421 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001422 if (ret > 1) {
1423 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1424 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1425 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001426 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001427 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001428 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001429 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001430
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 switch (ret) {
1432 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001433 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1435 goto next;
1436
1437 case 1: /* retry later */
1438 goto stop;
1439
1440 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001441 /* HELLO handshake is finished, set the idle timeout and
1442 * add the applet in the list of running applets. */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001443 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001444 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001445 SPOE_APPCTX(appctx)->node.key = 0;
1446 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001447
1448 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001449 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001450 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001451 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001452
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001454 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001455 if (trash.data)
1456 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001457
1458 SPOE_APPCTX(appctx)->task->expire =
1459 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001460 return 0;
1461 stop:
1462 return 1;
1463 exit:
1464 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1465 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001466}
1467
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001468
Christopher Fauleta1cda022016-12-21 08:58:06 +01001469static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001470spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001471{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001472 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1473 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001474 char *frame, *buf;
1475 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001476
Christopher Faulet8ef75252017-02-20 22:56:03 +01001477 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1478 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001479 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001480
1481 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1482 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1483 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1484 SPOE_APPCTX(appctx)->max_frame_size);
1485 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001486 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001487 *skip = 1;
1488 ret = 1;
1489 goto end;
1490 }
1491 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001492 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001493 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1494 SPOE_APPCTX(appctx)->max_frame_size);
1495
1496 }
1497
Christopher Faulet8ef75252017-02-20 22:56:03 +01001498 if (ret > 1)
1499 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001500
Christopher Faulet8ef75252017-02-20 22:56:03 +01001501 switch (ret) {
1502 case -1: /* error */
1503 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1504 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001505
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 case 0: /* ignore */
1507 if (ctx == NULL)
1508 goto abort_frag_frame;
1509
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001510 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511 LIST_DEL(&ctx->list);
1512 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001513 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001514 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001515 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001516 ctx->state = SPOE_CTX_ST_ERROR;
1517 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1518 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001519 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001520 break;
1521
1522 case 1: /* retry */
1523 *skip = 1;
1524 break;
1525
1526 default:
1527 if (ctx == NULL)
1528 goto abort_frag_frame;
1529
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001530 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 LIST_DEL(&ctx->list);
1532 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001533 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001534 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001535 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001536 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1537 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1538 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001539 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 }
1542 goto end;
1543
1544 frag_frame_sent:
1545 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001546 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001547 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1548 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1549 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1551 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1552 goto end;
1553
1554 no_frag_frame_sent:
1555 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1556 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001557 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001558 }
1559 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1560 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1561 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1562 }
1563 else {
1564 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001565 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001566 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1567 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001568 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001569 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001570 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1571 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1572 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001573 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001574
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1576 goto end;
1577
1578 abort_frag_frame:
1579 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1580 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1581 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1582 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1583 goto end;
1584
1585 end:
1586 return ret;
1587}
1588
1589static int
1590spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1591{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001592 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001593 struct spoe_context *ctx = NULL;
1594 char *frame;
1595 int ret;
1596
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001597 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001598 ret = spoe_recv_frame(appctx, frame,
1599 SPOE_APPCTX(appctx)->max_frame_size);
1600 if (ret > 1) {
1601 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1602 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001603 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001604 goto end;
1605 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001606 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001607 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1608 }
1609 switch (ret) {
1610 case -1: /* error */
1611 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1612 break;
1613
1614 case 0: /* ignore */
1615 break;
1616
1617 case 1: /* retry */
1618 *skip = 1;
1619 break;
1620
1621 default:
1622 LIST_DEL(&ctx->list);
1623 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001624 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001625 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1626 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001627 if (ctx->spoe_appctx) {
1628 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001629 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001630 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001631 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1632 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1633 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1634 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1635 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1636 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1637 }
1638 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1639 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001640 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1641 break;
1642 }
1643
1644 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001645 if (trash.data)
1646 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001647 end:
1648 return ret;
1649}
1650
1651static int
1652spoe_handle_processing_appctx(struct appctx *appctx)
1653{
1654 struct stream_interface *si = appctx->owner;
1655 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001656 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001657
1658 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1660 goto exit;
1661 }
1662
1663 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1664 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1665 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1666 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1667 goto next;
1668 }
1669
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001670 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001671 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001672 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001673 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1674 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001675 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001676
Christopher Faulet8f82b202018-01-24 16:23:03 +01001677 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1678 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001679
Christopher Faulet8f82b202018-01-24 16:23:03 +01001680 /* receiving_frame loop */
1681 while (!skip_receiving) {
1682 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1683 switch (ret) {
1684 case -1: /* error */
1685 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001686
Christopher Faulet8f82b202018-01-24 16:23:03 +01001687 case 0: /* ignore */
1688 active_r = 1;
1689 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001690
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 case 1: /* retry */
1692 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001693
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 default:
1695 active_r = 1;
1696 break;
1697 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001698 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001699
Christopher Faulet8f82b202018-01-24 16:23:03 +01001700 /* send_frame loop */
1701 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1702 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1703 switch (ret) {
1704 case -1: /* error */
1705 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706
Christopher Faulet8f82b202018-01-24 16:23:03 +01001707 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001708 if (SPOE_APPCTX(appctx)->node.key)
1709 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 active_s++;
1711 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001712
Christopher Faulet8f82b202018-01-24 16:23:03 +01001713 case 1: /* retry */
1714 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001717 if (SPOE_APPCTX(appctx)->node.key)
1718 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001719 active_s++;
1720 break;
1721 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001722 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001723
Christopher Faulet8f82b202018-01-24 16:23:03 +01001724 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001725 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1726 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1727 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001728
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001730 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001731 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001732 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001733 }
1734 return 1;
1735
Christopher Faulet8f82b202018-01-24 16:23:03 +01001736 next:
1737 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1738 return 0;
1739
Christopher Fauleta1cda022016-12-21 08:58:06 +01001740 exit:
1741 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1742 return 0;
1743}
1744
1745static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001746spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747{
1748 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001749 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001750 char *frame, *buf;
1751 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001752
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1754 goto exit;
1755
1756 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1757 goto exit;
1758
Christopher Faulet8ef75252017-02-20 22:56:03 +01001759 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1760 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001761 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001762 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1763 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001764 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001765 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766
1767 switch (ret) {
1768 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001769 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 goto exit;
1771
1772 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 goto stop;
1774
1775 default:
1776 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1777 " - disconnected by HAProxy (%d): %s\n",
1778 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001779 __FUNCTION__, appctx,
1780 SPOE_APPCTX(appctx)->status_code,
1781 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001782
1783 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1784 goto next;
1785 }
1786
1787 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001788 SPOE_APPCTX(appctx)->task->expire =
1789 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790 return 0;
1791 stop:
1792 return 1;
1793 exit:
1794 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1795 return 0;
1796}
1797
1798static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001800{
1801 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802 char *frame;
1803 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804
Christopher Fauletb067b062017-01-04 16:39:11 +01001805 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001806 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001808 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809
Christopher Fauletb067b062017-01-04 16:39:11 +01001810 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001813 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001815 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001816 ret = spoe_recv_frame(appctx, frame,
1817 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001819 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001820 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821 }
1822
1823 switch (ret) {
1824 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1826 " - error on frame (%s)\n",
1827 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001828 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001829 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001830 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831 goto exit;
1832
1833 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 goto next;
1835
1836 case 1: /* retry */
1837 goto stop;
1838
1839 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001840 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001841 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001843 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001844 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1845 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 goto exit;
1847 }
1848
1849 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001851 if (trash.data)
1852 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001853
Christopher Fauleta1cda022016-12-21 08:58:06 +01001854 return 0;
1855 stop:
1856 return 1;
1857 exit:
1858 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1859 return 0;
1860}
1861
1862/* I/O Handler processing messages exchanged with the agent */
1863static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 struct stream_interface *si = appctx->owner;
1867 struct spoe_agent *agent;
1868
1869 if (SPOE_APPCTX(appctx) == NULL)
1870 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001871
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1873 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001874
Christopher Fauleta1cda022016-12-21 08:58:06 +01001875 switchstate:
1876 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1877 " - appctx-state=%s\n",
1878 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1879 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1880
1881 switch (appctx->st0) {
1882 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001883 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001884 goto out;
1885 goto switchstate;
1886
1887 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001888 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001889 goto out;
1890 goto switchstate;
1891
1892 case SPOE_APPCTX_ST_IDLE:
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001893 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001894 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001896 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001897 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001898 SPOE_APPCTX(appctx)->task->expire =
1899 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001900 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001901 goto switchstate;
1902 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001903 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1904 /* fall through */
1905
1906 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001907 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1908 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001909 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 goto out;
1911 goto switchstate;
1912
1913 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001914 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001915 goto out;
1916 goto switchstate;
1917
1918 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001920 goto out;
1921 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922
1923 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001924 appctx->st0 = SPOE_APPCTX_ST_END;
1925 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1926
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001927 si_shutw(si);
1928 si_shutr(si);
1929 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001930 /* fall through */
1931
1932 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001933 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001934 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001935 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001936 if (stopping)
1937 spoe_wakeup_appctx(appctx);
1938
Christopher Faulet42bfa462017-01-04 14:14:19 +01001939 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1940 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 si_oc(si)->flags |= CF_READ_DONTWAIT;
1942 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001943}
1944
1945struct applet spoe_applet = {
1946 .obj_type = OBJ_TYPE_APPLET,
1947 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001948 .fct = spoe_handle_appctx,
1949 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001950};
1951
1952/* Create a SPOE applet. On success, the created applet is returned, else
1953 * NULL. */
1954static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001955spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001956{
1957 struct appctx *appctx;
1958 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001960
Emeric Brun1138fd02017-06-19 12:38:55 +02001961 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001962 goto out_error;
1963
Willy Tarreaubafbe012017-11-24 17:34:44 +01001964 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001965 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001966 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001967 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001968
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001970 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001971 goto out_free_spoe_appctx;
1972
1973 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001974 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001975 SPOE_APPCTX(appctx)->task->context = appctx;
1976 SPOE_APPCTX(appctx)->agent = conf->agent;
1977 SPOE_APPCTX(appctx)->version = 0;
1978 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1979 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001980 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001981 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001982 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001983
1984 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1985 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001986 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001987
1988 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1989 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990
Willy Tarreau5820a362016-12-22 15:59:02 +01001991 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992 if (!sess)
1993 goto out_free_spoe;
1994
Willy Tarreau87787ac2017-08-28 16:22:54 +02001995 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 goto out_free_sess;
1997
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998 stream_set_backend(strm, conf->agent->b.be);
1999
2000 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002001 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002 appctx_wakeup(appctx);
2003
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004 strm->do_log = NULL;
2005 strm->res.flags |= CF_READ_DONTWAIT;
2006
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002007 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002008 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002009 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002010 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002011
Emeric Brunc60def82017-09-27 14:59:38 +02002012 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002013 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002014 return appctx;
2015
2016 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017 out_free_sess:
2018 session_free(sess);
2019 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002020 task_free(SPOE_APPCTX(appctx)->task);
2021 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002022 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023 out_free_appctx:
2024 appctx_free(appctx);
2025 out_error:
2026 return NULL;
2027}
2028
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002030spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002031{
2032 struct spoe_config *conf = FLT_CONF(ctx->filter);
2033 struct spoe_agent *agent = conf->agent;
2034 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002035 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002036
Christopher Fauleta1cda022016-12-21 08:58:06 +01002037 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002038 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002039 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002040 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041
2042 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002043 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002044 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2045 ctx->strm);
2046
Christopher Fauleta1cda022016-12-21 08:58:06 +01002047 /* Do not try to create a new applet if there is no server up for the
2048 * agent's backend. */
2049 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2050 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2051 " - cannot create SPOE appctx: no server up\n",
2052 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2053 __FUNCTION__, ctx->strm);
2054 goto end;
2055 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002056
Christopher Fauleta1cda022016-12-21 08:58:06 +01002057 /* Do not try to create a new applet if we have reached the maximum of
2058 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002059 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002060 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2062 " - cannot create SPOE appctx: max CPS reached\n",
2063 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2064 __FUNCTION__, ctx->strm);
2065 goto end;
2066 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002067 }
2068
Christopher Faulet8ef75252017-02-20 22:56:03 +01002069 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002070 if (appctx == NULL) {
2071 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2072 " - failed to create SPOE appctx\n",
2073 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2074 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002075 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002076 "SPOE: [%s] failed to create SPOE applet\n",
2077 agent->id);
2078
Christopher Fauleta1cda022016-12-21 08:58:06 +01002079 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002080 }
2081
Christopher Fauleta1cda022016-12-21 08:58:06 +01002082 /* Increase the per-process number of cumulated connections */
2083 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002084 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002085
Christopher Fauleta1cda022016-12-21 08:58:06 +01002086 end:
2087 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002088 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002089 ctx->status_code = SPOE_CTX_ERR_RES;
2090 return -1;
2091 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002092
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002093 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002094 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002095 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002096 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2097 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002098
2099 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002100 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002101 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002102 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002103 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002104 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002105
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002106 /* Finally try to wakeup an IDLE applet. */
2107 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2108 struct eb32_node *node;
2109
2110 node = eb32_first(&agent->rt[tid].idle_applets);
2111 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2112 if (node && spoe_appctx) {
2113 eb32_delete(&spoe_appctx->node);
2114 spoe_appctx->node.key++;
2115 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2116 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002117 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002118 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002119 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002120}
2121
2122/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002123 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002124 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002125/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2126 * fragmented_content. If the next message can be processed, it returns 0. If
2127 * the message is too big, it returns -1.*/
2128static int
2129spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2130 struct spoe_message *msg, int dir,
2131 char **buf, char *end)
2132{
2133 struct sample *smp;
2134 struct spoe_arg *arg;
2135 int ret;
2136
2137 if (msg->cond) {
2138 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2139 ret = acl_pass(ret);
2140 if (msg->cond->pol == ACL_COND_UNLESS)
2141 ret = !ret;
2142
2143 /* the rule does not match */
2144 if (!ret)
2145 goto next;
2146 }
2147
2148 /* Resume encoding of a SPOE argument */
2149 if (ctx->frag_ctx.curarg != NULL) {
2150 arg = ctx->frag_ctx.curarg;
2151 goto encode_argument;
2152 }
2153
2154 if (ctx->frag_ctx.curoff != UINT_MAX)
2155 goto encode_msg_payload;
2156
2157 /* Check if there is enough space for the message name and the
2158 * number of arguments. It implies <msg->id_len> is encoded on 2
2159 * bytes, at most (< 2288). */
2160 if (*buf + 2 + msg->id_len + 1 > end)
2161 goto too_big;
2162
2163 /* Encode the message name */
2164 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2165 goto too_big;
2166
2167 /* Set the number of arguments for this message */
2168 **buf = msg->nargs;
2169 (*buf)++;
2170
2171 ctx->frag_ctx.curoff = 0;
2172 encode_msg_payload:
2173
2174 /* Loop on arguments */
2175 list_for_each_entry(arg, &msg->args, list) {
2176 ctx->frag_ctx.curarg = arg;
2177 ctx->frag_ctx.curoff = UINT_MAX;
2178
2179 encode_argument:
2180 if (ctx->frag_ctx.curoff != UINT_MAX)
2181 goto encode_arg_value;
2182
Joseph Herlantf7f60312018-11-15 13:46:49 -08002183 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002184 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2185 goto too_big;
2186
2187 ctx->frag_ctx.curoff = 0;
2188 encode_arg_value:
2189
Joseph Herlantf7f60312018-11-15 13:46:49 -08002190 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002191 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2192 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2193 if (ret == -1 || ctx->frag_ctx.curoff)
2194 goto too_big;
2195 }
2196
2197 next:
2198 return 0;
2199
2200 too_big:
2201 return -1;
2202}
2203
Christopher Fauletc718b822017-09-21 16:50:56 +02002204/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2205 * handle fragmented content. On success it returns 1. If an error occurred, -1
2206 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2207 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002208static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002209spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002210 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002211{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002212 struct spoe_config *conf = FLT_CONF(ctx->filter);
2213 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002214 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002215 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002216
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002217 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002218 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002219
Christopher Fauletc718b822017-09-21 16:50:56 +02002220 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2221 /* Resume encoding of a SPOE message */
2222 if (ctx->frag_ctx.curmsg != NULL) {
2223 msg = ctx->frag_ctx.curmsg;
2224 goto encode_evt_message;
2225 }
2226
2227 list_for_each_entry(msg, messages, by_evt) {
2228 ctx->frag_ctx.curmsg = msg;
2229 ctx->frag_ctx.curarg = NULL;
2230 ctx->frag_ctx.curoff = UINT_MAX;
2231
2232 encode_evt_message:
2233 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2234 goto too_big;
2235 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002236 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002237 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2238 /* Resume encoding of a SPOE message */
2239 if (ctx->frag_ctx.curmsg != NULL) {
2240 msg = ctx->frag_ctx.curmsg;
2241 goto encode_grp_message;
2242 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002243
Christopher Fauletc718b822017-09-21 16:50:56 +02002244 list_for_each_entry(msg, messages, by_grp) {
2245 ctx->frag_ctx.curmsg = msg;
2246 ctx->frag_ctx.curarg = NULL;
2247 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002248
Christopher Fauletc718b822017-09-21 16:50:56 +02002249 encode_grp_message:
2250 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2251 goto too_big;
2252 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002253 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002254 else
2255 goto skip;
2256
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002257
Christopher Faulet57583e42017-09-04 15:41:09 +02002258 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002259 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002260 goto skip;
2261
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002262 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002263 " - encode %s messages - spoe_appctx=%p"
2264 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002265 (int)now.tv_sec, (int)now.tv_usec,
2266 agent->id, __FUNCTION__, s,
2267 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002268 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002269 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002270
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002271 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002272 ctx->frag_ctx.curmsg = NULL;
2273 ctx->frag_ctx.curarg = NULL;
2274 ctx->frag_ctx.curoff = 0;
2275 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002276
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002277 return 1;
2278
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002279 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002280 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2281 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2282 return -1;
2283 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002284
2285 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286 " - encode fragmented messages - spoe_appctx=%p"
2287 " - curmsg=%p - curarg=%p - curoff=%u"
2288 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002289 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002290 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002291 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002292 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002293
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002294 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002295 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2296 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2297 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002298
2299 skip:
2300 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2301 " - skip the frame because nothing has been encoded\n",
2302 (int)now.tv_sec, (int)now.tv_usec,
2303 agent->id, __FUNCTION__, s);
2304 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002305}
2306
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002307
2308/***************************************************************************
2309 * Functions that handle SPOE actions
2310 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002311/* Helper function to set a variable */
2312static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002313spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002314 struct sample *smp)
2315{
2316 struct spoe_config *conf = FLT_CONF(ctx->filter);
2317 struct spoe_agent *agent = conf->agent;
2318 char varname[64];
2319
2320 memset(varname, 0, sizeof(varname));
2321 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2322 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002323 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2324 vars_set_by_name(varname, len, smp);
2325 else
2326 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002327}
2328
2329/* Helper function to unset a variable */
2330static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002331spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002332 struct sample *smp)
2333{
2334 struct spoe_config *conf = FLT_CONF(ctx->filter);
2335 struct spoe_agent *agent = conf->agent;
2336 char varname[64];
2337
2338 memset(varname, 0, sizeof(varname));
2339 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2340 scope, agent->var_pfx, len, name);
2341 vars_unset_by_name_ifexist(varname, len, smp);
2342}
2343
2344
Christopher Faulet8ef75252017-02-20 22:56:03 +01002345static inline int
2346spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2347 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002349 char *str, *scope, *p = *buf;
2350 struct sample smp;
2351 uint64_t sz;
2352 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354 if (p + 2 >= end)
2355 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002356
Christopher Faulet8ef75252017-02-20 22:56:03 +01002357 /* SET-VAR requires 3 arguments */
2358 if (*p++ != 3)
2359 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002360
Christopher Faulet8ef75252017-02-20 22:56:03 +01002361 switch (*p++) {
2362 case SPOE_SCOPE_PROC: scope = "proc"; break;
2363 case SPOE_SCOPE_SESS: scope = "sess"; break;
2364 case SPOE_SCOPE_TXN : scope = "txn"; break;
2365 case SPOE_SCOPE_REQ : scope = "req"; break;
2366 case SPOE_SCOPE_RES : scope = "res"; break;
2367 default: goto skip;
2368 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002369
Christopher Faulet8ef75252017-02-20 22:56:03 +01002370 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2371 goto skip;
2372 memset(&smp, 0, sizeof(smp));
2373 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002374
Christopher Faulet8ef75252017-02-20 22:56:03 +01002375 if (spoe_decode_data(&p, end, &smp) == -1)
2376 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002377
Christopher Faulet8ef75252017-02-20 22:56:03 +01002378 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2379 " - set-var '%s.%s.%.*s'\n",
2380 (int)now.tv_sec, (int)now.tv_usec,
2381 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2382 __FUNCTION__, s, scope,
2383 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2384 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002385
Christopher Faulet8ef75252017-02-20 22:56:03 +01002386 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388 ret = (p - *buf);
2389 *buf = p;
2390 return ret;
2391 skip:
2392 return 0;
2393}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395static inline int
2396spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2397 char **buf, char *end, int dir)
2398{
2399 char *str, *scope, *p = *buf;
2400 struct sample smp;
2401 uint64_t sz;
2402 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 if (p + 2 >= end)
2405 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002406
Christopher Faulet8ef75252017-02-20 22:56:03 +01002407 /* UNSET-VAR requires 2 arguments */
2408 if (*p++ != 2)
2409 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002410
Christopher Faulet8ef75252017-02-20 22:56:03 +01002411 switch (*p++) {
2412 case SPOE_SCOPE_PROC: scope = "proc"; break;
2413 case SPOE_SCOPE_SESS: scope = "sess"; break;
2414 case SPOE_SCOPE_TXN : scope = "txn"; break;
2415 case SPOE_SCOPE_REQ : scope = "req"; break;
2416 case SPOE_SCOPE_RES : scope = "res"; break;
2417 default: goto skip;
2418 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002419
Christopher Faulet8ef75252017-02-20 22:56:03 +01002420 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2421 goto skip;
2422 memset(&smp, 0, sizeof(smp));
2423 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002424
Christopher Faulet8ef75252017-02-20 22:56:03 +01002425 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2426 " - unset-var '%s.%s.%.*s'\n",
2427 (int)now.tv_sec, (int)now.tv_usec,
2428 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2429 __FUNCTION__, s, scope,
2430 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2431 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002432
Christopher Faulet8ef75252017-02-20 22:56:03 +01002433 spoe_unset_var(ctx, scope, str, sz, &smp);
2434
2435 ret = (p - *buf);
2436 *buf = p;
2437 return ret;
2438 skip:
2439 return 0;
2440}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002441
Christopher Faulet8ef75252017-02-20 22:56:03 +01002442/* Process SPOE actions for a specific event. It returns 1 on success. If an
2443 * error occurred, 0 is returned. */
2444static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002445spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002446{
2447 char *p, *end;
2448 int ret;
2449
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002450 p = b_head(&ctx->buffer);
2451 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002452
2453 while (p < end) {
2454 enum spoe_action_type type;
2455
2456 type = *p++;
2457 switch (type) {
2458 case SPOE_ACT_T_SET_VAR:
2459 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2460 if (!ret)
2461 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002462 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002463
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464 case SPOE_ACT_T_UNSET_VAR:
2465 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2466 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002467 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002468 break;
2469
2470 default:
2471 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002472 }
2473 }
2474
2475 return 1;
2476 skip:
2477 return 0;
2478}
2479
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002480/***************************************************************************
2481 * Functions that process SPOE events
2482 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002483static void
2484spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2485 struct spoe_context *ctx, int dir)
2486{
2487 if (!tv_iszero(&ctx->stats.tv_start)) {
2488 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2489 ctx->stats.t_total += ctx->stats.t_process;
2490 tv_zero(&ctx->stats.tv_request);
2491 tv_zero(&ctx->stats.tv_queue);
2492 tv_zero(&ctx->stats.tv_wait);
2493 tv_zero(&ctx->stats.tv_response);
2494 }
2495
2496 if (agent->var_t_process) {
2497 struct sample smp;
2498
2499 memset(&smp, 0, sizeof(smp));
2500 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2501 smp.data.u.sint = ctx->stats.t_process;
2502 smp.data.type = SMP_T_SINT;
2503
2504 spoe_set_var(ctx, "txn", agent->var_t_process,
2505 strlen(agent->var_t_process), &smp);
2506 }
2507
2508 if (agent->var_t_total) {
2509 struct sample smp;
2510
2511 memset(&smp, 0, sizeof(smp));
2512 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2513 smp.data.u.sint = ctx->stats.t_total;
2514 smp.data.type = SMP_T_SINT;
2515
2516 spoe_set_var(ctx, "txn", agent->var_t_total,
2517 strlen(agent->var_t_total), &smp);
2518 }
2519}
2520
2521static void
2522spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2523 struct spoe_context *ctx, int dir)
2524{
2525 if (agent->eps_max > 0)
2526 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2527
2528 if (agent->var_on_error) {
2529 struct sample smp;
2530
2531 memset(&smp, 0, sizeof(smp));
2532 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2533 smp.data.u.sint = ctx->status_code;
2534 smp.data.type = SMP_T_BOOL;
2535
2536 spoe_set_var(ctx, "txn", agent->var_on_error,
2537 strlen(agent->var_on_error), &smp);
2538 }
2539
2540 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2541 ? SPOE_CTX_ST_READY
2542 : SPOE_CTX_ST_NONE);
2543}
2544
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002545static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002546spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002547{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002548 /* If a process is already started for this SPOE context, retry
2549 * later. */
2550 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002551 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002552
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002553 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002554 ctx->stats.tv_start = now;
2555 ctx->stats.tv_request = now;
2556 ctx->stats.t_request = -1;
2557 ctx->stats.t_queue = -1;
2558 ctx->stats.t_waiting = -1;
2559 ctx->stats.t_response = -1;
2560 ctx->stats.t_process = -1;
2561
2562 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002563
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564 /* Set the right flag to prevent request and response processing
2565 * in same time. */
2566 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2567 ? SPOE_CTX_FL_REQ_PROCESS
2568 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002569 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570}
2571
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002572static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002573spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002574{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002575 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002576
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002577 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2578 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002579 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002580 if (sa) {
2581 if (sa->frag_ctx.ctx == ctx) {
2582 sa->frag_ctx.ctx = NULL;
2583 spoe_wakeup_appctx(sa->owner);
2584 }
2585 else
2586 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002587 }
2588
Christopher Fauleta1cda022016-12-21 08:58:06 +01002589 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002590 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002591 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592
2593 /* Reset processing timer */
2594 ctx->process_exp = TICK_ETERNITY;
2595
Christopher Faulet8ef75252017-02-20 22:56:03 +01002596 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002597
Christopher Fauletfce747b2018-01-24 15:59:32 +01002598 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002599 ctx->frag_ctx.curmsg = NULL;
2600 ctx->frag_ctx.curarg = NULL;
2601 ctx->frag_ctx.curoff = 0;
2602 ctx->frag_ctx.flags = 0;
2603
Christopher Fauleta1cda022016-12-21 08:58:06 +01002604 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002605 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002606 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002607 else
2608 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2609
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610 LIST_DEL(&ctx->list);
2611 LIST_INIT(&ctx->list);
2612 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002613}
2614
Christopher Faulet58d03682017-09-21 16:57:24 +02002615/* Process a list of SPOE messages. First, this functions will process messages
2616 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2617 * to process corresponding actions. During all the processing, it returns 0
2618 * and it returns 1 when the processing is finished. If an error occurred, -1
2619 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002620static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002621spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2622 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002623{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002624 struct spoe_config *conf = FLT_CONF(ctx->filter);
2625 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002626 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002627
2628 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002629 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002630
2631 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2632 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002633 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002634 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002635 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002636 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002637 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002638 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002639
2640 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002642 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002643 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002644 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002645 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002646 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002647 goto skip;
2648 }
2649 }
2650
Christopher Fauletf7a30922016-11-10 15:04:51 +01002651 if (!tick_isset(ctx->process_exp)) {
2652 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2653 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2654 ctx->process_exp);
2655 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002656 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002657 if (!ret)
2658 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002659
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002660 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002662 }
2663
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002664 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002665 if (!tv_iszero(&ctx->stats.tv_request))
2666 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002667 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002668 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002669 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002670 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002671 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002672 if (!ret)
2673 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002674 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002675 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002676 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2677 }
2678
2679 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002680 if (ctx->spoe_appctx)
2681 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002682 ret = 0;
2683 goto out;
2684 }
2685
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002686 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2687 ret = 0;
2688 goto out;
2689 }
2690
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002691 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002692 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002693 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002694 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002695 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002696 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002697 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002698 }
2699
2700 out:
2701 return ret;
2702
Christopher Fauleta1cda022016-12-21 08:58:06 +01002703 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002704 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002705 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002706 spoe_stop_processing(agent, ctx);
2707 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002708
Christopher Fauleta1cda022016-12-21 08:58:06 +01002709 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002710 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002711 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002712 if (ctx->status_code) {
2713 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2714 spoe_handle_processing_error(s, agent, ctx, dir);
2715 ret = 1;
2716 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002717 return ret;
2718}
2719
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002720/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2721 * See spoe_process_message for details. */
2722static int
2723spoe_process_group(struct stream *s, struct spoe_context *ctx,
2724 struct spoe_group *group, int dir)
2725{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002726 struct spoe_config *conf = FLT_CONF(ctx->filter);
2727 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002728 int ret;
2729
2730 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2731 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002732 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002733 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2734 group->id);
2735
2736 if (LIST_ISEMPTY(&group->messages))
2737 return 1;
2738
2739 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002740 if (ret && ctx->stats.t_process != -1) {
2741 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002742 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002743 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002744 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002745 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002746 ctx->stats.t_response, ctx->stats.t_process,
2747 agent->counters.idles, agent->counters.applets,
2748 agent->counters.nb_sending, agent->counters.nb_waiting,
2749 agent->counters.nb_errors, agent->counters.nb_processed,
2750 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2751 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2752 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2753 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2754 agent->id, group->id, s->uniq_id, ctx->status_code,
2755 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2756 ctx->stats.t_response, ctx->stats.t_process,
2757 agent->counters.idles, agent->counters.applets,
2758 agent->counters.nb_sending, agent->counters.nb_waiting,
2759 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002760 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002761 return ret;
2762}
2763
Christopher Faulet58d03682017-09-21 16:57:24 +02002764/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2765 * See spoe_process_message for details. */
2766static int
2767spoe_process_event(struct stream *s, struct spoe_context *ctx,
2768 enum spoe_event ev)
2769{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002770 struct spoe_config *conf = FLT_CONF(ctx->filter);
2771 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002772 int dir, ret;
2773
2774 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002775 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002776 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002777 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2778 spoe_event_str[ev]);
2779
2780 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2781
2782 if (LIST_ISEMPTY(&(ctx->events[ev])))
2783 return 1;
2784
2785 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002786 if (ret && ctx->stats.t_process != -1) {
2787 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002788 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002789 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2790 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2791 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002792 ctx->stats.t_response, ctx->stats.t_process,
2793 agent->counters.idles, agent->counters.applets,
2794 agent->counters.nb_sending, agent->counters.nb_waiting,
2795 agent->counters.nb_errors, agent->counters.nb_processed,
2796 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2797 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2798 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2799 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2800 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2801 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2802 ctx->stats.t_response, ctx->stats.t_process,
2803 agent->counters.idles, agent->counters.applets,
2804 agent->counters.nb_sending, agent->counters.nb_waiting,
2805 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002806 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002807 return ret;
2808}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002809
2810/***************************************************************************
2811 * Functions that create/destroy SPOE contexts
2812 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002813static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002814spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002815{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002816 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002817 return 1;
2818
Christopher Faulet4596fb72017-01-11 14:05:19 +01002819 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002820 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002821 LIST_DEL(&buffer_wait->list);
2822 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002823 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002824 }
2825
Christopher Faulet4596fb72017-01-11 14:05:19 +01002826 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002827 return 1;
2828
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002829 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002830 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002831 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002832 return 0;
2833}
2834
2835static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002836spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002837{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002838 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002839 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002840 LIST_DEL(&buffer_wait->list);
2841 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002842 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002843 }
2844
2845 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002846 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002847 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002848 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002849 }
2850}
2851
Christopher Faulet4596fb72017-01-11 14:05:19 +01002852static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002853spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002854{
2855 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2856 return 1;
2857}
2858
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002859static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002860spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861{
2862 struct spoe_config *conf = FLT_CONF(filter);
2863 struct spoe_context *ctx;
2864
Willy Tarreaubafbe012017-11-24 17:34:44 +01002865 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002866 if (ctx == NULL) {
2867 return NULL;
2868 }
2869 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002870 ctx->filter = filter;
2871 ctx->state = SPOE_CTX_ST_NONE;
2872 ctx->status_code = SPOE_CTX_ERR_NONE;
2873 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002874 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002875 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002876 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002877 LIST_INIT(&ctx->buffer_wait.list);
2878 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002879 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002880 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002881
Christopher Fauletf7a30922016-11-10 15:04:51 +01002882 ctx->stream_id = 0;
2883 ctx->frame_id = 1;
2884 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002885
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002886 tv_zero(&ctx->stats.tv_start);
2887 tv_zero(&ctx->stats.tv_request);
2888 tv_zero(&ctx->stats.tv_queue);
2889 tv_zero(&ctx->stats.tv_wait);
2890 tv_zero(&ctx->stats.tv_response);
2891 ctx->stats.t_request = -1;
2892 ctx->stats.t_queue = -1;
2893 ctx->stats.t_waiting = -1;
2894 ctx->stats.t_response = -1;
2895 ctx->stats.t_process = -1;
2896 ctx->stats.t_total = 0;
2897
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002898 ctx->strm = s;
2899 ctx->state = SPOE_CTX_ST_READY;
2900 filter->ctx = ctx;
2901
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902 return ctx;
2903}
2904
2905static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002906spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002907{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002908 struct spoe_config *conf = FLT_CONF(filter);
2909 struct spoe_context *ctx = filter->ctx;
2910
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002911 if (!ctx)
2912 return;
2913
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002914 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002915 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002916 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002917}
2918
2919static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002920spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002921{
2922 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002923 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002924
2925 tv_zero(&ctx->stats.tv_start);
2926 tv_zero(&ctx->stats.tv_request);
2927 tv_zero(&ctx->stats.tv_queue);
2928 tv_zero(&ctx->stats.tv_wait);
2929 tv_zero(&ctx->stats.tv_response);
2930 ctx->stats.t_request = -1;
2931 ctx->stats.t_queue = -1;
2932 ctx->stats.t_waiting = -1;
2933 ctx->stats.t_response = -1;
2934 ctx->stats.t_process = -1;
2935 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002936}
2937
2938
2939/***************************************************************************
2940 * Hooks that manage the filter lifecycle (init/check/deinit)
2941 **************************************************************************/
2942/* Signal handler: Do a soft stop, wakeup SPOE applet */
2943static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002944spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002945{
2946 struct proxy *p;
2947
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002948 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002949 while (p) {
2950 struct flt_conf *fconf;
2951
2952 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002953 struct spoe_config *conf;
2954 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002955 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002956 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957
Christopher Faulet3b386a32017-02-23 10:17:15 +01002958 if (fconf->id != spoe_filter_id)
2959 continue;
2960
2961 conf = fconf->conf;
2962 agent = conf->agent;
2963
Christopher Faulet24289f22017-09-25 14:48:02 +02002964 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002965 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002966 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2967 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002968 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002969 }
2970 }
2971 p = p->next;
2972 }
2973}
2974
2975
2976/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2977static int
2978spoe_init(struct proxy *px, struct flt_conf *fconf)
2979{
2980 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002981
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002982 /* conf->agent_fe was already initialized during the config
2983 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002984 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002985 conf->agent_fe.cap = PR_CAP_FE;
2986 conf->agent_fe.mode = PR_MODE_TCP;
2987 conf->agent_fe.maxconn = 0;
2988 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2989 conf->agent_fe.conn_retries = CONN_RETRIES;
2990 conf->agent_fe.accept = frontend_accept;
2991 conf->agent_fe.srv = NULL;
2992 conf->agent_fe.timeout.client = TICK_ETERNITY;
2993 conf->agent_fe.default_target = &spoe_applet.obj_type;
2994 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2995
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002996 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002997 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002998 sighandler_registered = 1;
2999 }
3000
3001 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003002}
3003
3004/* Free ressources allocated by the SPOE filter. */
3005static void
3006spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3007{
3008 struct spoe_config *conf = fconf->conf;
3009
3010 if (conf) {
3011 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003012
Christopher Faulet8ef75252017-02-20 22:56:03 +01003013 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003014 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003015 free(conf);
3016 }
3017 fconf->conf = NULL;
3018}
3019
3020/* Check configuration of a SPOE filter for a specified proxy.
3021 * Return 1 on error, else 0. */
3022static int
3023spoe_check(struct proxy *px, struct flt_conf *fconf)
3024{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003025 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003026 struct spoe_config *conf = fconf->conf;
3027 struct proxy *target;
3028
Christopher Faulet7ee86672017-09-19 11:08:28 +02003029 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3030 * are uniq */
3031 list_for_each_entry(f, &px->filter_configs, list) {
3032 struct spoe_config *c = f->conf;
3033
3034 /* This is not an SPOE filter */
3035 if (f->id != spoe_filter_id)
3036 continue;
3037 /* This is the current SPOE filter */
3038 if (f == fconf)
3039 continue;
3040
3041 /* Check engine Id. It should be uniq */
3042 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003043 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3044 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003045 return 1;
3046 }
3047 }
3048
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003049 target = proxy_be_by_name(conf->agent->b.name);
3050 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003051 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3052 " declared at %s:%d.\n",
3053 px->id, conf->agent->b.name, conf->agent->id,
3054 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003055 return 1;
3056 }
3057 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003058 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3059 " at %s:%d does not support HTTP mode.\n",
3060 px->id, target->id, conf->agent->id,
3061 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003062 return 1;
3063 }
3064
3065 free(conf->agent->b.name);
3066 conf->agent->b.name = NULL;
3067 conf->agent->b.be = target;
3068 return 0;
3069}
3070
3071/**************************************************************************
3072 * Hooks attached to a stream
3073 *************************************************************************/
3074/* Called when a filter instance is created and attach to a stream. It creates
3075 * the context that will be used to process this stream. */
3076static int
3077spoe_start(struct stream *s, struct filter *filter)
3078{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003079 struct spoe_config *conf = FLT_CONF(filter);
3080 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003081 struct spoe_context *ctx;
3082
3083 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003084 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003085 __FUNCTION__, s);
3086
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003087 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003088 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3089 " - failed to create SPOE context\n",
3090 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003091 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003092 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003093 "SPOE: [%s] failed to create SPOE context\n",
3094 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003095 return 0;
3096 }
3097
Christopher Faulet11610f32017-09-21 10:23:10 +02003098 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003099 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3100
Christopher Faulet11610f32017-09-21 10:23:10 +02003101 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003102 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3103
Christopher Faulet11610f32017-09-21 10:23:10 +02003104 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003105 filter->pre_analyzers |= AN_RES_INSPECT;
3106
Christopher Faulet11610f32017-09-21 10:23:10 +02003107 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003108 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3109
Christopher Faulet11610f32017-09-21 10:23:10 +02003110 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003111 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3112
Christopher Faulet11610f32017-09-21 10:23:10 +02003113 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003114 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3115
3116 return 1;
3117}
3118
3119/* Called when a filter instance is detached from a stream. It release the
3120 * attached SPOE context. */
3121static void
3122spoe_stop(struct stream *s, struct filter *filter)
3123{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3125 (int)now.tv_sec, (int)now.tv_usec,
3126 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3127 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003128 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003129}
3130
Christopher Fauletf7a30922016-11-10 15:04:51 +01003131
3132/*
3133 * Called when the stream is woken up because of expired timer.
3134 */
3135static void
3136spoe_check_timeouts(struct stream *s, struct filter *filter)
3137{
3138 struct spoe_context *ctx = filter->ctx;
3139
Christopher Fauletac580602018-03-20 16:09:20 +01003140 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003141 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003142}
3143
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003144/* Called when we are ready to filter data on a channel */
3145static int
3146spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3147{
3148 struct spoe_context *ctx = filter->ctx;
3149 int ret = 1;
3150
3151 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3152 " - ctx-flags=0x%08x\n",
3153 (int)now.tv_sec, (int)now.tv_usec,
3154 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3155 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3156
Christopher Fauletb067b062017-01-04 16:39:11 +01003157 if (ctx->state == SPOE_CTX_ST_NONE)
3158 goto out;
3159
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003160 if (!(chn->flags & CF_ISRESP)) {
3161 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3162 chn->analysers |= AN_REQ_INSPECT_FE;
3163 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3164 chn->analysers |= AN_REQ_INSPECT_BE;
3165
3166 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3167 goto out;
3168
3169 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003170 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003171 if (!ret)
3172 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003173 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3174 }
3175 else {
3176 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3177 chn->analysers |= AN_RES_INSPECT;
3178
3179 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3180 goto out;
3181
Christopher Faulet8ef75252017-02-20 22:56:03 +01003182 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003183 if (!ret) {
3184 channel_dont_read(chn);
3185 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003186 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003187 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003188 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003189 }
3190
3191 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003192 return ret;
3193}
3194
3195/* Called before a processing happens on a given channel */
3196static int
3197spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3198 struct channel *chn, unsigned an_bit)
3199{
3200 struct spoe_context *ctx = filter->ctx;
3201 int ret = 1;
3202
3203 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3204 " - ctx-flags=0x%08x - ana=0x%08x\n",
3205 (int)now.tv_sec, (int)now.tv_usec,
3206 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3207 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3208 ctx->flags, an_bit);
3209
Christopher Fauletb067b062017-01-04 16:39:11 +01003210 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 goto out;
3212
3213 switch (an_bit) {
3214 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003215 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003216 break;
3217 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003218 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003219 break;
3220 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003221 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003222 break;
3223 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003224 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003225 break;
3226 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003227 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228 break;
3229 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003230 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003231 break;
3232 }
3233
3234 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003235 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003236 channel_dont_read(chn);
3237 channel_dont_close(chn);
3238 }
3239 return ret;
3240}
3241
3242/* Called when the filtering on the channel ends. */
3243static int
3244spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3245{
3246 struct spoe_context *ctx = filter->ctx;
3247
3248 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3249 " - ctx-flags=0x%08x\n",
3250 (int)now.tv_sec, (int)now.tv_usec,
3251 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3252 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3253
3254 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003255 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003256 }
3257
3258 return 1;
3259}
3260
3261/********************************************************************
3262 * Functions that manage the filter initialization
3263 ********************************************************************/
3264struct flt_ops spoe_ops = {
3265 /* Manage SPOE filter, called for each filter declaration */
3266 .init = spoe_init,
3267 .deinit = spoe_deinit,
3268 .check = spoe_check,
3269
3270 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003271 .attach = spoe_start,
3272 .detach = spoe_stop,
3273 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003274
3275 /* Handle channels activity */
3276 .channel_start_analyze = spoe_start_analyze,
3277 .channel_pre_analyze = spoe_chn_pre_analyze,
3278 .channel_end_analyze = spoe_end_analyze,
3279};
3280
3281
3282static int
3283cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3284{
3285 const char *err;
3286 int i, err_code = 0;
3287
3288 if ((cfg_scope == NULL && curengine != NULL) ||
3289 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003290 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003291 goto out;
3292
3293 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3294 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003295 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3296 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297 err_code |= ERR_ALERT | ERR_ABORT;
3298 goto out;
3299 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003300 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3301 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003302 goto out;
3303 }
3304
3305 err = invalid_char(args[1]);
3306 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003307 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3308 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003309 err_code |= ERR_ALERT | ERR_ABORT;
3310 goto out;
3311 }
3312
3313 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003314 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3315 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003316 err_code |= ERR_ALERT | ERR_ABORT;
3317 goto out;
3318 }
3319 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003320 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003321 err_code |= ERR_ALERT | ERR_ABORT;
3322 goto out;
3323 }
3324
3325 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003326
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003327 curagent->conf.file = strdup(file);
3328 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003329
3330 curagent->timeout.hello = TICK_ETERNITY;
3331 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003332 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003333
3334 curagent->engine_id = NULL;
3335 curagent->var_pfx = NULL;
3336 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003337 curagent->var_t_process = NULL;
3338 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003339 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3340 if (global.nbthread == 1)
3341 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003342 curagent->cps_max = 0;
3343 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003344 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003345 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003346
3347 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003348 LIST_INIT(&curagent->events[i]);
3349 LIST_INIT(&curagent->groups);
3350 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003351
Christopher Faulet24289f22017-09-25 14:48:02 +02003352 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003353 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003354 err_code |= ERR_ALERT | ERR_ABORT;
3355 goto out;
3356 }
3357 for (i = 0; i < global.nbthread; ++i) {
3358 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003359 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003360 LIST_INIT(&curagent->rt[i].applets);
3361 LIST_INIT(&curagent->rt[i].sending_queue);
3362 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003363 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003364 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003365 }
3366 else if (!strcmp(args[0], "use-backend")) {
3367 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003368 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3369 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003370 err_code |= ERR_ALERT | ERR_FATAL;
3371 goto out;
3372 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003373 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003374 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003375 free(curagent->b.name);
3376 curagent->b.name = strdup(args[1]);
3377 }
3378 else if (!strcmp(args[0], "messages")) {
3379 int cur_arg = 1;
3380 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003381 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003382
Christopher Faulet11610f32017-09-21 10:23:10 +02003383 list_for_each_entry(ph, &curmphs, list) {
3384 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003385 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3386 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003387 err_code |= ERR_ALERT | ERR_FATAL;
3388 goto out;
3389 }
3390 }
3391
Christopher Faulet11610f32017-09-21 10:23:10 +02003392 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003393 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 err_code |= ERR_ALERT | ERR_ABORT;
3395 goto out;
3396 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003397 ph->id = strdup(args[cur_arg]);
3398 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003399 cur_arg++;
3400 }
3401 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003402 else if (!strcmp(args[0], "groups")) {
3403 int cur_arg = 1;
3404 while (*args[cur_arg]) {
3405 struct spoe_placeholder *ph = NULL;
3406
3407 list_for_each_entry(ph, &curgphs, list) {
3408 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003409 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3410 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003411 err_code |= ERR_ALERT | ERR_FATAL;
3412 goto out;
3413 }
3414 }
3415
3416 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003417 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003418 err_code |= ERR_ALERT | ERR_ABORT;
3419 goto out;
3420 }
3421 ph->id = strdup(args[cur_arg]);
3422 LIST_ADDQ(&curgphs, &ph->list);
3423 cur_arg++;
3424 }
3425 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003426 else if (!strcmp(args[0], "timeout")) {
3427 unsigned int *tv = NULL;
3428 const char *res;
3429 unsigned timeout;
3430
3431 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003432 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3433 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434 err_code |= ERR_ALERT | ERR_FATAL;
3435 goto out;
3436 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003437 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3438 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003439 if (!strcmp(args[1], "hello"))
3440 tv = &curagent->timeout.hello;
3441 else if (!strcmp(args[1], "idle"))
3442 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003443 else if (!strcmp(args[1], "processing"))
3444 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003446 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3447 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003448 err_code |= ERR_ALERT | ERR_FATAL;
3449 goto out;
3450 }
3451 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003452 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3453 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 err_code |= ERR_ALERT | ERR_FATAL;
3455 goto out;
3456 }
3457 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3458 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003459 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3460 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003461 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003462 goto out;
3463 }
3464 *tv = MS_TO_TICKS(timeout);
3465 }
3466 else if (!strcmp(args[0], "option")) {
3467 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003468 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3469 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003470 err_code |= ERR_ALERT | ERR_FATAL;
3471 goto out;
3472 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003473
Christopher Faulet305c6072017-02-23 16:17:53 +01003474 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003475 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003476 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003477 if (kwm == 1)
3478 curagent->flags &= ~SPOE_FL_PIPELINING;
3479 else
3480 curagent->flags |= SPOE_FL_PIPELINING;
3481 goto out;
3482 }
3483 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003484 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003485 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003486 if (kwm == 1)
3487 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003488 else {
3489 if (global.nbthread == 1)
3490 curagent->flags |= SPOE_FL_ASYNC;
3491 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003492 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3493 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003494 err_code |= ERR_WARN;
3495 }
3496 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003497 goto out;
3498 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003499 else if (!strcmp(args[1], "send-frag-payload")) {
3500 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3501 goto out;
3502 if (kwm == 1)
3503 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3504 else
3505 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3506 goto out;
3507 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003508 else if (!strcmp(args[1], "dontlog-normal")) {
3509 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3510 goto out;
3511 if (kwm == 1)
3512 curpxopts2 &= ~PR_O2_NOLOGNORM;
3513 else
3514 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003515 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003516 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003517
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003518 /* Following options does not support negation */
3519 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003520 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3521 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003522 err_code |= ERR_ALERT | ERR_FATAL;
3523 goto out;
3524 }
3525
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003526 if (!strcmp(args[1], "var-prefix")) {
3527 char *tmp;
3528
3529 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003530 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3531 file, linenum, args[0],
3532 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003533 err_code |= ERR_ALERT | ERR_FATAL;
3534 goto out;
3535 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003536 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3537 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003538 tmp = args[2];
3539 while (*tmp) {
3540 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003541 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003542 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003543 err_code |= ERR_ALERT | ERR_FATAL;
3544 goto out;
3545 }
3546 tmp++;
3547 }
3548 curagent->var_pfx = strdup(args[2]);
3549 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003550 else if (!strcmp(args[1], "force-set-var")) {
3551 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3552 goto out;
3553 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3554 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003555 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003556 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003557 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003558 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3559 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003560 else if (!strcmp(args[1], "set-on-error")) {
3561 char *tmp;
3562
3563 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003564 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3565 file, linenum, args[0],
3566 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003567 err_code |= ERR_ALERT | ERR_FATAL;
3568 goto out;
3569 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003570 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3571 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003572 tmp = args[2];
3573 while (*tmp) {
3574 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003575 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003576 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003577 err_code |= ERR_ALERT | ERR_FATAL;
3578 goto out;
3579 }
3580 tmp++;
3581 }
3582 curagent->var_on_error = strdup(args[2]);
3583 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003584 else if (!strcmp(args[1], "set-process-time")) {
3585 char *tmp;
3586
3587 if (!*args[2]) {
3588 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3589 file, linenum, args[0],
3590 args[1]);
3591 err_code |= ERR_ALERT | ERR_FATAL;
3592 goto out;
3593 }
3594 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3595 goto out;
3596 tmp = args[2];
3597 while (*tmp) {
3598 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003599 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003600 file, linenum, args[0], args[1]);
3601 err_code |= ERR_ALERT | ERR_FATAL;
3602 goto out;
3603 }
3604 tmp++;
3605 }
3606 curagent->var_t_process = strdup(args[2]);
3607 }
3608 else if (!strcmp(args[1], "set-total-time")) {
3609 char *tmp;
3610
3611 if (!*args[2]) {
3612 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3613 file, linenum, args[0],
3614 args[1]);
3615 err_code |= ERR_ALERT | ERR_FATAL;
3616 goto out;
3617 }
3618 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3619 goto out;
3620 tmp = args[2];
3621 while (*tmp) {
3622 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003623 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003624 file, linenum, args[0], args[1]);
3625 err_code |= ERR_ALERT | ERR_FATAL;
3626 goto out;
3627 }
3628 tmp++;
3629 }
3630 curagent->var_t_total = strdup(args[2]);
3631 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003632 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003633 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3634 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003635 err_code |= ERR_ALERT | ERR_FATAL;
3636 goto out;
3637 }
Christopher Faulet48026722016-11-16 15:01:12 +01003638 }
3639 else if (!strcmp(args[0], "maxconnrate")) {
3640 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003641 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3642 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003643 err_code |= ERR_ALERT | ERR_FATAL;
3644 goto out;
3645 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003646 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003647 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003648 curagent->cps_max = atol(args[1]);
3649 }
3650 else if (!strcmp(args[0], "maxerrrate")) {
3651 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003652 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3653 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003654 err_code |= ERR_ALERT | ERR_FATAL;
3655 goto out;
3656 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003657 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003658 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003659 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003660 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003661 else if (!strcmp(args[0], "max-frame-size")) {
3662 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003663 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3664 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003665 err_code |= ERR_ALERT | ERR_FATAL;
3666 goto out;
3667 }
3668 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3669 goto out;
3670 curagent->max_frame_size = atol(args[1]);
3671 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3672 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003673 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3674 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003675 err_code |= ERR_ALERT | ERR_FATAL;
3676 goto out;
3677 }
3678 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003679 else if (!strcmp(args[0], "max-waiting-frames")) {
3680 if (!*args[1]) {
3681 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3682 file, linenum, args[0]);
3683 err_code |= ERR_ALERT | ERR_FATAL;
3684 goto out;
3685 }
3686 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3687 goto out;
3688 curagent->max_fpa = atol(args[1]);
3689 if (curagent->max_fpa < 1) {
3690 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3691 file, linenum, args[0]);
3692 err_code |= ERR_ALERT | ERR_FATAL;
3693 goto out;
3694 }
3695 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003696 else if (!strcmp(args[0], "register-var-names")) {
3697 int cur_arg;
3698
3699 if (!*args[1]) {
3700 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3701 file, linenum, args[0]);
3702 err_code |= ERR_ALERT | ERR_FATAL;
3703 goto out;
3704 }
3705 cur_arg = 1;
3706 while (*args[cur_arg]) {
3707 struct spoe_var_placeholder *vph;
3708
3709 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3710 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3711 err_code |= ERR_ALERT | ERR_ABORT;
3712 goto out;
3713 }
3714 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3715 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3716 err_code |= ERR_ALERT | ERR_ABORT;
3717 goto out;
3718 }
3719 LIST_ADDQ(&curvars, &vph->list);
3720 cur_arg++;
3721 }
3722 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003723 else if (!strcmp(args[0], "log")) {
3724 char *errmsg = NULL;
3725
3726 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3727 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3728 err_code |= ERR_ALERT | ERR_FATAL;
3729 goto out;
3730 }
3731 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003732 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003733 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3734 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003735 err_code |= ERR_ALERT | ERR_FATAL;
3736 goto out;
3737 }
3738 out:
3739 return err_code;
3740}
Christopher Faulet11610f32017-09-21 10:23:10 +02003741static int
3742cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3743{
3744 struct spoe_group *grp;
3745 const char *err;
3746 int err_code = 0;
3747
3748 if ((cfg_scope == NULL && curengine != NULL) ||
3749 (cfg_scope != NULL && curengine == NULL) ||
3750 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3751 goto out;
3752
3753 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3754 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003755 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3756 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003757 err_code |= ERR_ALERT | ERR_ABORT;
3758 goto out;
3759 }
3760 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3761 err_code |= ERR_ABORT;
3762 goto out;
3763 }
3764
3765 err = invalid_char(args[1]);
3766 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003767 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3768 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003769 err_code |= ERR_ALERT | ERR_ABORT;
3770 goto out;
3771 }
3772
3773 list_for_each_entry(grp, &curgrps, list) {
3774 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003775 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3776 " name as another one declared at %s:%d.\n",
3777 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003778 err_code |= ERR_ALERT | ERR_FATAL;
3779 goto out;
3780 }
3781 }
3782
3783 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003784 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003785 err_code |= ERR_ALERT | ERR_ABORT;
3786 goto out;
3787 }
3788
3789 curgrp->id = strdup(args[1]);
3790 curgrp->conf.file = strdup(file);
3791 curgrp->conf.line = linenum;
3792 LIST_INIT(&curgrp->phs);
3793 LIST_INIT(&curgrp->messages);
3794 LIST_ADDQ(&curgrps, &curgrp->list);
3795 }
3796 else if (!strcmp(args[0], "messages")) {
3797 int cur_arg = 1;
3798 while (*args[cur_arg]) {
3799 struct spoe_placeholder *ph = NULL;
3800
3801 list_for_each_entry(ph, &curgrp->phs, list) {
3802 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003803 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3804 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003805 err_code |= ERR_ALERT | ERR_FATAL;
3806 goto out;
3807 }
3808 }
3809
3810 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003811 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003812 err_code |= ERR_ALERT | ERR_ABORT;
3813 goto out;
3814 }
3815 ph->id = strdup(args[cur_arg]);
3816 LIST_ADDQ(&curgrp->phs, &ph->list);
3817 cur_arg++;
3818 }
3819 }
3820 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003821 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3822 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003823 err_code |= ERR_ALERT | ERR_FATAL;
3824 goto out;
3825 }
3826 out:
3827 return err_code;
3828}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003829
3830static int
3831cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3832{
3833 struct spoe_message *msg;
3834 struct spoe_arg *arg;
3835 const char *err;
3836 char *errmsg = NULL;
3837 int err_code = 0;
3838
3839 if ((cfg_scope == NULL && curengine != NULL) ||
3840 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003841 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003842 goto out;
3843
3844 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3845 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003846 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3847 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003848 err_code |= ERR_ALERT | ERR_ABORT;
3849 goto out;
3850 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003851 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3852 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003853 goto out;
3854 }
3855
3856 err = invalid_char(args[1]);
3857 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003858 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3859 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003860 err_code |= ERR_ALERT | ERR_ABORT;
3861 goto out;
3862 }
3863
3864 list_for_each_entry(msg, &curmsgs, list) {
3865 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003866 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3867 " name as another one declared at %s:%d.\n",
3868 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003869 err_code |= ERR_ALERT | ERR_FATAL;
3870 goto out;
3871 }
3872 }
3873
3874 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003875 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003876 err_code |= ERR_ALERT | ERR_ABORT;
3877 goto out;
3878 }
3879
3880 curmsg->id = strdup(args[1]);
3881 curmsg->id_len = strlen(curmsg->id);
3882 curmsg->event = SPOE_EV_NONE;
3883 curmsg->conf.file = strdup(file);
3884 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003885 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003886 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003887 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003888 LIST_INIT(&curmsg->by_evt);
3889 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003890 LIST_ADDQ(&curmsgs, &curmsg->list);
3891 }
3892 else if (!strcmp(args[0], "args")) {
3893 int cur_arg = 1;
3894
3895 curproxy->conf.args.ctx = ARGC_SPOE;
3896 curproxy->conf.args.file = file;
3897 curproxy->conf.args.line = linenum;
3898 while (*args[cur_arg]) {
3899 char *delim = strchr(args[cur_arg], '=');
3900 int idx = 0;
3901
3902 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003903 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003904 err_code |= ERR_ALERT | ERR_ABORT;
3905 goto out;
3906 }
3907
3908 if (!delim) {
3909 arg->name = NULL;
3910 arg->name_len = 0;
3911 delim = args[cur_arg];
3912 }
3913 else {
3914 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3915 arg->name_len = delim - args[cur_arg];
3916 delim++;
3917 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003918 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3919 &idx, file, linenum, &errmsg,
3920 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003921 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003922 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003923 err_code |= ERR_ALERT | ERR_FATAL;
3924 free(arg->name);
3925 free(arg);
3926 goto out;
3927 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003928 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003929 LIST_ADDQ(&curmsg->args, &arg->list);
3930 cur_arg++;
3931 }
3932 curproxy->conf.args.file = NULL;
3933 curproxy->conf.args.line = 0;
3934 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003935 else if (!strcmp(args[0], "acl")) {
3936 err = invalid_char(args[1]);
3937 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003938 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3939 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003940 err_code |= ERR_ALERT | ERR_FATAL;
3941 goto out;
3942 }
3943 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003944 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3945 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003946 err_code |= ERR_ALERT | ERR_FATAL;
3947 goto out;
3948 }
3949 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003950 else if (!strcmp(args[0], "event")) {
3951 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003952 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003953 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003954 goto out;
3955 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003956 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3957 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003958
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003959 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3960 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3961 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3962 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3963
3964 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3965 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3966 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3967 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3968 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3969 curmsg->event = SPOE_EV_ON_TCP_RSP;
3970
3971 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3972 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3973 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3974 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3975 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3976 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3977 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08003978 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003979 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003980 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 goto out;
3982 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003983
3984 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3985 struct acl_cond *cond;
3986
3987 cond = build_acl_cond(file, linenum, &curmsg->acls,
3988 curproxy, (const char **)args+2,
3989 &errmsg);
3990 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003991 ha_alert("parsing [%s:%d] : error detected while "
3992 "parsing an 'event %s' condition : %s.\n",
3993 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003994 err_code |= ERR_ALERT | ERR_FATAL;
3995 goto out;
3996 }
3997 curmsg->cond = cond;
3998 }
3999 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004000 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4001 "or 'unless' followed by a condition but found '%s'.\n",
4002 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004003 err_code |= ERR_ALERT | ERR_FATAL;
4004 goto out;
4005 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004006 }
4007 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004008 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4009 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004010 err_code |= ERR_ALERT | ERR_FATAL;
4011 goto out;
4012 }
4013 out:
4014 free(errmsg);
4015 return err_code;
4016}
4017
4018/* Return -1 on error, else 0 */
4019static int
4020parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4021 struct flt_conf *fconf, char **err, void *private)
4022{
4023 struct list backup_sections;
4024 struct spoe_config *conf;
4025 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004026 struct spoe_group *grp, *grpback;
4027 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004028 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004029 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004030 char *file = NULL, *engine = NULL;
4031 int ret, pos = *cur_arg + 1;
4032
Christopher Faulet84c844e2018-03-23 14:37:14 +01004033 LIST_INIT(&curmsgs);
4034 LIST_INIT(&curgrps);
4035 LIST_INIT(&curmphs);
4036 LIST_INIT(&curgphs);
4037 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004038 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004039 curpxopts = 0;
4040 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004042 conf = calloc(1, sizeof(*conf));
4043 if (conf == NULL) {
4044 memprintf(err, "%s: out of memory", args[*cur_arg]);
4045 goto error;
4046 }
4047 conf->proxy = px;
4048
4049 while (*args[pos]) {
4050 if (!strcmp(args[pos], "config")) {
4051 if (!*args[pos+1]) {
4052 memprintf(err, "'%s' : '%s' option without value",
4053 args[*cur_arg], args[pos]);
4054 goto error;
4055 }
4056 file = args[pos+1];
4057 pos += 2;
4058 }
4059 else if (!strcmp(args[pos], "engine")) {
4060 if (!*args[pos+1]) {
4061 memprintf(err, "'%s' : '%s' option without value",
4062 args[*cur_arg], args[pos]);
4063 goto error;
4064 }
4065 engine = args[pos+1];
4066 pos += 2;
4067 }
4068 else {
4069 memprintf(err, "unknown keyword '%s'", args[pos]);
4070 goto error;
4071 }
4072 }
4073 if (file == NULL) {
4074 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4075 goto error;
4076 }
4077
4078 /* backup sections and register SPOE sections */
4079 LIST_INIT(&backup_sections);
4080 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004081 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4082 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004083 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004084
4085 /* Parse SPOE filter configuration file */
4086 curengine = engine;
4087 curproxy = px;
4088 curagent = NULL;
4089 curmsg = NULL;
4090 ret = readcfgfile(file);
4091 curproxy = NULL;
4092
4093 /* unregister SPOE sections and restore previous sections */
4094 cfg_unregister_sections();
4095 cfg_restore_sections(&backup_sections);
4096
4097 if (ret == -1) {
4098 memprintf(err, "Could not open configuration file %s : %s",
4099 file, strerror(errno));
4100 goto error;
4101 }
4102 if (ret & (ERR_ABORT|ERR_FATAL)) {
4103 memprintf(err, "Error(s) found in configuration file %s", file);
4104 goto error;
4105 }
4106
4107 /* Check SPOE agent */
4108 if (curagent == NULL) {
4109 memprintf(err, "No SPOE agent found in file %s", file);
4110 goto error;
4111 }
4112 if (curagent->b.name == NULL) {
4113 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4114 curagent->id, curagent->conf.file, curagent->conf.line);
4115 goto error;
4116 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004117 if (curagent->timeout.hello == TICK_ETERNITY ||
4118 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004119 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004120 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4121 " | While not properly invalid, you will certainly encounter various problems\n"
4122 " | with such a configuration. To fix this, please ensure that all following\n"
4123 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4124 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004125 }
4126 if (curagent->var_pfx == NULL) {
4127 char *tmp = curagent->id;
4128
4129 while (*tmp) {
4130 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4131 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4132 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4133 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4134 goto error;
4135 }
4136 tmp++;
4137 }
4138 curagent->var_pfx = strdup(curagent->id);
4139 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004140 if (curagent->engine_id == NULL)
4141 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004142
Christopher Fauletb7426d12018-03-21 14:12:17 +01004143 if (curagent->var_on_error) {
4144 struct arg arg;
4145
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004146 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004147 curagent->var_pfx, curagent->var_on_error);
4148
4149 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004150 arg.data.str.area = trash.area;
4151 arg.data.str.data = trash.data;
Christopher Fauletb7426d12018-03-21 14:12:17 +01004152 if (!vars_check_arg(&arg, err)) {
4153 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4154 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4155 goto error;
4156 }
4157 }
4158
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004159 if (curagent->var_t_process) {
4160 struct arg arg;
4161
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004162 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004163 curagent->var_pfx, curagent->var_t_process);
4164
4165 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004166 arg.data.str.area = trash.area;
4167 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004168 if (!vars_check_arg(&arg, err)) {
4169 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4170 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4171 goto error;
4172 }
4173 }
4174
4175 if (curagent->var_t_total) {
4176 struct arg arg;
4177
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004178 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004179 curagent->var_pfx, curagent->var_t_total);
4180
4181 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004182 arg.data.str.area = trash.area;
4183 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004184 if (!vars_check_arg(&arg, err)) {
4185 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4186 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4187 goto error;
4188 }
4189 }
4190
Christopher Faulet11610f32017-09-21 10:23:10 +02004191 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004192 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4193 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004194 goto finish;
4195 }
4196
Christopher Faulet11610f32017-09-21 10:23:10 +02004197 /* Replace placeholders by the corresponding messages for the SPOE
4198 * agent */
4199 list_for_each_entry(ph, &curmphs, list) {
4200 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004201 struct spoe_arg *arg;
4202 unsigned int where;
4203
Christopher Faulet11610f32017-09-21 10:23:10 +02004204 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004205 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4206 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4207 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4208 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4209 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4210 }
4211 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4212 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4213 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004214 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4215 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004216 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004217 }
4218 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004219 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4220 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004221 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004222 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004223
4224 where = 0;
4225 switch (msg->event) {
4226 case SPOE_EV_ON_CLIENT_SESS:
4227 where |= SMP_VAL_FE_CON_ACC;
4228 break;
4229
4230 case SPOE_EV_ON_TCP_REQ_FE:
4231 where |= SMP_VAL_FE_REQ_CNT;
4232 break;
4233
4234 case SPOE_EV_ON_HTTP_REQ_FE:
4235 where |= SMP_VAL_FE_HRQ_HDR;
4236 break;
4237
4238 case SPOE_EV_ON_TCP_REQ_BE:
4239 if (px->cap & PR_CAP_FE)
4240 where |= SMP_VAL_FE_REQ_CNT;
4241 if (px->cap & PR_CAP_BE)
4242 where |= SMP_VAL_BE_REQ_CNT;
4243 break;
4244
4245 case SPOE_EV_ON_HTTP_REQ_BE:
4246 if (px->cap & PR_CAP_FE)
4247 where |= SMP_VAL_FE_HRQ_HDR;
4248 if (px->cap & PR_CAP_BE)
4249 where |= SMP_VAL_BE_HRQ_HDR;
4250 break;
4251
4252 case SPOE_EV_ON_SERVER_SESS:
4253 where |= SMP_VAL_BE_SRV_CON;
4254 break;
4255
4256 case SPOE_EV_ON_TCP_RSP:
4257 if (px->cap & PR_CAP_FE)
4258 where |= SMP_VAL_FE_RES_CNT;
4259 if (px->cap & PR_CAP_BE)
4260 where |= SMP_VAL_BE_RES_CNT;
4261 break;
4262
4263 case SPOE_EV_ON_HTTP_RSP:
4264 if (px->cap & PR_CAP_FE)
4265 where |= SMP_VAL_FE_HRS_HDR;
4266 if (px->cap & PR_CAP_BE)
4267 where |= SMP_VAL_BE_HRS_HDR;
4268 break;
4269
4270 default:
4271 break;
4272 }
4273
4274 list_for_each_entry(arg, &msg->args, list) {
4275 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004276 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004277 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004278 "none of which is available here ('%s')",
4279 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004280 sample_ckp_names(arg->expr->fetch->use),
4281 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004282 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004283 }
4284 }
4285
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004286 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004287 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4288 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004289 }
4290 }
4291 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004292 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004293 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004294 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004295 continue;
4296 }
4297
Christopher Faulet11610f32017-09-21 10:23:10 +02004298 /* Replace placeholders by the corresponding groups for the SPOE
4299 * agent */
4300 list_for_each_entry(ph, &curgphs, list) {
4301 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4302 if (!strcmp(grp->id, ph->id)) {
4303 grp->agent = curagent;
4304 LIST_DEL(&grp->list);
4305 LIST_ADDQ(&curagent->groups, &grp->list);
4306 goto next_aph;
4307 }
4308 }
4309 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4310 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4311 goto error;
4312 next_aph:
4313 continue;
4314 }
4315
4316 /* Replace placeholders by the corresponding message for each SPOE
4317 * group of the SPOE agent */
4318 list_for_each_entry(grp, &curagent->groups, list) {
4319 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4320 list_for_each_entry(msg, &curmsgs, list) {
4321 if (!strcmp(msg->id, ph->id)) {
4322 if (msg->group != NULL) {
4323 memprintf(err, "SPOE message '%s' already belongs to "
4324 "the SPOE group '%s' declare at %s:%d",
4325 msg->id, msg->group->id,
4326 msg->group->conf.file,
4327 msg->group->conf.line);
4328 goto error;
4329 }
4330
4331 /* Scope for arguments are not checked for now. We will check
4332 * them only if a rule use the corresponding SPOE group. */
4333 msg->agent = curagent;
4334 msg->group = grp;
4335 LIST_DEL(&ph->list);
4336 LIST_ADDQ(&grp->messages, &msg->by_grp);
4337 goto next_mph_grp;
4338 }
4339 }
4340 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4341 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4342 goto error;
4343 next_mph_grp:
4344 continue;
4345 }
4346 }
4347
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004348 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004349 /* move curmsgs to the agent message list */
4350 curmsgs.n->p = &curagent->messages;
4351 curmsgs.p->n = &curagent->messages;
4352 curagent->messages = curmsgs;
4353 LIST_INIT(&curmsgs);
4354
Christopher Faulet7ee86672017-09-19 11:08:28 +02004355 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004356 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004357
4358 /* Start agent's proxy initialization here. It will be finished during
4359 * the filter init. */
4360 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4361 init_new_proxy(&conf->agent_fe);
4362 conf->agent_fe.id = conf->agent->id;
4363 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004364 conf->agent_fe.options |= curpxopts;
4365 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004366
4367 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4368 LIST_DEL(&logsrv->list);
4369 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4370 }
4371
Christopher Faulet11610f32017-09-21 10:23:10 +02004372 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4373 LIST_DEL(&ph->list);
4374 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004375 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004376 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4377 LIST_DEL(&ph->list);
4378 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004379 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004380 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4381 struct arg arg;
4382
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004383 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004384 curagent->var_pfx, vph->name);
4385
4386 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004387 arg.data.str.area = trash.area;
4388 arg.data.str.data = trash.data;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004389 if (!vars_check_arg(&arg, err)) {
4390 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4391 curagent->id, curagent->var_pfx, vph->name, *err);
4392 goto error;
4393 }
4394
4395 LIST_DEL(&vph->list);
4396 free(vph->name);
4397 free(vph);
4398 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004399 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4400 LIST_DEL(&grp->list);
4401 spoe_release_group(grp);
4402 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004403 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004404 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004405 fconf->ops = &spoe_ops;
4406 fconf->conf = conf;
4407 return 0;
4408
4409 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004410 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004411 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4412 LIST_DEL(&ph->list);
4413 spoe_release_placeholder(ph);
4414 }
4415 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4416 LIST_DEL(&ph->list);
4417 spoe_release_placeholder(ph);
4418 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004419 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4420 LIST_DEL(&vph->list);
4421 free(vph->name);
4422 free(vph);
4423 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004424 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4425 LIST_DEL(&grp->list);
4426 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004427 }
4428 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4429 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004430 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004431 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004432 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4433 LIST_DEL(&logsrv->list);
4434 free(logsrv);
4435 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004436 free(conf);
4437 return -1;
4438}
4439
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004440/* Send message of a SPOE group. This is the action_ptr callback of a rule
4441 * associated to a "send-spoe-group" action.
4442 *
4443 * It returns ACT_RET_CONT is processing is finished without error, it returns
4444 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4445 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004446static enum act_return
4447spoe_send_group(struct act_rule *rule, struct proxy *px,
4448 struct session *sess, struct stream *s, int flags)
4449{
4450 struct filter *filter;
4451 struct spoe_agent *agent = NULL;
4452 struct spoe_group *group = NULL;
4453 struct spoe_context *ctx = NULL;
4454 int ret, dir;
4455
4456 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4457 if (filter->config == rule->arg.act.p[0]) {
4458 agent = rule->arg.act.p[2];
4459 group = rule->arg.act.p[3];
4460 ctx = filter->ctx;
4461 break;
4462 }
4463 }
4464 if (agent == NULL || group == NULL || ctx == NULL)
4465 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004466 if (ctx->state == SPOE_CTX_ST_NONE)
4467 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004468
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004469 switch (rule->from) {
4470 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4471 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4472 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4473 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4474 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4475 default:
4476 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4477 " - internal error while execute spoe-send-group\n",
4478 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4479 __FUNCTION__, s);
4480 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4481 agent->id);
4482 return ACT_RET_CONT;
4483 }
4484
4485 ret = spoe_process_group(s, ctx, group, dir);
4486 if (ret == 1)
4487 return ACT_RET_CONT;
4488 else if (ret == 0) {
4489 if (flags & ACT_FLAG_FINAL) {
4490 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4491 " - failed to process group '%s': interrupted by caller\n",
4492 (int)now.tv_sec, (int)now.tv_usec,
4493 agent->id, __FUNCTION__, s, group->id);
4494 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004495 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004496 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004497 return ACT_RET_CONT;
4498 }
4499 return ACT_RET_YIELD;
4500 }
4501 else
4502 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004503}
4504
4505/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4506 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4507 * action should be:
4508 *
4509 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4510 *
4511 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4512 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4513 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4514 * group.
4515 *
4516 * The function returns 1 in success case, otherwise, it returns 0 and err is
4517 * filled.
4518 */
4519static int
4520check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4521{
4522 struct flt_conf *fconf;
4523 struct spoe_config *conf;
4524 struct spoe_agent *agent = NULL;
4525 struct spoe_group *group;
4526 struct spoe_message *msg;
4527 char *engine_id = rule->arg.act.p[0];
4528 char *group_id = rule->arg.act.p[1];
4529 unsigned int where = 0;
4530
4531 switch (rule->from) {
4532 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4533 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4534 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4535 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4536 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4537 default:
4538 memprintf(err,
4539 "internal error, unexpected rule->from=%d, please report this bug!",
4540 rule->from);
4541 goto error;
4542 }
4543
4544 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4545 * <px> */
4546 list_for_each_entry(fconf, &px->filter_configs, list) {
4547 conf = fconf->conf;
4548
4549 /* This is not an SPOE filter */
4550 if (fconf->id != spoe_filter_id)
4551 continue;
4552
4553 /* This is the good engine */
4554 if (!strcmp(conf->id, engine_id)) {
4555 agent = conf->agent;
4556 break;
4557 }
4558 }
4559 if (agent == NULL) {
4560 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4561 engine_id, group_id);
4562 goto error;
4563 }
4564
4565 /* Try to find the right group */
4566 list_for_each_entry(group, &agent->groups, list) {
4567 /* This is the good group */
4568 if (!strcmp(group->id, group_id))
4569 break;
4570 }
4571 if (&group->list == &agent->groups) {
4572 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4573 group_id, engine_id);
4574 goto error;
4575 }
4576
4577 /* Ok, we found the group, we need to check messages and their
4578 * arguments */
4579 list_for_each_entry(msg, &group->messages, by_grp) {
4580 struct spoe_arg *arg;
4581
4582 list_for_each_entry(arg, &msg->args, list) {
4583 if (!(arg->expr->fetch->val & where)) {
4584 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4585 "some args extract information from '%s',"
4586 "none of which is available here ('%s')",
4587 msg->id, group->id, msg->conf.file, msg->conf.line,
4588 sample_ckp_names(arg->expr->fetch->use),
4589 sample_ckp_names(where));
4590 goto error;
4591 }
4592 }
4593 }
4594
4595 free(engine_id);
4596 free(group_id);
4597 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4598 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4599 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4600 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4601 return 1;
4602
4603 error:
4604 free(engine_id);
4605 free(group_id);
4606 return 0;
4607}
4608
4609/* Parse 'send-spoe-group' action following the format:
4610 *
4611 * ... send-spoe-group <engine-id> <group-id>
4612 *
4613 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4614 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4615 * ids are saved and used later, when the rule will be checked.
4616 */
4617static enum act_parse_ret
4618parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4619 struct act_rule *rule, char **err)
4620{
4621 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4622 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4623 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4624 return ACT_RET_PRS_ERR;
4625 }
4626 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4627 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4628
4629 (*orig_arg) += 2;
4630
4631 rule->action = ACT_CUSTOM;
4632 rule->action_ptr = spoe_send_group;
4633 rule->check_ptr = check_send_spoe_group;
4634 return ACT_RET_PRS_OK;
4635}
4636
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004637
4638/* Declare the filter parser for "spoe" keyword */
4639static struct flt_kw_list flt_kws = { "SPOE", { }, {
4640 { "spoe", parse_spoe_flt, NULL },
4641 { NULL, NULL, NULL },
4642 }
4643};
4644
Willy Tarreau0108d902018-11-25 19:14:37 +01004645INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4646
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004647/* Delcate the action parser for "spoe-action" keyword */
4648static struct action_kw_list tcp_req_action_kws = { { }, {
4649 { "send-spoe-group", parse_send_spoe_group },
4650 { /* END */ },
4651 }
4652};
Willy Tarreau0108d902018-11-25 19:14:37 +01004653
4654INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4655
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004656static struct action_kw_list tcp_res_action_kws = { { }, {
4657 { "send-spoe-group", parse_send_spoe_group },
4658 { /* END */ },
4659 }
4660};
Willy Tarreau0108d902018-11-25 19:14:37 +01004661
4662INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4663
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004664static struct action_kw_list http_req_action_kws = { { }, {
4665 { "send-spoe-group", parse_send_spoe_group },
4666 { /* END */ },
4667 }
4668};
Willy Tarreau0108d902018-11-25 19:14:37 +01004669
4670INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4671
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004672static struct action_kw_list http_res_action_kws = { { }, {
4673 { "send-spoe-group", parse_send_spoe_group },
4674 { /* END */ },
4675 }
4676};
4677
Willy Tarreau0108d902018-11-25 19:14:37 +01004678INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);