blob: 51d4f275a855b70ff5b8e17f52a1f8c1c1894c2c [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 Faulet11389012019-02-07 16:13:26 +0100828 if (!agent)
829 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
830 else {
831 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
832 flags &= ~SPOE_APPCTX_FL_PIPELINING;
833 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
834 flags &= ~SPOE_APPCTX_FL_ASYNC;
835 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836
Christopher Faulet42bfa462017-01-04 14:14:19 +0100837 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
838 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
839 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840
841 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842}
843
844/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
845 * bytes on success, 0 if the frame can be ignored and -1 if an error
846 * occurred. */
847static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100851 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852
853 p = frame;
854 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855
856 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
858 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200861
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 if (size < 7 /* TYPE + METADATA */) {
863 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
864 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865 }
866
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100867 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200869 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100872 /* Fragmentation is not supported for DISCONNECT frame */
873 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100875 return -1;
876 }
877
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100879 if (*p != 0 || *(p+1) != 0) {
880 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
881 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200882 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884
885 /* There are 2 mandatory items: "status-code" and "message" */
886
887 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100888 while (p < end) {
889 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200890 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892
893 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 ret = spoe_decode_buffer(&p, end, &str, &sz);
895 if (ret == -1 || !sz) {
896 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
897 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 }
899
900 /* Check "status-code" K/V item */
901 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903
904 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
906 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
907 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
908 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
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 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200912 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
914 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
918
919 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 else if (!memcmp(str, MSG_KEY, sz)) {
921 int type = *p++;
922
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100924 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
925 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
926 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 ret = spoe_decode_buffer(&p, end, &str, &sz);
929 if (ret == -1 || sz > 255) {
930 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
931 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
934 SPOE_APPCTX(appctx)->reason = str;
935 SPOE_APPCTX(appctx)->rlen = sz;
936#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937 }
938 else {
939 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940 if (spoe_skip_data(&p, end) == -1) {
941 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
942 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944 }
945 }
946
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948}
949
950
Christopher Fauleta1cda022016-12-21 08:58:06 +0100951/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 * success, 0 if the frame can be ignored and -1 if an error occurred. */
953static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100955 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200956{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100957 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962
963 p = frame;
964 end = frame + size;
965 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200966
967 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
969 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200972
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 if (size < 7 /* TYPE + METADATA */) {
974 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
975 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200976 }
977
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100978 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100979 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200980 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100981 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200982
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100983 /* Fragmentation is not supported for now */
984 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100986 return -1;
987 }
988
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200989 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200990 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100991 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100992 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100993 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200994 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100995 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200996 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100997 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100998
Christopher Faulet8ef75252017-02-20 22:56:03 +0100999 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +01001000 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +02001001 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001002 if ((*ctx)->stream_id == (unsigned int)stream_id &&
1003 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001004 goto found;
1005 }
1006 }
1007 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1009 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001010 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001011 goto found;
1012 }
1013 }
1014
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001015 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1016 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1017 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1018
1019 /* ABRT bit is set for an unfinished fragmented frame */
1020 if (flags & SPOE_FRM_FL_ABRT) {
1021 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001022 (*ctx)->state = SPOE_CTX_ST_ERROR;
1023 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1024 /* Ignore the payload */
1025 goto end;
1026 }
1027 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1028 /* For now, we ignore the ack */
1029 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1030 return 0;
1031 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001032
Christopher Fauleta1cda022016-12-21 08:58:06 +01001033 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001034 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1035 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036 " - stream-id=%u - frame-id=%u\n",
1037 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1038 __FUNCTION__, appctx,
1039 (unsigned int)stream_id, (unsigned int)frame_id);
1040
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001041 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001042 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1043 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044 return 0;
1045
1046 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001047 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1048 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001049 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001050 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001052
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001053 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001055 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1056 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001057 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001058
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001059 /* Transfer the buffer ownership to the SPOE context */
1060 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001061 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001062
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 (*ctx)->state = SPOE_CTX_ST_DONE;
1064
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001065 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001066 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001067 " - ACK frame received"
1068 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001069 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1071 (*ctx)->frame_id, flags);
1072 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001073}
1074
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075/* This function is used in cfgparse.c and declared in proto/checks.h. It
1076 * prepare the request to send to agents during a healthcheck. It returns 0 on
1077 * success and -1 if an error occurred. */
1078int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001081 struct appctx appctx;
1082 struct spoe_appctx spoe_appctx;
1083 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1084 size_t sz;
1085 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet42bfa462017-01-04 14:14:19 +01001087 memset(&appctx, 0, sizeof(appctx));
1088 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001090
1091 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001092 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1095 end = frame + MAX_FRAME_SIZE;
1096
1097 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1098 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001100 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001101
Christopher Faulet8ef75252017-02-20 22:56:03 +01001102 /* Add "healthcheck" K/V item */
1103 sz = SLEN(HEALTHCHECK_KEY);
1104 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1105 return -1;
1106 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107
Christopher Faulet8ef75252017-02-20 22:56:03 +01001108 *len = frame - buf;
1109 sz = htonl(*len - 4);
1110 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet8ef75252017-02-20 22:56:03 +01001112 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001113 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001114 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115 return 0;
1116}
1117
1118/* This function is used in checks.c and declared in proto/checks.h. It decode
1119 * the response received from an agent during a healthcheck. It returns 0 on
1120 * success and -1 if an error occurred. */
1121int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001123{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001124 struct appctx appctx;
1125 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126
Christopher Faulet42bfa462017-01-04 14:14:19 +01001127 memset(&appctx, 0, sizeof(appctx));
1128 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001129
Christopher Faulet42bfa462017-01-04 14:14:19 +01001130 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001131 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001132
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1134 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001135 goto error;
1136 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001137 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1138 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001139
1140 return 0;
1141
1142 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001143 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1144 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1145 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001146 return -1;
1147}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001148
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1150 * the frame can be ignored, 1 to retry later, and the frame legnth on
1151 * success. */
1152static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001153spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154{
1155 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001156 int ret;
1157 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1160 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 netint = htonl(framesz);
1162 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001163 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001165 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001166 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001168 }
1169 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170 return -1; /* error */
1171 }
1172 return framesz;
1173}
1174
1175/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1176 * when the frame can be ignored, 1 to retry later and the frame length on
1177 * success. */
1178static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001179spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180{
1181 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 int ret;
1183 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001184
Willy Tarreau06d80a92017-10-19 14:32:15 +02001185 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001186 if (ret > 0) {
1187 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001188 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001189 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001190 return -1;
1191 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001192 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001193 }
1194 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001195 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001196 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001197 }
1198 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001199 return -1; /* error */
1200 }
1201 return framesz;
1202}
1203
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001204/********************************************************************
1205 * Functions that manage the SPOE applet
1206 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001207static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001209{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001210 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001211 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001212 appctx_wakeup(appctx);
1213 return 1;
1214}
1215
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216/* Callback function that catches applet timeouts. If a timeout occurred, we set
1217 * <appctx->st1> flag and the SPOE applet is woken up. */
1218static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001219spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001221 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001222
1223 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1224 if (tick_is_expired(task->expire, now_ms)) {
1225 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001227 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001228 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001229 return task;
1230}
1231
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001232/* Callback function that releases a SPOE applet. This happens when the
1233 * connection with the agent is closed. */
1234static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001236{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 struct stream_interface *si = appctx->owner;
1238 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1239 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241
1242 if (spoe_appctx == NULL)
1243 return;
1244
1245 appctx->ctx.spoe.ptr = NULL;
1246 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001247
1248 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1249 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1250 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001251
Christopher Faulet8ef75252017-02-20 22:56:03 +01001252 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001253 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001254 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1256 LIST_DEL(&spoe_appctx->list);
1257 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001258 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001259 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001260
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001262 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001263 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1264 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001265 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001266 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001267
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001268 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1270 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001271
1272 si_shutw(si);
1273 si_shutr(si);
1274 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 }
1276
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001278 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001279
Christopher Faulet8ef75252017-02-20 22:56:03 +01001280 /* Notify all waiting streams */
1281 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001282 LIST_DEL(&ctx->list);
1283 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001284 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001285 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001286 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001287 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001289 }
1290
Christopher Faulet8ef75252017-02-20 22:56:03 +01001291 /* If the applet was processing a fragmented frame, notify the
1292 * corresponding stream. */
1293 if (spoe_appctx->frag_ctx.ctx) {
1294 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001295 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001296 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001297 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001298 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1299 }
1300
Christopher Faulet24289f22017-09-25 14:48:02 +02001301 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001302 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001303
Christopher Faulet8ef75252017-02-20 22:56:03 +01001304 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001305 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001306 LIST_DEL(&ctx->list);
1307 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001308 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001309 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001313 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001314 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001315 LIST_DEL(&ctx->list);
1316 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001317 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001318 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001319 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001320 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001321 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1322 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001323
1324 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001325 /* Release allocated memory */
1326 spoe_release_buffer(&spoe_appctx->buffer,
1327 &spoe_appctx->buffer_wait);
1328 pool_free(pool_head_spoe_appctx, spoe_appctx);
1329
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001330 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001331 agent->rt[tid].frame_size = agent->max_frame_size;
1332 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1333 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001334}
1335
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001336static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001338{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001339 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001340 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001341 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343
Christopher Fauleta1cda022016-12-21 08:58:06 +01001344 if (si->state <= SI_ST_CON) {
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001345 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1347 goto stop;
1348 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001349 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001350 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001351 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001352 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001353
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1356 " - Connection timed out\n",
1357 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1358 __FUNCTION__, appctx);
1359 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001360 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001361 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001362
Christopher Faulet42bfa462017-01-04 14:14:19 +01001363 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 SPOE_APPCTX(appctx)->task->expire =
1365 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366
Christopher Faulet8ef75252017-02-20 22:56:03 +01001367 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1368 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001369 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 ret = spoe_prepare_hahello_frame(appctx, frame,
1371 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001373 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001374
1375 switch (ret) {
1376 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001377 case 0: /* ignore => an error, cannot be ignored */
1378 goto exit;
1379
1380 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001381 goto stop;
1382
Christopher Faulet8ef75252017-02-20 22:56:03 +01001383 default:
1384 /* HELLO frame successfully sent, now wait for the
1385 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001386 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1387 goto next;
1388 }
1389
1390 next:
1391 return 0;
1392 stop:
1393 return 1;
1394 exit:
1395 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1396 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001397}
1398
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001399static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001400spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001401{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001403 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 char *frame;
1405 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001406
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001407
Christopher Fauletb067b062017-01-04 16:39:11 +01001408 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001409 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001410 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001411 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001412
Christopher Fauleta1cda022016-12-21 08:58:06 +01001413 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1415 " - Connection timed out\n",
1416 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1417 __FUNCTION__, appctx);
1418 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 goto exit;
1420 }
1421
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001422 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001423 ret = spoe_recv_frame(appctx, frame,
1424 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 if (ret > 1) {
1426 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1427 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1428 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001429 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001430 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001431 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001432 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001433
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434 switch (ret) {
1435 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001436 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1438 goto next;
1439
1440 case 1: /* retry later */
1441 goto stop;
1442
1443 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001444 /* HELLO handshake is finished, set the idle timeout and
1445 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001446 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001447 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001448 SPOE_APPCTX(appctx)->node.key = 0;
1449 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001450
1451 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001452 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001454 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001455
Christopher Fauleta1cda022016-12-21 08:58:06 +01001456 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001457 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001458 if (trash.data)
1459 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460
1461 SPOE_APPCTX(appctx)->task->expire =
1462 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001463 return 0;
1464 stop:
1465 return 1;
1466 exit:
1467 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1468 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001469}
1470
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001471
Christopher Fauleta1cda022016-12-21 08:58:06 +01001472static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001473spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001474{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1476 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001477 char *frame, *buf;
1478 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001479
Christopher Faulet8ef75252017-02-20 22:56:03 +01001480 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1481 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001482 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001483
1484 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1485 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1486 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1487 SPOE_APPCTX(appctx)->max_frame_size);
1488 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001489 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001490 *skip = 1;
1491 ret = 1;
1492 goto end;
1493 }
1494 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001495 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001496 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1497 SPOE_APPCTX(appctx)->max_frame_size);
1498
1499 }
1500
Christopher Faulet8ef75252017-02-20 22:56:03 +01001501 if (ret > 1)
1502 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001503
Christopher Faulet8ef75252017-02-20 22:56:03 +01001504 switch (ret) {
1505 case -1: /* error */
1506 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1507 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001508
Christopher Faulet8ef75252017-02-20 22:56:03 +01001509 case 0: /* ignore */
1510 if (ctx == NULL)
1511 goto abort_frag_frame;
1512
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001513 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001514 LIST_DEL(&ctx->list);
1515 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001516 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001517 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001518 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001519 ctx->state = SPOE_CTX_ST_ERROR;
1520 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1521 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001522 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523 break;
1524
1525 case 1: /* retry */
1526 *skip = 1;
1527 break;
1528
1529 default:
1530 if (ctx == NULL)
1531 goto abort_frag_frame;
1532
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001533 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 LIST_DEL(&ctx->list);
1535 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001536 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001537 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001538 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001539 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1540 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1541 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001542 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001543 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001544 }
1545 goto end;
1546
1547 frag_frame_sent:
1548 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001549 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1551 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1552 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001553 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1554 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1555 goto end;
1556
1557 no_frag_frame_sent:
1558 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1559 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001560 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001561 }
1562 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1563 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1564 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1565 }
1566 else {
1567 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001568 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001569 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1570 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001571 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001572 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001573 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1574 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1575 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001576 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001577
Christopher Faulet8ef75252017-02-20 22:56:03 +01001578 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1579 goto end;
1580
1581 abort_frag_frame:
1582 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1583 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1584 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1585 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1586 goto end;
1587
1588 end:
1589 return ret;
1590}
1591
1592static int
1593spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1594{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001595 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001596 struct spoe_context *ctx = NULL;
1597 char *frame;
1598 int ret;
1599
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001600 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001601 ret = spoe_recv_frame(appctx, frame,
1602 SPOE_APPCTX(appctx)->max_frame_size);
1603 if (ret > 1) {
1604 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1605 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001606 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001607 goto end;
1608 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001609 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1611 }
1612 switch (ret) {
1613 case -1: /* error */
1614 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1615 break;
1616
1617 case 0: /* ignore */
1618 break;
1619
1620 case 1: /* retry */
1621 *skip = 1;
1622 break;
1623
1624 default:
1625 LIST_DEL(&ctx->list);
1626 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001627 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001628 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1629 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001630 if (ctx->spoe_appctx) {
1631 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001632 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001633 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001634 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1635 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1636 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1637 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1638 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1639 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1640 }
1641 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1642 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001643 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1644 break;
1645 }
1646
1647 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001648 if (trash.data)
1649 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001650 end:
1651 return ret;
1652}
1653
1654static int
1655spoe_handle_processing_appctx(struct appctx *appctx)
1656{
1657 struct stream_interface *si = appctx->owner;
1658 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001659 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001660
1661 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1663 goto exit;
1664 }
1665
1666 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1667 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1668 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1669 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1670 goto next;
1671 }
1672
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001673 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001674 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001675 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001676 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1677 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001678 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001679
Christopher Faulet8f82b202018-01-24 16:23:03 +01001680 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1681 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001682
Christopher Faulet8f82b202018-01-24 16:23:03 +01001683 /* receiving_frame loop */
1684 while (!skip_receiving) {
1685 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1686 switch (ret) {
1687 case -1: /* error */
1688 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001689
Christopher Faulet8f82b202018-01-24 16:23:03 +01001690 case 0: /* ignore */
1691 active_r = 1;
1692 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001693
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 case 1: /* retry */
1695 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 default:
1698 active_r = 1;
1699 break;
1700 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001701 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001702
Christopher Faulet8f82b202018-01-24 16:23:03 +01001703 /* send_frame loop */
1704 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1705 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1706 switch (ret) {
1707 case -1: /* error */
1708 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001709
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001711 if (SPOE_APPCTX(appctx)->node.key)
1712 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001713 active_s++;
1714 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 case 1: /* retry */
1717 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001718
Christopher Faulet8f82b202018-01-24 16:23:03 +01001719 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001720 if (SPOE_APPCTX(appctx)->node.key)
1721 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001722 active_s++;
1723 break;
1724 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001725 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001726
Christopher Faulet8f82b202018-01-24 16:23:03 +01001727 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001728 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1729 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1730 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001731
Christopher Faulet8f82b202018-01-24 16:23:03 +01001732 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001733 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001734 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001735 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001736 }
1737 return 1;
1738
Christopher Faulet8f82b202018-01-24 16:23:03 +01001739 next:
1740 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1741 return 0;
1742
Christopher Fauleta1cda022016-12-21 08:58:06 +01001743 exit:
1744 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1745 return 0;
1746}
1747
1748static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750{
1751 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001752 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001753 char *frame, *buf;
1754 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001755
Christopher Fauleta1cda022016-12-21 08:58:06 +01001756 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1757 goto exit;
1758
1759 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1760 goto exit;
1761
Christopher Faulet8ef75252017-02-20 22:56:03 +01001762 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1763 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001764 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001765 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1766 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001769
1770 switch (ret) {
1771 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 goto exit;
1774
1775 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001776 goto stop;
1777
1778 default:
1779 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1780 " - disconnected by HAProxy (%d): %s\n",
1781 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782 __FUNCTION__, appctx,
1783 SPOE_APPCTX(appctx)->status_code,
1784 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785
1786 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1787 goto next;
1788 }
1789
1790 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001791 SPOE_APPCTX(appctx)->task->expire =
1792 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793 return 0;
1794 stop:
1795 return 1;
1796 exit:
1797 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1798 return 0;
1799}
1800
1801static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001802spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803{
1804 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805 char *frame;
1806 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807
Christopher Fauletb067b062017-01-04 16:39:11 +01001808 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001809 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001810 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001811 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812
Christopher Fauletb067b062017-01-04 16:39:11 +01001813 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001816 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001818 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001819 ret = spoe_recv_frame(appctx, frame,
1820 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001822 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001823 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 }
1825
1826 switch (ret) {
1827 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1829 " - error on frame (%s)\n",
1830 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001831 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001832 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 goto exit;
1835
1836 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 goto next;
1838
1839 case 1: /* retry */
1840 goto stop;
1841
1842 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001843 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001844 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001846 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1848 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849 goto exit;
1850 }
1851
1852 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001853 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001854 if (trash.data)
1855 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856
Christopher Fauleta1cda022016-12-21 08:58:06 +01001857 return 0;
1858 stop:
1859 return 1;
1860 exit:
1861 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1862 return 0;
1863}
1864
1865/* I/O Handler processing messages exchanged with the agent */
1866static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001869 struct stream_interface *si = appctx->owner;
1870 struct spoe_agent *agent;
1871
1872 if (SPOE_APPCTX(appctx) == NULL)
1873 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874
Christopher Faulet8ef75252017-02-20 22:56:03 +01001875 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1876 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001877
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878 switchstate:
1879 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1880 " - appctx-state=%s\n",
1881 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1882 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1883
1884 switch (appctx->st0) {
1885 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001886 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001887 goto out;
1888 goto switchstate;
1889
1890 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001891 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001892 goto out;
1893 goto switchstate;
1894
1895 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001896 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001897 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001899 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001900 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001901 SPOE_APPCTX(appctx)->task->expire =
1902 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001903 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001904 goto switchstate;
1905 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001906 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1907 /* fall through */
1908
1909 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001910 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1911 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001912 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001913 goto out;
1914 goto switchstate;
1915
1916 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001917 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001918 goto out;
1919 goto switchstate;
1920
1921 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001922 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001923 goto out;
1924 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925
1926 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001927 appctx->st0 = SPOE_APPCTX_ST_END;
1928 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1929
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001930 si_shutw(si);
1931 si_shutr(si);
1932 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933 /* fall through */
1934
1935 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001936 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001937 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001938 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001939 if (stopping)
1940 spoe_wakeup_appctx(appctx);
1941
Christopher Faulet42bfa462017-01-04 14:14:19 +01001942 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1943 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944}
1945
1946struct applet spoe_applet = {
1947 .obj_type = OBJ_TYPE_APPLET,
1948 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001949 .fct = spoe_handle_appctx,
1950 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001951};
1952
1953/* Create a SPOE applet. On success, the created applet is returned, else
1954 * NULL. */
1955static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001956spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001957{
1958 struct appctx *appctx;
1959 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001960 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961
Emeric Brun1138fd02017-06-19 12:38:55 +02001962 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963 goto out_error;
1964
Willy Tarreaubafbe012017-11-24 17:34:44 +01001965 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001966 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001968 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001969
Christopher Faulet42bfa462017-01-04 14:14:19 +01001970 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001971 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001972 goto out_free_spoe_appctx;
1973
1974 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001975 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001976 SPOE_APPCTX(appctx)->task->context = appctx;
1977 SPOE_APPCTX(appctx)->agent = conf->agent;
1978 SPOE_APPCTX(appctx)->version = 0;
1979 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1980 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001981 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001982 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001983 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001984
1985 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1986 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001987 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001988
1989 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1990 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991
Willy Tarreau5820a362016-12-22 15:59:02 +01001992 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001993 if (!sess)
1994 goto out_free_spoe;
1995
Willy Tarreau87787ac2017-08-28 16:22:54 +02001996 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 goto out_free_sess;
1998
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 stream_set_backend(strm, conf->agent->b.be);
2000
2001 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002002 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002003 appctx_wakeup(appctx);
2004
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005 strm->do_log = NULL;
2006 strm->res.flags |= CF_READ_DONTWAIT;
2007
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002008 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002009 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002010 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002011 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002012
Emeric Brunc60def82017-09-27 14:59:38 +02002013 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002014 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002015 return appctx;
2016
2017 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018 out_free_sess:
2019 session_free(sess);
2020 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002021 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002022 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002023 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024 out_free_appctx:
2025 appctx_free(appctx);
2026 out_error:
2027 return NULL;
2028}
2029
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002030static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002031spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002032{
2033 struct spoe_config *conf = FLT_CONF(ctx->filter);
2034 struct spoe_agent *agent = conf->agent;
2035 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002036 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002037
Christopher Fauleta1cda022016-12-21 08:58:06 +01002038 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002039 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002040 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002042
2043 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002044 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2046 ctx->strm);
2047
Christopher Fauleta1cda022016-12-21 08:58:06 +01002048 /* Do not try to create a new applet if there is no server up for the
2049 * agent's backend. */
2050 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2052 " - cannot create SPOE appctx: no server up\n",
2053 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2054 __FUNCTION__, ctx->strm);
2055 goto end;
2056 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002057
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 /* Do not try to create a new applet if we have reached the maximum of
2059 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002060 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002061 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002062 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2063 " - cannot create SPOE appctx: max CPS reached\n",
2064 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2065 __FUNCTION__, ctx->strm);
2066 goto end;
2067 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068 }
2069
Christopher Faulet8ef75252017-02-20 22:56:03 +01002070 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002071 if (appctx == NULL) {
2072 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2073 " - failed to create SPOE appctx\n",
2074 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2075 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002076 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002077 "SPOE: [%s] failed to create SPOE applet\n",
2078 agent->id);
2079
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002081 }
2082
Christopher Fauleta1cda022016-12-21 08:58:06 +01002083 /* Increase the per-process number of cumulated connections */
2084 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002085 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002086
Christopher Fauleta1cda022016-12-21 08:58:06 +01002087 end:
2088 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002089 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002090 ctx->status_code = SPOE_CTX_ERR_RES;
2091 return -1;
2092 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002093
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002094 /* Add the SPOE context in the sending queue if the stream has no applet
2095 * already assigned and wakeup all idle applets. Otherwise, don't queue
2096 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002097 _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 Faulet3e86cec2019-04-10 14:02:12 +02002100 if (ctx->spoe_appctx)
2101 return 1;
2102 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002103
2104 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002105 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002106 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002107 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002108 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002109 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002110
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002111 /* Finally try to wakeup an IDLE applet. */
2112 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2113 struct eb32_node *node;
2114
2115 node = eb32_first(&agent->rt[tid].idle_applets);
2116 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2117 if (node && spoe_appctx) {
2118 eb32_delete(&spoe_appctx->node);
2119 spoe_appctx->node.key++;
2120 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2121 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002122 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002123 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002124 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002125}
2126
2127/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002128 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002129 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002130/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2131 * fragmented_content. If the next message can be processed, it returns 0. If
2132 * the message is too big, it returns -1.*/
2133static int
2134spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2135 struct spoe_message *msg, int dir,
2136 char **buf, char *end)
2137{
2138 struct sample *smp;
2139 struct spoe_arg *arg;
2140 int ret;
2141
2142 if (msg->cond) {
2143 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2144 ret = acl_pass(ret);
2145 if (msg->cond->pol == ACL_COND_UNLESS)
2146 ret = !ret;
2147
2148 /* the rule does not match */
2149 if (!ret)
2150 goto next;
2151 }
2152
2153 /* Resume encoding of a SPOE argument */
2154 if (ctx->frag_ctx.curarg != NULL) {
2155 arg = ctx->frag_ctx.curarg;
2156 goto encode_argument;
2157 }
2158
2159 if (ctx->frag_ctx.curoff != UINT_MAX)
2160 goto encode_msg_payload;
2161
2162 /* Check if there is enough space for the message name and the
2163 * number of arguments. It implies <msg->id_len> is encoded on 2
2164 * bytes, at most (< 2288). */
2165 if (*buf + 2 + msg->id_len + 1 > end)
2166 goto too_big;
2167
2168 /* Encode the message name */
2169 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2170 goto too_big;
2171
2172 /* Set the number of arguments for this message */
2173 **buf = msg->nargs;
2174 (*buf)++;
2175
2176 ctx->frag_ctx.curoff = 0;
2177 encode_msg_payload:
2178
2179 /* Loop on arguments */
2180 list_for_each_entry(arg, &msg->args, list) {
2181 ctx->frag_ctx.curarg = arg;
2182 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002183 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002184
2185 encode_argument:
2186 if (ctx->frag_ctx.curoff != UINT_MAX)
2187 goto encode_arg_value;
2188
Joseph Herlantf7f60312018-11-15 13:46:49 -08002189 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002190 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2191 goto too_big;
2192
2193 ctx->frag_ctx.curoff = 0;
2194 encode_arg_value:
2195
Joseph Herlantf7f60312018-11-15 13:46:49 -08002196 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002197 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002198 if (smp) {
2199 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2200 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2201 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002202 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002203 if (ret == -1 || ctx->frag_ctx.curoff)
2204 goto too_big;
2205 }
2206
2207 next:
2208 return 0;
2209
2210 too_big:
2211 return -1;
2212}
2213
Christopher Fauletc718b822017-09-21 16:50:56 +02002214/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2215 * handle fragmented content. On success it returns 1. If an error occurred, -1
2216 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2217 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002218static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002219spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002220 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002221{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002222 struct spoe_config *conf = FLT_CONF(ctx->filter);
2223 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002224 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002225 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002226
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002227 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002228 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002229
Christopher Fauletc718b822017-09-21 16:50:56 +02002230 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2231 /* Resume encoding of a SPOE message */
2232 if (ctx->frag_ctx.curmsg != NULL) {
2233 msg = ctx->frag_ctx.curmsg;
2234 goto encode_evt_message;
2235 }
2236
2237 list_for_each_entry(msg, messages, by_evt) {
2238 ctx->frag_ctx.curmsg = msg;
2239 ctx->frag_ctx.curarg = NULL;
2240 ctx->frag_ctx.curoff = UINT_MAX;
2241
2242 encode_evt_message:
2243 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2244 goto too_big;
2245 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002246 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002247 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2248 /* Resume encoding of a SPOE message */
2249 if (ctx->frag_ctx.curmsg != NULL) {
2250 msg = ctx->frag_ctx.curmsg;
2251 goto encode_grp_message;
2252 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002253
Christopher Fauletc718b822017-09-21 16:50:56 +02002254 list_for_each_entry(msg, messages, by_grp) {
2255 ctx->frag_ctx.curmsg = msg;
2256 ctx->frag_ctx.curarg = NULL;
2257 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002258
Christopher Fauletc718b822017-09-21 16:50:56 +02002259 encode_grp_message:
2260 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2261 goto too_big;
2262 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002263 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002264 else
2265 goto skip;
2266
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267
Christopher Faulet57583e42017-09-04 15:41:09 +02002268 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002269 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002270 goto skip;
2271
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002272 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002273 " - encode %s messages - spoe_appctx=%p"
2274 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002275 (int)now.tv_sec, (int)now.tv_usec,
2276 agent->id, __FUNCTION__, s,
2277 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002278 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002279 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002281 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282 ctx->frag_ctx.curmsg = NULL;
2283 ctx->frag_ctx.curarg = NULL;
2284 ctx->frag_ctx.curoff = 0;
2285 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002286
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002287 return 1;
2288
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002289 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002290 /* Return an error if fragmentation is unsupported or if nothing has
2291 * been encoded because its too big and not splittable. */
2292 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002293 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2294 return -1;
2295 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002296
2297 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002298 " - encode fragmented messages - spoe_appctx=%p"
2299 " - curmsg=%p - curarg=%p - curoff=%u"
2300 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002301 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002302 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002303 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002304 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002305
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002306 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002307 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2308 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2309 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002310
2311 skip:
2312 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2313 " - skip the frame because nothing has been encoded\n",
2314 (int)now.tv_sec, (int)now.tv_usec,
2315 agent->id, __FUNCTION__, s);
2316 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002317}
2318
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002319
2320/***************************************************************************
2321 * Functions that handle SPOE actions
2322 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323/* Helper function to set a variable */
2324static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002325spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326 struct sample *smp)
2327{
2328 struct spoe_config *conf = FLT_CONF(ctx->filter);
2329 struct spoe_agent *agent = conf->agent;
2330 char varname[64];
2331
2332 memset(varname, 0, sizeof(varname));
2333 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2334 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002335 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2336 vars_set_by_name(varname, len, smp);
2337 else
2338 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002339}
2340
2341/* Helper function to unset a variable */
2342static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002343spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002344 struct sample *smp)
2345{
2346 struct spoe_config *conf = FLT_CONF(ctx->filter);
2347 struct spoe_agent *agent = conf->agent;
2348 char varname[64];
2349
2350 memset(varname, 0, sizeof(varname));
2351 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2352 scope, agent->var_pfx, len, name);
2353 vars_unset_by_name_ifexist(varname, len, smp);
2354}
2355
2356
Christopher Faulet8ef75252017-02-20 22:56:03 +01002357static inline int
2358spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2359 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002360{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002361 char *str, *scope, *p = *buf;
2362 struct sample smp;
2363 uint64_t sz;
2364 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002365
Christopher Faulet8ef75252017-02-20 22:56:03 +01002366 if (p + 2 >= end)
2367 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368
Christopher Faulet8ef75252017-02-20 22:56:03 +01002369 /* SET-VAR requires 3 arguments */
2370 if (*p++ != 3)
2371 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002372
Christopher Faulet8ef75252017-02-20 22:56:03 +01002373 switch (*p++) {
2374 case SPOE_SCOPE_PROC: scope = "proc"; break;
2375 case SPOE_SCOPE_SESS: scope = "sess"; break;
2376 case SPOE_SCOPE_TXN : scope = "txn"; break;
2377 case SPOE_SCOPE_REQ : scope = "req"; break;
2378 case SPOE_SCOPE_RES : scope = "res"; break;
2379 default: goto skip;
2380 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002381
Christopher Faulet8ef75252017-02-20 22:56:03 +01002382 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2383 goto skip;
2384 memset(&smp, 0, sizeof(smp));
2385 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002386
Christopher Faulet8ef75252017-02-20 22:56:03 +01002387 if (spoe_decode_data(&p, end, &smp) == -1)
2388 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2391 " - set-var '%s.%s.%.*s'\n",
2392 (int)now.tv_sec, (int)now.tv_usec,
2393 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2394 __FUNCTION__, s, scope,
2395 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2396 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002397
Christopher Faulet8ef75252017-02-20 22:56:03 +01002398 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002399
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400 ret = (p - *buf);
2401 *buf = p;
2402 return ret;
2403 skip:
2404 return 0;
2405}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002406
Christopher Faulet8ef75252017-02-20 22:56:03 +01002407static inline int
2408spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2409 char **buf, char *end, int dir)
2410{
2411 char *str, *scope, *p = *buf;
2412 struct sample smp;
2413 uint64_t sz;
2414 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002415
Christopher Faulet8ef75252017-02-20 22:56:03 +01002416 if (p + 2 >= end)
2417 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418
Christopher Faulet8ef75252017-02-20 22:56:03 +01002419 /* UNSET-VAR requires 2 arguments */
2420 if (*p++ != 2)
2421 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422
Christopher Faulet8ef75252017-02-20 22:56:03 +01002423 switch (*p++) {
2424 case SPOE_SCOPE_PROC: scope = "proc"; break;
2425 case SPOE_SCOPE_SESS: scope = "sess"; break;
2426 case SPOE_SCOPE_TXN : scope = "txn"; break;
2427 case SPOE_SCOPE_REQ : scope = "req"; break;
2428 case SPOE_SCOPE_RES : scope = "res"; break;
2429 default: goto skip;
2430 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002431
Christopher Faulet8ef75252017-02-20 22:56:03 +01002432 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2433 goto skip;
2434 memset(&smp, 0, sizeof(smp));
2435 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002436
Christopher Faulet8ef75252017-02-20 22:56:03 +01002437 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2438 " - unset-var '%s.%s.%.*s'\n",
2439 (int)now.tv_sec, (int)now.tv_usec,
2440 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2441 __FUNCTION__, s, scope,
2442 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2443 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002444
Christopher Faulet8ef75252017-02-20 22:56:03 +01002445 spoe_unset_var(ctx, scope, str, sz, &smp);
2446
2447 ret = (p - *buf);
2448 *buf = p;
2449 return ret;
2450 skip:
2451 return 0;
2452}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002453
Christopher Faulet8ef75252017-02-20 22:56:03 +01002454/* Process SPOE actions for a specific event. It returns 1 on success. If an
2455 * error occurred, 0 is returned. */
2456static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002457spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002458{
2459 char *p, *end;
2460 int ret;
2461
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002462 p = b_head(&ctx->buffer);
2463 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464
2465 while (p < end) {
2466 enum spoe_action_type type;
2467
2468 type = *p++;
2469 switch (type) {
2470 case SPOE_ACT_T_SET_VAR:
2471 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2472 if (!ret)
2473 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002474 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002475
Christopher Faulet8ef75252017-02-20 22:56:03 +01002476 case SPOE_ACT_T_UNSET_VAR:
2477 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2478 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002479 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002480 break;
2481
2482 default:
2483 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002484 }
2485 }
2486
2487 return 1;
2488 skip:
2489 return 0;
2490}
2491
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002492/***************************************************************************
2493 * Functions that process SPOE events
2494 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002495static void
2496spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2497 struct spoe_context *ctx, int dir)
2498{
2499 if (!tv_iszero(&ctx->stats.tv_start)) {
2500 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2501 ctx->stats.t_total += ctx->stats.t_process;
2502 tv_zero(&ctx->stats.tv_request);
2503 tv_zero(&ctx->stats.tv_queue);
2504 tv_zero(&ctx->stats.tv_wait);
2505 tv_zero(&ctx->stats.tv_response);
2506 }
2507
2508 if (agent->var_t_process) {
2509 struct sample smp;
2510
2511 memset(&smp, 0, sizeof(smp));
2512 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2513 smp.data.u.sint = ctx->stats.t_process;
2514 smp.data.type = SMP_T_SINT;
2515
2516 spoe_set_var(ctx, "txn", agent->var_t_process,
2517 strlen(agent->var_t_process), &smp);
2518 }
2519
2520 if (agent->var_t_total) {
2521 struct sample smp;
2522
2523 memset(&smp, 0, sizeof(smp));
2524 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2525 smp.data.u.sint = ctx->stats.t_total;
2526 smp.data.type = SMP_T_SINT;
2527
2528 spoe_set_var(ctx, "txn", agent->var_t_total,
2529 strlen(agent->var_t_total), &smp);
2530 }
2531}
2532
2533static void
2534spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2535 struct spoe_context *ctx, int dir)
2536{
2537 if (agent->eps_max > 0)
2538 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2539
2540 if (agent->var_on_error) {
2541 struct sample smp;
2542
2543 memset(&smp, 0, sizeof(smp));
2544 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2545 smp.data.u.sint = ctx->status_code;
2546 smp.data.type = SMP_T_BOOL;
2547
2548 spoe_set_var(ctx, "txn", agent->var_on_error,
2549 strlen(agent->var_on_error), &smp);
2550 }
2551
2552 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2553 ? SPOE_CTX_ST_READY
2554 : SPOE_CTX_ST_NONE);
2555}
2556
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002557static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002558spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002560 /* If a process is already started for this SPOE context, retry
2561 * later. */
2562 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002563 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002565 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002566 ctx->stats.tv_start = now;
2567 ctx->stats.tv_request = now;
2568 ctx->stats.t_request = -1;
2569 ctx->stats.t_queue = -1;
2570 ctx->stats.t_waiting = -1;
2571 ctx->stats.t_response = -1;
2572 ctx->stats.t_process = -1;
2573
2574 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002575
Christopher Fauleta1cda022016-12-21 08:58:06 +01002576 /* Set the right flag to prevent request and response processing
2577 * in same time. */
2578 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2579 ? SPOE_CTX_FL_REQ_PROCESS
2580 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002581 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582}
2583
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002584static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002585spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002586{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002587 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002588
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002589 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2590 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002591 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002592 if (sa) {
2593 if (sa->frag_ctx.ctx == ctx) {
2594 sa->frag_ctx.ctx = NULL;
2595 spoe_wakeup_appctx(sa->owner);
2596 }
2597 else
2598 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002599 }
2600
Christopher Fauleta1cda022016-12-21 08:58:06 +01002601 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002602 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002603 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002604
2605 /* Reset processing timer */
2606 ctx->process_exp = TICK_ETERNITY;
2607
Christopher Faulet8ef75252017-02-20 22:56:03 +01002608 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002609
Christopher Fauletfce747b2018-01-24 15:59:32 +01002610 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002611 ctx->frag_ctx.curmsg = NULL;
2612 ctx->frag_ctx.curarg = NULL;
2613 ctx->frag_ctx.curoff = 0;
2614 ctx->frag_ctx.flags = 0;
2615
Christopher Fauleta1cda022016-12-21 08:58:06 +01002616 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002617 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002618 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002619 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002620 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002621
Christopher Fauleta1cda022016-12-21 08:58:06 +01002622 LIST_DEL(&ctx->list);
2623 LIST_INIT(&ctx->list);
2624 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002625}
2626
Christopher Faulet58d03682017-09-21 16:57:24 +02002627/* Process a list of SPOE messages. First, this functions will process messages
2628 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2629 * to process corresponding actions. During all the processing, it returns 0
2630 * and it returns 1 when the processing is finished. If an error occurred, -1
2631 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002632static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002633spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2634 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002635{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002636 struct spoe_config *conf = FLT_CONF(ctx->filter);
2637 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002638 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002639
2640 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002641 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002642
2643 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2644 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002645 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002646 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002647 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002648 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002649 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002650 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002651
2652 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002653 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002654 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002655 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002656 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002657 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002658 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002659 goto skip;
2660 }
2661 }
2662
Christopher Fauletf7a30922016-11-10 15:04:51 +01002663 if (!tick_isset(ctx->process_exp)) {
2664 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2665 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2666 ctx->process_exp);
2667 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002668 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002669 if (!ret)
2670 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002671
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002672 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002673 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002674 }
2675
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002676 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002677 if (tv_iszero(&ctx->stats.tv_request))
2678 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002679 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002680 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002681 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002682 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002683 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002684 if (!ret)
2685 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002686 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002687 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002688 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2689 }
2690
2691 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002692 if (ctx->spoe_appctx)
2693 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002694 ret = 0;
2695 goto out;
2696 }
2697
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002698 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2699 ret = 0;
2700 goto out;
2701 }
2702
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002703 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002704 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002705 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002706 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002707 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002708 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002709 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002710 }
2711
2712 out:
2713 return ret;
2714
Christopher Fauleta1cda022016-12-21 08:58:06 +01002715 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002716 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002717 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002718 spoe_stop_processing(agent, ctx);
2719 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002720
Christopher Fauleta1cda022016-12-21 08:58:06 +01002721 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002722 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002723 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002724 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002725 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002726 spoe_handle_processing_error(s, agent, ctx, dir);
2727 ret = 1;
2728 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002729 return ret;
2730}
2731
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002732/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2733 * See spoe_process_message for details. */
2734static int
2735spoe_process_group(struct stream *s, struct spoe_context *ctx,
2736 struct spoe_group *group, int dir)
2737{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002738 struct spoe_config *conf = FLT_CONF(ctx->filter);
2739 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002740 int ret;
2741
2742 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2743 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002744 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002745 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2746 group->id);
2747
2748 if (LIST_ISEMPTY(&group->messages))
2749 return 1;
2750
2751 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002752 if (ret && ctx->stats.t_process != -1) {
2753 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002754 " - <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 +01002755 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002756 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002757 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002758 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,
2762 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2763 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2764 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2765 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2766 agent->id, group->id, s->uniq_id, ctx->status_code,
2767 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2768 ctx->stats.t_response, ctx->stats.t_process,
2769 agent->counters.idles, agent->counters.applets,
2770 agent->counters.nb_sending, agent->counters.nb_waiting,
2771 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002772 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002773 return ret;
2774}
2775
Christopher Faulet58d03682017-09-21 16:57:24 +02002776/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2777 * See spoe_process_message for details. */
2778static int
2779spoe_process_event(struct stream *s, struct spoe_context *ctx,
2780 enum spoe_event ev)
2781{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002782 struct spoe_config *conf = FLT_CONF(ctx->filter);
2783 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002784 int dir, ret;
2785
2786 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002787 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002788 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002789 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2790 spoe_event_str[ev]);
2791
2792 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2793
2794 if (LIST_ISEMPTY(&(ctx->events[ev])))
2795 return 1;
2796
2797 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002798 if (ret && ctx->stats.t_process != -1) {
2799 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002800 " - <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 +01002801 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2802 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2803 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002804 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,
2808 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2809 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2810 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2811 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2812 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2813 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2814 ctx->stats.t_response, ctx->stats.t_process,
2815 agent->counters.idles, agent->counters.applets,
2816 agent->counters.nb_sending, agent->counters.nb_waiting,
2817 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002818 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002819 return ret;
2820}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002821
2822/***************************************************************************
2823 * Functions that create/destroy SPOE contexts
2824 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002825static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002826spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002827{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002828 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002829 return 1;
2830
Christopher Faulet4596fb72017-01-11 14:05:19 +01002831 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002832 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002833 LIST_DEL(&buffer_wait->list);
2834 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002835 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002836 }
2837
Christopher Faulet4596fb72017-01-11 14:05:19 +01002838 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002839 return 1;
2840
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_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002843 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002844 return 0;
2845}
2846
2847static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002849{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002850 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002851 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002852 LIST_DEL(&buffer_wait->list);
2853 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002854 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002855 }
2856
2857 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002858 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002859 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002860 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002861 }
2862}
2863
Christopher Faulet4596fb72017-01-11 14:05:19 +01002864static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002865spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002866{
2867 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2868 return 1;
2869}
2870
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002871static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002872spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002873{
2874 struct spoe_config *conf = FLT_CONF(filter);
2875 struct spoe_context *ctx;
2876
Willy Tarreaubafbe012017-11-24 17:34:44 +01002877 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002878 if (ctx == NULL) {
2879 return NULL;
2880 }
2881 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002882 ctx->filter = filter;
2883 ctx->state = SPOE_CTX_ST_NONE;
2884 ctx->status_code = SPOE_CTX_ERR_NONE;
2885 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002886 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002887 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002888 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002889 LIST_INIT(&ctx->buffer_wait.list);
2890 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002891 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002892 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002893
Christopher Fauletf7a30922016-11-10 15:04:51 +01002894 ctx->stream_id = 0;
2895 ctx->frame_id = 1;
2896 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002897
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002898 tv_zero(&ctx->stats.tv_start);
2899 tv_zero(&ctx->stats.tv_request);
2900 tv_zero(&ctx->stats.tv_queue);
2901 tv_zero(&ctx->stats.tv_wait);
2902 tv_zero(&ctx->stats.tv_response);
2903 ctx->stats.t_request = -1;
2904 ctx->stats.t_queue = -1;
2905 ctx->stats.t_waiting = -1;
2906 ctx->stats.t_response = -1;
2907 ctx->stats.t_process = -1;
2908 ctx->stats.t_total = 0;
2909
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002910 ctx->strm = s;
2911 ctx->state = SPOE_CTX_ST_READY;
2912 filter->ctx = ctx;
2913
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914 return ctx;
2915}
2916
2917static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002918spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002920 struct spoe_config *conf = FLT_CONF(filter);
2921 struct spoe_context *ctx = filter->ctx;
2922
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002923 if (!ctx)
2924 return;
2925
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002926 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002927 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002928 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002929}
2930
2931static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002932spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002933{
2934 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002935 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002936
2937 tv_zero(&ctx->stats.tv_start);
2938 tv_zero(&ctx->stats.tv_request);
2939 tv_zero(&ctx->stats.tv_queue);
2940 tv_zero(&ctx->stats.tv_wait);
2941 tv_zero(&ctx->stats.tv_response);
2942 ctx->stats.t_request = -1;
2943 ctx->stats.t_queue = -1;
2944 ctx->stats.t_waiting = -1;
2945 ctx->stats.t_response = -1;
2946 ctx->stats.t_process = -1;
2947 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002948}
2949
2950
2951/***************************************************************************
2952 * Hooks that manage the filter lifecycle (init/check/deinit)
2953 **************************************************************************/
2954/* Signal handler: Do a soft stop, wakeup SPOE applet */
2955static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002956spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957{
2958 struct proxy *p;
2959
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002960 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002961 while (p) {
2962 struct flt_conf *fconf;
2963
2964 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002965 struct spoe_config *conf;
2966 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002967 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002968 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002969
Christopher Faulet3b386a32017-02-23 10:17:15 +01002970 if (fconf->id != spoe_filter_id)
2971 continue;
2972
2973 conf = fconf->conf;
2974 agent = conf->agent;
2975
Christopher Faulet24289f22017-09-25 14:48:02 +02002976 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002977 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002978 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2979 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002980 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002981 }
2982 }
2983 p = p->next;
2984 }
2985}
2986
2987
2988/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2989static int
2990spoe_init(struct proxy *px, struct flt_conf *fconf)
2991{
2992 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002994 /* conf->agent_fe was already initialized during the config
2995 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002996 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997 conf->agent_fe.cap = PR_CAP_FE;
2998 conf->agent_fe.mode = PR_MODE_TCP;
2999 conf->agent_fe.maxconn = 0;
3000 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3001 conf->agent_fe.conn_retries = CONN_RETRIES;
3002 conf->agent_fe.accept = frontend_accept;
3003 conf->agent_fe.srv = NULL;
3004 conf->agent_fe.timeout.client = TICK_ETERNITY;
3005 conf->agent_fe.default_target = &spoe_applet.obj_type;
3006 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3007
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003008 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003009 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 sighandler_registered = 1;
3011 }
3012
Christopher Faulet00292352019-01-09 15:05:10 +01003013 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003015}
3016
3017/* Free ressources allocated by the SPOE filter. */
3018static void
3019spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3020{
3021 struct spoe_config *conf = fconf->conf;
3022
3023 if (conf) {
3024 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003025
Christopher Faulet8ef75252017-02-20 22:56:03 +01003026 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003027 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003028 free(conf);
3029 }
3030 fconf->conf = NULL;
3031}
3032
3033/* Check configuration of a SPOE filter for a specified proxy.
3034 * Return 1 on error, else 0. */
3035static int
3036spoe_check(struct proxy *px, struct flt_conf *fconf)
3037{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003038 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003039 struct spoe_config *conf = fconf->conf;
3040 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003041 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042
Christopher Faulet7ee86672017-09-19 11:08:28 +02003043 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3044 * are uniq */
3045 list_for_each_entry(f, &px->filter_configs, list) {
3046 struct spoe_config *c = f->conf;
3047
3048 /* This is not an SPOE filter */
3049 if (f->id != spoe_filter_id)
3050 continue;
3051 /* This is the current SPOE filter */
3052 if (f == fconf)
3053 continue;
3054
3055 /* Check engine Id. It should be uniq */
3056 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003057 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3058 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003059 return 1;
3060 }
3061 }
3062
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003063 target = proxy_be_by_name(conf->agent->b.name);
3064 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003065 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3066 " declared at %s:%d.\n",
3067 px->id, conf->agent->b.name, conf->agent->id,
3068 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003069 return 1;
3070 }
3071 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003072 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3073 " at %s:%d does not support HTTP mode.\n",
3074 px->id, target->id, conf->agent->id,
3075 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003076 return 1;
3077 }
3078
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003079 if (px->bind_proc & ~target->bind_proc) {
3080 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3081 " at %s:%d does not cover all of its processes.\n",
3082 px->id, target->id, conf->agent->id,
3083 conf->agent->conf.file, conf->agent->conf.line);
3084 return 1;
3085 }
3086
Willy Tarreaub0769b22019-02-07 13:40:33 +01003087 /* finish per-thread agent initialization */
3088 if (global.nbthread == 1)
3089 conf->agent->flags |= SPOE_FL_ASYNC;
3090
Christopher Fauletfe261552019-03-18 13:57:42 +01003091 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003092 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3093 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3094 return 1;
3095 }
3096 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletfe261552019-03-18 13:57:42 +01003097 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3098 conf->agent->rt[i].processing = 0;
3099 LIST_INIT(&conf->agent->rt[i].applets);
3100 LIST_INIT(&conf->agent->rt[i].sending_queue);
3101 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3102 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003103 }
3104
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003105 free(conf->agent->b.name);
3106 conf->agent->b.name = NULL;
3107 conf->agent->b.be = target;
3108 return 0;
3109}
3110
3111/**************************************************************************
3112 * Hooks attached to a stream
3113 *************************************************************************/
3114/* Called when a filter instance is created and attach to a stream. It creates
3115 * the context that will be used to process this stream. */
3116static int
3117spoe_start(struct stream *s, struct filter *filter)
3118{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003119 struct spoe_config *conf = FLT_CONF(filter);
3120 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003121 struct spoe_context *ctx;
3122
3123 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003124 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003125 __FUNCTION__, s);
3126
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003127 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003128 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3129 " - failed to create SPOE context\n",
3130 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003131 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003132 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003133 "SPOE: [%s] failed to create SPOE context\n",
3134 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003135 return 0;
3136 }
3137
Christopher Faulet11610f32017-09-21 10:23:10 +02003138 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3140
Christopher Faulet11610f32017-09-21 10:23:10 +02003141 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3143
Christopher Faulet11610f32017-09-21 10:23:10 +02003144 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003145 filter->pre_analyzers |= AN_RES_INSPECT;
3146
Christopher Faulet11610f32017-09-21 10:23:10 +02003147 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003148 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3149
Christopher Faulet11610f32017-09-21 10:23:10 +02003150 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003151 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3152
Christopher Faulet11610f32017-09-21 10:23:10 +02003153 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003154 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3155
3156 return 1;
3157}
3158
3159/* Called when a filter instance is detached from a stream. It release the
3160 * attached SPOE context. */
3161static void
3162spoe_stop(struct stream *s, struct filter *filter)
3163{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003164 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3165 (int)now.tv_sec, (int)now.tv_usec,
3166 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3167 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003168 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169}
3170
Christopher Fauletf7a30922016-11-10 15:04:51 +01003171
3172/*
3173 * Called when the stream is woken up because of expired timer.
3174 */
3175static void
3176spoe_check_timeouts(struct stream *s, struct filter *filter)
3177{
3178 struct spoe_context *ctx = filter->ctx;
3179
Christopher Fauletac580602018-03-20 16:09:20 +01003180 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003181 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003182}
3183
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003184/* Called when we are ready to filter data on a channel */
3185static int
3186spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3187{
3188 struct spoe_context *ctx = filter->ctx;
3189 int ret = 1;
3190
3191 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3192 " - ctx-flags=0x%08x\n",
3193 (int)now.tv_sec, (int)now.tv_usec,
3194 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3195 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3196
Christopher Fauletb067b062017-01-04 16:39:11 +01003197 if (ctx->state == SPOE_CTX_ST_NONE)
3198 goto out;
3199
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003200 if (!(chn->flags & CF_ISRESP)) {
3201 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3202 chn->analysers |= AN_REQ_INSPECT_FE;
3203 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3204 chn->analysers |= AN_REQ_INSPECT_BE;
3205
3206 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3207 goto out;
3208
3209 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003210 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003211 if (!ret)
3212 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003213 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3214 }
3215 else {
3216 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3217 chn->analysers |= AN_RES_INSPECT;
3218
3219 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3220 goto out;
3221
Christopher Faulet8ef75252017-02-20 22:56:03 +01003222 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003223 if (!ret) {
3224 channel_dont_read(chn);
3225 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003226 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003227 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003228 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003229 }
3230
3231 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003232 return ret;
3233}
3234
3235/* Called before a processing happens on a given channel */
3236static int
3237spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3238 struct channel *chn, unsigned an_bit)
3239{
3240 struct spoe_context *ctx = filter->ctx;
3241 int ret = 1;
3242
3243 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3244 " - ctx-flags=0x%08x - ana=0x%08x\n",
3245 (int)now.tv_sec, (int)now.tv_usec,
3246 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3247 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3248 ctx->flags, an_bit);
3249
Christopher Fauletb067b062017-01-04 16:39:11 +01003250 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003251 goto out;
3252
3253 switch (an_bit) {
3254 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003255 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003256 break;
3257 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003258 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003259 break;
3260 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003261 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003262 break;
3263 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003264 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003265 break;
3266 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003267 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003268 break;
3269 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003270 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003271 break;
3272 }
3273
3274 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003275 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276 channel_dont_read(chn);
3277 channel_dont_close(chn);
3278 }
3279 return ret;
3280}
3281
3282/* Called when the filtering on the channel ends. */
3283static int
3284spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3285{
3286 struct spoe_context *ctx = filter->ctx;
3287
3288 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3289 " - ctx-flags=0x%08x\n",
3290 (int)now.tv_sec, (int)now.tv_usec,
3291 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3292 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3293
3294 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003295 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003296 }
3297
3298 return 1;
3299}
3300
3301/********************************************************************
3302 * Functions that manage the filter initialization
3303 ********************************************************************/
3304struct flt_ops spoe_ops = {
3305 /* Manage SPOE filter, called for each filter declaration */
3306 .init = spoe_init,
3307 .deinit = spoe_deinit,
3308 .check = spoe_check,
3309
3310 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003311 .attach = spoe_start,
3312 .detach = spoe_stop,
3313 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003314
3315 /* Handle channels activity */
3316 .channel_start_analyze = spoe_start_analyze,
3317 .channel_pre_analyze = spoe_chn_pre_analyze,
3318 .channel_end_analyze = spoe_end_analyze,
3319};
3320
3321
3322static int
3323cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3324{
3325 const char *err;
3326 int i, err_code = 0;
3327
3328 if ((cfg_scope == NULL && curengine != NULL) ||
3329 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003330 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003331 goto out;
3332
3333 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3334 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003335 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3336 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003337 err_code |= ERR_ALERT | ERR_ABORT;
3338 goto out;
3339 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003340 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3341 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003342 goto out;
3343 }
3344
3345 err = invalid_char(args[1]);
3346 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003347 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3348 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003349 err_code |= ERR_ALERT | ERR_ABORT;
3350 goto out;
3351 }
3352
3353 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003354 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3355 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003356 err_code |= ERR_ALERT | ERR_ABORT;
3357 goto out;
3358 }
3359 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003360 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003361 err_code |= ERR_ALERT | ERR_ABORT;
3362 goto out;
3363 }
3364
3365 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003366
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003367 curagent->conf.file = strdup(file);
3368 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003369
3370 curagent->timeout.hello = TICK_ETERNITY;
3371 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003372 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003373
3374 curagent->engine_id = NULL;
3375 curagent->var_pfx = NULL;
3376 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003377 curagent->var_t_process = NULL;
3378 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003379 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003380 curagent->cps_max = 0;
3381 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003382 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003383 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003384
3385 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003386 LIST_INIT(&curagent->events[i]);
3387 LIST_INIT(&curagent->groups);
3388 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 }
3390 else if (!strcmp(args[0], "use-backend")) {
3391 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003392 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3393 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 err_code |= ERR_ALERT | ERR_FATAL;
3395 goto out;
3396 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003397 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003399 free(curagent->b.name);
3400 curagent->b.name = strdup(args[1]);
3401 }
3402 else if (!strcmp(args[0], "messages")) {
3403 int cur_arg = 1;
3404 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003405 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003406
Christopher Faulet11610f32017-09-21 10:23:10 +02003407 list_for_each_entry(ph, &curmphs, list) {
3408 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003409 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3410 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003411 err_code |= ERR_ALERT | ERR_FATAL;
3412 goto out;
3413 }
3414 }
3415
Christopher Faulet11610f32017-09-21 10:23:10 +02003416 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003417 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003418 err_code |= ERR_ALERT | ERR_ABORT;
3419 goto out;
3420 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003421 ph->id = strdup(args[cur_arg]);
3422 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003423 cur_arg++;
3424 }
3425 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003426 else if (!strcmp(args[0], "groups")) {
3427 int cur_arg = 1;
3428 while (*args[cur_arg]) {
3429 struct spoe_placeholder *ph = NULL;
3430
3431 list_for_each_entry(ph, &curgphs, list) {
3432 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003433 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3434 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003435 err_code |= ERR_ALERT | ERR_FATAL;
3436 goto out;
3437 }
3438 }
3439
3440 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003442 err_code |= ERR_ALERT | ERR_ABORT;
3443 goto out;
3444 }
3445 ph->id = strdup(args[cur_arg]);
3446 LIST_ADDQ(&curgphs, &ph->list);
3447 cur_arg++;
3448 }
3449 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450 else if (!strcmp(args[0], "timeout")) {
3451 unsigned int *tv = NULL;
3452 const char *res;
3453 unsigned timeout;
3454
3455 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003456 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3457 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003458 err_code |= ERR_ALERT | ERR_FATAL;
3459 goto out;
3460 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003461 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3462 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003463 if (!strcmp(args[1], "hello"))
3464 tv = &curagent->timeout.hello;
3465 else if (!strcmp(args[1], "idle"))
3466 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003467 else if (!strcmp(args[1], "processing"))
3468 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003469 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003470 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3471 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003472 err_code |= ERR_ALERT | ERR_FATAL;
3473 goto out;
3474 }
3475 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003476 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3477 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003478 err_code |= ERR_ALERT | ERR_FATAL;
3479 goto out;
3480 }
3481 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3482 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003483 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3484 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003485 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003486 goto out;
3487 }
3488 *tv = MS_TO_TICKS(timeout);
3489 }
3490 else if (!strcmp(args[0], "option")) {
3491 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003492 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3493 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003494 err_code |= ERR_ALERT | ERR_FATAL;
3495 goto out;
3496 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003497
Christopher Faulet305c6072017-02-23 16:17:53 +01003498 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003499 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003500 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003501 if (kwm == 1)
3502 curagent->flags &= ~SPOE_FL_PIPELINING;
3503 else
3504 curagent->flags |= SPOE_FL_PIPELINING;
3505 goto out;
3506 }
3507 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003508 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003509 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003510 if (kwm == 1)
3511 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003512 else {
3513 if (global.nbthread == 1)
3514 curagent->flags |= SPOE_FL_ASYNC;
3515 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3517 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003518 err_code |= ERR_WARN;
3519 }
3520 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003521 goto out;
3522 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003523 else if (!strcmp(args[1], "send-frag-payload")) {
3524 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3525 goto out;
3526 if (kwm == 1)
3527 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3528 else
3529 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3530 goto out;
3531 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003532 else if (!strcmp(args[1], "dontlog-normal")) {
3533 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3534 goto out;
3535 if (kwm == 1)
3536 curpxopts2 &= ~PR_O2_NOLOGNORM;
3537 else
3538 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003539 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003540 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003541
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003542 /* Following options does not support negation */
3543 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003544 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3545 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003546 err_code |= ERR_ALERT | ERR_FATAL;
3547 goto out;
3548 }
3549
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003550 if (!strcmp(args[1], "var-prefix")) {
3551 char *tmp;
3552
3553 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003554 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3555 file, linenum, args[0],
3556 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003557 err_code |= ERR_ALERT | ERR_FATAL;
3558 goto out;
3559 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003560 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3561 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003562 tmp = args[2];
3563 while (*tmp) {
3564 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003565 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003566 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003567 err_code |= ERR_ALERT | ERR_FATAL;
3568 goto out;
3569 }
3570 tmp++;
3571 }
3572 curagent->var_pfx = strdup(args[2]);
3573 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003574 else if (!strcmp(args[1], "force-set-var")) {
3575 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3576 goto out;
3577 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3578 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003579 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003580 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003581 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003582 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3583 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003584 else if (!strcmp(args[1], "set-on-error")) {
3585 char *tmp;
3586
3587 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003588 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3589 file, linenum, args[0],
3590 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003591 err_code |= ERR_ALERT | ERR_FATAL;
3592 goto out;
3593 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003594 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3595 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003596 tmp = args[2];
3597 while (*tmp) {
3598 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003599 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003600 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003601 err_code |= ERR_ALERT | ERR_FATAL;
3602 goto out;
3603 }
3604 tmp++;
3605 }
3606 curagent->var_on_error = strdup(args[2]);
3607 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003608 else if (!strcmp(args[1], "set-process-time")) {
3609 char *tmp;
3610
3611 if (!*args[2]) {
3612 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3613 file, linenum, args[0],
3614 args[1]);
3615 err_code |= ERR_ALERT | ERR_FATAL;
3616 goto out;
3617 }
3618 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3619 goto out;
3620 tmp = args[2];
3621 while (*tmp) {
3622 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003623 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003624 file, linenum, args[0], args[1]);
3625 err_code |= ERR_ALERT | ERR_FATAL;
3626 goto out;
3627 }
3628 tmp++;
3629 }
3630 curagent->var_t_process = strdup(args[2]);
3631 }
3632 else if (!strcmp(args[1], "set-total-time")) {
3633 char *tmp;
3634
3635 if (!*args[2]) {
3636 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3637 file, linenum, args[0],
3638 args[1]);
3639 err_code |= ERR_ALERT | ERR_FATAL;
3640 goto out;
3641 }
3642 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3643 goto out;
3644 tmp = args[2];
3645 while (*tmp) {
3646 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003647 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003648 file, linenum, args[0], args[1]);
3649 err_code |= ERR_ALERT | ERR_FATAL;
3650 goto out;
3651 }
3652 tmp++;
3653 }
3654 curagent->var_t_total = strdup(args[2]);
3655 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003656 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003657 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3658 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003659 err_code |= ERR_ALERT | ERR_FATAL;
3660 goto out;
3661 }
Christopher Faulet48026722016-11-16 15:01:12 +01003662 }
3663 else if (!strcmp(args[0], "maxconnrate")) {
3664 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003665 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3666 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003667 err_code |= ERR_ALERT | ERR_FATAL;
3668 goto out;
3669 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003670 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003671 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003672 curagent->cps_max = atol(args[1]);
3673 }
3674 else if (!strcmp(args[0], "maxerrrate")) {
3675 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003676 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3677 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003678 err_code |= ERR_ALERT | ERR_FATAL;
3679 goto out;
3680 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003681 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003682 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003683 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003684 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003685 else if (!strcmp(args[0], "max-frame-size")) {
3686 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003687 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3688 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003689 err_code |= ERR_ALERT | ERR_FATAL;
3690 goto out;
3691 }
3692 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3693 goto out;
3694 curagent->max_frame_size = atol(args[1]);
3695 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3696 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003697 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3698 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003699 err_code |= ERR_ALERT | ERR_FATAL;
3700 goto out;
3701 }
3702 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003703 else if (!strcmp(args[0], "max-waiting-frames")) {
3704 if (!*args[1]) {
3705 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3706 file, linenum, args[0]);
3707 err_code |= ERR_ALERT | ERR_FATAL;
3708 goto out;
3709 }
3710 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3711 goto out;
3712 curagent->max_fpa = atol(args[1]);
3713 if (curagent->max_fpa < 1) {
3714 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3715 file, linenum, args[0]);
3716 err_code |= ERR_ALERT | ERR_FATAL;
3717 goto out;
3718 }
3719 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003720 else if (!strcmp(args[0], "register-var-names")) {
3721 int cur_arg;
3722
3723 if (!*args[1]) {
3724 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3725 file, linenum, args[0]);
3726 err_code |= ERR_ALERT | ERR_FATAL;
3727 goto out;
3728 }
3729 cur_arg = 1;
3730 while (*args[cur_arg]) {
3731 struct spoe_var_placeholder *vph;
3732
3733 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3734 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3735 err_code |= ERR_ALERT | ERR_ABORT;
3736 goto out;
3737 }
3738 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3739 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3740 err_code |= ERR_ALERT | ERR_ABORT;
3741 goto out;
3742 }
3743 LIST_ADDQ(&curvars, &vph->list);
3744 cur_arg++;
3745 }
3746 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003747 else if (!strcmp(args[0], "log")) {
3748 char *errmsg = NULL;
3749
3750 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3751 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3752 err_code |= ERR_ALERT | ERR_FATAL;
3753 goto out;
3754 }
3755 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003756 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003757 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3758 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003759 err_code |= ERR_ALERT | ERR_FATAL;
3760 goto out;
3761 }
3762 out:
3763 return err_code;
3764}
Christopher Faulet11610f32017-09-21 10:23:10 +02003765static int
3766cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3767{
3768 struct spoe_group *grp;
3769 const char *err;
3770 int err_code = 0;
3771
3772 if ((cfg_scope == NULL && curengine != NULL) ||
3773 (cfg_scope != NULL && curengine == NULL) ||
3774 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3775 goto out;
3776
3777 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3778 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003779 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3780 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003781 err_code |= ERR_ALERT | ERR_ABORT;
3782 goto out;
3783 }
3784 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3785 err_code |= ERR_ABORT;
3786 goto out;
3787 }
3788
3789 err = invalid_char(args[1]);
3790 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003791 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3792 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003793 err_code |= ERR_ALERT | ERR_ABORT;
3794 goto out;
3795 }
3796
3797 list_for_each_entry(grp, &curgrps, list) {
3798 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003799 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3800 " name as another one declared at %s:%d.\n",
3801 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003802 err_code |= ERR_ALERT | ERR_FATAL;
3803 goto out;
3804 }
3805 }
3806
3807 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003808 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003809 err_code |= ERR_ALERT | ERR_ABORT;
3810 goto out;
3811 }
3812
3813 curgrp->id = strdup(args[1]);
3814 curgrp->conf.file = strdup(file);
3815 curgrp->conf.line = linenum;
3816 LIST_INIT(&curgrp->phs);
3817 LIST_INIT(&curgrp->messages);
3818 LIST_ADDQ(&curgrps, &curgrp->list);
3819 }
3820 else if (!strcmp(args[0], "messages")) {
3821 int cur_arg = 1;
3822 while (*args[cur_arg]) {
3823 struct spoe_placeholder *ph = NULL;
3824
3825 list_for_each_entry(ph, &curgrp->phs, list) {
3826 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003827 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3828 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003829 err_code |= ERR_ALERT | ERR_FATAL;
3830 goto out;
3831 }
3832 }
3833
3834 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003835 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003836 err_code |= ERR_ALERT | ERR_ABORT;
3837 goto out;
3838 }
3839 ph->id = strdup(args[cur_arg]);
3840 LIST_ADDQ(&curgrp->phs, &ph->list);
3841 cur_arg++;
3842 }
3843 }
3844 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003845 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3846 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003847 err_code |= ERR_ALERT | ERR_FATAL;
3848 goto out;
3849 }
3850 out:
3851 return err_code;
3852}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003853
3854static int
3855cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3856{
3857 struct spoe_message *msg;
3858 struct spoe_arg *arg;
3859 const char *err;
3860 char *errmsg = NULL;
3861 int err_code = 0;
3862
3863 if ((cfg_scope == NULL && curengine != NULL) ||
3864 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003865 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003866 goto out;
3867
3868 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3869 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003870 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3871 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003872 err_code |= ERR_ALERT | ERR_ABORT;
3873 goto out;
3874 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003875 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3876 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003877 goto out;
3878 }
3879
3880 err = invalid_char(args[1]);
3881 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003882 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3883 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003884 err_code |= ERR_ALERT | ERR_ABORT;
3885 goto out;
3886 }
3887
3888 list_for_each_entry(msg, &curmsgs, list) {
3889 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003890 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3891 " name as another one declared at %s:%d.\n",
3892 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003893 err_code |= ERR_ALERT | ERR_FATAL;
3894 goto out;
3895 }
3896 }
3897
3898 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003899 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003900 err_code |= ERR_ALERT | ERR_ABORT;
3901 goto out;
3902 }
3903
3904 curmsg->id = strdup(args[1]);
3905 curmsg->id_len = strlen(curmsg->id);
3906 curmsg->event = SPOE_EV_NONE;
3907 curmsg->conf.file = strdup(file);
3908 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003909 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003910 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003911 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003912 LIST_INIT(&curmsg->by_evt);
3913 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003914 LIST_ADDQ(&curmsgs, &curmsg->list);
3915 }
3916 else if (!strcmp(args[0], "args")) {
3917 int cur_arg = 1;
3918
3919 curproxy->conf.args.ctx = ARGC_SPOE;
3920 curproxy->conf.args.file = file;
3921 curproxy->conf.args.line = linenum;
3922 while (*args[cur_arg]) {
3923 char *delim = strchr(args[cur_arg], '=');
3924 int idx = 0;
3925
3926 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003927 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003928 err_code |= ERR_ALERT | ERR_ABORT;
3929 goto out;
3930 }
3931
3932 if (!delim) {
3933 arg->name = NULL;
3934 arg->name_len = 0;
3935 delim = args[cur_arg];
3936 }
3937 else {
3938 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3939 arg->name_len = delim - args[cur_arg];
3940 delim++;
3941 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003942 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3943 &idx, file, linenum, &errmsg,
3944 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003945 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003946 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003947 err_code |= ERR_ALERT | ERR_FATAL;
3948 free(arg->name);
3949 free(arg);
3950 goto out;
3951 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003952 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003953 LIST_ADDQ(&curmsg->args, &arg->list);
3954 cur_arg++;
3955 }
3956 curproxy->conf.args.file = NULL;
3957 curproxy->conf.args.line = 0;
3958 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003959 else if (!strcmp(args[0], "acl")) {
3960 err = invalid_char(args[1]);
3961 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003962 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3963 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003964 err_code |= ERR_ALERT | ERR_FATAL;
3965 goto out;
3966 }
3967 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003968 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3969 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003970 err_code |= ERR_ALERT | ERR_FATAL;
3971 goto out;
3972 }
3973 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003974 else if (!strcmp(args[0], "event")) {
3975 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003976 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003977 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003978 goto out;
3979 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003980 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3981 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003982
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003983 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3984 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3985 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3986 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3987
3988 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3989 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3990 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3991 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3992 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3993 curmsg->event = SPOE_EV_ON_TCP_RSP;
3994
3995 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3996 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3997 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3998 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3999 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
4000 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4001 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004002 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004003 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004004 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004005 goto out;
4006 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004007
4008 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4009 struct acl_cond *cond;
4010
4011 cond = build_acl_cond(file, linenum, &curmsg->acls,
4012 curproxy, (const char **)args+2,
4013 &errmsg);
4014 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004015 ha_alert("parsing [%s:%d] : error detected while "
4016 "parsing an 'event %s' condition : %s.\n",
4017 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004018 err_code |= ERR_ALERT | ERR_FATAL;
4019 goto out;
4020 }
4021 curmsg->cond = cond;
4022 }
4023 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004024 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4025 "or 'unless' followed by a condition but found '%s'.\n",
4026 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004027 err_code |= ERR_ALERT | ERR_FATAL;
4028 goto out;
4029 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004030 }
4031 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004032 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4033 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004034 err_code |= ERR_ALERT | ERR_FATAL;
4035 goto out;
4036 }
4037 out:
4038 free(errmsg);
4039 return err_code;
4040}
4041
4042/* Return -1 on error, else 0 */
4043static int
4044parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4045 struct flt_conf *fconf, char **err, void *private)
4046{
4047 struct list backup_sections;
4048 struct spoe_config *conf;
4049 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004050 struct spoe_group *grp, *grpback;
4051 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004052 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004053 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004054 char *file = NULL, *engine = NULL;
4055 int ret, pos = *cur_arg + 1;
4056
Christopher Faulet84c844e2018-03-23 14:37:14 +01004057 LIST_INIT(&curmsgs);
4058 LIST_INIT(&curgrps);
4059 LIST_INIT(&curmphs);
4060 LIST_INIT(&curgphs);
4061 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004062 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004063 curpxopts = 0;
4064 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004065
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004066 conf = calloc(1, sizeof(*conf));
4067 if (conf == NULL) {
4068 memprintf(err, "%s: out of memory", args[*cur_arg]);
4069 goto error;
4070 }
4071 conf->proxy = px;
4072
4073 while (*args[pos]) {
4074 if (!strcmp(args[pos], "config")) {
4075 if (!*args[pos+1]) {
4076 memprintf(err, "'%s' : '%s' option without value",
4077 args[*cur_arg], args[pos]);
4078 goto error;
4079 }
4080 file = args[pos+1];
4081 pos += 2;
4082 }
4083 else if (!strcmp(args[pos], "engine")) {
4084 if (!*args[pos+1]) {
4085 memprintf(err, "'%s' : '%s' option without value",
4086 args[*cur_arg], args[pos]);
4087 goto error;
4088 }
4089 engine = args[pos+1];
4090 pos += 2;
4091 }
4092 else {
4093 memprintf(err, "unknown keyword '%s'", args[pos]);
4094 goto error;
4095 }
4096 }
4097 if (file == NULL) {
4098 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4099 goto error;
4100 }
4101
4102 /* backup sections and register SPOE sections */
4103 LIST_INIT(&backup_sections);
4104 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004105 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4106 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004107 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004108
4109 /* Parse SPOE filter configuration file */
4110 curengine = engine;
4111 curproxy = px;
4112 curagent = NULL;
4113 curmsg = NULL;
4114 ret = readcfgfile(file);
4115 curproxy = NULL;
4116
4117 /* unregister SPOE sections and restore previous sections */
4118 cfg_unregister_sections();
4119 cfg_restore_sections(&backup_sections);
4120
4121 if (ret == -1) {
4122 memprintf(err, "Could not open configuration file %s : %s",
4123 file, strerror(errno));
4124 goto error;
4125 }
4126 if (ret & (ERR_ABORT|ERR_FATAL)) {
4127 memprintf(err, "Error(s) found in configuration file %s", file);
4128 goto error;
4129 }
4130
4131 /* Check SPOE agent */
4132 if (curagent == NULL) {
4133 memprintf(err, "No SPOE agent found in file %s", file);
4134 goto error;
4135 }
4136 if (curagent->b.name == NULL) {
4137 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4138 curagent->id, curagent->conf.file, curagent->conf.line);
4139 goto error;
4140 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004141 if (curagent->timeout.hello == TICK_ETERNITY ||
4142 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004143 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004144 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4145 " | While not properly invalid, you will certainly encounter various problems\n"
4146 " | with such a configuration. To fix this, please ensure that all following\n"
4147 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4148 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004149 }
4150 if (curagent->var_pfx == NULL) {
4151 char *tmp = curagent->id;
4152
4153 while (*tmp) {
4154 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4155 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4156 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4157 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4158 goto error;
4159 }
4160 tmp++;
4161 }
4162 curagent->var_pfx = strdup(curagent->id);
4163 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004164 if (curagent->engine_id == NULL)
4165 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004166
Christopher Fauletb7426d12018-03-21 14:12:17 +01004167 if (curagent->var_on_error) {
4168 struct arg arg;
4169
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004170 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004171 curagent->var_pfx, curagent->var_on_error);
4172
4173 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004174 arg.data.str.area = trash.area;
4175 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004176 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004177 if (!vars_check_arg(&arg, err)) {
4178 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4179 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4180 goto error;
4181 }
4182 }
4183
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004184 if (curagent->var_t_process) {
4185 struct arg arg;
4186
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004187 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004188 curagent->var_pfx, curagent->var_t_process);
4189
4190 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004191 arg.data.str.area = trash.area;
4192 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004193 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004194 if (!vars_check_arg(&arg, err)) {
4195 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4196 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4197 goto error;
4198 }
4199 }
4200
4201 if (curagent->var_t_total) {
4202 struct arg arg;
4203
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004204 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004205 curagent->var_pfx, curagent->var_t_total);
4206
4207 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004208 arg.data.str.area = trash.area;
4209 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004210 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004211 if (!vars_check_arg(&arg, err)) {
4212 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4213 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4214 goto error;
4215 }
4216 }
4217
Christopher Faulet11610f32017-09-21 10:23:10 +02004218 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004219 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4220 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004221 goto finish;
4222 }
4223
Christopher Faulet11610f32017-09-21 10:23:10 +02004224 /* Replace placeholders by the corresponding messages for the SPOE
4225 * agent */
4226 list_for_each_entry(ph, &curmphs, list) {
4227 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004228 struct spoe_arg *arg;
4229 unsigned int where;
4230
Christopher Faulet11610f32017-09-21 10:23:10 +02004231 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004232 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4233 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4234 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4235 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4236 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4237 }
4238 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4239 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4240 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004241 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4242 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004243 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004244 }
4245 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004246 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4247 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004248 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004249 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004250
4251 where = 0;
4252 switch (msg->event) {
4253 case SPOE_EV_ON_CLIENT_SESS:
4254 where |= SMP_VAL_FE_CON_ACC;
4255 break;
4256
4257 case SPOE_EV_ON_TCP_REQ_FE:
4258 where |= SMP_VAL_FE_REQ_CNT;
4259 break;
4260
4261 case SPOE_EV_ON_HTTP_REQ_FE:
4262 where |= SMP_VAL_FE_HRQ_HDR;
4263 break;
4264
4265 case SPOE_EV_ON_TCP_REQ_BE:
4266 if (px->cap & PR_CAP_FE)
4267 where |= SMP_VAL_FE_REQ_CNT;
4268 if (px->cap & PR_CAP_BE)
4269 where |= SMP_VAL_BE_REQ_CNT;
4270 break;
4271
4272 case SPOE_EV_ON_HTTP_REQ_BE:
4273 if (px->cap & PR_CAP_FE)
4274 where |= SMP_VAL_FE_HRQ_HDR;
4275 if (px->cap & PR_CAP_BE)
4276 where |= SMP_VAL_BE_HRQ_HDR;
4277 break;
4278
4279 case SPOE_EV_ON_SERVER_SESS:
4280 where |= SMP_VAL_BE_SRV_CON;
4281 break;
4282
4283 case SPOE_EV_ON_TCP_RSP:
4284 if (px->cap & PR_CAP_FE)
4285 where |= SMP_VAL_FE_RES_CNT;
4286 if (px->cap & PR_CAP_BE)
4287 where |= SMP_VAL_BE_RES_CNT;
4288 break;
4289
4290 case SPOE_EV_ON_HTTP_RSP:
4291 if (px->cap & PR_CAP_FE)
4292 where |= SMP_VAL_FE_HRS_HDR;
4293 if (px->cap & PR_CAP_BE)
4294 where |= SMP_VAL_BE_HRS_HDR;
4295 break;
4296
4297 default:
4298 break;
4299 }
4300
4301 list_for_each_entry(arg, &msg->args, list) {
4302 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004303 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004304 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004305 "none of which is available here ('%s')",
4306 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004307 sample_ckp_names(arg->expr->fetch->use),
4308 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004309 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004310 }
4311 }
4312
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004313 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004314 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4315 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004316 }
4317 }
4318 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004319 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004320 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004321 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004322 continue;
4323 }
4324
Christopher Faulet11610f32017-09-21 10:23:10 +02004325 /* Replace placeholders by the corresponding groups for the SPOE
4326 * agent */
4327 list_for_each_entry(ph, &curgphs, list) {
4328 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4329 if (!strcmp(grp->id, ph->id)) {
4330 grp->agent = curagent;
4331 LIST_DEL(&grp->list);
4332 LIST_ADDQ(&curagent->groups, &grp->list);
4333 goto next_aph;
4334 }
4335 }
4336 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4337 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4338 goto error;
4339 next_aph:
4340 continue;
4341 }
4342
4343 /* Replace placeholders by the corresponding message for each SPOE
4344 * group of the SPOE agent */
4345 list_for_each_entry(grp, &curagent->groups, list) {
4346 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4347 list_for_each_entry(msg, &curmsgs, list) {
4348 if (!strcmp(msg->id, ph->id)) {
4349 if (msg->group != NULL) {
4350 memprintf(err, "SPOE message '%s' already belongs to "
4351 "the SPOE group '%s' declare at %s:%d",
4352 msg->id, msg->group->id,
4353 msg->group->conf.file,
4354 msg->group->conf.line);
4355 goto error;
4356 }
4357
4358 /* Scope for arguments are not checked for now. We will check
4359 * them only if a rule use the corresponding SPOE group. */
4360 msg->agent = curagent;
4361 msg->group = grp;
4362 LIST_DEL(&ph->list);
4363 LIST_ADDQ(&grp->messages, &msg->by_grp);
4364 goto next_mph_grp;
4365 }
4366 }
4367 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4368 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4369 goto error;
4370 next_mph_grp:
4371 continue;
4372 }
4373 }
4374
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004375 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004376 /* move curmsgs to the agent message list */
4377 curmsgs.n->p = &curagent->messages;
4378 curmsgs.p->n = &curagent->messages;
4379 curagent->messages = curmsgs;
4380 LIST_INIT(&curmsgs);
4381
Christopher Faulet7ee86672017-09-19 11:08:28 +02004382 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004383 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004384
4385 /* Start agent's proxy initialization here. It will be finished during
4386 * the filter init. */
4387 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4388 init_new_proxy(&conf->agent_fe);
4389 conf->agent_fe.id = conf->agent->id;
4390 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004391 conf->agent_fe.options |= curpxopts;
4392 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004393
4394 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4395 LIST_DEL(&logsrv->list);
4396 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4397 }
4398
Christopher Faulet11610f32017-09-21 10:23:10 +02004399 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4400 LIST_DEL(&ph->list);
4401 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004402 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004403 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4404 LIST_DEL(&ph->list);
4405 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004406 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004407 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4408 struct arg arg;
4409
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004410 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004411 curagent->var_pfx, vph->name);
4412
4413 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004414 arg.data.str.area = trash.area;
4415 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004416 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004417 if (!vars_check_arg(&arg, err)) {
4418 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4419 curagent->id, curagent->var_pfx, vph->name, *err);
4420 goto error;
4421 }
4422
4423 LIST_DEL(&vph->list);
4424 free(vph->name);
4425 free(vph);
4426 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004427 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4428 LIST_DEL(&grp->list);
4429 spoe_release_group(grp);
4430 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004431 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004432 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004433 fconf->ops = &spoe_ops;
4434 fconf->conf = conf;
4435 return 0;
4436
4437 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004438 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004439 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4440 LIST_DEL(&ph->list);
4441 spoe_release_placeholder(ph);
4442 }
4443 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4444 LIST_DEL(&ph->list);
4445 spoe_release_placeholder(ph);
4446 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004447 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4448 LIST_DEL(&vph->list);
4449 free(vph->name);
4450 free(vph);
4451 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004452 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4453 LIST_DEL(&grp->list);
4454 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004455 }
4456 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4457 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004458 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004459 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004460 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4461 LIST_DEL(&logsrv->list);
4462 free(logsrv);
4463 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004464 free(conf);
4465 return -1;
4466}
4467
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004468/* Send message of a SPOE group. This is the action_ptr callback of a rule
4469 * associated to a "send-spoe-group" action.
4470 *
4471 * It returns ACT_RET_CONT is processing is finished without error, it returns
4472 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4473 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004474static enum act_return
4475spoe_send_group(struct act_rule *rule, struct proxy *px,
4476 struct session *sess, struct stream *s, int flags)
4477{
4478 struct filter *filter;
4479 struct spoe_agent *agent = NULL;
4480 struct spoe_group *group = NULL;
4481 struct spoe_context *ctx = NULL;
4482 int ret, dir;
4483
4484 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4485 if (filter->config == rule->arg.act.p[0]) {
4486 agent = rule->arg.act.p[2];
4487 group = rule->arg.act.p[3];
4488 ctx = filter->ctx;
4489 break;
4490 }
4491 }
4492 if (agent == NULL || group == NULL || ctx == NULL)
4493 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004494 if (ctx->state == SPOE_CTX_ST_NONE)
4495 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004496
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004497 switch (rule->from) {
4498 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4499 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4500 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4501 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4502 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4503 default:
4504 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4505 " - internal error while execute spoe-send-group\n",
4506 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4507 __FUNCTION__, s);
4508 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4509 agent->id);
4510 return ACT_RET_CONT;
4511 }
4512
4513 ret = spoe_process_group(s, ctx, group, dir);
4514 if (ret == 1)
4515 return ACT_RET_CONT;
4516 else if (ret == 0) {
4517 if (flags & ACT_FLAG_FINAL) {
4518 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4519 " - failed to process group '%s': interrupted by caller\n",
4520 (int)now.tv_sec, (int)now.tv_usec,
4521 agent->id, __FUNCTION__, s, group->id);
4522 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004523 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004524 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004525 return ACT_RET_CONT;
4526 }
4527 return ACT_RET_YIELD;
4528 }
4529 else
4530 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004531}
4532
4533/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4534 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4535 * action should be:
4536 *
4537 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4538 *
4539 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4540 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4541 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4542 * group.
4543 *
4544 * The function returns 1 in success case, otherwise, it returns 0 and err is
4545 * filled.
4546 */
4547static int
4548check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4549{
4550 struct flt_conf *fconf;
4551 struct spoe_config *conf;
4552 struct spoe_agent *agent = NULL;
4553 struct spoe_group *group;
4554 struct spoe_message *msg;
4555 char *engine_id = rule->arg.act.p[0];
4556 char *group_id = rule->arg.act.p[1];
4557 unsigned int where = 0;
4558
4559 switch (rule->from) {
4560 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4561 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4562 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4563 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4564 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4565 default:
4566 memprintf(err,
4567 "internal error, unexpected rule->from=%d, please report this bug!",
4568 rule->from);
4569 goto error;
4570 }
4571
4572 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4573 * <px> */
4574 list_for_each_entry(fconf, &px->filter_configs, list) {
4575 conf = fconf->conf;
4576
4577 /* This is not an SPOE filter */
4578 if (fconf->id != spoe_filter_id)
4579 continue;
4580
4581 /* This is the good engine */
4582 if (!strcmp(conf->id, engine_id)) {
4583 agent = conf->agent;
4584 break;
4585 }
4586 }
4587 if (agent == NULL) {
4588 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4589 engine_id, group_id);
4590 goto error;
4591 }
4592
4593 /* Try to find the right group */
4594 list_for_each_entry(group, &agent->groups, list) {
4595 /* This is the good group */
4596 if (!strcmp(group->id, group_id))
4597 break;
4598 }
4599 if (&group->list == &agent->groups) {
4600 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4601 group_id, engine_id);
4602 goto error;
4603 }
4604
4605 /* Ok, we found the group, we need to check messages and their
4606 * arguments */
4607 list_for_each_entry(msg, &group->messages, by_grp) {
4608 struct spoe_arg *arg;
4609
4610 list_for_each_entry(arg, &msg->args, list) {
4611 if (!(arg->expr->fetch->val & where)) {
4612 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4613 "some args extract information from '%s',"
4614 "none of which is available here ('%s')",
4615 msg->id, group->id, msg->conf.file, msg->conf.line,
4616 sample_ckp_names(arg->expr->fetch->use),
4617 sample_ckp_names(where));
4618 goto error;
4619 }
4620 }
4621 }
4622
4623 free(engine_id);
4624 free(group_id);
4625 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4626 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4627 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4628 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4629 return 1;
4630
4631 error:
4632 free(engine_id);
4633 free(group_id);
4634 return 0;
4635}
4636
4637/* Parse 'send-spoe-group' action following the format:
4638 *
4639 * ... send-spoe-group <engine-id> <group-id>
4640 *
4641 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4642 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4643 * ids are saved and used later, when the rule will be checked.
4644 */
4645static enum act_parse_ret
4646parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4647 struct act_rule *rule, char **err)
4648{
4649 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4650 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4651 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4652 return ACT_RET_PRS_ERR;
4653 }
4654 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4655 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4656
4657 (*orig_arg) += 2;
4658
4659 rule->action = ACT_CUSTOM;
4660 rule->action_ptr = spoe_send_group;
4661 rule->check_ptr = check_send_spoe_group;
4662 return ACT_RET_PRS_OK;
4663}
4664
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004665
4666/* Declare the filter parser for "spoe" keyword */
4667static struct flt_kw_list flt_kws = { "SPOE", { }, {
4668 { "spoe", parse_spoe_flt, NULL },
4669 { NULL, NULL, NULL },
4670 }
4671};
4672
Willy Tarreau0108d902018-11-25 19:14:37 +01004673INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4674
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004675/* Delcate the action parser for "spoe-action" keyword */
4676static struct action_kw_list tcp_req_action_kws = { { }, {
4677 { "send-spoe-group", parse_send_spoe_group },
4678 { /* END */ },
4679 }
4680};
Willy Tarreau0108d902018-11-25 19:14:37 +01004681
4682INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4683
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004684static struct action_kw_list tcp_res_action_kws = { { }, {
4685 { "send-spoe-group", parse_send_spoe_group },
4686 { /* END */ },
4687 }
4688};
Willy Tarreau0108d902018-11-25 19:14:37 +01004689
4690INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4691
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004692static struct action_kw_list http_req_action_kws = { { }, {
4693 { "send-spoe-group", parse_send_spoe_group },
4694 { /* END */ },
4695 }
4696};
Willy Tarreau0108d902018-11-25 19:14:37 +01004697
4698INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4699
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004700static struct action_kw_list http_res_action_kws = { { }, {
4701 { "send-spoe-group", parse_send_spoe_group },
4702 { /* END */ },
4703 }
4704};
4705
Willy Tarreau0108d902018-11-25 19:14:37 +01004706INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);