blob: 9418ff20c7532752d5991e552a7204c5e89d9077 [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 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 if (agent->rt) {
188 for (i = 0; i < global.nbthread; ++i)
189 HA_SPIN_DESTROY(&agent->rt[i].lock);
190 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200191 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200192 free(agent);
193}
194
195static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100196 [SPOE_FRM_ERR_NONE] = "normal",
197 [SPOE_FRM_ERR_IO] = "I/O error",
198 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
199 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
200 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
201 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
202 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
203 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
204 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
205 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
206 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
207 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100208 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100209 [SPOE_FRM_ERR_RES] = "resource allocation error",
210 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200211};
212
213static const char *spoe_event_str[SPOE_EV_EVENTS] = {
214 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
215 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
216 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
217 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
218 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
219
220 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
221 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
222 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
223};
224
225
226#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
227
228static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100229 [SPOE_CTX_ST_NONE] = "NONE",
230 [SPOE_CTX_ST_READY] = "READY",
231 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
232 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
233 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
234 [SPOE_CTX_ST_DONE] = "DONE",
235 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200236};
237
238static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100239 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
240 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
241 [SPOE_APPCTX_ST_IDLE] = "IDLE",
242 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
243 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
244 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
245 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
246 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
247 [SPOE_APPCTX_ST_EXIT] = "EXIT",
248 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200249};
250
251#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252
Christopher Faulet8ef75252017-02-20 22:56:03 +0100253/* Used to generates a unique id for an engine. On success, it returns a
254 * allocated string. So it is the caller's reponsibility to release it. If the
255 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100256static char *
257generate_pseudo_uuid()
258{
259 static int init = 0;
260
261 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
262 const char uuid_chr[] = "0123456789ABCDEF-";
263 char *uuid;
264 int i;
265
266 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
267 return NULL;
268
269 if (!init) {
270 srand(now_ms);
271 init = 1;
272 }
273
274 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
275 int r = rand () % 16;
276
277 switch (uuid_fmt[i]) {
278 case 'x' : uuid[i] = uuid_chr[r]; break;
279 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
280 default : uuid[i] = uuid_fmt[i]; break;
281 }
282 }
283 return uuid;
284}
285
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100286
287static inline void
288spoe_update_stat_time(struct timeval *tv, long *t)
289{
290 if (*t == -1)
291 *t = tv_ms_elapsed(tv, &now);
292 else
293 *t += tv_ms_elapsed(tv, &now);
294 tv_zero(tv);
295}
296
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200297/********************************************************************
298 * Functions that encode/decode SPOE frames
299 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200300/* Helper to get static string length, excluding the terminating null byte */
301#define SLEN(str) (sizeof(str)-1)
302
303/* Predefined key used in HELLO/DISCONNECT frames */
304#define SUPPORTED_VERSIONS_KEY "supported-versions"
305#define VERSION_KEY "version"
306#define MAX_FRAME_SIZE_KEY "max-frame-size"
307#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100308#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100309#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310#define STATUS_CODE_KEY "status-code"
311#define MSG_KEY "message"
312
313struct spoe_version {
314 char *str;
315 int min;
316 int max;
317};
318
319/* All supported versions */
320static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200321 /* 1.0 is now unsupported because of a bug about frame's flags*/
322 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200323 {NULL, 0, 0}
324};
325
326/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200327#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200328
Christopher Faulet8ef75252017-02-20 22:56:03 +0100329/* Convert a string to a SPOE version value. The string must follow the format
330 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
331 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100335 const char *p, *end;
336 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 p = str;
339 end = str+len;
340 maj = min = 0;
341 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200342
Christopher Faulet8ef75252017-02-20 22:56:03 +0100343 /* skip leading spaces */
344 while (p < end && isspace(*p))
345 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347 /* parse Major number, until the '.' */
348 while (*p != '.') {
349 if (p >= end || *p < '0' || *p > '9')
350 goto out;
351 maj *= 10;
352 maj += (*p - '0');
353 p++;
354 }
355
356 /* check Major version */
357 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200358 goto out;
359
Christopher Faulet8ef75252017-02-20 22:56:03 +0100360 p++; /* skip the '.' */
361 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
362 goto out;
363
364 /* Parse Minor number */
365 while (p < end) {
366 if (*p < '0' || *p > '9')
367 break;
368 min *= 10;
369 min += (*p - '0');
370 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200371 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100372
373 /* check Minor number */
374 if (min > 999)
375 goto out;
376
377 /* skip trailing spaces */
378 while (p < end && isspace(*p))
379 p++;
380 if (p != end)
381 goto out;
382
383 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384 out:
385 return vsn;
386}
387
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800389 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100390 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200391static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200393{
Willy Tarreau83061a82018-07-13 11:56:34 +0200394 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100395 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100396 char *p, *end;
397 unsigned int flags = SPOE_FRM_FL_FIN;
398 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200399
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 p = frame;
401 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200402
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 /* Set Frame type */
404 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200405
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100406 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200407 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 memcpy(p, (char *)&flags, 4);
409 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200410
411 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
414 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
415 * and "capabilities" */
416
417 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100418 sz = SLEN(SUPPORTED_VERSIONS_KEY);
419 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
420 goto too_big;
421
422 *p++ = SPOE_DATA_T_STR;
423 sz = SLEN(SUPPORTED_VERSIONS_VAL);
424 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
425 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200426
427 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100428 sz = SLEN(MAX_FRAME_SIZE_KEY);
429 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
430 goto too_big;
431
432 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200433 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200435
436 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100437 sz = SLEN(CAPABILITIES_KEY);
438 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
439 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200440
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100442 chk = get_trash_chunk();
443 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200444 memcpy(chk->area, "pipelining", 10);
445 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100446 }
447 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200448 if (chk->data) chk->area[chk->data++] = ',';
449 memcpy(chk->area+chk->data, "async", 5);
450 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100451 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100452 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200453 if (chk->data) chk->area[chk->data++] = ',';
454 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100455 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100456 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200457 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458 goto too_big;
459
460 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100461 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 sz = SLEN(ENGINE_ID_KEY);
463 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
464 goto too_big;
465
466 *p++ = SPOE_DATA_T_STR;
467 sz = strlen(agent->engine_id);
468 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
469 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100470 }
471
Christopher Faulet8ef75252017-02-20 22:56:03 +0100472 return (p - frame);
473
474 too_big:
475 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
476 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477}
478
Christopher Faulet8ef75252017-02-20 22:56:03 +0100479/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
480 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
481 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100485 const char *reason;
486 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100487 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 p = frame;
491 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200492
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 /* Set Frame type */
494 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100496 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200497 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100498 memcpy(p, (char *)&flags, 4);
499 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200500
501 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100502 *p++ = 0; *p++ = 0;
503
504 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
505 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200506
507 /* There are 2 mandatory items: "status-code" and "message" */
508
509 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100510 sz = SLEN(STATUS_CODE_KEY);
511 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
512 goto too_big;
513
514 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200515 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517
518 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100519 sz = SLEN(MSG_KEY);
520 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
521 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 /*Get the message corresponding to the status code */
524 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
525
526 *p++ = SPOE_DATA_T_STR;
527 sz = strlen(reason);
528 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
529 goto too_big;
530
531 return (p - frame);
532
533 too_big:
534 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
535 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200536}
537
Christopher Faulet8ef75252017-02-20 22:56:03 +0100538/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
539 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
540 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200541static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100542spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100543 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200544{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100545 char *p, *end;
546 unsigned int stream_id, frame_id;
547 unsigned int flags = SPOE_FRM_FL_FIN;
548 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200549
Christopher Faulet8ef75252017-02-20 22:56:03 +0100550 p = frame;
551 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200552
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100553 stream_id = ctx->stream_id;
554 frame_id = ctx->frame_id;
555
556 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
557 /* The fragmentation is not supported by the applet */
558 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
559 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
560 return -1;
561 }
562 flags = ctx->frag_ctx.flags;
563 }
564
565 /* Set Frame type */
566 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
567
568 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200569 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100570 memcpy(p, (char *)&flags, 4);
571 p += 4;
572
573 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200574 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100575 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200576 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100577 goto too_big;
578
579 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200580 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100581 if (p + sz >= end)
582 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200583 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100584 p += sz;
585
586 return (p - frame);
587
588 too_big:
589 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
590 return 0;
591}
592
593/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
594 * returns the number of encoded bytes in the frame on success, 0 if an encoding
595 * error occurred and -1 if a fatal error occurred. */
596static int
597spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
598 char *frame, size_t size)
599{
600 char *p, *end;
601 unsigned int stream_id, frame_id;
602 unsigned int flags;
603 size_t sz;
604
605 p = frame;
606 end = frame+size;
607
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 /* <ctx> is null when the stream has aborted the processing of a
609 * fragmented frame. In this case, we must notify the corresponding
610 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100611 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100613 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
614 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
615 }
616 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100617 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100618 stream_id = ctx->stream_id;
619 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100620 }
621
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100623 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100624
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100625 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200626 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627 memcpy(p, (char *)&flags, 4);
628 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200629
630 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200631 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200633 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200635
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100636 if (ctx == NULL)
637 goto end;
638
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200640 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641 if (p + sz >= end)
642 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200643 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100645
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100646 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 return (p - frame);
648
649 too_big:
650 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
651 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200652}
653
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654/* Decode and process the HELLO frame sent by an agent. It returns the number of
655 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
656 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200657static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659{
Christopher Faulet305c6072017-02-23 16:17:53 +0100660 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
661 char *p, *end;
662 int vsn, max_frame_size;
663 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664
665 p = frame;
666 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667
668 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
670 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100672 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200673
Christopher Faulet8ef75252017-02-20 22:56:03 +0100674 if (size < 7 /* TYPE + METADATA */) {
675 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
676 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677 }
678
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100679 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100680 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200681 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200683
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100684 /* Fragmentation is not supported for HELLO frame */
685 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100687 return -1;
688 }
689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 if (*p != 0 || *(p+1) != 0) {
692 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
693 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696
697 /* There are 3 mandatory items: "version", "max-frame-size" and
698 * "capabilities" */
699
700 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100701 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 while (p < end) {
703 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200704 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100705 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200706
707 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708 ret = spoe_decode_buffer(&p, end, &str, &sz);
709 if (ret == -1 || !sz) {
710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
711 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714 /* Check "version" K/V item */
715 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 int i, type = *p++;
717
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100719 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
720 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
721 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
724 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
725 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 }
727
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100730 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200731 return -1;
732 }
733 for (i = 0; supported_versions[i].str != NULL; ++i) {
734 if (vsn >= supported_versions[i].min &&
735 vsn <= supported_versions[i].max)
736 break;
737 }
738 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200740 return -1;
741 }
742 }
743 /* Check "max-frame-size" K/V item */
744 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200746
747 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200748 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
749 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
750 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
751 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
753 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200754 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200755 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
757 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200758 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 if (sz < MIN_FRAME_SIZE ||
760 sz > SPOE_APPCTX(appctx)->max_frame_size) {
761 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200762 return -1;
763 }
764 max_frame_size = sz;
765 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766 /* Check "capabilities" K/V item */
767 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100768 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769
770 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100771 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
772 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
773 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
776 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
777 return 0;
778 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779
Christopher Faulet8ef75252017-02-20 22:56:03 +0100780 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100781 char *delim;
782
783 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100784 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100785
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
787 str += 10; sz -= 10;
788 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789 flags |= SPOE_APPCTX_FL_PIPELINING;
790 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100791 else if (sz >= 5 && !strncmp(str, "async", 5)) {
792 str += 5; sz -= 5;
793 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100794 flags |= SPOE_APPCTX_FL_ASYNC;
795 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100796 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
797 str += 13; sz -= 13;
798 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100799 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
800 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100801
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 /* Get the next comma or break */
803 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100804 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100805 delim++;
806 sz -= (delim - str);
807 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100808 }
809 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810 else {
811 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100812 if (spoe_skip_data(&p, end) == -1) {
813 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
814 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816 }
817 }
818
819 /* Final checks */
820 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822 return -1;
823 }
824 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826 return -1;
827 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100828 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
829 flags &= ~SPOE_APPCTX_FL_PIPELINING;
830 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
831 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832
Christopher Faulet42bfa462017-01-04 14:14:19 +0100833 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
834 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
835 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836
837 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838}
839
840/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
841 * bytes on success, 0 if the frame can be ignored and -1 if an error
842 * occurred. */
843static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100847 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848
849 p = frame;
850 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200851
852 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
854 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200857
Christopher Faulet8ef75252017-02-20 22:56:03 +0100858 if (size < 7 /* TYPE + METADATA */) {
859 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
860 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200861 }
862
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100863 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200865 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200867
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100868 /* Fragmentation is not supported for DISCONNECT frame */
869 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100871 return -1;
872 }
873
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100875 if (*p != 0 || *(p+1) != 0) {
876 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
877 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200880
881 /* There are 2 mandatory items: "status-code" and "message" */
882
883 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100884 while (p < end) {
885 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200886 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888
889 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100890 ret = spoe_decode_buffer(&p, end, &str, &sz);
891 if (ret == -1 || !sz) {
892 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
893 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200894 }
895
896 /* Check "status-code" K/V item */
897 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899
900 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200901 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
902 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
903 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
904 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
906 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200908 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
910 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200911 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100912 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
914
915 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 else if (!memcmp(str, MSG_KEY, sz)) {
917 int type = *p++;
918
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
921 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
922 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924 ret = spoe_decode_buffer(&p, end, &str, &sz);
925 if (ret == -1 || sz > 255) {
926 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
927 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100929#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
930 SPOE_APPCTX(appctx)->reason = str;
931 SPOE_APPCTX(appctx)->rlen = sz;
932#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200933 }
934 else {
935 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 if (spoe_skip_data(&p, end) == -1) {
937 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
938 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200940 }
941 }
942
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944}
945
946
Christopher Fauleta1cda022016-12-21 08:58:06 +0100947/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948 * success, 0 if the frame can be ignored and -1 if an error occurred. */
949static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100951 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100953 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100955 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100957 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958
959 p = frame;
960 end = frame + size;
961 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200962
963 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200968
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 if (size < 7 /* TYPE + METADATA */) {
970 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
971 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200972 }
973
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100974 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200976 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200978
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100979 /* Fragmentation is not supported for now */
980 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100981 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100982 return -1;
983 }
984
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200985 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200986 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100987 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100988 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100989 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200990 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100991 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200992 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100993 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100994
Christopher Faulet8ef75252017-02-20 22:56:03 +0100995 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100996 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200997 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100998 if ((*ctx)->stream_id == (unsigned int)stream_id &&
999 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001000 goto found;
1001 }
1002 }
1003 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001004 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1005 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001006 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001007 goto found;
1008 }
1009 }
1010
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001011 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1012 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1013 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1014
1015 /* ABRT bit is set for an unfinished fragmented frame */
1016 if (flags & SPOE_FRM_FL_ABRT) {
1017 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001018 (*ctx)->state = SPOE_CTX_ST_ERROR;
1019 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1020 /* Ignore the payload */
1021 goto end;
1022 }
1023 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1024 /* For now, we ignore the ack */
1025 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1026 return 0;
1027 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001028
Christopher Fauleta1cda022016-12-21 08:58:06 +01001029 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001030 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1031 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001032 " - stream-id=%u - frame-id=%u\n",
1033 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1034 __FUNCTION__, appctx,
1035 (unsigned int)stream_id, (unsigned int)frame_id);
1036
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001037 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001038 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1039 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001040 return 0;
1041
1042 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1044 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001045 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001046 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001047 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001048
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001049 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001051 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1052 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001053 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001054
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001055 /* Transfer the buffer ownership to the SPOE context */
1056 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001057 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001058
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059 (*ctx)->state = SPOE_CTX_ST_DONE;
1060
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001061 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001062 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 " - ACK frame received"
1064 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001065 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001066 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1067 (*ctx)->frame_id, flags);
1068 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001069}
1070
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071/* This function is used in cfgparse.c and declared in proto/checks.h. It
1072 * prepare the request to send to agents during a healthcheck. It returns 0 on
1073 * success and -1 if an error occurred. */
1074int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001076{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001077 struct appctx appctx;
1078 struct spoe_appctx spoe_appctx;
1079 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1080 size_t sz;
1081 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082
Christopher Faulet42bfa462017-01-04 14:14:19 +01001083 memset(&appctx, 0, sizeof(appctx));
1084 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001085 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001086
1087 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001088 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1091 end = frame + MAX_FRAME_SIZE;
1092
1093 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1094 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097
Christopher Faulet8ef75252017-02-20 22:56:03 +01001098 /* Add "healthcheck" K/V item */
1099 sz = SLEN(HEALTHCHECK_KEY);
1100 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1101 return -1;
1102 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001103
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104 *len = frame - buf;
1105 sz = htonl(*len - 4);
1106 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107
Christopher Faulet8ef75252017-02-20 22:56:03 +01001108 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001110 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111 return 0;
1112}
1113
1114/* This function is used in checks.c and declared in proto/checks.h. It decode
1115 * the response received from an agent during a healthcheck. It returns 0 on
1116 * success and -1 if an error occurred. */
1117int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001119{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001120 struct appctx appctx;
1121 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001122
Christopher Faulet42bfa462017-01-04 14:14:19 +01001123 memset(&appctx, 0, sizeof(appctx));
1124 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001125
Christopher Faulet42bfa462017-01-04 14:14:19 +01001126 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001127 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001128
Christopher Faulet8ef75252017-02-20 22:56:03 +01001129 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1130 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001131 goto error;
1132 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1134 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001135
1136 return 0;
1137
1138 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1140 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1141 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001142 return -1;
1143}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001144
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1146 * the frame can be ignored, 1 to retry later, and the frame legnth on
1147 * success. */
1148static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001149spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150{
1151 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001152 int ret;
1153 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154
Christopher Faulet8ef75252017-02-20 22:56:03 +01001155 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1156 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001157 netint = htonl(framesz);
1158 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001159 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001160 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001161 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001162 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001163 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164 }
1165 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166 return -1; /* error */
1167 }
1168 return framesz;
1169}
1170
1171/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1172 * when the frame can be ignored, 1 to retry later and the frame length on
1173 * success. */
1174static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001176{
1177 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001178 int ret;
1179 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180
Willy Tarreau06d80a92017-10-19 14:32:15 +02001181 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 if (ret > 0) {
1183 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001184 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001185 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001186 return -1;
1187 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001188 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001189 }
1190 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001191 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001192 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001193 }
1194 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001195 return -1; /* error */
1196 }
1197 return framesz;
1198}
1199
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001200/********************************************************************
1201 * Functions that manage the SPOE applet
1202 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001203static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001204spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001205{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001206 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001207 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001208 appctx_wakeup(appctx);
1209 return 1;
1210}
1211
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001212/* Callback function that catches applet timeouts. If a timeout occurred, we set
1213 * <appctx->st1> flag and the SPOE applet is woken up. */
1214static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001215spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001217 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001218
1219 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1220 if (tick_is_expired(task->expire, now_ms)) {
1221 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001222 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001223 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225 return task;
1226}
1227
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001228/* Callback function that releases a SPOE applet. This happens when the
1229 * connection with the agent is closed. */
1230static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001231spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001232{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001233 struct stream_interface *si = appctx->owner;
1234 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1235 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237
1238 if (spoe_appctx == NULL)
1239 return;
1240
1241 appctx->ctx.spoe.ptr = NULL;
1242 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001243
1244 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1245 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1246 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001247
Christopher Faulet8ef75252017-02-20 22:56:03 +01001248 /* Remove applet from the list of running applets */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001249 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001250 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001251 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1252 LIST_DEL(&spoe_appctx->list);
1253 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001254 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001255 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001256
Christopher Faulet8ef75252017-02-20 22:56:03 +01001257 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001258 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001259 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1260 eb32_delete(&spoe_appctx->node);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001261 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001262 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001263
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1266 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001267
1268 si_shutw(si);
1269 si_shutr(si);
1270 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001271 }
1272
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273 /* Destroy the task attached to this applet */
1274 if (spoe_appctx->task) {
1275 task_delete(spoe_appctx->task);
1276 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001277 }
1278
Christopher Faulet8ef75252017-02-20 22:56:03 +01001279 /* Notify all waiting streams */
1280 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001281 LIST_DEL(&ctx->list);
1282 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001283 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001284 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001285 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001286 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001287 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288 }
1289
Christopher Faulet8ef75252017-02-20 22:56:03 +01001290 /* If the applet was processing a fragmented frame, notify the
1291 * corresponding stream. */
1292 if (spoe_appctx->frag_ctx.ctx) {
1293 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001294 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001295 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001296 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001297 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1298 }
1299
Christopher Faulet8ef75252017-02-20 22:56:03 +01001300 /* Release allocated memory */
1301 spoe_release_buffer(&spoe_appctx->buffer,
1302 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001303 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001304
Christopher Faulet24289f22017-09-25 14:48:02 +02001305 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001306 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001307
Christopher Faulet8ef75252017-02-20 22:56:03 +01001308 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001309 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 LIST_DEL(&ctx->list);
1311 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001312 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001313 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001315 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001317 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001318 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 LIST_DEL(&ctx->list);
1320 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001321 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001322 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001323 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001324 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001325 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1326 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001327
1328 end:
1329 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001330 agent->rt[tid].frame_size = agent->max_frame_size;
1331 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1332 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001333}
1334
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001335static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001336spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001338 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001339 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001340 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001342
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343 if (si->state <= SI_ST_CON) {
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001344 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1346 goto stop;
1347 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001348 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001349 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001350 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001351 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001352
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1355 " - Connection timed out\n",
1356 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1357 __FUNCTION__, appctx);
1358 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001359 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001360 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001361
Christopher Faulet42bfa462017-01-04 14:14:19 +01001362 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001363 SPOE_APPCTX(appctx)->task->expire =
1364 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001365
Christopher Faulet8ef75252017-02-20 22:56:03 +01001366 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1367 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001368 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001369 ret = spoe_prepare_hahello_frame(appctx, frame,
1370 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001371 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001372 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373
1374 switch (ret) {
1375 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001376 case 0: /* ignore => an error, cannot be ignored */
1377 goto exit;
1378
1379 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380 goto stop;
1381
Christopher Faulet8ef75252017-02-20 22:56:03 +01001382 default:
1383 /* HELLO frame successfully sent, now wait for the
1384 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001385 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1386 goto next;
1387 }
1388
1389 next:
1390 return 0;
1391 stop:
1392 return 1;
1393 exit:
1394 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1395 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001396}
1397
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001399spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001400{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001401 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001402 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001403 char *frame;
1404 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001405
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406
Christopher Fauletb067b062017-01-04 16:39:11 +01001407 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001408 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001409 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001410 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001411
Christopher Fauleta1cda022016-12-21 08:58:06 +01001412 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001413 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1414 " - Connection timed out\n",
1415 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1416 __FUNCTION__, appctx);
1417 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418 goto exit;
1419 }
1420
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001421 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422 ret = spoe_recv_frame(appctx, frame,
1423 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001424 if (ret > 1) {
1425 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1426 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1427 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001428 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001429 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001430 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001431 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001432
Christopher Fauleta1cda022016-12-21 08:58:06 +01001433 switch (ret) {
1434 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1437 goto next;
1438
1439 case 1: /* retry later */
1440 goto stop;
1441
1442 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001443 /* HELLO handshake is finished, set the idle timeout and
1444 * add the applet in the list of running applets. */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001445 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001446 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001447 SPOE_APPCTX(appctx)->node.key = 0;
1448 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001449
1450 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001451 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001452 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001453 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001454
Christopher Fauleta1cda022016-12-21 08:58:06 +01001455 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001456 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001457 if (trash.data)
1458 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001459
1460 SPOE_APPCTX(appctx)->task->expire =
1461 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001462 return 0;
1463 stop:
1464 return 1;
1465 exit:
1466 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1467 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001468}
1469
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001470
Christopher Fauleta1cda022016-12-21 08:58:06 +01001471static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001472spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001473{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001474 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1475 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001476 char *frame, *buf;
1477 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001478
Christopher Faulet8ef75252017-02-20 22:56:03 +01001479 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1480 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001481 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001482
1483 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1484 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1485 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1486 SPOE_APPCTX(appctx)->max_frame_size);
1487 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001488 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001489 *skip = 1;
1490 ret = 1;
1491 goto end;
1492 }
1493 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001494 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001495 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1496 SPOE_APPCTX(appctx)->max_frame_size);
1497
1498 }
1499
Christopher Faulet8ef75252017-02-20 22:56:03 +01001500 if (ret > 1)
1501 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001502
Christopher Faulet8ef75252017-02-20 22:56:03 +01001503 switch (ret) {
1504 case -1: /* error */
1505 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1506 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001507
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 case 0: /* ignore */
1509 if (ctx == NULL)
1510 goto abort_frag_frame;
1511
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001512 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001513 LIST_DEL(&ctx->list);
1514 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001515 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001516 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001517 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001518 ctx->state = SPOE_CTX_ST_ERROR;
1519 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1520 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001521 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001522 break;
1523
1524 case 1: /* retry */
1525 *skip = 1;
1526 break;
1527
1528 default:
1529 if (ctx == NULL)
1530 goto abort_frag_frame;
1531
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001532 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 LIST_DEL(&ctx->list);
1534 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001535 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001536 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001537 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001538 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1539 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1540 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001541 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001542 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001543 }
1544 goto end;
1545
1546 frag_frame_sent:
1547 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001548 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1550 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1551 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001552 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1553 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1554 goto end;
1555
1556 no_frag_frame_sent:
1557 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1558 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001559 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001560 }
1561 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1562 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1563 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1564 }
1565 else {
1566 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001567 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001568 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1569 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001570 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001571 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001572 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1573 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1574 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001575 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001576
Christopher Faulet8ef75252017-02-20 22:56:03 +01001577 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1578 goto end;
1579
1580 abort_frag_frame:
1581 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1582 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1583 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1584 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1585 goto end;
1586
1587 end:
1588 return ret;
1589}
1590
1591static int
1592spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1593{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001594 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001595 struct spoe_context *ctx = NULL;
1596 char *frame;
1597 int ret;
1598
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001599 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001600 ret = spoe_recv_frame(appctx, frame,
1601 SPOE_APPCTX(appctx)->max_frame_size);
1602 if (ret > 1) {
1603 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1604 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001605 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001606 goto end;
1607 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001608 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001609 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1610 }
1611 switch (ret) {
1612 case -1: /* error */
1613 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1614 break;
1615
1616 case 0: /* ignore */
1617 break;
1618
1619 case 1: /* retry */
1620 *skip = 1;
1621 break;
1622
1623 default:
1624 LIST_DEL(&ctx->list);
1625 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001626 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001627 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1628 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001629 if (ctx->spoe_appctx) {
1630 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001631 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001632 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001633 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1634 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1635 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1636 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1637 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1638 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1639 }
1640 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1641 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001642 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1643 break;
1644 }
1645
1646 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001647 if (trash.data)
1648 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001649 end:
1650 return ret;
1651}
1652
1653static int
1654spoe_handle_processing_appctx(struct appctx *appctx)
1655{
1656 struct stream_interface *si = appctx->owner;
1657 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001658 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001659
1660 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1661 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1662 goto exit;
1663 }
1664
1665 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1666 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1667 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1668 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1669 goto next;
1670 }
1671
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001672 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001673 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001674 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1676 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001677 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001678
Christopher Faulet8f82b202018-01-24 16:23:03 +01001679 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1680 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001681
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 /* receiving_frame loop */
1683 while (!skip_receiving) {
1684 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1685 switch (ret) {
1686 case -1: /* error */
1687 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001688
Christopher Faulet8f82b202018-01-24 16:23:03 +01001689 case 0: /* ignore */
1690 active_r = 1;
1691 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001692
Christopher Faulet8f82b202018-01-24 16:23:03 +01001693 case 1: /* retry */
1694 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001695
Christopher Faulet8f82b202018-01-24 16:23:03 +01001696 default:
1697 active_r = 1;
1698 break;
1699 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001700 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001701
Christopher Faulet8f82b202018-01-24 16:23:03 +01001702 /* send_frame loop */
1703 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1704 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1705 switch (ret) {
1706 case -1: /* error */
1707 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001708
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001710 if (SPOE_APPCTX(appctx)->node.key)
1711 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001712 active_s++;
1713 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714
Christopher Faulet8f82b202018-01-24 16:23:03 +01001715 case 1: /* retry */
1716 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717
Christopher Faulet8f82b202018-01-24 16:23:03 +01001718 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001719 if (SPOE_APPCTX(appctx)->node.key)
1720 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001721 active_s++;
1722 break;
1723 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001724 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001725
Christopher Faulet8f82b202018-01-24 16:23:03 +01001726 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001727 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1728 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1729 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001730
Christopher Faulet8f82b202018-01-24 16:23:03 +01001731 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001732 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001733 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001734 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001735 }
1736 return 1;
1737
Christopher Faulet8f82b202018-01-24 16:23:03 +01001738 next:
1739 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1740 return 0;
1741
Christopher Fauleta1cda022016-12-21 08:58:06 +01001742 exit:
1743 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1744 return 0;
1745}
1746
1747static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001748spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001749{
1750 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001751 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001752 char *frame, *buf;
1753 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001754
Christopher Fauleta1cda022016-12-21 08:58:06 +01001755 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1756 goto exit;
1757
1758 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1759 goto exit;
1760
Christopher Faulet8ef75252017-02-20 22:56:03 +01001761 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1762 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001763 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1765 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001767 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001768
1769 switch (ret) {
1770 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772 goto exit;
1773
1774 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001775 goto stop;
1776
1777 default:
1778 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1779 " - disconnected by HAProxy (%d): %s\n",
1780 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001781 __FUNCTION__, appctx,
1782 SPOE_APPCTX(appctx)->status_code,
1783 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784
1785 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1786 goto next;
1787 }
1788
1789 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 SPOE_APPCTX(appctx)->task->expire =
1791 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001792 return 0;
1793 stop:
1794 return 1;
1795 exit:
1796 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1797 return 0;
1798}
1799
1800static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802{
1803 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001804 char *frame;
1805 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806
Christopher Fauletb067b062017-01-04 16:39:11 +01001807 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001810 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811
Christopher Fauletb067b062017-01-04 16:39:11 +01001812 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001815 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001817 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001818 ret = spoe_recv_frame(appctx, frame,
1819 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001821 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001822 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 }
1824
1825 switch (ret) {
1826 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001827 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1828 " - error on frame (%s)\n",
1829 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001830 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001832 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 goto exit;
1834
1835 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001836 goto next;
1837
1838 case 1: /* retry */
1839 goto stop;
1840
1841 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001843 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001844 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001845 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001846 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1847 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848 goto exit;
1849 }
1850
1851 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 if (trash.data)
1854 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001855
Christopher Fauleta1cda022016-12-21 08:58:06 +01001856 return 0;
1857 stop:
1858 return 1;
1859 exit:
1860 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1861 return 0;
1862}
1863
1864/* I/O Handler processing messages exchanged with the agent */
1865static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001868 struct stream_interface *si = appctx->owner;
1869 struct spoe_agent *agent;
1870
1871 if (SPOE_APPCTX(appctx) == NULL)
1872 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873
Christopher Faulet8ef75252017-02-20 22:56:03 +01001874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1875 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001876
Christopher Fauleta1cda022016-12-21 08:58:06 +01001877 switchstate:
1878 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1879 " - appctx-state=%s\n",
1880 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1881 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1882
1883 switch (appctx->st0) {
1884 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001885 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001886 goto out;
1887 goto switchstate;
1888
1889 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001890 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001891 goto out;
1892 goto switchstate;
1893
1894 case SPOE_APPCTX_ST_IDLE:
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001895 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001896 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001897 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001898 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001899 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001900 SPOE_APPCTX(appctx)->task->expire =
1901 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001902 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001903 goto switchstate;
1904 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001905 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1906 /* fall through */
1907
1908 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001909 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1910 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001911 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001912 goto out;
1913 goto switchstate;
1914
1915 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001916 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 goto out;
1918 goto switchstate;
1919
1920 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001921 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001922 goto out;
1923 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001924
1925 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001926 appctx->st0 = SPOE_APPCTX_ST_END;
1927 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1928
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001929 si_shutw(si);
1930 si_shutr(si);
1931 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932 /* fall through */
1933
1934 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001935 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001937 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001938 if (stopping)
1939 spoe_wakeup_appctx(appctx);
1940
Christopher Faulet42bfa462017-01-04 14:14:19 +01001941 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1942 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001943 si_oc(si)->flags |= CF_READ_DONTWAIT;
1944 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945}
1946
1947struct applet spoe_applet = {
1948 .obj_type = OBJ_TYPE_APPLET,
1949 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001950 .fct = spoe_handle_appctx,
1951 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001952};
1953
1954/* Create a SPOE applet. On success, the created applet is returned, else
1955 * NULL. */
1956static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001957spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001958{
1959 struct appctx *appctx;
1960 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001962
Emeric Brun1138fd02017-06-19 12:38:55 +02001963 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001964 goto out_error;
1965
Willy Tarreaubafbe012017-11-24 17:34:44 +01001966 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001967 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001968 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001969 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001970
Christopher Faulet42bfa462017-01-04 14:14:19 +01001971 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001972 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001973 goto out_free_spoe_appctx;
1974
1975 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001976 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001977 SPOE_APPCTX(appctx)->task->context = appctx;
1978 SPOE_APPCTX(appctx)->agent = conf->agent;
1979 SPOE_APPCTX(appctx)->version = 0;
1980 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1981 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001982 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001983 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001984 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001985
1986 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1987 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001988 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001989
1990 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1991 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992
Willy Tarreau5820a362016-12-22 15:59:02 +01001993 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 if (!sess)
1995 goto out_free_spoe;
1996
Willy Tarreau87787ac2017-08-28 16:22:54 +02001997 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998 goto out_free_sess;
1999
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002000 stream_set_backend(strm, conf->agent->b.be);
2001
2002 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002003 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002004 appctx_wakeup(appctx);
2005
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006 strm->do_log = NULL;
2007 strm->res.flags |= CF_READ_DONTWAIT;
2008
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002009 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002010 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002011 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002012 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002013
Emeric Brunc60def82017-09-27 14:59:38 +02002014 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002015 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002016 return appctx;
2017
2018 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002019 out_free_sess:
2020 session_free(sess);
2021 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002022 task_free(SPOE_APPCTX(appctx)->task);
2023 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002024 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002025 out_free_appctx:
2026 appctx_free(appctx);
2027 out_error:
2028 return NULL;
2029}
2030
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002031static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002032spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033{
2034 struct spoe_config *conf = FLT_CONF(ctx->filter);
2035 struct spoe_agent *agent = conf->agent;
2036 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002037 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038
Christopher Fauleta1cda022016-12-21 08:58:06 +01002039 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002040 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002041 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002043
2044 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002045 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002046 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2047 ctx->strm);
2048
Christopher Fauleta1cda022016-12-21 08:58:06 +01002049 /* Do not try to create a new applet if there is no server up for the
2050 * agent's backend. */
2051 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2052 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2053 " - cannot create SPOE appctx: no server up\n",
2054 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2055 __FUNCTION__, ctx->strm);
2056 goto end;
2057 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002058
Christopher Fauleta1cda022016-12-21 08:58:06 +01002059 /* Do not try to create a new applet if we have reached the maximum of
2060 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002061 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002062 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002063 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2064 " - cannot create SPOE appctx: max CPS reached\n",
2065 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2066 __FUNCTION__, ctx->strm);
2067 goto end;
2068 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002069 }
2070
Christopher Faulet8ef75252017-02-20 22:56:03 +01002071 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002072 if (appctx == NULL) {
2073 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2074 " - failed to create SPOE appctx\n",
2075 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2076 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002077 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002078 "SPOE: [%s] failed to create SPOE applet\n",
2079 agent->id);
2080
Christopher Fauleta1cda022016-12-21 08:58:06 +01002081 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002082 }
2083
Christopher Fauleta1cda022016-12-21 08:58:06 +01002084 /* Increase the per-process number of cumulated connections */
2085 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002086 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002087
Christopher Fauleta1cda022016-12-21 08:58:06 +01002088 end:
2089 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002090 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002091 ctx->status_code = SPOE_CTX_ERR_RES;
2092 return -1;
2093 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002094
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002095 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002096 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002097 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002098 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2099 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002100
2101 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002102 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002103 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002104 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002105 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002106 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002107
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002108 /* Finally try to wakeup an IDLE applet. */
2109 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2110 struct eb32_node *node;
2111
2112 node = eb32_first(&agent->rt[tid].idle_applets);
2113 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2114 if (node && spoe_appctx) {
2115 eb32_delete(&spoe_appctx->node);
2116 spoe_appctx->node.key++;
2117 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2118 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002119 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002120 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002121 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002122}
2123
2124/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002125 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002126 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002127/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2128 * fragmented_content. If the next message can be processed, it returns 0. If
2129 * the message is too big, it returns -1.*/
2130static int
2131spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2132 struct spoe_message *msg, int dir,
2133 char **buf, char *end)
2134{
2135 struct sample *smp;
2136 struct spoe_arg *arg;
2137 int ret;
2138
2139 if (msg->cond) {
2140 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2141 ret = acl_pass(ret);
2142 if (msg->cond->pol == ACL_COND_UNLESS)
2143 ret = !ret;
2144
2145 /* the rule does not match */
2146 if (!ret)
2147 goto next;
2148 }
2149
2150 /* Resume encoding of a SPOE argument */
2151 if (ctx->frag_ctx.curarg != NULL) {
2152 arg = ctx->frag_ctx.curarg;
2153 goto encode_argument;
2154 }
2155
2156 if (ctx->frag_ctx.curoff != UINT_MAX)
2157 goto encode_msg_payload;
2158
2159 /* Check if there is enough space for the message name and the
2160 * number of arguments. It implies <msg->id_len> is encoded on 2
2161 * bytes, at most (< 2288). */
2162 if (*buf + 2 + msg->id_len + 1 > end)
2163 goto too_big;
2164
2165 /* Encode the message name */
2166 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2167 goto too_big;
2168
2169 /* Set the number of arguments for this message */
2170 **buf = msg->nargs;
2171 (*buf)++;
2172
2173 ctx->frag_ctx.curoff = 0;
2174 encode_msg_payload:
2175
2176 /* Loop on arguments */
2177 list_for_each_entry(arg, &msg->args, list) {
2178 ctx->frag_ctx.curarg = arg;
2179 ctx->frag_ctx.curoff = UINT_MAX;
2180
2181 encode_argument:
2182 if (ctx->frag_ctx.curoff != UINT_MAX)
2183 goto encode_arg_value;
2184
Joseph Herlantf7f60312018-11-15 13:46:49 -08002185 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002186 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2187 goto too_big;
2188
2189 ctx->frag_ctx.curoff = 0;
2190 encode_arg_value:
2191
Joseph Herlantf7f60312018-11-15 13:46:49 -08002192 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002193 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2194 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2195 if (ret == -1 || ctx->frag_ctx.curoff)
2196 goto too_big;
2197 }
2198
2199 next:
2200 return 0;
2201
2202 too_big:
2203 return -1;
2204}
2205
Christopher Fauletc718b822017-09-21 16:50:56 +02002206/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2207 * handle fragmented content. On success it returns 1. If an error occurred, -1
2208 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2209 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002210static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002211spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002212 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002213{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002214 struct spoe_config *conf = FLT_CONF(ctx->filter);
2215 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002216 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002217 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002218
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002219 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002220 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002221
Christopher Fauletc718b822017-09-21 16:50:56 +02002222 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2223 /* Resume encoding of a SPOE message */
2224 if (ctx->frag_ctx.curmsg != NULL) {
2225 msg = ctx->frag_ctx.curmsg;
2226 goto encode_evt_message;
2227 }
2228
2229 list_for_each_entry(msg, messages, by_evt) {
2230 ctx->frag_ctx.curmsg = msg;
2231 ctx->frag_ctx.curarg = NULL;
2232 ctx->frag_ctx.curoff = UINT_MAX;
2233
2234 encode_evt_message:
2235 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2236 goto too_big;
2237 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002238 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002239 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2240 /* Resume encoding of a SPOE message */
2241 if (ctx->frag_ctx.curmsg != NULL) {
2242 msg = ctx->frag_ctx.curmsg;
2243 goto encode_grp_message;
2244 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245
Christopher Fauletc718b822017-09-21 16:50:56 +02002246 list_for_each_entry(msg, messages, by_grp) {
2247 ctx->frag_ctx.curmsg = msg;
2248 ctx->frag_ctx.curarg = NULL;
2249 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002250
Christopher Fauletc718b822017-09-21 16:50:56 +02002251 encode_grp_message:
2252 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2253 goto too_big;
2254 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002255 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002256 else
2257 goto skip;
2258
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002259
Christopher Faulet57583e42017-09-04 15:41:09 +02002260 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002261 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002262 goto skip;
2263
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002264 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002265 " - encode %s messages - spoe_appctx=%p"
2266 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267 (int)now.tv_sec, (int)now.tv_usec,
2268 agent->id, __FUNCTION__, s,
2269 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002270 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002271 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002272
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002273 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002274 ctx->frag_ctx.curmsg = NULL;
2275 ctx->frag_ctx.curarg = NULL;
2276 ctx->frag_ctx.curoff = 0;
2277 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002278
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002279 return 1;
2280
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002281 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002282 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2283 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2284 return -1;
2285 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286
2287 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002288 " - encode fragmented messages - spoe_appctx=%p"
2289 " - curmsg=%p - curarg=%p - curoff=%u"
2290 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002291 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002292 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002293 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002294 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002295
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002296 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002297 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2298 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2299 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002300
2301 skip:
2302 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2303 " - skip the frame because nothing has been encoded\n",
2304 (int)now.tv_sec, (int)now.tv_usec,
2305 agent->id, __FUNCTION__, s);
2306 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002307}
2308
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002309
2310/***************************************************************************
2311 * Functions that handle SPOE actions
2312 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002313/* Helper function to set a variable */
2314static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002315spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002316 struct sample *smp)
2317{
2318 struct spoe_config *conf = FLT_CONF(ctx->filter);
2319 struct spoe_agent *agent = conf->agent;
2320 char varname[64];
2321
2322 memset(varname, 0, sizeof(varname));
2323 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2324 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002325 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2326 vars_set_by_name(varname, len, smp);
2327 else
2328 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002329}
2330
2331/* Helper function to unset a variable */
2332static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002333spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002334 struct sample *smp)
2335{
2336 struct spoe_config *conf = FLT_CONF(ctx->filter);
2337 struct spoe_agent *agent = conf->agent;
2338 char varname[64];
2339
2340 memset(varname, 0, sizeof(varname));
2341 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2342 scope, agent->var_pfx, len, name);
2343 vars_unset_by_name_ifexist(varname, len, smp);
2344}
2345
2346
Christopher Faulet8ef75252017-02-20 22:56:03 +01002347static inline int
2348spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2349 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002350{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002351 char *str, *scope, *p = *buf;
2352 struct sample smp;
2353 uint64_t sz;
2354 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 if (p + 2 >= end)
2357 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002358
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359 /* SET-VAR requires 3 arguments */
2360 if (*p++ != 3)
2361 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002362
Christopher Faulet8ef75252017-02-20 22:56:03 +01002363 switch (*p++) {
2364 case SPOE_SCOPE_PROC: scope = "proc"; break;
2365 case SPOE_SCOPE_SESS: scope = "sess"; break;
2366 case SPOE_SCOPE_TXN : scope = "txn"; break;
2367 case SPOE_SCOPE_REQ : scope = "req"; break;
2368 case SPOE_SCOPE_RES : scope = "res"; break;
2369 default: goto skip;
2370 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2373 goto skip;
2374 memset(&smp, 0, sizeof(smp));
2375 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002376
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377 if (spoe_decode_data(&p, end, &smp) == -1)
2378 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002379
Christopher Faulet8ef75252017-02-20 22:56:03 +01002380 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2381 " - set-var '%s.%s.%.*s'\n",
2382 (int)now.tv_sec, (int)now.tv_usec,
2383 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2384 __FUNCTION__, s, scope,
2385 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2386 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 ret = (p - *buf);
2391 *buf = p;
2392 return ret;
2393 skip:
2394 return 0;
2395}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397static inline int
2398spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2399 char **buf, char *end, int dir)
2400{
2401 char *str, *scope, *p = *buf;
2402 struct sample smp;
2403 uint64_t sz;
2404 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406 if (p + 2 >= end)
2407 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002408
Christopher Faulet8ef75252017-02-20 22:56:03 +01002409 /* UNSET-VAR requires 2 arguments */
2410 if (*p++ != 2)
2411 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413 switch (*p++) {
2414 case SPOE_SCOPE_PROC: scope = "proc"; break;
2415 case SPOE_SCOPE_SESS: scope = "sess"; break;
2416 case SPOE_SCOPE_TXN : scope = "txn"; break;
2417 case SPOE_SCOPE_REQ : scope = "req"; break;
2418 case SPOE_SCOPE_RES : scope = "res"; break;
2419 default: goto skip;
2420 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2423 goto skip;
2424 memset(&smp, 0, sizeof(smp));
2425 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002426
Christopher Faulet8ef75252017-02-20 22:56:03 +01002427 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2428 " - unset-var '%s.%s.%.*s'\n",
2429 (int)now.tv_sec, (int)now.tv_usec,
2430 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2431 __FUNCTION__, s, scope,
2432 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2433 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002434
Christopher Faulet8ef75252017-02-20 22:56:03 +01002435 spoe_unset_var(ctx, scope, str, sz, &smp);
2436
2437 ret = (p - *buf);
2438 *buf = p;
2439 return ret;
2440 skip:
2441 return 0;
2442}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002443
Christopher Faulet8ef75252017-02-20 22:56:03 +01002444/* Process SPOE actions for a specific event. It returns 1 on success. If an
2445 * error occurred, 0 is returned. */
2446static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002447spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002448{
2449 char *p, *end;
2450 int ret;
2451
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002452 p = b_head(&ctx->buffer);
2453 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002454
2455 while (p < end) {
2456 enum spoe_action_type type;
2457
2458 type = *p++;
2459 switch (type) {
2460 case SPOE_ACT_T_SET_VAR:
2461 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2462 if (!ret)
2463 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002464 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002465
Christopher Faulet8ef75252017-02-20 22:56:03 +01002466 case SPOE_ACT_T_UNSET_VAR:
2467 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2468 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002469 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002470 break;
2471
2472 default:
2473 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002474 }
2475 }
2476
2477 return 1;
2478 skip:
2479 return 0;
2480}
2481
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002482/***************************************************************************
2483 * Functions that process SPOE events
2484 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002485static void
2486spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2487 struct spoe_context *ctx, int dir)
2488{
2489 if (!tv_iszero(&ctx->stats.tv_start)) {
2490 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2491 ctx->stats.t_total += ctx->stats.t_process;
2492 tv_zero(&ctx->stats.tv_request);
2493 tv_zero(&ctx->stats.tv_queue);
2494 tv_zero(&ctx->stats.tv_wait);
2495 tv_zero(&ctx->stats.tv_response);
2496 }
2497
2498 if (agent->var_t_process) {
2499 struct sample smp;
2500
2501 memset(&smp, 0, sizeof(smp));
2502 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2503 smp.data.u.sint = ctx->stats.t_process;
2504 smp.data.type = SMP_T_SINT;
2505
2506 spoe_set_var(ctx, "txn", agent->var_t_process,
2507 strlen(agent->var_t_process), &smp);
2508 }
2509
2510 if (agent->var_t_total) {
2511 struct sample smp;
2512
2513 memset(&smp, 0, sizeof(smp));
2514 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2515 smp.data.u.sint = ctx->stats.t_total;
2516 smp.data.type = SMP_T_SINT;
2517
2518 spoe_set_var(ctx, "txn", agent->var_t_total,
2519 strlen(agent->var_t_total), &smp);
2520 }
2521}
2522
2523static void
2524spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2525 struct spoe_context *ctx, int dir)
2526{
2527 if (agent->eps_max > 0)
2528 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2529
2530 if (agent->var_on_error) {
2531 struct sample smp;
2532
2533 memset(&smp, 0, sizeof(smp));
2534 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2535 smp.data.u.sint = ctx->status_code;
2536 smp.data.type = SMP_T_BOOL;
2537
2538 spoe_set_var(ctx, "txn", agent->var_on_error,
2539 strlen(agent->var_on_error), &smp);
2540 }
2541
2542 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2543 ? SPOE_CTX_ST_READY
2544 : SPOE_CTX_ST_NONE);
2545}
2546
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002547static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002548spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002549{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002550 /* If a process is already started for this SPOE context, retry
2551 * later. */
2552 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002553 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002554
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002555 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002556 ctx->stats.tv_start = now;
2557 ctx->stats.tv_request = now;
2558 ctx->stats.t_request = -1;
2559 ctx->stats.t_queue = -1;
2560 ctx->stats.t_waiting = -1;
2561 ctx->stats.t_response = -1;
2562 ctx->stats.t_process = -1;
2563
2564 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002565
Christopher Fauleta1cda022016-12-21 08:58:06 +01002566 /* Set the right flag to prevent request and response processing
2567 * in same time. */
2568 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2569 ? SPOE_CTX_FL_REQ_PROCESS
2570 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002571 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002572}
2573
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002574static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002575spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002576{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002577 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002578
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002579 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2580 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002581 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002582 if (sa) {
2583 if (sa->frag_ctx.ctx == ctx) {
2584 sa->frag_ctx.ctx = NULL;
2585 spoe_wakeup_appctx(sa->owner);
2586 }
2587 else
2588 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002589 }
2590
Christopher Fauleta1cda022016-12-21 08:58:06 +01002591 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002592 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002593 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002594
2595 /* Reset processing timer */
2596 ctx->process_exp = TICK_ETERNITY;
2597
Christopher Faulet8ef75252017-02-20 22:56:03 +01002598 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599
Christopher Fauletfce747b2018-01-24 15:59:32 +01002600 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002601 ctx->frag_ctx.curmsg = NULL;
2602 ctx->frag_ctx.curarg = NULL;
2603 ctx->frag_ctx.curoff = 0;
2604 ctx->frag_ctx.flags = 0;
2605
Christopher Fauleta1cda022016-12-21 08:58:06 +01002606 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002607 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002608 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002609 else
2610 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2611
Christopher Fauleta1cda022016-12-21 08:58:06 +01002612 LIST_DEL(&ctx->list);
2613 LIST_INIT(&ctx->list);
2614 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002615}
2616
Christopher Faulet58d03682017-09-21 16:57:24 +02002617/* Process a list of SPOE messages. First, this functions will process messages
2618 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2619 * to process corresponding actions. During all the processing, it returns 0
2620 * and it returns 1 when the processing is finished. If an error occurred, -1
2621 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002622static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002623spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2624 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002625{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002626 struct spoe_config *conf = FLT_CONF(ctx->filter);
2627 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002628 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002629
2630 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002631 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002632
2633 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2634 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002635 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002636 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002637 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002638 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002639 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002640 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002641
2642 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002643 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002644 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002645 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002646 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002647 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002648 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002649 goto skip;
2650 }
2651 }
2652
Christopher Fauletf7a30922016-11-10 15:04:51 +01002653 if (!tick_isset(ctx->process_exp)) {
2654 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2655 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2656 ctx->process_exp);
2657 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002658 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002659 if (!ret)
2660 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002662 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002663 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002664 }
2665
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002666 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002667 if (!tv_iszero(&ctx->stats.tv_request))
2668 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002669 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002670 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002671 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002672 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002673 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002674 if (!ret)
2675 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002676 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002677 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002678 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2679 }
2680
2681 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002682 if (ctx->spoe_appctx)
2683 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684 ret = 0;
2685 goto out;
2686 }
2687
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002688 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2689 ret = 0;
2690 goto out;
2691 }
2692
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002694 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002695 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002696 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002697 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002698 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002699 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700 }
2701
2702 out:
2703 return ret;
2704
Christopher Fauleta1cda022016-12-21 08:58:06 +01002705 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002706 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002707 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002708 spoe_stop_processing(agent, ctx);
2709 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002710
Christopher Fauleta1cda022016-12-21 08:58:06 +01002711 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002712 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002713 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002714 if (ctx->status_code) {
2715 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2716 spoe_handle_processing_error(s, agent, ctx, dir);
2717 ret = 1;
2718 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002719 return ret;
2720}
2721
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002722/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2723 * See spoe_process_message for details. */
2724static int
2725spoe_process_group(struct stream *s, struct spoe_context *ctx,
2726 struct spoe_group *group, int dir)
2727{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002728 struct spoe_config *conf = FLT_CONF(ctx->filter);
2729 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002730 int ret;
2731
2732 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2733 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002734 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002735 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2736 group->id);
2737
2738 if (LIST_ISEMPTY(&group->messages))
2739 return 1;
2740
2741 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002742 if (ret && ctx->stats.t_process != -1) {
2743 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002744 " - <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 +01002745 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002746 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002747 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002748 ctx->stats.t_response, ctx->stats.t_process,
2749 agent->counters.idles, agent->counters.applets,
2750 agent->counters.nb_sending, agent->counters.nb_waiting,
2751 agent->counters.nb_errors, agent->counters.nb_processed,
2752 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2753 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2754 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2755 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2756 agent->id, group->id, s->uniq_id, ctx->status_code,
2757 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2758 ctx->stats.t_response, ctx->stats.t_process,
2759 agent->counters.idles, agent->counters.applets,
2760 agent->counters.nb_sending, agent->counters.nb_waiting,
2761 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002762 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002763 return ret;
2764}
2765
Christopher Faulet58d03682017-09-21 16:57:24 +02002766/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2767 * See spoe_process_message for details. */
2768static int
2769spoe_process_event(struct stream *s, struct spoe_context *ctx,
2770 enum spoe_event ev)
2771{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002772 struct spoe_config *conf = FLT_CONF(ctx->filter);
2773 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002774 int dir, ret;
2775
2776 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002777 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002778 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002779 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2780 spoe_event_str[ev]);
2781
2782 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2783
2784 if (LIST_ISEMPTY(&(ctx->events[ev])))
2785 return 1;
2786
2787 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002788 if (ret && ctx->stats.t_process != -1) {
2789 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002790 " - <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 +01002791 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2792 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2793 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002794 ctx->stats.t_response, ctx->stats.t_process,
2795 agent->counters.idles, agent->counters.applets,
2796 agent->counters.nb_sending, agent->counters.nb_waiting,
2797 agent->counters.nb_errors, agent->counters.nb_processed,
2798 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2799 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2800 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2801 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2802 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2803 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2804 ctx->stats.t_response, ctx->stats.t_process,
2805 agent->counters.idles, agent->counters.applets,
2806 agent->counters.nb_sending, agent->counters.nb_waiting,
2807 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002808 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002809 return ret;
2810}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002811
2812/***************************************************************************
2813 * Functions that create/destroy SPOE contexts
2814 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002815static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002816spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002817{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002818 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002819 return 1;
2820
Christopher Faulet4596fb72017-01-11 14:05:19 +01002821 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002822 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002823 LIST_DEL(&buffer_wait->list);
2824 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002825 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002826 }
2827
Christopher Faulet4596fb72017-01-11 14:05:19 +01002828 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002829 return 1;
2830
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002831 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002832 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002833 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002834 return 0;
2835}
2836
2837static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002838spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002839{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002840 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002841 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002842 LIST_DEL(&buffer_wait->list);
2843 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002844 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002845 }
2846
2847 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002849 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002850 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002851 }
2852}
2853
Christopher Faulet4596fb72017-01-11 14:05:19 +01002854static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002855spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002856{
2857 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2858 return 1;
2859}
2860
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002862spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002863{
2864 struct spoe_config *conf = FLT_CONF(filter);
2865 struct spoe_context *ctx;
2866
Willy Tarreaubafbe012017-11-24 17:34:44 +01002867 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002868 if (ctx == NULL) {
2869 return NULL;
2870 }
2871 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002872 ctx->filter = filter;
2873 ctx->state = SPOE_CTX_ST_NONE;
2874 ctx->status_code = SPOE_CTX_ERR_NONE;
2875 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002876 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002877 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002878 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002879 LIST_INIT(&ctx->buffer_wait.list);
2880 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002881 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002882 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002883
Christopher Fauletf7a30922016-11-10 15:04:51 +01002884 ctx->stream_id = 0;
2885 ctx->frame_id = 1;
2886 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002887
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002888 tv_zero(&ctx->stats.tv_start);
2889 tv_zero(&ctx->stats.tv_request);
2890 tv_zero(&ctx->stats.tv_queue);
2891 tv_zero(&ctx->stats.tv_wait);
2892 tv_zero(&ctx->stats.tv_response);
2893 ctx->stats.t_request = -1;
2894 ctx->stats.t_queue = -1;
2895 ctx->stats.t_waiting = -1;
2896 ctx->stats.t_response = -1;
2897 ctx->stats.t_process = -1;
2898 ctx->stats.t_total = 0;
2899
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002900 ctx->strm = s;
2901 ctx->state = SPOE_CTX_ST_READY;
2902 filter->ctx = ctx;
2903
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002904 return ctx;
2905}
2906
2907static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002908spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002909{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002910 struct spoe_config *conf = FLT_CONF(filter);
2911 struct spoe_context *ctx = filter->ctx;
2912
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002913 if (!ctx)
2914 return;
2915
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002916 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002917 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002918 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919}
2920
2921static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002922spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002923{
2924 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002925 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002926
2927 tv_zero(&ctx->stats.tv_start);
2928 tv_zero(&ctx->stats.tv_request);
2929 tv_zero(&ctx->stats.tv_queue);
2930 tv_zero(&ctx->stats.tv_wait);
2931 tv_zero(&ctx->stats.tv_response);
2932 ctx->stats.t_request = -1;
2933 ctx->stats.t_queue = -1;
2934 ctx->stats.t_waiting = -1;
2935 ctx->stats.t_response = -1;
2936 ctx->stats.t_process = -1;
2937 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002938}
2939
2940
2941/***************************************************************************
2942 * Hooks that manage the filter lifecycle (init/check/deinit)
2943 **************************************************************************/
2944/* Signal handler: Do a soft stop, wakeup SPOE applet */
2945static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002946spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002947{
2948 struct proxy *p;
2949
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002950 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002951 while (p) {
2952 struct flt_conf *fconf;
2953
2954 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002955 struct spoe_config *conf;
2956 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002957 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002958 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002959
Christopher Faulet3b386a32017-02-23 10:17:15 +01002960 if (fconf->id != spoe_filter_id)
2961 continue;
2962
2963 conf = fconf->conf;
2964 agent = conf->agent;
2965
Christopher Faulet24289f22017-09-25 14:48:02 +02002966 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002967 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002968 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2969 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002970 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002971 }
2972 }
2973 p = p->next;
2974 }
2975}
2976
2977
2978/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2979static int
2980spoe_init(struct proxy *px, struct flt_conf *fconf)
2981{
2982 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002983
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002984 /* conf->agent_fe was already initialized during the config
2985 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002987 conf->agent_fe.cap = PR_CAP_FE;
2988 conf->agent_fe.mode = PR_MODE_TCP;
2989 conf->agent_fe.maxconn = 0;
2990 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2991 conf->agent_fe.conn_retries = CONN_RETRIES;
2992 conf->agent_fe.accept = frontend_accept;
2993 conf->agent_fe.srv = NULL;
2994 conf->agent_fe.timeout.client = TICK_ETERNITY;
2995 conf->agent_fe.default_target = &spoe_applet.obj_type;
2996 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2997
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002998 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002999 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003000 sighandler_registered = 1;
3001 }
3002
Christopher Faulet00292352019-01-09 15:05:10 +01003003 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003004 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003005}
3006
3007/* Free ressources allocated by the SPOE filter. */
3008static void
3009spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3010{
3011 struct spoe_config *conf = fconf->conf;
3012
3013 if (conf) {
3014 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003015
Christopher Faulet8ef75252017-02-20 22:56:03 +01003016 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003017 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003018 free(conf);
3019 }
3020 fconf->conf = NULL;
3021}
3022
3023/* Check configuration of a SPOE filter for a specified proxy.
3024 * Return 1 on error, else 0. */
3025static int
3026spoe_check(struct proxy *px, struct flt_conf *fconf)
3027{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003028 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003029 struct spoe_config *conf = fconf->conf;
3030 struct proxy *target;
3031
Christopher Faulet7ee86672017-09-19 11:08:28 +02003032 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3033 * are uniq */
3034 list_for_each_entry(f, &px->filter_configs, list) {
3035 struct spoe_config *c = f->conf;
3036
3037 /* This is not an SPOE filter */
3038 if (f->id != spoe_filter_id)
3039 continue;
3040 /* This is the current SPOE filter */
3041 if (f == fconf)
3042 continue;
3043
3044 /* Check engine Id. It should be uniq */
3045 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003046 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3047 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003048 return 1;
3049 }
3050 }
3051
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003052 target = proxy_be_by_name(conf->agent->b.name);
3053 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003054 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3055 " declared at %s:%d.\n",
3056 px->id, conf->agent->b.name, conf->agent->id,
3057 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003058 return 1;
3059 }
3060 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003061 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3062 " at %s:%d does not support HTTP mode.\n",
3063 px->id, target->id, conf->agent->id,
3064 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003065 return 1;
3066 }
3067
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003068 if (px->bind_proc & ~target->bind_proc) {
3069 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3070 " at %s:%d does not cover all of its processes.\n",
3071 px->id, target->id, conf->agent->id,
3072 conf->agent->conf.file, conf->agent->conf.line);
3073 return 1;
3074 }
3075
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 free(conf->agent->b.name);
3077 conf->agent->b.name = NULL;
3078 conf->agent->b.be = target;
3079 return 0;
3080}
3081
3082/**************************************************************************
3083 * Hooks attached to a stream
3084 *************************************************************************/
3085/* Called when a filter instance is created and attach to a stream. It creates
3086 * the context that will be used to process this stream. */
3087static int
3088spoe_start(struct stream *s, struct filter *filter)
3089{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003090 struct spoe_config *conf = FLT_CONF(filter);
3091 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003092 struct spoe_context *ctx;
3093
3094 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003095 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003096 __FUNCTION__, s);
3097
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003098 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003099 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3100 " - failed to create SPOE context\n",
3101 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003102 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003103 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003104 "SPOE: [%s] failed to create SPOE context\n",
3105 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003106 return 0;
3107 }
3108
Christopher Faulet11610f32017-09-21 10:23:10 +02003109 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003110 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3111
Christopher Faulet11610f32017-09-21 10:23:10 +02003112 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003113 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3114
Christopher Faulet11610f32017-09-21 10:23:10 +02003115 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003116 filter->pre_analyzers |= AN_RES_INSPECT;
3117
Christopher Faulet11610f32017-09-21 10:23:10 +02003118 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003119 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3120
Christopher Faulet11610f32017-09-21 10:23:10 +02003121 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003122 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3123
Christopher Faulet11610f32017-09-21 10:23:10 +02003124 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003125 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3126
3127 return 1;
3128}
3129
3130/* Called when a filter instance is detached from a stream. It release the
3131 * attached SPOE context. */
3132static void
3133spoe_stop(struct stream *s, struct filter *filter)
3134{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003135 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3136 (int)now.tv_sec, (int)now.tv_usec,
3137 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3138 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003139 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003140}
3141
Christopher Fauletf7a30922016-11-10 15:04:51 +01003142
3143/*
3144 * Called when the stream is woken up because of expired timer.
3145 */
3146static void
3147spoe_check_timeouts(struct stream *s, struct filter *filter)
3148{
3149 struct spoe_context *ctx = filter->ctx;
3150
Christopher Fauletac580602018-03-20 16:09:20 +01003151 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003152 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003153}
3154
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003155/* Called when we are ready to filter data on a channel */
3156static int
3157spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3158{
3159 struct spoe_context *ctx = filter->ctx;
3160 int ret = 1;
3161
3162 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3163 " - ctx-flags=0x%08x\n",
3164 (int)now.tv_sec, (int)now.tv_usec,
3165 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3166 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3167
Christopher Fauletb067b062017-01-04 16:39:11 +01003168 if (ctx->state == SPOE_CTX_ST_NONE)
3169 goto out;
3170
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003171 if (!(chn->flags & CF_ISRESP)) {
3172 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3173 chn->analysers |= AN_REQ_INSPECT_FE;
3174 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3175 chn->analysers |= AN_REQ_INSPECT_BE;
3176
3177 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3178 goto out;
3179
3180 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003181 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003182 if (!ret)
3183 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003184 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3185 }
3186 else {
3187 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3188 chn->analysers |= AN_RES_INSPECT;
3189
3190 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3191 goto out;
3192
Christopher Faulet8ef75252017-02-20 22:56:03 +01003193 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003194 if (!ret) {
3195 channel_dont_read(chn);
3196 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003197 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003198 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003199 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003200 }
3201
3202 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003203 return ret;
3204}
3205
3206/* Called before a processing happens on a given channel */
3207static int
3208spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3209 struct channel *chn, unsigned an_bit)
3210{
3211 struct spoe_context *ctx = filter->ctx;
3212 int ret = 1;
3213
3214 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3215 " - ctx-flags=0x%08x - ana=0x%08x\n",
3216 (int)now.tv_sec, (int)now.tv_usec,
3217 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3218 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3219 ctx->flags, an_bit);
3220
Christopher Fauletb067b062017-01-04 16:39:11 +01003221 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003222 goto out;
3223
3224 switch (an_bit) {
3225 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003226 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227 break;
3228 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003229 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230 break;
3231 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003232 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003233 break;
3234 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003235 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003236 break;
3237 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003238 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003239 break;
3240 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003241 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003242 break;
3243 }
3244
3245 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003246 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003247 channel_dont_read(chn);
3248 channel_dont_close(chn);
3249 }
3250 return ret;
3251}
3252
3253/* Called when the filtering on the channel ends. */
3254static int
3255spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3256{
3257 struct spoe_context *ctx = filter->ctx;
3258
3259 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3260 " - ctx-flags=0x%08x\n",
3261 (int)now.tv_sec, (int)now.tv_usec,
3262 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3263 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3264
3265 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003266 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003267 }
3268
3269 return 1;
3270}
3271
3272/********************************************************************
3273 * Functions that manage the filter initialization
3274 ********************************************************************/
3275struct flt_ops spoe_ops = {
3276 /* Manage SPOE filter, called for each filter declaration */
3277 .init = spoe_init,
3278 .deinit = spoe_deinit,
3279 .check = spoe_check,
3280
3281 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003282 .attach = spoe_start,
3283 .detach = spoe_stop,
3284 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003285
3286 /* Handle channels activity */
3287 .channel_start_analyze = spoe_start_analyze,
3288 .channel_pre_analyze = spoe_chn_pre_analyze,
3289 .channel_end_analyze = spoe_end_analyze,
3290};
3291
3292
3293static int
3294cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3295{
3296 const char *err;
3297 int i, err_code = 0;
3298
3299 if ((cfg_scope == NULL && curengine != NULL) ||
3300 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003301 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003302 goto out;
3303
3304 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3305 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003306 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3307 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003308 err_code |= ERR_ALERT | ERR_ABORT;
3309 goto out;
3310 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003311 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3312 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003313 goto out;
3314 }
3315
3316 err = invalid_char(args[1]);
3317 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003318 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3319 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003320 err_code |= ERR_ALERT | ERR_ABORT;
3321 goto out;
3322 }
3323
3324 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003325 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3326 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003327 err_code |= ERR_ALERT | ERR_ABORT;
3328 goto out;
3329 }
3330 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003331 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003332 err_code |= ERR_ALERT | ERR_ABORT;
3333 goto out;
3334 }
3335
3336 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003337
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003338 curagent->conf.file = strdup(file);
3339 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003340
3341 curagent->timeout.hello = TICK_ETERNITY;
3342 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003343 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003344
3345 curagent->engine_id = NULL;
3346 curagent->var_pfx = NULL;
3347 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003348 curagent->var_t_process = NULL;
3349 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003350 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3351 if (global.nbthread == 1)
3352 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003353 curagent->cps_max = 0;
3354 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003355 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003356 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003357
3358 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003359 LIST_INIT(&curagent->events[i]);
3360 LIST_INIT(&curagent->groups);
3361 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003362
Christopher Faulet24289f22017-09-25 14:48:02 +02003363 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003364 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003365 err_code |= ERR_ALERT | ERR_ABORT;
3366 goto out;
3367 }
3368 for (i = 0; i < global.nbthread; ++i) {
3369 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003370 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003371 LIST_INIT(&curagent->rt[i].applets);
3372 LIST_INIT(&curagent->rt[i].sending_queue);
3373 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003374 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003375 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003376 }
3377 else if (!strcmp(args[0], "use-backend")) {
3378 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003379 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3380 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 err_code |= ERR_ALERT | ERR_FATAL;
3382 goto out;
3383 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003384 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003385 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003386 free(curagent->b.name);
3387 curagent->b.name = strdup(args[1]);
3388 }
3389 else if (!strcmp(args[0], "messages")) {
3390 int cur_arg = 1;
3391 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003392 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003393
Christopher Faulet11610f32017-09-21 10:23:10 +02003394 list_for_each_entry(ph, &curmphs, list) {
3395 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003396 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3397 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 err_code |= ERR_ALERT | ERR_FATAL;
3399 goto out;
3400 }
3401 }
3402
Christopher Faulet11610f32017-09-21 10:23:10 +02003403 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003404 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405 err_code |= ERR_ALERT | ERR_ABORT;
3406 goto out;
3407 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003408 ph->id = strdup(args[cur_arg]);
3409 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 cur_arg++;
3411 }
3412 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003413 else if (!strcmp(args[0], "groups")) {
3414 int cur_arg = 1;
3415 while (*args[cur_arg]) {
3416 struct spoe_placeholder *ph = NULL;
3417
3418 list_for_each_entry(ph, &curgphs, list) {
3419 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003420 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3421 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003422 err_code |= ERR_ALERT | ERR_FATAL;
3423 goto out;
3424 }
3425 }
3426
3427 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003428 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003429 err_code |= ERR_ALERT | ERR_ABORT;
3430 goto out;
3431 }
3432 ph->id = strdup(args[cur_arg]);
3433 LIST_ADDQ(&curgphs, &ph->list);
3434 cur_arg++;
3435 }
3436 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003437 else if (!strcmp(args[0], "timeout")) {
3438 unsigned int *tv = NULL;
3439 const char *res;
3440 unsigned timeout;
3441
3442 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003443 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3444 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 err_code |= ERR_ALERT | ERR_FATAL;
3446 goto out;
3447 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003448 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3449 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450 if (!strcmp(args[1], "hello"))
3451 tv = &curagent->timeout.hello;
3452 else if (!strcmp(args[1], "idle"))
3453 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003454 else if (!strcmp(args[1], "processing"))
3455 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003456 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003457 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3458 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459 err_code |= ERR_ALERT | ERR_FATAL;
3460 goto out;
3461 }
3462 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003463 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3464 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003465 err_code |= ERR_ALERT | ERR_FATAL;
3466 goto out;
3467 }
3468 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3469 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003470 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3471 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003472 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003473 goto out;
3474 }
3475 *tv = MS_TO_TICKS(timeout);
3476 }
3477 else if (!strcmp(args[0], "option")) {
3478 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003479 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3480 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003481 err_code |= ERR_ALERT | ERR_FATAL;
3482 goto out;
3483 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003484
Christopher Faulet305c6072017-02-23 16:17:53 +01003485 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003486 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003487 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003488 if (kwm == 1)
3489 curagent->flags &= ~SPOE_FL_PIPELINING;
3490 else
3491 curagent->flags |= SPOE_FL_PIPELINING;
3492 goto out;
3493 }
3494 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003495 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003496 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003497 if (kwm == 1)
3498 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003499 else {
3500 if (global.nbthread == 1)
3501 curagent->flags |= SPOE_FL_ASYNC;
3502 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003503 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3504 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003505 err_code |= ERR_WARN;
3506 }
3507 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003508 goto out;
3509 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003510 else if (!strcmp(args[1], "send-frag-payload")) {
3511 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3512 goto out;
3513 if (kwm == 1)
3514 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3515 else
3516 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3517 goto out;
3518 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003519 else if (!strcmp(args[1], "dontlog-normal")) {
3520 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3521 goto out;
3522 if (kwm == 1)
3523 curpxopts2 &= ~PR_O2_NOLOGNORM;
3524 else
3525 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003526 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003527 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003528
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003529 /* Following options does not support negation */
3530 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003531 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3532 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003533 err_code |= ERR_ALERT | ERR_FATAL;
3534 goto out;
3535 }
3536
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003537 if (!strcmp(args[1], "var-prefix")) {
3538 char *tmp;
3539
3540 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003541 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3542 file, linenum, args[0],
3543 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003544 err_code |= ERR_ALERT | ERR_FATAL;
3545 goto out;
3546 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003547 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3548 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003549 tmp = args[2];
3550 while (*tmp) {
3551 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003552 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003553 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003554 err_code |= ERR_ALERT | ERR_FATAL;
3555 goto out;
3556 }
3557 tmp++;
3558 }
3559 curagent->var_pfx = strdup(args[2]);
3560 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003561 else if (!strcmp(args[1], "force-set-var")) {
3562 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3563 goto out;
3564 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3565 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003566 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003567 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003568 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003569 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3570 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003571 else if (!strcmp(args[1], "set-on-error")) {
3572 char *tmp;
3573
3574 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003575 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3576 file, linenum, args[0],
3577 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003578 err_code |= ERR_ALERT | ERR_FATAL;
3579 goto out;
3580 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003581 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3582 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003583 tmp = args[2];
3584 while (*tmp) {
3585 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003586 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003587 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003588 err_code |= ERR_ALERT | ERR_FATAL;
3589 goto out;
3590 }
3591 tmp++;
3592 }
3593 curagent->var_on_error = strdup(args[2]);
3594 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003595 else if (!strcmp(args[1], "set-process-time")) {
3596 char *tmp;
3597
3598 if (!*args[2]) {
3599 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3600 file, linenum, args[0],
3601 args[1]);
3602 err_code |= ERR_ALERT | ERR_FATAL;
3603 goto out;
3604 }
3605 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3606 goto out;
3607 tmp = args[2];
3608 while (*tmp) {
3609 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003610 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003611 file, linenum, args[0], args[1]);
3612 err_code |= ERR_ALERT | ERR_FATAL;
3613 goto out;
3614 }
3615 tmp++;
3616 }
3617 curagent->var_t_process = strdup(args[2]);
3618 }
3619 else if (!strcmp(args[1], "set-total-time")) {
3620 char *tmp;
3621
3622 if (!*args[2]) {
3623 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3624 file, linenum, args[0],
3625 args[1]);
3626 err_code |= ERR_ALERT | ERR_FATAL;
3627 goto out;
3628 }
3629 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3630 goto out;
3631 tmp = args[2];
3632 while (*tmp) {
3633 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003634 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003635 file, linenum, args[0], args[1]);
3636 err_code |= ERR_ALERT | ERR_FATAL;
3637 goto out;
3638 }
3639 tmp++;
3640 }
3641 curagent->var_t_total = strdup(args[2]);
3642 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003643 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003644 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3645 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003646 err_code |= ERR_ALERT | ERR_FATAL;
3647 goto out;
3648 }
Christopher Faulet48026722016-11-16 15:01:12 +01003649 }
3650 else if (!strcmp(args[0], "maxconnrate")) {
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->cps_max = atol(args[1]);
3660 }
3661 else if (!strcmp(args[0], "maxerrrate")) {
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 Faulet48026722016-11-16 15:01:12 +01003665 err_code |= ERR_ALERT | ERR_FATAL;
3666 goto out;
3667 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003668 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003669 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003670 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003671 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003672 else if (!strcmp(args[0], "max-frame-size")) {
3673 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003674 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3675 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003676 err_code |= ERR_ALERT | ERR_FATAL;
3677 goto out;
3678 }
3679 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3680 goto out;
3681 curagent->max_frame_size = atol(args[1]);
3682 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3683 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003684 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3685 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003686 err_code |= ERR_ALERT | ERR_FATAL;
3687 goto out;
3688 }
3689 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003690 else if (!strcmp(args[0], "max-waiting-frames")) {
3691 if (!*args[1]) {
3692 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3693 file, linenum, args[0]);
3694 err_code |= ERR_ALERT | ERR_FATAL;
3695 goto out;
3696 }
3697 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3698 goto out;
3699 curagent->max_fpa = atol(args[1]);
3700 if (curagent->max_fpa < 1) {
3701 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3702 file, linenum, args[0]);
3703 err_code |= ERR_ALERT | ERR_FATAL;
3704 goto out;
3705 }
3706 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003707 else if (!strcmp(args[0], "register-var-names")) {
3708 int cur_arg;
3709
3710 if (!*args[1]) {
3711 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3712 file, linenum, args[0]);
3713 err_code |= ERR_ALERT | ERR_FATAL;
3714 goto out;
3715 }
3716 cur_arg = 1;
3717 while (*args[cur_arg]) {
3718 struct spoe_var_placeholder *vph;
3719
3720 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3721 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3722 err_code |= ERR_ALERT | ERR_ABORT;
3723 goto out;
3724 }
3725 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3726 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3727 err_code |= ERR_ALERT | ERR_ABORT;
3728 goto out;
3729 }
3730 LIST_ADDQ(&curvars, &vph->list);
3731 cur_arg++;
3732 }
3733 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003734 else if (!strcmp(args[0], "log")) {
3735 char *errmsg = NULL;
3736
3737 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3738 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3739 err_code |= ERR_ALERT | ERR_FATAL;
3740 goto out;
3741 }
3742 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003743 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003744 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3745 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003746 err_code |= ERR_ALERT | ERR_FATAL;
3747 goto out;
3748 }
3749 out:
3750 return err_code;
3751}
Christopher Faulet11610f32017-09-21 10:23:10 +02003752static int
3753cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3754{
3755 struct spoe_group *grp;
3756 const char *err;
3757 int err_code = 0;
3758
3759 if ((cfg_scope == NULL && curengine != NULL) ||
3760 (cfg_scope != NULL && curengine == NULL) ||
3761 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3762 goto out;
3763
3764 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3765 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003766 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3767 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003768 err_code |= ERR_ALERT | ERR_ABORT;
3769 goto out;
3770 }
3771 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3772 err_code |= ERR_ABORT;
3773 goto out;
3774 }
3775
3776 err = invalid_char(args[1]);
3777 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003778 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3779 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003780 err_code |= ERR_ALERT | ERR_ABORT;
3781 goto out;
3782 }
3783
3784 list_for_each_entry(grp, &curgrps, list) {
3785 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003786 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3787 " name as another one declared at %s:%d.\n",
3788 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003789 err_code |= ERR_ALERT | ERR_FATAL;
3790 goto out;
3791 }
3792 }
3793
3794 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003795 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003796 err_code |= ERR_ALERT | ERR_ABORT;
3797 goto out;
3798 }
3799
3800 curgrp->id = strdup(args[1]);
3801 curgrp->conf.file = strdup(file);
3802 curgrp->conf.line = linenum;
3803 LIST_INIT(&curgrp->phs);
3804 LIST_INIT(&curgrp->messages);
3805 LIST_ADDQ(&curgrps, &curgrp->list);
3806 }
3807 else if (!strcmp(args[0], "messages")) {
3808 int cur_arg = 1;
3809 while (*args[cur_arg]) {
3810 struct spoe_placeholder *ph = NULL;
3811
3812 list_for_each_entry(ph, &curgrp->phs, list) {
3813 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003814 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3815 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003816 err_code |= ERR_ALERT | ERR_FATAL;
3817 goto out;
3818 }
3819 }
3820
3821 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003822 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003823 err_code |= ERR_ALERT | ERR_ABORT;
3824 goto out;
3825 }
3826 ph->id = strdup(args[cur_arg]);
3827 LIST_ADDQ(&curgrp->phs, &ph->list);
3828 cur_arg++;
3829 }
3830 }
3831 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003832 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3833 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003834 err_code |= ERR_ALERT | ERR_FATAL;
3835 goto out;
3836 }
3837 out:
3838 return err_code;
3839}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003840
3841static int
3842cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3843{
3844 struct spoe_message *msg;
3845 struct spoe_arg *arg;
3846 const char *err;
3847 char *errmsg = NULL;
3848 int err_code = 0;
3849
3850 if ((cfg_scope == NULL && curengine != NULL) ||
3851 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003852 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003853 goto out;
3854
3855 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3856 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003857 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3858 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003859 err_code |= ERR_ALERT | ERR_ABORT;
3860 goto out;
3861 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003862 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3863 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003864 goto out;
3865 }
3866
3867 err = invalid_char(args[1]);
3868 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003869 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3870 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003871 err_code |= ERR_ALERT | ERR_ABORT;
3872 goto out;
3873 }
3874
3875 list_for_each_entry(msg, &curmsgs, list) {
3876 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003877 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3878 " name as another one declared at %s:%d.\n",
3879 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003880 err_code |= ERR_ALERT | ERR_FATAL;
3881 goto out;
3882 }
3883 }
3884
3885 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003886 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003887 err_code |= ERR_ALERT | ERR_ABORT;
3888 goto out;
3889 }
3890
3891 curmsg->id = strdup(args[1]);
3892 curmsg->id_len = strlen(curmsg->id);
3893 curmsg->event = SPOE_EV_NONE;
3894 curmsg->conf.file = strdup(file);
3895 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003896 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003897 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003898 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003899 LIST_INIT(&curmsg->by_evt);
3900 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003901 LIST_ADDQ(&curmsgs, &curmsg->list);
3902 }
3903 else if (!strcmp(args[0], "args")) {
3904 int cur_arg = 1;
3905
3906 curproxy->conf.args.ctx = ARGC_SPOE;
3907 curproxy->conf.args.file = file;
3908 curproxy->conf.args.line = linenum;
3909 while (*args[cur_arg]) {
3910 char *delim = strchr(args[cur_arg], '=');
3911 int idx = 0;
3912
3913 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003914 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003915 err_code |= ERR_ALERT | ERR_ABORT;
3916 goto out;
3917 }
3918
3919 if (!delim) {
3920 arg->name = NULL;
3921 arg->name_len = 0;
3922 delim = args[cur_arg];
3923 }
3924 else {
3925 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3926 arg->name_len = delim - args[cur_arg];
3927 delim++;
3928 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003929 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3930 &idx, file, linenum, &errmsg,
3931 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003932 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003933 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003934 err_code |= ERR_ALERT | ERR_FATAL;
3935 free(arg->name);
3936 free(arg);
3937 goto out;
3938 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003939 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003940 LIST_ADDQ(&curmsg->args, &arg->list);
3941 cur_arg++;
3942 }
3943 curproxy->conf.args.file = NULL;
3944 curproxy->conf.args.line = 0;
3945 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003946 else if (!strcmp(args[0], "acl")) {
3947 err = invalid_char(args[1]);
3948 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003949 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3950 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003951 err_code |= ERR_ALERT | ERR_FATAL;
3952 goto out;
3953 }
3954 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003955 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3956 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003957 err_code |= ERR_ALERT | ERR_FATAL;
3958 goto out;
3959 }
3960 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003961 else if (!strcmp(args[0], "event")) {
3962 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003963 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003964 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003965 goto out;
3966 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003967 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3968 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003969
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003970 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3971 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3972 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3973 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3974
3975 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3976 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3977 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3978 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3979 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3980 curmsg->event = SPOE_EV_ON_TCP_RSP;
3981
3982 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3983 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3984 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3985 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3986 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3987 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3988 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08003989 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003990 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003991 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003992 goto out;
3993 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003994
3995 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3996 struct acl_cond *cond;
3997
3998 cond = build_acl_cond(file, linenum, &curmsg->acls,
3999 curproxy, (const char **)args+2,
4000 &errmsg);
4001 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004002 ha_alert("parsing [%s:%d] : error detected while "
4003 "parsing an 'event %s' condition : %s.\n",
4004 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004005 err_code |= ERR_ALERT | ERR_FATAL;
4006 goto out;
4007 }
4008 curmsg->cond = cond;
4009 }
4010 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004011 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4012 "or 'unless' followed by a condition but found '%s'.\n",
4013 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004014 err_code |= ERR_ALERT | ERR_FATAL;
4015 goto out;
4016 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004017 }
4018 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004019 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4020 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004021 err_code |= ERR_ALERT | ERR_FATAL;
4022 goto out;
4023 }
4024 out:
4025 free(errmsg);
4026 return err_code;
4027}
4028
4029/* Return -1 on error, else 0 */
4030static int
4031parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4032 struct flt_conf *fconf, char **err, void *private)
4033{
4034 struct list backup_sections;
4035 struct spoe_config *conf;
4036 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004037 struct spoe_group *grp, *grpback;
4038 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004039 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004040 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004041 char *file = NULL, *engine = NULL;
4042 int ret, pos = *cur_arg + 1;
4043
Christopher Faulet84c844e2018-03-23 14:37:14 +01004044 LIST_INIT(&curmsgs);
4045 LIST_INIT(&curgrps);
4046 LIST_INIT(&curmphs);
4047 LIST_INIT(&curgphs);
4048 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004049 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004050 curpxopts = 0;
4051 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004052
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004053 conf = calloc(1, sizeof(*conf));
4054 if (conf == NULL) {
4055 memprintf(err, "%s: out of memory", args[*cur_arg]);
4056 goto error;
4057 }
4058 conf->proxy = px;
4059
4060 while (*args[pos]) {
4061 if (!strcmp(args[pos], "config")) {
4062 if (!*args[pos+1]) {
4063 memprintf(err, "'%s' : '%s' option without value",
4064 args[*cur_arg], args[pos]);
4065 goto error;
4066 }
4067 file = args[pos+1];
4068 pos += 2;
4069 }
4070 else if (!strcmp(args[pos], "engine")) {
4071 if (!*args[pos+1]) {
4072 memprintf(err, "'%s' : '%s' option without value",
4073 args[*cur_arg], args[pos]);
4074 goto error;
4075 }
4076 engine = args[pos+1];
4077 pos += 2;
4078 }
4079 else {
4080 memprintf(err, "unknown keyword '%s'", args[pos]);
4081 goto error;
4082 }
4083 }
4084 if (file == NULL) {
4085 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4086 goto error;
4087 }
4088
4089 /* backup sections and register SPOE sections */
4090 LIST_INIT(&backup_sections);
4091 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004092 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4093 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004094 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004095
4096 /* Parse SPOE filter configuration file */
4097 curengine = engine;
4098 curproxy = px;
4099 curagent = NULL;
4100 curmsg = NULL;
4101 ret = readcfgfile(file);
4102 curproxy = NULL;
4103
4104 /* unregister SPOE sections and restore previous sections */
4105 cfg_unregister_sections();
4106 cfg_restore_sections(&backup_sections);
4107
4108 if (ret == -1) {
4109 memprintf(err, "Could not open configuration file %s : %s",
4110 file, strerror(errno));
4111 goto error;
4112 }
4113 if (ret & (ERR_ABORT|ERR_FATAL)) {
4114 memprintf(err, "Error(s) found in configuration file %s", file);
4115 goto error;
4116 }
4117
4118 /* Check SPOE agent */
4119 if (curagent == NULL) {
4120 memprintf(err, "No SPOE agent found in file %s", file);
4121 goto error;
4122 }
4123 if (curagent->b.name == NULL) {
4124 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4125 curagent->id, curagent->conf.file, curagent->conf.line);
4126 goto error;
4127 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004128 if (curagent->timeout.hello == TICK_ETERNITY ||
4129 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004130 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004131 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4132 " | While not properly invalid, you will certainly encounter various problems\n"
4133 " | with such a configuration. To fix this, please ensure that all following\n"
4134 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4135 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004136 }
4137 if (curagent->var_pfx == NULL) {
4138 char *tmp = curagent->id;
4139
4140 while (*tmp) {
4141 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4142 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4143 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4144 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4145 goto error;
4146 }
4147 tmp++;
4148 }
4149 curagent->var_pfx = strdup(curagent->id);
4150 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004151 if (curagent->engine_id == NULL)
4152 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004153
Christopher Fauletb7426d12018-03-21 14:12:17 +01004154 if (curagent->var_on_error) {
4155 struct arg arg;
4156
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004157 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004158 curagent->var_pfx, curagent->var_on_error);
4159
4160 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004161 arg.data.str.area = trash.area;
4162 arg.data.str.data = trash.data;
Christopher Fauletb7426d12018-03-21 14:12:17 +01004163 if (!vars_check_arg(&arg, err)) {
4164 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4165 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4166 goto error;
4167 }
4168 }
4169
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004170 if (curagent->var_t_process) {
4171 struct arg arg;
4172
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004173 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004174 curagent->var_pfx, curagent->var_t_process);
4175
4176 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004177 arg.data.str.area = trash.area;
4178 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004179 if (!vars_check_arg(&arg, err)) {
4180 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4181 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4182 goto error;
4183 }
4184 }
4185
4186 if (curagent->var_t_total) {
4187 struct arg arg;
4188
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004189 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004190 curagent->var_pfx, curagent->var_t_total);
4191
4192 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004193 arg.data.str.area = trash.area;
4194 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004195 if (!vars_check_arg(&arg, err)) {
4196 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4197 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4198 goto error;
4199 }
4200 }
4201
Christopher Faulet11610f32017-09-21 10:23:10 +02004202 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004203 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4204 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004205 goto finish;
4206 }
4207
Christopher Faulet11610f32017-09-21 10:23:10 +02004208 /* Replace placeholders by the corresponding messages for the SPOE
4209 * agent */
4210 list_for_each_entry(ph, &curmphs, list) {
4211 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004212 struct spoe_arg *arg;
4213 unsigned int where;
4214
Christopher Faulet11610f32017-09-21 10:23:10 +02004215 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004216 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4217 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4218 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4219 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4220 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4221 }
4222 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4223 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4224 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004225 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4226 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004227 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004228 }
4229 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004230 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4231 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004232 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004233 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004234
4235 where = 0;
4236 switch (msg->event) {
4237 case SPOE_EV_ON_CLIENT_SESS:
4238 where |= SMP_VAL_FE_CON_ACC;
4239 break;
4240
4241 case SPOE_EV_ON_TCP_REQ_FE:
4242 where |= SMP_VAL_FE_REQ_CNT;
4243 break;
4244
4245 case SPOE_EV_ON_HTTP_REQ_FE:
4246 where |= SMP_VAL_FE_HRQ_HDR;
4247 break;
4248
4249 case SPOE_EV_ON_TCP_REQ_BE:
4250 if (px->cap & PR_CAP_FE)
4251 where |= SMP_VAL_FE_REQ_CNT;
4252 if (px->cap & PR_CAP_BE)
4253 where |= SMP_VAL_BE_REQ_CNT;
4254 break;
4255
4256 case SPOE_EV_ON_HTTP_REQ_BE:
4257 if (px->cap & PR_CAP_FE)
4258 where |= SMP_VAL_FE_HRQ_HDR;
4259 if (px->cap & PR_CAP_BE)
4260 where |= SMP_VAL_BE_HRQ_HDR;
4261 break;
4262
4263 case SPOE_EV_ON_SERVER_SESS:
4264 where |= SMP_VAL_BE_SRV_CON;
4265 break;
4266
4267 case SPOE_EV_ON_TCP_RSP:
4268 if (px->cap & PR_CAP_FE)
4269 where |= SMP_VAL_FE_RES_CNT;
4270 if (px->cap & PR_CAP_BE)
4271 where |= SMP_VAL_BE_RES_CNT;
4272 break;
4273
4274 case SPOE_EV_ON_HTTP_RSP:
4275 if (px->cap & PR_CAP_FE)
4276 where |= SMP_VAL_FE_HRS_HDR;
4277 if (px->cap & PR_CAP_BE)
4278 where |= SMP_VAL_BE_HRS_HDR;
4279 break;
4280
4281 default:
4282 break;
4283 }
4284
4285 list_for_each_entry(arg, &msg->args, list) {
4286 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004287 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004288 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004289 "none of which is available here ('%s')",
4290 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004291 sample_ckp_names(arg->expr->fetch->use),
4292 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004293 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004294 }
4295 }
4296
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004297 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004298 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4299 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004300 }
4301 }
4302 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004303 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004304 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004305 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004306 continue;
4307 }
4308
Christopher Faulet11610f32017-09-21 10:23:10 +02004309 /* Replace placeholders by the corresponding groups for the SPOE
4310 * agent */
4311 list_for_each_entry(ph, &curgphs, list) {
4312 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4313 if (!strcmp(grp->id, ph->id)) {
4314 grp->agent = curagent;
4315 LIST_DEL(&grp->list);
4316 LIST_ADDQ(&curagent->groups, &grp->list);
4317 goto next_aph;
4318 }
4319 }
4320 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4321 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4322 goto error;
4323 next_aph:
4324 continue;
4325 }
4326
4327 /* Replace placeholders by the corresponding message for each SPOE
4328 * group of the SPOE agent */
4329 list_for_each_entry(grp, &curagent->groups, list) {
4330 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4331 list_for_each_entry(msg, &curmsgs, list) {
4332 if (!strcmp(msg->id, ph->id)) {
4333 if (msg->group != NULL) {
4334 memprintf(err, "SPOE message '%s' already belongs to "
4335 "the SPOE group '%s' declare at %s:%d",
4336 msg->id, msg->group->id,
4337 msg->group->conf.file,
4338 msg->group->conf.line);
4339 goto error;
4340 }
4341
4342 /* Scope for arguments are not checked for now. We will check
4343 * them only if a rule use the corresponding SPOE group. */
4344 msg->agent = curagent;
4345 msg->group = grp;
4346 LIST_DEL(&ph->list);
4347 LIST_ADDQ(&grp->messages, &msg->by_grp);
4348 goto next_mph_grp;
4349 }
4350 }
4351 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4352 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4353 goto error;
4354 next_mph_grp:
4355 continue;
4356 }
4357 }
4358
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004359 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004360 /* move curmsgs to the agent message list */
4361 curmsgs.n->p = &curagent->messages;
4362 curmsgs.p->n = &curagent->messages;
4363 curagent->messages = curmsgs;
4364 LIST_INIT(&curmsgs);
4365
Christopher Faulet7ee86672017-09-19 11:08:28 +02004366 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004367 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004368
4369 /* Start agent's proxy initialization here. It will be finished during
4370 * the filter init. */
4371 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4372 init_new_proxy(&conf->agent_fe);
4373 conf->agent_fe.id = conf->agent->id;
4374 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004375 conf->agent_fe.options |= curpxopts;
4376 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004377
4378 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4379 LIST_DEL(&logsrv->list);
4380 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4381 }
4382
Christopher Faulet11610f32017-09-21 10:23:10 +02004383 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4384 LIST_DEL(&ph->list);
4385 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004386 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004387 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4388 LIST_DEL(&ph->list);
4389 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004390 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004391 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4392 struct arg arg;
4393
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004394 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004395 curagent->var_pfx, vph->name);
4396
4397 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004398 arg.data.str.area = trash.area;
4399 arg.data.str.data = trash.data;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004400 if (!vars_check_arg(&arg, err)) {
4401 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4402 curagent->id, curagent->var_pfx, vph->name, *err);
4403 goto error;
4404 }
4405
4406 LIST_DEL(&vph->list);
4407 free(vph->name);
4408 free(vph);
4409 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004410 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4411 LIST_DEL(&grp->list);
4412 spoe_release_group(grp);
4413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004414 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004415 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004416 fconf->ops = &spoe_ops;
4417 fconf->conf = conf;
4418 return 0;
4419
4420 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004421 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004422 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4423 LIST_DEL(&ph->list);
4424 spoe_release_placeholder(ph);
4425 }
4426 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4427 LIST_DEL(&ph->list);
4428 spoe_release_placeholder(ph);
4429 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004430 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4431 LIST_DEL(&vph->list);
4432 free(vph->name);
4433 free(vph);
4434 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004435 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4436 LIST_DEL(&grp->list);
4437 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004438 }
4439 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4440 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004441 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004442 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004443 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4444 LIST_DEL(&logsrv->list);
4445 free(logsrv);
4446 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004447 free(conf);
4448 return -1;
4449}
4450
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004451/* Send message of a SPOE group. This is the action_ptr callback of a rule
4452 * associated to a "send-spoe-group" action.
4453 *
4454 * It returns ACT_RET_CONT is processing is finished without error, it returns
4455 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4456 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004457static enum act_return
4458spoe_send_group(struct act_rule *rule, struct proxy *px,
4459 struct session *sess, struct stream *s, int flags)
4460{
4461 struct filter *filter;
4462 struct spoe_agent *agent = NULL;
4463 struct spoe_group *group = NULL;
4464 struct spoe_context *ctx = NULL;
4465 int ret, dir;
4466
4467 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4468 if (filter->config == rule->arg.act.p[0]) {
4469 agent = rule->arg.act.p[2];
4470 group = rule->arg.act.p[3];
4471 ctx = filter->ctx;
4472 break;
4473 }
4474 }
4475 if (agent == NULL || group == NULL || ctx == NULL)
4476 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004477 if (ctx->state == SPOE_CTX_ST_NONE)
4478 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004479
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004480 switch (rule->from) {
4481 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4482 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4483 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4484 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4485 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4486 default:
4487 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4488 " - internal error while execute spoe-send-group\n",
4489 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4490 __FUNCTION__, s);
4491 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4492 agent->id);
4493 return ACT_RET_CONT;
4494 }
4495
4496 ret = spoe_process_group(s, ctx, group, dir);
4497 if (ret == 1)
4498 return ACT_RET_CONT;
4499 else if (ret == 0) {
4500 if (flags & ACT_FLAG_FINAL) {
4501 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4502 " - failed to process group '%s': interrupted by caller\n",
4503 (int)now.tv_sec, (int)now.tv_usec,
4504 agent->id, __FUNCTION__, s, group->id);
4505 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004506 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004507 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004508 return ACT_RET_CONT;
4509 }
4510 return ACT_RET_YIELD;
4511 }
4512 else
4513 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004514}
4515
4516/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4517 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4518 * action should be:
4519 *
4520 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4521 *
4522 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4523 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4524 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4525 * group.
4526 *
4527 * The function returns 1 in success case, otherwise, it returns 0 and err is
4528 * filled.
4529 */
4530static int
4531check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4532{
4533 struct flt_conf *fconf;
4534 struct spoe_config *conf;
4535 struct spoe_agent *agent = NULL;
4536 struct spoe_group *group;
4537 struct spoe_message *msg;
4538 char *engine_id = rule->arg.act.p[0];
4539 char *group_id = rule->arg.act.p[1];
4540 unsigned int where = 0;
4541
4542 switch (rule->from) {
4543 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4544 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4545 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4546 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4547 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4548 default:
4549 memprintf(err,
4550 "internal error, unexpected rule->from=%d, please report this bug!",
4551 rule->from);
4552 goto error;
4553 }
4554
4555 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4556 * <px> */
4557 list_for_each_entry(fconf, &px->filter_configs, list) {
4558 conf = fconf->conf;
4559
4560 /* This is not an SPOE filter */
4561 if (fconf->id != spoe_filter_id)
4562 continue;
4563
4564 /* This is the good engine */
4565 if (!strcmp(conf->id, engine_id)) {
4566 agent = conf->agent;
4567 break;
4568 }
4569 }
4570 if (agent == NULL) {
4571 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4572 engine_id, group_id);
4573 goto error;
4574 }
4575
4576 /* Try to find the right group */
4577 list_for_each_entry(group, &agent->groups, list) {
4578 /* This is the good group */
4579 if (!strcmp(group->id, group_id))
4580 break;
4581 }
4582 if (&group->list == &agent->groups) {
4583 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4584 group_id, engine_id);
4585 goto error;
4586 }
4587
4588 /* Ok, we found the group, we need to check messages and their
4589 * arguments */
4590 list_for_each_entry(msg, &group->messages, by_grp) {
4591 struct spoe_arg *arg;
4592
4593 list_for_each_entry(arg, &msg->args, list) {
4594 if (!(arg->expr->fetch->val & where)) {
4595 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4596 "some args extract information from '%s',"
4597 "none of which is available here ('%s')",
4598 msg->id, group->id, msg->conf.file, msg->conf.line,
4599 sample_ckp_names(arg->expr->fetch->use),
4600 sample_ckp_names(where));
4601 goto error;
4602 }
4603 }
4604 }
4605
4606 free(engine_id);
4607 free(group_id);
4608 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4609 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4610 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4611 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4612 return 1;
4613
4614 error:
4615 free(engine_id);
4616 free(group_id);
4617 return 0;
4618}
4619
4620/* Parse 'send-spoe-group' action following the format:
4621 *
4622 * ... send-spoe-group <engine-id> <group-id>
4623 *
4624 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4625 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4626 * ids are saved and used later, when the rule will be checked.
4627 */
4628static enum act_parse_ret
4629parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4630 struct act_rule *rule, char **err)
4631{
4632 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4633 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4634 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4635 return ACT_RET_PRS_ERR;
4636 }
4637 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4638 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4639
4640 (*orig_arg) += 2;
4641
4642 rule->action = ACT_CUSTOM;
4643 rule->action_ptr = spoe_send_group;
4644 rule->check_ptr = check_send_spoe_group;
4645 return ACT_RET_PRS_OK;
4646}
4647
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004648
4649/* Declare the filter parser for "spoe" keyword */
4650static struct flt_kw_list flt_kws = { "SPOE", { }, {
4651 { "spoe", parse_spoe_flt, NULL },
4652 { NULL, NULL, NULL },
4653 }
4654};
4655
Willy Tarreau0108d902018-11-25 19:14:37 +01004656INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4657
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004658/* Delcate the action parser for "spoe-action" keyword */
4659static struct action_kw_list tcp_req_action_kws = { { }, {
4660 { "send-spoe-group", parse_send_spoe_group },
4661 { /* END */ },
4662 }
4663};
Willy Tarreau0108d902018-11-25 19:14:37 +01004664
4665INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4666
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004667static struct action_kw_list tcp_res_action_kws = { { }, {
4668 { "send-spoe-group", parse_send_spoe_group },
4669 { /* END */ },
4670 }
4671};
Willy Tarreau0108d902018-11-25 19:14:37 +01004672
4673INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4674
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004675static struct action_kw_list http_req_action_kws = { { }, {
4676 { "send-spoe-group", parse_send_spoe_group },
4677 { /* END */ },
4678 }
4679};
Willy Tarreau0108d902018-11-25 19:14:37 +01004680
4681INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4682
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004683static struct action_kw_list http_res_action_kws = { { }, {
4684 { "send-spoe-group", parse_send_spoe_group },
4685 { /* END */ },
4686 }
4687};
4688
Willy Tarreau0108d902018-11-25 19:14:37 +01004689INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);