blob: da574c4463548cbc6afc9e84f2be732bc31f9b55 [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 Fauletd6e54662021-08-02 17:53:56 +0200112static struct appctx *spoe_create_appctx(struct spoe_config *conf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113
114/********************************************************************
115 * helper functions/globals
116 ********************************************************************/
117static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200118spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119{
Christopher Faulet11610f32017-09-21 10:23:10 +0200120 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200122 free(ph->id);
123 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200124}
125
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200126static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100127spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128{
Christopher Faulet57583e42017-09-04 15:41:09 +0200129 struct spoe_arg *arg, *argback;
130 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200131
132 if (!msg)
133 return;
134 free(msg->id);
135 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200136 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200137 release_sample_expr(arg->expr);
138 free(arg->name);
139 LIST_DEL(&arg->list);
140 free(arg);
141 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200142 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
143 LIST_DEL(&acl->list);
144 prune_acl(acl);
145 free(acl);
146 }
147 if (msg->cond) {
148 prune_acl_cond(msg->cond);
149 free(msg->cond);
150 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200151 free(msg);
152}
153
154static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200155spoe_release_group(struct spoe_group *grp)
156{
157 if (!grp)
158 return;
159 free(grp->id);
160 free(grp->conf.file);
161 free(grp);
162}
163
164static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100165spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200166{
Christopher Faulet11610f32017-09-21 10:23:10 +0200167 struct spoe_message *msg, *msgback;
168 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200169 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200170
171 if (!agent)
172 return;
173 free(agent->id);
174 free(agent->conf.file);
175 free(agent->var_pfx);
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) {
Christopher Faulet46c76822019-09-17 11:55:52 +0200188 for (i = 0; i < global.nbthread; ++i) {
189 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100190 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet46c76822019-09-17 11:55:52 +0200191 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100192 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200193 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200194 free(agent);
195}
196
197static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100198 [SPOE_FRM_ERR_NONE] = "normal",
199 [SPOE_FRM_ERR_IO] = "I/O error",
200 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
201 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
202 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
203 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
204 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
205 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
206 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
207 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
208 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
209 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100210 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100211 [SPOE_FRM_ERR_RES] = "resource allocation error",
212 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200213};
214
215static const char *spoe_event_str[SPOE_EV_EVENTS] = {
216 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
217 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
218 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
219 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
220 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
221
222 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
223 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
224 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
225};
226
227
228#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
229
230static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100231 [SPOE_CTX_ST_NONE] = "NONE",
232 [SPOE_CTX_ST_READY] = "READY",
233 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
234 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
235 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
236 [SPOE_CTX_ST_DONE] = "DONE",
237 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200238};
239
240static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100241 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
242 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
243 [SPOE_APPCTX_ST_IDLE] = "IDLE",
244 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
245 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
246 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
247 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
248 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
249 [SPOE_APPCTX_ST_EXIT] = "EXIT",
250 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200251};
252
253#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100254
Christopher Faulet8ef75252017-02-20 22:56:03 +0100255/* Used to generates a unique id for an engine. On success, it returns a
256 * allocated string. So it is the caller's reponsibility to release it. If the
257 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100258static char *
259generate_pseudo_uuid()
260{
Christopher Fauleta1cda022016-12-21 08:58:06 +0100261 char *uuid;
Christopher Fauleteaf11912019-09-17 15:07:02 +0200262 uint32_t rnd[4] = { 0, 0, 0, 0 };
263 uint64_t last = 0;
264 int byte = 0;
265 uint8_t bits = 0;
266 unsigned int rand_max_bits = my_flsl(RAND_MAX);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100267
Christopher Fauleteaf11912019-09-17 15:07:02 +0200268 if ((uuid = calloc(1, 37)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100269 return NULL;
270
Christopher Fauleteaf11912019-09-17 15:07:02 +0200271 while (byte < 4) {
272 while (bits < 32) {
Willy Tarreau861c4ef2020-03-08 00:42:37 +0100273 last |= (uint64_t)ha_random() << bits;
Christopher Fauleteaf11912019-09-17 15:07:02 +0200274 bits += rand_max_bits;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100275 }
Christopher Fauleteaf11912019-09-17 15:07:02 +0200276 rnd[byte++] = last;
277 last >>= 32u;
278 bits -= 32;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100279 }
Willy Tarreau079ec122019-10-29 10:25:49 +0100280 snprintf(uuid, 37, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx",
Christopher Fauleteaf11912019-09-17 15:07:02 +0200281 rnd[0],
282 rnd[1] & 0xFFFF,
283 ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version
284 (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10),
285 (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull
286 );
Christopher Fauleta1cda022016-12-21 08:58:06 +0100287 return uuid;
288}
289
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100290
291static inline void
292spoe_update_stat_time(struct timeval *tv, long *t)
293{
294 if (*t == -1)
295 *t = tv_ms_elapsed(tv, &now);
296 else
297 *t += tv_ms_elapsed(tv, &now);
298 tv_zero(tv);
299}
300
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301/********************************************************************
302 * Functions that encode/decode SPOE frames
303 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304/* Helper to get static string length, excluding the terminating null byte */
305#define SLEN(str) (sizeof(str)-1)
306
307/* Predefined key used in HELLO/DISCONNECT frames */
308#define SUPPORTED_VERSIONS_KEY "supported-versions"
309#define VERSION_KEY "version"
310#define MAX_FRAME_SIZE_KEY "max-frame-size"
311#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100312#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100313#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200314#define STATUS_CODE_KEY "status-code"
315#define MSG_KEY "message"
316
317struct spoe_version {
318 char *str;
319 int min;
320 int max;
321};
322
323/* All supported versions */
324static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200325 /* 1.0 is now unsupported because of a bug about frame's flags*/
326 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200327 {NULL, 0, 0}
328};
329
330/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200331#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333/* Convert a string to a SPOE version value. The string must follow the format
334 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
335 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200336static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100337spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200338{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100339 const char *p, *end;
340 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200341
Christopher Faulet8ef75252017-02-20 22:56:03 +0100342 p = str;
343 end = str+len;
344 maj = min = 0;
345 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347 /* skip leading spaces */
348 while (p < end && isspace(*p))
349 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200350
Christopher Faulet8ef75252017-02-20 22:56:03 +0100351 /* parse Major number, until the '.' */
352 while (*p != '.') {
353 if (p >= end || *p < '0' || *p > '9')
354 goto out;
355 maj *= 10;
356 maj += (*p - '0');
357 p++;
358 }
359
360 /* check Major version */
361 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200362 goto out;
363
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364 p++; /* skip the '.' */
365 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
366 goto out;
367
368 /* Parse Minor number */
369 while (p < end) {
370 if (*p < '0' || *p > '9')
371 break;
372 min *= 10;
373 min += (*p - '0');
374 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376
377 /* check Minor number */
378 if (min > 999)
379 goto out;
380
381 /* skip trailing spaces */
382 while (p < end && isspace(*p))
383 p++;
384 if (p != end)
385 goto out;
386
387 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388 out:
389 return vsn;
390}
391
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800393 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200395static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100396spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397{
Willy Tarreau83061a82018-07-13 11:56:34 +0200398 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100399 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 char *p, *end;
401 unsigned int flags = SPOE_FRM_FL_FIN;
402 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200403
Christopher Faulet8ef75252017-02-20 22:56:03 +0100404 p = frame;
405 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200406
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 /* Set Frame type */
408 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200409
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100410 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200411 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 memcpy(p, (char *)&flags, 4);
413 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200414
415 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100416 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200417
418 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
419 * and "capabilities" */
420
421 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100422 sz = SLEN(SUPPORTED_VERSIONS_KEY);
423 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
424 goto too_big;
425
426 *p++ = SPOE_DATA_T_STR;
427 sz = SLEN(SUPPORTED_VERSIONS_VAL);
428 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
429 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200430
431 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 sz = SLEN(MAX_FRAME_SIZE_KEY);
433 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
434 goto too_big;
435
436 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200437 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100438 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200439
440 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441 sz = SLEN(CAPABILITIES_KEY);
442 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
443 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200444
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100446 chk = get_trash_chunk();
447 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200448 memcpy(chk->area, "pipelining", 10);
449 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100450 }
451 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200452 if (chk->data) chk->area[chk->data++] = ',';
453 memcpy(chk->area+chk->data, "async", 5);
454 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100455 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100456 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200457 if (chk->data) chk->area[chk->data++] = ',';
458 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100459 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100460 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200461 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 goto too_big;
463
464 /* (optionnal) "engine-id" K/V item, if present */
Christopher Faulet46c76822019-09-17 11:55:52 +0200465 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 sz = SLEN(ENGINE_ID_KEY);
467 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
468 goto too_big;
469
470 *p++ = SPOE_DATA_T_STR;
Christopher Faulet46c76822019-09-17 11:55:52 +0200471 sz = strlen(agent->rt[tid].engine_id);
472 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100474 }
475
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476 return (p - frame);
477
478 too_big:
479 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
480 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200481}
482
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
484 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
485 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 const char *reason;
490 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100491 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 p = frame;
495 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200496
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 /* Set Frame type */
498 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200499
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100500 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200501 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100502 memcpy(p, (char *)&flags, 4);
503 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200504
505 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100506 *p++ = 0; *p++ = 0;
507
508 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
509 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200510
511 /* There are 2 mandatory items: "status-code" and "message" */
512
513 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514 sz = SLEN(STATUS_CODE_KEY);
515 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
516 goto too_big;
517
518 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200519 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100520 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200521
522 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 sz = SLEN(MSG_KEY);
524 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
525 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200526
Christopher Faulet8ef75252017-02-20 22:56:03 +0100527 /*Get the message corresponding to the status code */
528 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
529
530 *p++ = SPOE_DATA_T_STR;
531 sz = strlen(reason);
532 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
533 goto too_big;
534
535 return (p - frame);
536
537 too_big:
538 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
539 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200540}
541
Christopher Faulet8ef75252017-02-20 22:56:03 +0100542/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
543 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
544 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200545static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100546spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100547 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200548{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100549 char *p, *end;
550 unsigned int stream_id, frame_id;
551 unsigned int flags = SPOE_FRM_FL_FIN;
552 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200553
Christopher Faulet8ef75252017-02-20 22:56:03 +0100554 p = frame;
555 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200556
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 stream_id = ctx->stream_id;
558 frame_id = ctx->frame_id;
559
560 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
561 /* The fragmentation is not supported by the applet */
562 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
563 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
564 return -1;
565 }
566 flags = ctx->frag_ctx.flags;
567 }
568
569 /* Set Frame type */
570 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
571
572 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200573 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100574 memcpy(p, (char *)&flags, 4);
575 p += 4;
576
577 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200578 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100579 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200580 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100581 goto too_big;
582
583 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200584 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100585 if (p + sz >= end)
586 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200587 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100588 p += sz;
589
590 return (p - frame);
591
592 too_big:
593 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
594 return 0;
595}
596
597/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
598 * returns the number of encoded bytes in the frame on success, 0 if an encoding
599 * error occurred and -1 if a fatal error occurred. */
600static int
601spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
602 char *frame, size_t size)
603{
604 char *p, *end;
605 unsigned int stream_id, frame_id;
606 unsigned int flags;
607 size_t sz;
608
609 p = frame;
610 end = frame+size;
611
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 /* <ctx> is null when the stream has aborted the processing of a
613 * fragmented frame. In this case, we must notify the corresponding
614 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100615 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100616 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100617 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
618 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
619 }
620 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100621 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100622 stream_id = ctx->stream_id;
623 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100624 }
625
Christopher Faulet8ef75252017-02-20 22:56:03 +0100626 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100627 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100629 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200630 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631 memcpy(p, (char *)&flags, 4);
632 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633
634 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200635 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200637 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100640 if (ctx == NULL)
641 goto end;
642
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200644 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (p + sz >= end)
646 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200647 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100649
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100650 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 return (p - frame);
652
653 too_big:
654 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
655 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200656}
657
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658/* Decode and process the HELLO frame sent by an agent. It returns the number of
659 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
660 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663{
Christopher Faulet305c6072017-02-23 16:17:53 +0100664 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
665 char *p, *end;
666 int vsn, max_frame_size;
667 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668
669 p = frame;
670 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671
672 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
674 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200675 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 if (size < 7 /* TYPE + METADATA */) {
679 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
680 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200681 }
682
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100683 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200685 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100688 /* Fragmentation is not supported for HELLO frame */
689 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100691 return -1;
692 }
693
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 if (*p != 0 || *(p+1) != 0) {
696 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
697 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200698 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700
701 /* There are 3 mandatory items: "version", "max-frame-size" and
702 * "capabilities" */
703
704 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100705 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 while (p < end) {
707 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200708 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100709 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200710
711 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 ret = spoe_decode_buffer(&p, end, &str, &sz);
713 if (ret == -1 || !sz) {
714 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
715 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100717
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 /* Check "version" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200719 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 int i, type = *p++;
721
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
724 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
725 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
728 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
729 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 }
731
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200733 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 for (i = 0; supported_versions[i].str != NULL; ++i) {
738 if (vsn >= supported_versions[i].min &&
739 vsn <= supported_versions[i].max)
740 break;
741 }
742 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200744 return -1;
745 }
746 }
747 /* Check "max-frame-size" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200748 else if (sz >= strlen(MAX_FRAME_SIZE_KEY) && !memcmp(str, MAX_FRAME_SIZE_KEY, strlen(MAX_FRAME_SIZE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200750
751 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200752 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
753 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
754 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
755 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
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 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200759 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100760 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
761 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200762 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100763 if (sz < MIN_FRAME_SIZE ||
764 sz > SPOE_APPCTX(appctx)->max_frame_size) {
765 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200766 return -1;
767 }
768 max_frame_size = sz;
769 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 /* Check "capabilities" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200771 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100773
774 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
776 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
777 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100778 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
780 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
781 return 0;
782 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100783
Christopher Faulet8ef75252017-02-20 22:56:03 +0100784 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100785 char *delim;
786
787 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100789
Christopher Faulet8ef75252017-02-20 22:56:03 +0100790 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
791 str += 10; sz -= 10;
792 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100793 flags |= SPOE_APPCTX_FL_PIPELINING;
794 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 else if (sz >= 5 && !strncmp(str, "async", 5)) {
796 str += 5; sz -= 5;
797 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100798 flags |= SPOE_APPCTX_FL_ASYNC;
799 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
801 str += 13; sz -= 13;
802 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100803 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
804 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100805
Christopher Faulet8ef75252017-02-20 22:56:03 +0100806 /* Get the next comma or break */
807 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100808 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100809 delim++;
810 sz -= (delim - str);
811 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100812 }
813 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200814 else {
815 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816 if (spoe_skip_data(&p, end) == -1) {
817 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
818 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820 }
821 }
822
823 /* Final checks */
824 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826 return -1;
827 }
828 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830 return -1;
831 }
Christopher Faulet11389012019-02-07 16:13:26 +0100832 if (!agent)
833 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
834 else {
835 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
836 flags &= ~SPOE_APPCTX_FL_PIPELINING;
837 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
838 flags &= ~SPOE_APPCTX_FL_ASYNC;
839 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200840
Christopher Faulet42bfa462017-01-04 14:14:19 +0100841 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
842 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
843 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844
845 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200846}
847
848/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
849 * bytes on success, 0 if the frame can be ignored and -1 if an error
850 * occurred. */
851static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100855 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856
857 p = frame;
858 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859
860 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
862 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 if (size < 7 /* TYPE + METADATA */) {
867 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
868 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 }
870
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100871 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200873 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100876 /* Fragmentation is not supported for DISCONNECT frame */
877 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100879 return -1;
880 }
881
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200882 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 if (*p != 0 || *(p+1) != 0) {
884 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
885 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888
889 /* There are 2 mandatory items: "status-code" and "message" */
890
891 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 while (p < end) {
893 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200894 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896
897 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 ret = spoe_decode_buffer(&p, end, &str, &sz);
899 if (ret == -1 || !sz) {
900 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
901 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
903
904 /* Check "status-code" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200905 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907
908 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200909 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
910 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
911 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
912 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
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 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200916 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921 }
922
923 /* Check "message" K/V item */
Willy Tarreau249346d2020-06-16 17:58:14 +0200924 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925 int type = *p++;
926
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
929 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
930 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200931 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 ret = spoe_decode_buffer(&p, end, &str, &sz);
933 if (ret == -1 || sz > 255) {
934 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
935 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200936 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100937#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
938 SPOE_APPCTX(appctx)->reason = str;
939 SPOE_APPCTX(appctx)->rlen = sz;
940#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941 }
942 else {
943 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (spoe_skip_data(&p, end) == -1) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
946 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948 }
949 }
950
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952}
953
954
Christopher Fauleta1cda022016-12-21 08:58:06 +0100955/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200956 * success, 0 if the frame can be ignored and -1 if an error occurred. */
957static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200960{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100963 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100965 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966
967 p = frame;
968 end = frame + size;
969 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970
971 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
973 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200974 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200976
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 if (size < 7 /* TYPE + METADATA */) {
978 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
979 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200980 }
981
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100982 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200984 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200986
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100987 /* Fragmentation is not supported for now */
988 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100989 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100990 return -1;
991 }
992
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200993 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200994 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100995 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100996 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100997 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200998 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100999 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001000 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001001 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001002
Christopher Faulet8ef75252017-02-20 22:56:03 +01001003 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +01001004 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +02001005 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001006 if ((*ctx)->stream_id == (unsigned int)stream_id &&
1007 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001008 goto found;
1009 }
1010 }
1011 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001012 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1013 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001014 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001015 goto found;
1016 }
1017 }
1018
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001019 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1020 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1021 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1022
1023 /* ABRT bit is set for an unfinished fragmented frame */
1024 if (flags & SPOE_FRM_FL_ABRT) {
1025 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001026 (*ctx)->state = SPOE_CTX_ST_ERROR;
1027 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1028 /* Ignore the payload */
1029 goto end;
1030 }
1031 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1032 /* For now, we ignore the ack */
1033 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1034 return 0;
1035 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036
Christopher Fauleta1cda022016-12-21 08:58:06 +01001037 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001038 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1039 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 " - stream-id=%u - frame-id=%u\n",
1041 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1042 __FUNCTION__, appctx,
1043 (unsigned int)stream_id, (unsigned int)frame_id);
1044
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001045 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletf4e52512020-11-10 14:31:39 +01001046 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1047 /* Report an error if we are waiting the ack for another frame,
1048 * but not if there is no longer frame waiting for a ack
1049 * (timeout)
1050 */
1051 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1052 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1053 return -1;
1054 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1055 SPOE_APPCTX(appctx)->cur_fpa = 0;
1056 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001057 return 0;
1058
1059 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1061 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001062 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001063 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001064 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001065
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001066 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001067 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001068 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1069 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001070 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001071
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001072 /* Transfer the buffer ownership to the SPOE context */
1073 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001074 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 (*ctx)->state = SPOE_CTX_ST_DONE;
1077
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001078 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001079 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 " - ACK frame received"
1081 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001082 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1084 (*ctx)->frame_id, flags);
1085 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001086}
1087
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088/* This function is used in cfgparse.c and declared in proto/checks.h. It
1089 * prepare the request to send to agents during a healthcheck. It returns 0 on
1090 * success and -1 if an error occurred. */
1091int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 struct appctx appctx;
1095 struct spoe_appctx spoe_appctx;
1096 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1097 size_t sz;
1098 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001099
Christopher Faulet42bfa462017-01-04 14:14:19 +01001100 memset(&appctx, 0, sizeof(appctx));
1101 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001102 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001103
1104 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001105 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1108 end = frame + MAX_FRAME_SIZE;
1109
1110 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1111 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001112 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 /* Add "healthcheck" K/V item */
1116 sz = SLEN(HEALTHCHECK_KEY);
1117 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1118 return -1;
1119 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120
Christopher Faulet8ef75252017-02-20 22:56:03 +01001121 *len = frame - buf;
1122 sz = htonl(*len - 4);
1123 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001127 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 return 0;
1129}
1130
1131/* This function is used in checks.c and declared in proto/checks.h. It decode
1132 * the response received from an agent during a healthcheck. It returns 0 on
1133 * success and -1 if an error occurred. */
1134int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001136{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001137 struct appctx appctx;
1138 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001139
Christopher Faulet42bfa462017-01-04 14:14:19 +01001140 memset(&appctx, 0, sizeof(appctx));
1141 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001142
Christopher Faulet42bfa462017-01-04 14:14:19 +01001143 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001144 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001145
Christopher Faulet8ef75252017-02-20 22:56:03 +01001146 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1147 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001148 goto error;
1149 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001150 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1151 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001152
1153 return 0;
1154
1155 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001156 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1157 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1158 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001159 return -1;
1160}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001161
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1163 * the frame can be ignored, 1 to retry later, and the frame legnth on
1164 * success. */
1165static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001166spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167{
1168 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001169 int ret;
1170 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171
Christopher Faulet8ef75252017-02-20 22:56:03 +01001172 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1173 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174 netint = htonl(framesz);
1175 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001176 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001178 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001179 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001181 }
1182 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001183 return -1; /* error */
1184 }
1185 return framesz;
1186}
1187
1188/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1189 * when the frame can be ignored, 1 to retry later and the frame length on
1190 * success. */
1191static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001192spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001193{
1194 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001195 int ret;
1196 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001197
Willy Tarreau06d80a92017-10-19 14:32:15 +02001198 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001199 if (ret > 0) {
1200 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001201 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001202 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001203 return -1;
1204 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001205 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001206 }
1207 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001209 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210 }
1211 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001212 return -1; /* error */
1213 }
1214 return framesz;
1215}
1216
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001217/********************************************************************
1218 * Functions that manage the SPOE applet
1219 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001220static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001222{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001223 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001224 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001225 appctx_wakeup(appctx);
1226 return 1;
1227}
1228
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001229/* Callback function that catches applet timeouts. If a timeout occurred, we set
1230 * <appctx->st1> flag and the SPOE applet is woken up. */
1231static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001232spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001233{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001234 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001235
1236 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1237 if (tick_is_expired(task->expire, now_ms)) {
1238 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001239 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242 return task;
1243}
1244
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001245/* Callback function that releases a SPOE applet. This happens when the
1246 * connection with the agent is closed. */
1247static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001248spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 struct stream_interface *si = appctx->owner;
1251 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1252 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001253 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254
1255 if (spoe_appctx == NULL)
1256 return;
1257
1258 appctx->ctx.spoe.ptr = NULL;
1259 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260
1261 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1262 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1263 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001266 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001267 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1269 LIST_DEL(&spoe_appctx->list);
1270 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001271 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001272 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001273
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001276 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1277 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001278 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001279 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001280
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001281 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1283 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001284
1285 si_shutw(si);
1286 si_shutr(si);
1287 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288 }
1289
Christopher Faulet8ef75252017-02-20 22:56:03 +01001290 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001291 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 /* Notify all waiting streams */
1294 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001295 LIST_DEL(&ctx->list);
1296 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001297 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001298 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Faulet746326a2020-11-10 18:45:34 +01001299 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001301 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001302 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001303 }
1304
Christopher Faulet8ef75252017-02-20 22:56:03 +01001305 /* If the applet was processing a fragmented frame, notify the
1306 * corresponding stream. */
1307 if (spoe_appctx->frag_ctx.ctx) {
1308 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001309 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001310 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001311 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001312 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1313 }
1314
Christopher Faulet746326a2020-11-10 18:45:34 +01001315 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1316 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1317 if (ctx->spoe_appctx == spoe_appctx)
1318 ctx->spoe_appctx = NULL;
1319 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001320 goto end;
Christopher Faulet746326a2020-11-10 18:45:34 +01001321 }
Christopher Fauletd6e54662021-08-02 17:53:56 +02001322 else {
1323 /* It is the last running applet and the sending and waiting
1324 * queues are not empty. Try to start a new one if HAproxy is
1325 * not stopping.
1326 */
1327 if (!stopping &&
1328 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
1329 spoe_create_appctx(agent->spoe_conf))
1330 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001331
Christopher Fauletd6e54662021-08-02 17:53:56 +02001332 /* otherwise, notify all waiting streams */
1333 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1334 LIST_DEL(&ctx->list);
1335 LIST_INIT(&ctx->list);
1336 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
1337 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1338 ctx->spoe_appctx = NULL;
1339 ctx->state = SPOE_CTX_ST_ERROR;
1340 ctx->status_code = (spoe_appctx->status_code + 0x100);
1341 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1342 }
1343 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1344 LIST_DEL(&ctx->list);
1345 LIST_INIT(&ctx->list);
1346 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
1347 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1348 ctx->spoe_appctx = NULL;
1349 ctx->state = SPOE_CTX_ST_ERROR;
1350 ctx->status_code = (spoe_appctx->status_code + 0x100);
1351 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1352 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001354
1355 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001356 /* Release allocated memory */
1357 spoe_release_buffer(&spoe_appctx->buffer,
1358 &spoe_appctx->buffer_wait);
1359 pool_free(pool_head_spoe_appctx, spoe_appctx);
1360
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001361 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001362 agent->rt[tid].frame_size = agent->max_frame_size;
1363 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1364 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001365}
1366
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001367static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001368spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001369{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001370 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001371 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001372 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001374
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001375 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1376 /* closed */
1377 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1378 goto exit;
1379 }
1380
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001381 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001382 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001383 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001384 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1385 goto stop;
1386 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001387
Christopher Fauleta1cda022016-12-21 08:58:06 +01001388 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1390 " - Connection timed out\n",
1391 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1392 __FUNCTION__, appctx);
1393 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001395 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001396
Christopher Faulet42bfa462017-01-04 14:14:19 +01001397 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001398 SPOE_APPCTX(appctx)->task->expire =
1399 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1402 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001403 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 ret = spoe_prepare_hahello_frame(appctx, frame,
1405 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001407 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001408
1409 switch (ret) {
1410 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 case 0: /* ignore => an error, cannot be ignored */
1412 goto exit;
1413
1414 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 goto stop;
1416
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 default:
1418 /* HELLO frame successfully sent, now wait for the
1419 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001420 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1421 goto next;
1422 }
1423
1424 next:
1425 return 0;
1426 stop:
1427 return 1;
1428 exit:
1429 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1430 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001431}
1432
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001433static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001434spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001437 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 char *frame;
1439 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001440
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001441
Christopher Fauletb067b062017-01-04 16:39:11 +01001442 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001443 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001444 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001445 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001446
Christopher Fauleta1cda022016-12-21 08:58:06 +01001447 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001448 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1449 " - Connection timed out\n",
1450 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1451 __FUNCTION__, appctx);
1452 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453 goto exit;
1454 }
1455
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001456 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001457 ret = spoe_recv_frame(appctx, frame,
1458 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001459 if (ret > 1) {
1460 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1461 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1462 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001463 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001464 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001465 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001466 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001467
Christopher Fauleta1cda022016-12-21 08:58:06 +01001468 switch (ret) {
1469 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001470 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001471 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1472 goto next;
1473
1474 case 1: /* retry later */
1475 goto stop;
1476
1477 default:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001478 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001479 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001480 SPOE_APPCTX(appctx)->node.key = 0;
1481 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001482
1483 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001484 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001485 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001486 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001487
Christopher Fauleta1cda022016-12-21 08:58:06 +01001488 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001489 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001490 if (trash.data)
1491 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492
1493 SPOE_APPCTX(appctx)->task->expire =
1494 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001495 return 0;
1496 stop:
1497 return 1;
1498 exit:
1499 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1500 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001501}
1502
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001503
Christopher Fauleta1cda022016-12-21 08:58:06 +01001504static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001505spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001506{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001507 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1508 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001509 char *frame, *buf;
1510 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001511
Christopher Faulet8ef75252017-02-20 22:56:03 +01001512 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1513 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001514 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001515
1516 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1517 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1518 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1519 SPOE_APPCTX(appctx)->max_frame_size);
1520 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001521 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001522 *skip = 1;
1523 ret = 1;
1524 goto end;
1525 }
1526 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001527 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001528 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1529 SPOE_APPCTX(appctx)->max_frame_size);
1530
1531 }
1532
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 if (ret > 1)
1534 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001535
Christopher Faulet8ef75252017-02-20 22:56:03 +01001536 switch (ret) {
1537 case -1: /* error */
1538 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1539 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001540
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 case 0: /* ignore */
1542 if (ctx == NULL)
1543 goto abort_frag_frame;
1544
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001545 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001546 LIST_DEL(&ctx->list);
1547 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001548 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001549 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001550 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001551 ctx->state = SPOE_CTX_ST_ERROR;
1552 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1553 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001554 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 break;
1556
1557 case 1: /* retry */
1558 *skip = 1;
1559 break;
1560
1561 default:
1562 if (ctx == NULL)
1563 goto abort_frag_frame;
1564
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001565 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001566 LIST_DEL(&ctx->list);
1567 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001568 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001569 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001570 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001571 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1572 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1573 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001574 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001576 }
1577 goto end;
1578
1579 frag_frame_sent:
1580 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001581 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001582 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1583 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1584 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001585 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1586 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1587 goto end;
1588
1589 no_frag_frame_sent:
1590 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1591 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001592 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001593 }
1594 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1595 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1596 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1597 }
1598 else {
1599 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001600 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001601 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1602 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001603 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001604 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001605 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1606 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1607 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001608 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001609
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1611 goto end;
1612
1613 abort_frag_frame:
1614 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1615 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1616 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1617 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1618 goto end;
1619
1620 end:
1621 return ret;
1622}
1623
1624static int
1625spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1626{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001627 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 struct spoe_context *ctx = NULL;
1629 char *frame;
1630 int ret;
1631
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001632 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001633 ret = spoe_recv_frame(appctx, frame,
1634 SPOE_APPCTX(appctx)->max_frame_size);
1635 if (ret > 1) {
1636 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1637 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001638 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001639 goto end;
1640 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001641 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001642 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1643 }
1644 switch (ret) {
1645 case -1: /* error */
1646 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1647 break;
1648
1649 case 0: /* ignore */
1650 break;
1651
1652 case 1: /* retry */
1653 *skip = 1;
1654 break;
1655
1656 default:
1657 LIST_DEL(&ctx->list);
1658 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001659 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001660 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1661 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001662 if (ctx->spoe_appctx) {
1663 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001664 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001665 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001666 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1667 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1668 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1669 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1670 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1671 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1672 }
1673 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1674 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001675 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1676 break;
1677 }
1678
1679 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001680 if (trash.data)
1681 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001682 end:
1683 return ret;
1684}
1685
1686static int
1687spoe_handle_processing_appctx(struct appctx *appctx)
1688{
1689 struct stream_interface *si = appctx->owner;
Christopher Faulet32e6e482021-08-02 18:13:27 +02001690 struct server *srv = objt_server(si_strm(si)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001691 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet32e6e482021-08-02 18:13:27 +02001692 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001693
1694 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1695 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1696 goto exit;
1697 }
1698
1699 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1701 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1702 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1703 goto next;
1704 }
1705
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001706 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001707 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001708 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1710 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001711 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001712
Christopher Faulet32e6e482021-08-02 18:13:27 +02001713
1714 /* Close the applet ASAP because some sessions are waiting for a free
1715 * connection slot. It is only an issue in multithreaded mode.
1716 */
1717 close_asap = (global.nbthread > 1 &&
1718 (agent->b.be->nbpend ||
1719 (srv && (srv->nbpend || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1720
1721 /* Don"t try to send new frame we are waiting for at lease a ack, in
1722 * sync mode or if applet must be closed ASAP
1723 */
1724 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
Christopher Faulet8f82b202018-01-24 16:23:03 +01001725 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001726
Christopher Faulet8f82b202018-01-24 16:23:03 +01001727 /* receiving_frame loop */
1728 while (!skip_receiving) {
1729 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1730 switch (ret) {
1731 case -1: /* error */
1732 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001733
Christopher Faulet8f82b202018-01-24 16:23:03 +01001734 case 0: /* ignore */
1735 active_r = 1;
1736 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001737
Christopher Faulet8f82b202018-01-24 16:23:03 +01001738 case 1: /* retry */
1739 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001740
Christopher Faulet8f82b202018-01-24 16:23:03 +01001741 default:
1742 active_r = 1;
1743 break;
1744 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001745 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746
Christopher Faulet8f82b202018-01-24 16:23:03 +01001747 /* send_frame loop */
1748 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1749 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1750 switch (ret) {
1751 case -1: /* error */
1752 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753
Christopher Faulet8f82b202018-01-24 16:23:03 +01001754 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001755 if (SPOE_APPCTX(appctx)->node.key)
1756 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001757 active_s++;
1758 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001759
Christopher Faulet8f82b202018-01-24 16:23:03 +01001760 case 1: /* retry */
1761 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001762
Christopher Faulet8f82b202018-01-24 16:23:03 +01001763 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001764 if (SPOE_APPCTX(appctx)->node.key)
1765 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001766 active_s++;
1767 break;
1768 }
Christopher Faulet32e6e482021-08-02 18:13:27 +02001769
1770 /* if applet must be close ASAP, don't send more than a frame */
1771 if (close_asap)
1772 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774
Christopher Faulet8f82b202018-01-24 16:23:03 +01001775 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001776 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1777 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1778 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001779
Christopher Faulet8f82b202018-01-24 16:23:03 +01001780 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Faulet32e6e482021-08-02 18:13:27 +02001781 /* If applet must be closed, don't switch it in IDLE state and
1782 * close it when the last waiting frame is acknowledged.
Christopher Faulete7c50f12021-03-01 15:01:14 +01001783 */
Christopher Faulet32e6e482021-08-02 18:13:27 +02001784 if (close_asap) {
1785 if (SPOE_APPCTX(appctx)->cur_fpa)
1786 goto out;
Christopher Faulete7c50f12021-03-01 15:01:14 +01001787 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1788 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1789 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1790 goto next;
1791 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001792 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001793 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001794 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795 }
Christopher Faulet32e6e482021-08-02 18:13:27 +02001796
1797 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798 return 1;
1799
Christopher Faulet8f82b202018-01-24 16:23:03 +01001800 next:
1801 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1802 return 0;
1803
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 exit:
1805 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1806 return 0;
1807}
1808
1809static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811{
1812 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001813 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 char *frame, *buf;
1815 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001816
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1818 goto exit;
1819
1820 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1821 goto exit;
1822
Christopher Faulet8ef75252017-02-20 22:56:03 +01001823 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1824 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001825 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001826 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1827 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001829 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830
1831 switch (ret) {
1832 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834 goto exit;
1835
1836 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 goto stop;
1838
1839 default:
1840 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1841 " - disconnected by HAProxy (%d): %s\n",
1842 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001843 __FUNCTION__, appctx,
1844 SPOE_APPCTX(appctx)->status_code,
1845 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846
1847 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1848 goto next;
1849 }
1850
1851 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 SPOE_APPCTX(appctx)->task->expire =
1853 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001854 return 0;
1855 stop:
1856 return 1;
1857 exit:
1858 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1859 return 0;
1860}
1861
1862static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001863spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001864{
1865 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 char *frame;
1867 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868
Christopher Fauletb067b062017-01-04 16:39:11 +01001869 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001870 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001871 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001872 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873
Christopher Fauletb067b062017-01-04 16:39:11 +01001874 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001875 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001877 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001879 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 ret = spoe_recv_frame(appctx, frame,
1881 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001882 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001883 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001884 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001885 }
1886
1887 switch (ret) {
1888 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001889 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1890 " - error on frame (%s)\n",
1891 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001892 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001893 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto exit;
1896
1897 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 goto next;
1899
1900 case 1: /* retry */
1901 goto stop;
1902
1903 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001905 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001906 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001907 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1909 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 goto exit;
1911 }
1912
1913 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001914 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001915 if (trash.data)
1916 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001917
Christopher Fauleta1cda022016-12-21 08:58:06 +01001918 return 0;
1919 stop:
1920 return 1;
1921 exit:
1922 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1923 return 0;
1924}
1925
1926/* I/O Handler processing messages exchanged with the agent */
1927static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001928spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 struct stream_interface *si = appctx->owner;
1931 struct spoe_agent *agent;
1932
1933 if (SPOE_APPCTX(appctx) == NULL)
1934 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001935
Christopher Faulet8ef75252017-02-20 22:56:03 +01001936 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1937 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001938
Christopher Fauleta1cda022016-12-21 08:58:06 +01001939 switchstate:
1940 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1941 " - appctx-state=%s\n",
1942 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1943 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1944
1945 switch (appctx->st0) {
1946 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001947 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001948 goto out;
1949 goto switchstate;
1950
1951 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001952 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001953 goto out;
1954 goto switchstate;
1955
1956 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001957 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001958 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001959 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001960 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001961 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001962 SPOE_APPCTX(appctx)->task->expire =
1963 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001964 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965 goto switchstate;
1966 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001967 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1968 /* fall through */
1969
1970 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001971 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1972 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001973 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001974 goto out;
1975 goto switchstate;
1976
1977 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001978 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001979 goto out;
1980 goto switchstate;
1981
1982 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001983 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001984 goto out;
1985 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001986
1987 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001988 appctx->st0 = SPOE_APPCTX_ST_END;
1989 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1990
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991 si_shutw(si);
1992 si_shutr(si);
1993 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 /* fall through */
1995
1996 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001997 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001998 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001999 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02002000 if (stopping)
2001 spoe_wakeup_appctx(appctx);
2002
Christopher Faulet42bfa462017-01-04 14:14:19 +01002003 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
2004 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005}
2006
2007struct applet spoe_applet = {
2008 .obj_type = OBJ_TYPE_APPLET,
2009 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002010 .fct = spoe_handle_appctx,
2011 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002012};
2013
2014/* Create a SPOE applet. On success, the created applet is returned, else
2015 * NULL. */
2016static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002017spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018{
2019 struct appctx *appctx;
2020 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022
Emeric Brun1138fd02017-06-19 12:38:55 +02002023 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024 goto out_error;
2025
Willy Tarreaubafbe012017-11-24 17:34:44 +01002026 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002027 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01002029 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002030
Christopher Faulet42bfa462017-01-04 14:14:19 +01002031 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002032 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002033 goto out_free_spoe_appctx;
2034
2035 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002036 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002037 SPOE_APPCTX(appctx)->task->context = appctx;
2038 SPOE_APPCTX(appctx)->agent = conf->agent;
2039 SPOE_APPCTX(appctx)->version = 0;
2040 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
2041 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01002042 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002043 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01002044 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01002045
2046 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
2047 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002048 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002049
2050 LIST_INIT(&SPOE_APPCTX(appctx)->list);
2051 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002052
Willy Tarreau5820a362016-12-22 15:59:02 +01002053 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002054 if (!sess)
2055 goto out_free_spoe;
2056
Willy Tarreau87787ac2017-08-28 16:22:54 +02002057 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002058 goto out_free_sess;
2059
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002060 stream_set_backend(strm, conf->agent->b.be);
2061
2062 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002063 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002064 appctx_wakeup(appctx);
2065
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066 strm->do_log = NULL;
2067 strm->res.flags |= CF_READ_DONTWAIT;
2068
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002069 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002070 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002071 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002072 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002073
Emeric Brunc60def82017-09-27 14:59:38 +02002074 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002075 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002076 return appctx;
2077
2078 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079 out_free_sess:
2080 session_free(sess);
2081 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002082 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002083 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002084 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002085 out_free_appctx:
2086 appctx_free(appctx);
2087 out_error:
2088 return NULL;
2089}
2090
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002091static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002092spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002093{
2094 struct spoe_config *conf = FLT_CONF(ctx->filter);
2095 struct spoe_agent *agent = conf->agent;
2096 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002097 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002098
Christopher Fauleta1cda022016-12-21 08:58:06 +01002099 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002100 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauletd6cd2b32020-06-22 15:32:14 +02002101 (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec)))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002102 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002103
2104 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002105 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002106 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2107 ctx->strm);
2108
Christopher Fauleta1cda022016-12-21 08:58:06 +01002109 /* Do not try to create a new applet if there is no server up for the
2110 * agent's backend. */
2111 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2112 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2113 " - cannot create SPOE appctx: no server up\n",
2114 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2115 __FUNCTION__, ctx->strm);
2116 goto end;
2117 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002118
Christopher Fauleta1cda022016-12-21 08:58:06 +01002119 /* Do not try to create a new applet if we have reached the maximum of
2120 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002121 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002122 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002123 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2124 " - cannot create SPOE appctx: max CPS reached\n",
2125 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2126 __FUNCTION__, ctx->strm);
2127 goto end;
2128 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002129 }
2130
Christopher Faulet8ef75252017-02-20 22:56:03 +01002131 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002132 if (appctx == NULL) {
2133 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2134 " - failed to create SPOE appctx\n",
2135 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2136 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002137 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002138 "SPOE: [%s] failed to create SPOE applet\n",
2139 agent->id);
2140
Christopher Fauleta1cda022016-12-21 08:58:06 +01002141 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002142 }
2143
Christopher Fauleta1cda022016-12-21 08:58:06 +01002144 /* Increase the per-process number of cumulated connections */
2145 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002146 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002147
Christopher Fauleta1cda022016-12-21 08:58:06 +01002148 end:
2149 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002150 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002151 ctx->status_code = SPOE_CTX_ERR_RES;
2152 return -1;
2153 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002154
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002155 /* Add the SPOE context in the sending queue if the stream has no applet
2156 * already assigned and wakeup all idle applets. Otherwise, don't queue
2157 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002158 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002159 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2160 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002161 if (ctx->spoe_appctx)
2162 return 1;
2163 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002164
2165 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002166 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002167 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002168 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002169 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002170 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002171
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002172 /* Finally try to wakeup an IDLE applet. */
2173 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2174 struct eb32_node *node;
2175
2176 node = eb32_first(&agent->rt[tid].idle_applets);
2177 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2178 if (node && spoe_appctx) {
2179 eb32_delete(&spoe_appctx->node);
2180 spoe_appctx->node.key++;
2181 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2182 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002183 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002184 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002185 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002186}
2187
2188/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002189 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002190 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002191/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2192 * fragmented_content. If the next message can be processed, it returns 0. If
2193 * the message is too big, it returns -1.*/
2194static int
2195spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2196 struct spoe_message *msg, int dir,
2197 char **buf, char *end)
2198{
2199 struct sample *smp;
2200 struct spoe_arg *arg;
2201 int ret;
2202
2203 if (msg->cond) {
2204 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2205 ret = acl_pass(ret);
2206 if (msg->cond->pol == ACL_COND_UNLESS)
2207 ret = !ret;
2208
2209 /* the rule does not match */
2210 if (!ret)
2211 goto next;
2212 }
2213
2214 /* Resume encoding of a SPOE argument */
2215 if (ctx->frag_ctx.curarg != NULL) {
2216 arg = ctx->frag_ctx.curarg;
2217 goto encode_argument;
2218 }
2219
2220 if (ctx->frag_ctx.curoff != UINT_MAX)
2221 goto encode_msg_payload;
2222
2223 /* Check if there is enough space for the message name and the
2224 * number of arguments. It implies <msg->id_len> is encoded on 2
2225 * bytes, at most (< 2288). */
2226 if (*buf + 2 + msg->id_len + 1 > end)
2227 goto too_big;
2228
2229 /* Encode the message name */
2230 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2231 goto too_big;
2232
2233 /* Set the number of arguments for this message */
2234 **buf = msg->nargs;
2235 (*buf)++;
2236
2237 ctx->frag_ctx.curoff = 0;
2238 encode_msg_payload:
2239
2240 /* Loop on arguments */
2241 list_for_each_entry(arg, &msg->args, list) {
2242 ctx->frag_ctx.curarg = arg;
2243 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002244 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002245
2246 encode_argument:
2247 if (ctx->frag_ctx.curoff != UINT_MAX)
2248 goto encode_arg_value;
2249
Joseph Herlantf7f60312018-11-15 13:46:49 -08002250 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002251 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2252 goto too_big;
2253
2254 ctx->frag_ctx.curoff = 0;
2255 encode_arg_value:
2256
Joseph Herlantf7f60312018-11-15 13:46:49 -08002257 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002258 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002259 if (smp) {
2260 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2261 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2262 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002263 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002264 if (ret == -1 || ctx->frag_ctx.curoff)
2265 goto too_big;
2266 }
2267
2268 next:
2269 return 0;
2270
2271 too_big:
2272 return -1;
2273}
2274
Christopher Fauletc718b822017-09-21 16:50:56 +02002275/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2276 * handle fragmented content. On success it returns 1. If an error occurred, -1
2277 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2278 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002279static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002280spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002281 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002282{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002283 struct spoe_config *conf = FLT_CONF(ctx->filter);
2284 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002285 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002286 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002287
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002288 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002289 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002290
Christopher Fauletc718b822017-09-21 16:50:56 +02002291 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2292 /* Resume encoding of a SPOE message */
2293 if (ctx->frag_ctx.curmsg != NULL) {
2294 msg = ctx->frag_ctx.curmsg;
2295 goto encode_evt_message;
2296 }
2297
2298 list_for_each_entry(msg, messages, by_evt) {
2299 ctx->frag_ctx.curmsg = msg;
2300 ctx->frag_ctx.curarg = NULL;
2301 ctx->frag_ctx.curoff = UINT_MAX;
2302
2303 encode_evt_message:
2304 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2305 goto too_big;
2306 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002307 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002308 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2309 /* Resume encoding of a SPOE message */
2310 if (ctx->frag_ctx.curmsg != NULL) {
2311 msg = ctx->frag_ctx.curmsg;
2312 goto encode_grp_message;
2313 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002314
Christopher Fauletc718b822017-09-21 16:50:56 +02002315 list_for_each_entry(msg, messages, by_grp) {
2316 ctx->frag_ctx.curmsg = msg;
2317 ctx->frag_ctx.curarg = NULL;
2318 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002319
Christopher Fauletc718b822017-09-21 16:50:56 +02002320 encode_grp_message:
2321 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2322 goto too_big;
2323 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002324 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002325 else
2326 goto skip;
2327
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002328
Christopher Faulet57583e42017-09-04 15:41:09 +02002329 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002330 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002331 goto skip;
2332
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002333 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002334 " - encode %s messages - spoe_appctx=%p"
2335 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002336 (int)now.tv_sec, (int)now.tv_usec,
2337 agent->id, __FUNCTION__, s,
2338 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002339 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002340 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002341
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002342 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002343 ctx->frag_ctx.curmsg = NULL;
2344 ctx->frag_ctx.curarg = NULL;
2345 ctx->frag_ctx.curoff = 0;
2346 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002347
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348 return 1;
2349
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002350 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002351 /* Return an error if fragmentation is unsupported or if nothing has
2352 * been encoded because its too big and not splittable. */
2353 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002354 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2355 return -1;
2356 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002357
2358 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359 " - encode fragmented messages - spoe_appctx=%p"
2360 " - curmsg=%p - curarg=%p - curoff=%u"
2361 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002362 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002363 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002364 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002365 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002366
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002367 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002368 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2369 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2370 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002371
2372 skip:
2373 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2374 " - skip the frame because nothing has been encoded\n",
2375 (int)now.tv_sec, (int)now.tv_usec,
2376 agent->id, __FUNCTION__, s);
2377 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378}
2379
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002380
2381/***************************************************************************
2382 * Functions that handle SPOE actions
2383 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002384/* Helper function to set a variable */
2385static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002386spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002387 struct sample *smp)
2388{
2389 struct spoe_config *conf = FLT_CONF(ctx->filter);
2390 struct spoe_agent *agent = conf->agent;
2391 char varname[64];
2392
2393 memset(varname, 0, sizeof(varname));
2394 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2395 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002396 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2397 vars_set_by_name(varname, len, smp);
2398 else
2399 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400}
2401
2402/* Helper function to unset a variable */
2403static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002405 struct sample *smp)
2406{
2407 struct spoe_config *conf = FLT_CONF(ctx->filter);
2408 struct spoe_agent *agent = conf->agent;
2409 char varname[64];
2410
2411 memset(varname, 0, sizeof(varname));
2412 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2413 scope, agent->var_pfx, len, name);
2414 vars_unset_by_name_ifexist(varname, len, smp);
2415}
2416
2417
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418static inline int
2419spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2420 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 char *str, *scope, *p = *buf;
2423 struct sample smp;
2424 uint64_t sz;
2425 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002426
Christopher Faulet8ef75252017-02-20 22:56:03 +01002427 if (p + 2 >= end)
2428 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002429
Christopher Faulet8ef75252017-02-20 22:56:03 +01002430 /* SET-VAR requires 3 arguments */
2431 if (*p++ != 3)
2432 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002433
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434 switch (*p++) {
2435 case SPOE_SCOPE_PROC: scope = "proc"; break;
2436 case SPOE_SCOPE_SESS: scope = "sess"; break;
2437 case SPOE_SCOPE_TXN : scope = "txn"; break;
2438 case SPOE_SCOPE_REQ : scope = "req"; break;
2439 case SPOE_SCOPE_RES : scope = "res"; break;
2440 default: goto skip;
2441 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2444 goto skip;
2445 memset(&smp, 0, sizeof(smp));
2446 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002447
Christopher Faulet8ef75252017-02-20 22:56:03 +01002448 if (spoe_decode_data(&p, end, &smp) == -1)
2449 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002450
Christopher Faulet8ef75252017-02-20 22:56:03 +01002451 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2452 " - set-var '%s.%s.%.*s'\n",
2453 (int)now.tv_sec, (int)now.tv_usec,
2454 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2455 __FUNCTION__, s, scope,
2456 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2457 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002458
Christopher Fauletb135abc2020-10-15 16:08:30 +02002459 if (smp.data.type == SMP_T_ANY)
2460 spoe_unset_var(ctx, scope, str, sz, &smp);
2461 else
2462 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002463
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464 ret = (p - *buf);
2465 *buf = p;
2466 return ret;
2467 skip:
2468 return 0;
2469}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002470
Christopher Faulet8ef75252017-02-20 22:56:03 +01002471static inline int
2472spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2473 char **buf, char *end, int dir)
2474{
2475 char *str, *scope, *p = *buf;
2476 struct sample smp;
2477 uint64_t sz;
2478 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002479
Christopher Faulet8ef75252017-02-20 22:56:03 +01002480 if (p + 2 >= end)
2481 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002482
Christopher Faulet8ef75252017-02-20 22:56:03 +01002483 /* UNSET-VAR requires 2 arguments */
2484 if (*p++ != 2)
2485 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002486
Christopher Faulet8ef75252017-02-20 22:56:03 +01002487 switch (*p++) {
2488 case SPOE_SCOPE_PROC: scope = "proc"; break;
2489 case SPOE_SCOPE_SESS: scope = "sess"; break;
2490 case SPOE_SCOPE_TXN : scope = "txn"; break;
2491 case SPOE_SCOPE_REQ : scope = "req"; break;
2492 case SPOE_SCOPE_RES : scope = "res"; break;
2493 default: goto skip;
2494 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002495
Christopher Faulet8ef75252017-02-20 22:56:03 +01002496 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2497 goto skip;
2498 memset(&smp, 0, sizeof(smp));
2499 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002500
Christopher Faulet8ef75252017-02-20 22:56:03 +01002501 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2502 " - unset-var '%s.%s.%.*s'\n",
2503 (int)now.tv_sec, (int)now.tv_usec,
2504 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2505 __FUNCTION__, s, scope,
2506 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2507 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002508
Christopher Faulet8ef75252017-02-20 22:56:03 +01002509 spoe_unset_var(ctx, scope, str, sz, &smp);
2510
2511 ret = (p - *buf);
2512 *buf = p;
2513 return ret;
2514 skip:
2515 return 0;
2516}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002517
Christopher Faulet8ef75252017-02-20 22:56:03 +01002518/* Process SPOE actions for a specific event. It returns 1 on success. If an
2519 * error occurred, 0 is returned. */
2520static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002521spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002522{
2523 char *p, *end;
2524 int ret;
2525
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002526 p = b_head(&ctx->buffer);
2527 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002528
2529 while (p < end) {
2530 enum spoe_action_type type;
2531
2532 type = *p++;
2533 switch (type) {
2534 case SPOE_ACT_T_SET_VAR:
2535 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2536 if (!ret)
2537 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002538 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002539
Christopher Faulet8ef75252017-02-20 22:56:03 +01002540 case SPOE_ACT_T_UNSET_VAR:
2541 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2542 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002543 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002544 break;
2545
2546 default:
2547 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002548 }
2549 }
2550
2551 return 1;
2552 skip:
2553 return 0;
2554}
2555
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002556/***************************************************************************
2557 * Functions that process SPOE events
2558 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002559static void
2560spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2561 struct spoe_context *ctx, int dir)
2562{
2563 if (!tv_iszero(&ctx->stats.tv_start)) {
2564 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2565 ctx->stats.t_total += ctx->stats.t_process;
2566 tv_zero(&ctx->stats.tv_request);
2567 tv_zero(&ctx->stats.tv_queue);
2568 tv_zero(&ctx->stats.tv_wait);
2569 tv_zero(&ctx->stats.tv_response);
2570 }
2571
2572 if (agent->var_t_process) {
2573 struct sample smp;
2574
2575 memset(&smp, 0, sizeof(smp));
2576 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2577 smp.data.u.sint = ctx->stats.t_process;
2578 smp.data.type = SMP_T_SINT;
2579
2580 spoe_set_var(ctx, "txn", agent->var_t_process,
2581 strlen(agent->var_t_process), &smp);
2582 }
2583
2584 if (agent->var_t_total) {
2585 struct sample smp;
2586
2587 memset(&smp, 0, sizeof(smp));
2588 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2589 smp.data.u.sint = ctx->stats.t_total;
2590 smp.data.type = SMP_T_SINT;
2591
2592 spoe_set_var(ctx, "txn", agent->var_t_total,
2593 strlen(agent->var_t_total), &smp);
2594 }
2595}
2596
2597static void
2598spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2599 struct spoe_context *ctx, int dir)
2600{
2601 if (agent->eps_max > 0)
2602 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2603
2604 if (agent->var_on_error) {
2605 struct sample smp;
2606
2607 memset(&smp, 0, sizeof(smp));
2608 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2609 smp.data.u.sint = ctx->status_code;
2610 smp.data.type = SMP_T_BOOL;
2611
2612 spoe_set_var(ctx, "txn", agent->var_on_error,
2613 strlen(agent->var_on_error), &smp);
2614 }
2615
2616 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2617 ? SPOE_CTX_ST_READY
2618 : SPOE_CTX_ST_NONE);
2619}
2620
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002621static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002622spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002623{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002624 /* If a process is already started for this SPOE context, retry
2625 * later. */
2626 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002627 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002628
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002629 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002630 ctx->stats.tv_start = now;
2631 ctx->stats.tv_request = now;
2632 ctx->stats.t_request = -1;
2633 ctx->stats.t_queue = -1;
2634 ctx->stats.t_waiting = -1;
2635 ctx->stats.t_response = -1;
2636 ctx->stats.t_process = -1;
2637
2638 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002639
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640 /* Set the right flag to prevent request and response processing
2641 * in same time. */
2642 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2643 ? SPOE_CTX_FL_REQ_PROCESS
2644 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002645 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002646}
2647
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002648static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002649spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002650{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002651 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002652
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002653 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2654 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002655 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002656 if (sa) {
2657 if (sa->frag_ctx.ctx == ctx) {
2658 sa->frag_ctx.ctx = NULL;
2659 spoe_wakeup_appctx(sa->owner);
2660 }
2661 else
2662 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002663 }
2664
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002666 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002667 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002668
2669 /* Reset processing timer */
2670 ctx->process_exp = TICK_ETERNITY;
2671
Christopher Faulet8ef75252017-02-20 22:56:03 +01002672 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002673
Christopher Fauletfce747b2018-01-24 15:59:32 +01002674 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002675 ctx->frag_ctx.curmsg = NULL;
2676 ctx->frag_ctx.curarg = NULL;
2677 ctx->frag_ctx.curoff = 0;
2678 ctx->frag_ctx.flags = 0;
2679
Christopher Fauleta1cda022016-12-21 08:58:06 +01002680 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002681 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002682 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002683 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002684 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002685
Christopher Fauleta1cda022016-12-21 08:58:06 +01002686 LIST_DEL(&ctx->list);
2687 LIST_INIT(&ctx->list);
2688 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002689}
2690
Christopher Faulet58d03682017-09-21 16:57:24 +02002691/* Process a list of SPOE messages. First, this functions will process messages
2692 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2693 * to process corresponding actions. During all the processing, it returns 0
2694 * and it returns 1 when the processing is finished. If an error occurred, -1
2695 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002696static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002697spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2698 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002699{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002700 struct spoe_config *conf = FLT_CONF(ctx->filter);
2701 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002702 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002703
2704 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002705 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002706
2707 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2708 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002709 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002710 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002711 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002712 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002713 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002714 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002715
2716 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002717 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002718 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002719 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002720 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002721 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002722 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002723 goto skip;
2724 }
2725 }
2726
Christopher Fauletf7a30922016-11-10 15:04:51 +01002727 if (!tick_isset(ctx->process_exp)) {
2728 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2729 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2730 ctx->process_exp);
2731 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002732 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002733 if (!ret)
2734 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002735
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002736 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002737 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002738 }
2739
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002740 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002741 if (tv_iszero(&ctx->stats.tv_request))
2742 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002743 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002744 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002745 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002746 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002747 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002748 if (!ret)
2749 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002750 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002751 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002752 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2753 }
2754
2755 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002756 if (ctx->spoe_appctx)
2757 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002758 ret = 0;
2759 goto out;
2760 }
2761
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002762 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2763 ret = 0;
2764 goto out;
2765 }
2766
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002767 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002768 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002769 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002770 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002771 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002772 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002773 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002774 }
2775
2776 out:
2777 return ret;
2778
Christopher Fauleta1cda022016-12-21 08:58:06 +01002779 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002780 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002781 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002782 spoe_stop_processing(agent, ctx);
2783 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002784
Christopher Fauleta1cda022016-12-21 08:58:06 +01002785 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002786 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002787 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002788 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002789 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002790 spoe_handle_processing_error(s, agent, ctx, dir);
2791 ret = 1;
2792 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002793 return ret;
2794}
2795
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002796/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2797 * See spoe_process_message for details. */
2798static int
2799spoe_process_group(struct stream *s, struct spoe_context *ctx,
2800 struct spoe_group *group, int dir)
2801{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002802 struct spoe_config *conf = FLT_CONF(ctx->filter);
2803 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002804 int ret;
2805
2806 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2807 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002808 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002809 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2810 group->id);
2811
2812 if (LIST_ISEMPTY(&group->messages))
2813 return 1;
2814
2815 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002816 if (ret && ctx->stats.t_process != -1) {
2817 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002818 " - <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 +01002819 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002820 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002821 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002822 ctx->stats.t_response, ctx->stats.t_process,
2823 agent->counters.idles, agent->counters.applets,
2824 agent->counters.nb_sending, agent->counters.nb_waiting,
2825 agent->counters.nb_errors, agent->counters.nb_processed,
2826 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2827 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2828 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2829 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2830 agent->id, group->id, s->uniq_id, ctx->status_code,
2831 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2832 ctx->stats.t_response, ctx->stats.t_process,
2833 agent->counters.idles, agent->counters.applets,
2834 agent->counters.nb_sending, agent->counters.nb_waiting,
2835 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002836 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002837 return ret;
2838}
2839
Christopher Faulet58d03682017-09-21 16:57:24 +02002840/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2841 * See spoe_process_message for details. */
2842static int
2843spoe_process_event(struct stream *s, struct spoe_context *ctx,
2844 enum spoe_event ev)
2845{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002846 struct spoe_config *conf = FLT_CONF(ctx->filter);
2847 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002848 int dir, ret;
2849
2850 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002851 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002852 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002853 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2854 spoe_event_str[ev]);
2855
2856 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2857
2858 if (LIST_ISEMPTY(&(ctx->events[ev])))
2859 return 1;
2860
2861 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002862 if (ret && ctx->stats.t_process != -1) {
2863 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002864 " - <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 +01002865 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2866 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2867 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002868 ctx->stats.t_response, ctx->stats.t_process,
2869 agent->counters.idles, agent->counters.applets,
2870 agent->counters.nb_sending, agent->counters.nb_waiting,
2871 agent->counters.nb_errors, agent->counters.nb_processed,
2872 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2873 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2874 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2875 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2876 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2877 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2878 ctx->stats.t_response, ctx->stats.t_process,
2879 agent->counters.idles, agent->counters.applets,
2880 agent->counters.nb_sending, agent->counters.nb_waiting,
2881 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002882 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002883 return ret;
2884}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002885
2886/***************************************************************************
2887 * Functions that create/destroy SPOE contexts
2888 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002889static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002890spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002891{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002892 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002893 return 1;
2894
Christopher Faulet4596fb72017-01-11 14:05:19 +01002895 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002896 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002897 LIST_DEL(&buffer_wait->list);
2898 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002899 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002900 }
2901
Christopher Faulet4596fb72017-01-11 14:05:19 +01002902 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002903 return 1;
2904
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002905 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002906 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002907 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002908 return 0;
2909}
2910
2911static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002912spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002913{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002914 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002915 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002916 LIST_DEL(&buffer_wait->list);
2917 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002918 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002919 }
2920
2921 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002922 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002923 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002924 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002925 }
2926}
2927
Christopher Faulet4596fb72017-01-11 14:05:19 +01002928static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002929spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002930{
2931 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2932 return 1;
2933}
2934
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002936spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002937{
2938 struct spoe_config *conf = FLT_CONF(filter);
2939 struct spoe_context *ctx;
2940
Willy Tarreaubafbe012017-11-24 17:34:44 +01002941 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942 if (ctx == NULL) {
2943 return NULL;
2944 }
2945 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002946 ctx->filter = filter;
2947 ctx->state = SPOE_CTX_ST_NONE;
2948 ctx->status_code = SPOE_CTX_ERR_NONE;
2949 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002950 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002951 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002952 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002953 LIST_INIT(&ctx->buffer_wait.list);
2954 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002955 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002956 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957
Christopher Fauletf7a30922016-11-10 15:04:51 +01002958 ctx->stream_id = 0;
2959 ctx->frame_id = 1;
2960 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002961
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002962 tv_zero(&ctx->stats.tv_start);
2963 tv_zero(&ctx->stats.tv_request);
2964 tv_zero(&ctx->stats.tv_queue);
2965 tv_zero(&ctx->stats.tv_wait);
2966 tv_zero(&ctx->stats.tv_response);
2967 ctx->stats.t_request = -1;
2968 ctx->stats.t_queue = -1;
2969 ctx->stats.t_waiting = -1;
2970 ctx->stats.t_response = -1;
2971 ctx->stats.t_process = -1;
2972 ctx->stats.t_total = 0;
2973
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002974 ctx->strm = s;
2975 ctx->state = SPOE_CTX_ST_READY;
2976 filter->ctx = ctx;
2977
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978 return ctx;
2979}
2980
2981static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002982spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002983{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002984 struct spoe_config *conf = FLT_CONF(filter);
2985 struct spoe_context *ctx = filter->ctx;
2986
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002987 if (!ctx)
2988 return;
2989
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002990 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002991 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002992 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993}
2994
2995static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002996spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997{
2998 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002999 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01003000
3001 tv_zero(&ctx->stats.tv_start);
3002 tv_zero(&ctx->stats.tv_request);
3003 tv_zero(&ctx->stats.tv_queue);
3004 tv_zero(&ctx->stats.tv_wait);
3005 tv_zero(&ctx->stats.tv_response);
3006 ctx->stats.t_request = -1;
3007 ctx->stats.t_queue = -1;
3008 ctx->stats.t_waiting = -1;
3009 ctx->stats.t_response = -1;
3010 ctx->stats.t_process = -1;
3011 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003012}
3013
3014
3015/***************************************************************************
3016 * Hooks that manage the filter lifecycle (init/check/deinit)
3017 **************************************************************************/
3018/* Signal handler: Do a soft stop, wakeup SPOE applet */
3019static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01003020spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021{
3022 struct proxy *p;
3023
Olivier Houchardfbc74e82017-11-24 16:54:05 +01003024 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003025 while (p) {
3026 struct flt_conf *fconf;
3027
3028 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01003029 struct spoe_config *conf;
3030 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01003031 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02003032 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003033
Christopher Faulet3b386a32017-02-23 10:17:15 +01003034 if (fconf->id != spoe_filter_id)
3035 continue;
3036
3037 conf = fconf->conf;
3038 agent = conf->agent;
3039
Christopher Faulet24289f22017-09-25 14:48:02 +02003040 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003041 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003042 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3043 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003044 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045 }
3046 }
3047 p = p->next;
3048 }
3049}
3050
3051
3052/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3053static int
3054spoe_init(struct proxy *px, struct flt_conf *fconf)
3055{
3056 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003057
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003058 /* conf->agent_fe was already initialized during the config
3059 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003060 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003061 conf->agent_fe.cap = PR_CAP_FE;
3062 conf->agent_fe.mode = PR_MODE_TCP;
3063 conf->agent_fe.maxconn = 0;
3064 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3065 conf->agent_fe.conn_retries = CONN_RETRIES;
3066 conf->agent_fe.accept = frontend_accept;
3067 conf->agent_fe.srv = NULL;
3068 conf->agent_fe.timeout.client = TICK_ETERNITY;
3069 conf->agent_fe.default_target = &spoe_applet.obj_type;
3070 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3071
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003072 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003073 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003074 sighandler_registered = 1;
3075 }
3076
Christopher Faulet00292352019-01-09 15:05:10 +01003077 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003078 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003079}
3080
3081/* Free ressources allocated by the SPOE filter. */
3082static void
3083spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3084{
3085 struct spoe_config *conf = fconf->conf;
3086
3087 if (conf) {
3088 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003089
Christopher Faulet8ef75252017-02-20 22:56:03 +01003090 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003091 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003092 free(conf);
3093 }
3094 fconf->conf = NULL;
3095}
3096
3097/* Check configuration of a SPOE filter for a specified proxy.
3098 * Return 1 on error, else 0. */
3099static int
3100spoe_check(struct proxy *px, struct flt_conf *fconf)
3101{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003102 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103 struct spoe_config *conf = fconf->conf;
3104 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003105 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003106
Christopher Faulet7ee86672017-09-19 11:08:28 +02003107 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3108 * are uniq */
3109 list_for_each_entry(f, &px->filter_configs, list) {
3110 struct spoe_config *c = f->conf;
3111
3112 /* This is not an SPOE filter */
3113 if (f->id != spoe_filter_id)
3114 continue;
3115 /* This is the current SPOE filter */
3116 if (f == fconf)
3117 continue;
3118
3119 /* Check engine Id. It should be uniq */
3120 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003121 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3122 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003123 return 1;
3124 }
3125 }
3126
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003127 target = proxy_be_by_name(conf->agent->b.name);
3128 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003129 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3130 " declared at %s:%d.\n",
3131 px->id, conf->agent->b.name, conf->agent->id,
3132 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003133 return 1;
3134 }
3135 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003136 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3137 " at %s:%d does not support HTTP mode.\n",
3138 px->id, target->id, conf->agent->id,
3139 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003140 return 1;
3141 }
3142
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003143 if (px->bind_proc & ~target->bind_proc) {
3144 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3145 " at %s:%d does not cover all of its processes.\n",
3146 px->id, target->id, conf->agent->id,
3147 conf->agent->conf.file, conf->agent->conf.line);
3148 return 1;
3149 }
3150
Christopher Fauletfe261552019-03-18 13:57:42 +01003151 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003152 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3153 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3154 return 1;
3155 }
3156 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet46c76822019-09-17 11:55:52 +02003157 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003158 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3159 conf->agent->rt[i].processing = 0;
3160 LIST_INIT(&conf->agent->rt[i].applets);
3161 LIST_INIT(&conf->agent->rt[i].sending_queue);
3162 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3163 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003164 }
3165
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003166 free(conf->agent->b.name);
3167 conf->agent->b.name = NULL;
3168 conf->agent->b.be = target;
3169 return 0;
3170}
3171
Kevin Zhu652c8e22019-09-17 15:05:45 +02003172/* Initializes the SPOE filter for a proxy for a specific thread.
3173 * Returns a negative value if an error occurs. */
3174static int
3175spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3176{
3177 struct spoe_config *conf = fconf->conf;
3178 struct spoe_agent *agent = conf->agent;
3179
Christopher Faulet46c76822019-09-17 11:55:52 +02003180 agent->rt[tid].engine_id = generate_pseudo_uuid();
3181 if (agent->rt[tid].engine_id == NULL)
3182 return -1;
Kevin Zhu652c8e22019-09-17 15:05:45 +02003183 return 0;
3184}
3185
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003186/**************************************************************************
3187 * Hooks attached to a stream
3188 *************************************************************************/
3189/* Called when a filter instance is created and attach to a stream. It creates
3190 * the context that will be used to process this stream. */
3191static int
3192spoe_start(struct stream *s, struct filter *filter)
3193{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003194 struct spoe_config *conf = FLT_CONF(filter);
3195 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003196 struct spoe_context *ctx;
3197
3198 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003199 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003200 __FUNCTION__, s);
3201
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003202 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003203 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3204 " - failed to create SPOE context\n",
3205 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003206 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003207 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003208 "SPOE: [%s] failed to create SPOE context\n",
3209 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003210 return 0;
3211 }
3212
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003213 return 1;
3214}
3215
3216/* Called when a filter instance is detached from a stream. It release the
3217 * attached SPOE context. */
3218static void
3219spoe_stop(struct stream *s, struct filter *filter)
3220{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3222 (int)now.tv_sec, (int)now.tv_usec,
3223 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3224 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003225 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003226}
3227
Christopher Fauletf7a30922016-11-10 15:04:51 +01003228
3229/*
3230 * Called when the stream is woken up because of expired timer.
3231 */
3232static void
3233spoe_check_timeouts(struct stream *s, struct filter *filter)
3234{
3235 struct spoe_context *ctx = filter->ctx;
3236
Christopher Fauletac580602018-03-20 16:09:20 +01003237 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003238 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003239}
3240
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003241/* Called when we are ready to filter data on a channel */
3242static int
3243spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3244{
3245 struct spoe_context *ctx = filter->ctx;
3246 int ret = 1;
3247
3248 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3249 " - ctx-flags=0x%08x\n",
3250 (int)now.tv_sec, (int)now.tv_usec,
3251 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3252 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3253
Christopher Fauletb067b062017-01-04 16:39:11 +01003254 if (ctx->state == SPOE_CTX_ST_NONE)
3255 goto out;
3256
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003257 if (!(chn->flags & CF_ISRESP)) {
Christopher Fauletbebe2552021-06-25 19:11:49 +02003258 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
3259 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3260
3261 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
3262 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3263
3264 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
3265 filter->pre_analyzers |= AN_RES_INSPECT;
3266
3267 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
3268 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3269
3270 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
3271 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3272
3273 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
3274 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3275
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3277 chn->analysers |= AN_REQ_INSPECT_FE;
3278 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3279 chn->analysers |= AN_REQ_INSPECT_BE;
3280
3281 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3282 goto out;
3283
3284 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003285 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003286 if (!ret)
3287 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003288 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3289 }
3290 else {
Miroslav Zagoracd995d5f2020-06-19 22:17:17 +02003291 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 chn->analysers |= AN_RES_INSPECT;
3293
3294 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3295 goto out;
3296
Christopher Faulet8ef75252017-02-20 22:56:03 +01003297 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003298 if (!ret) {
3299 channel_dont_read(chn);
3300 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003301 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003302 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003303 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003304 }
3305
3306 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003307 return ret;
3308}
3309
3310/* Called before a processing happens on a given channel */
3311static int
3312spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3313 struct channel *chn, unsigned an_bit)
3314{
3315 struct spoe_context *ctx = filter->ctx;
3316 int ret = 1;
3317
3318 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3319 " - ctx-flags=0x%08x - ana=0x%08x\n",
3320 (int)now.tv_sec, (int)now.tv_usec,
3321 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3322 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3323 ctx->flags, an_bit);
3324
Christopher Fauletb067b062017-01-04 16:39:11 +01003325 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003326 goto out;
3327
3328 switch (an_bit) {
3329 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003330 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003331 break;
3332 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003333 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003334 break;
3335 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003336 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003337 break;
3338 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003339 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003340 break;
3341 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003342 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003343 break;
3344 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003345 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003346 break;
3347 }
3348
3349 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003350 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003351 channel_dont_read(chn);
3352 channel_dont_close(chn);
3353 }
3354 return ret;
3355}
3356
3357/* Called when the filtering on the channel ends. */
3358static int
3359spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3360{
3361 struct spoe_context *ctx = filter->ctx;
3362
3363 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3364 " - ctx-flags=0x%08x\n",
3365 (int)now.tv_sec, (int)now.tv_usec,
3366 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3367 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3368
3369 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003370 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 }
3372
3373 return 1;
3374}
3375
3376/********************************************************************
3377 * Functions that manage the filter initialization
3378 ********************************************************************/
3379struct flt_ops spoe_ops = {
3380 /* Manage SPOE filter, called for each filter declaration */
3381 .init = spoe_init,
3382 .deinit = spoe_deinit,
3383 .check = spoe_check,
Kevin Zhu652c8e22019-09-17 15:05:45 +02003384 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003385
3386 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003387 .attach = spoe_start,
3388 .detach = spoe_stop,
3389 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003390
3391 /* Handle channels activity */
3392 .channel_start_analyze = spoe_start_analyze,
3393 .channel_pre_analyze = spoe_chn_pre_analyze,
3394 .channel_end_analyze = spoe_end_analyze,
3395};
3396
3397
3398static int
3399cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3400{
3401 const char *err;
3402 int i, err_code = 0;
3403
3404 if ((cfg_scope == NULL && curengine != NULL) ||
3405 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003406 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003407 goto out;
3408
3409 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3410 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003411 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3412 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003413 err_code |= ERR_ALERT | ERR_ABORT;
3414 goto out;
3415 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003416 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3417 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003418 goto out;
3419 }
3420
3421 err = invalid_char(args[1]);
3422 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003423 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3424 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003425 err_code |= ERR_ALERT | ERR_ABORT;
3426 goto out;
3427 }
3428
3429 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003430 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3431 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 err_code |= ERR_ALERT | ERR_ABORT;
3433 goto out;
3434 }
3435 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003436 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003437 err_code |= ERR_ALERT | ERR_ABORT;
3438 goto out;
3439 }
3440
3441 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003442
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 curagent->conf.file = strdup(file);
3444 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003445
3446 curagent->timeout.hello = TICK_ETERNITY;
3447 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003448 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003449
Christopher Fauleta1cda022016-12-21 08:58:06 +01003450 curagent->var_pfx = NULL;
3451 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003452 curagent->var_t_process = NULL;
3453 curagent->var_t_total = NULL;
Christopher Faulet46c76822019-09-17 11:55:52 +02003454 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003455 curagent->cps_max = 0;
3456 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003457 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003458 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003459
3460 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003461 LIST_INIT(&curagent->events[i]);
3462 LIST_INIT(&curagent->groups);
3463 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003464 }
3465 else if (!strcmp(args[0], "use-backend")) {
3466 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003467 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3468 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003469 err_code |= ERR_ALERT | ERR_FATAL;
3470 goto out;
3471 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003472 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003473 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003474 free(curagent->b.name);
3475 curagent->b.name = strdup(args[1]);
3476 }
3477 else if (!strcmp(args[0], "messages")) {
3478 int cur_arg = 1;
3479 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003480 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003481
Christopher Faulet11610f32017-09-21 10:23:10 +02003482 list_for_each_entry(ph, &curmphs, list) {
3483 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003484 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3485 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003486 err_code |= ERR_ALERT | ERR_FATAL;
3487 goto out;
3488 }
3489 }
3490
Christopher Faulet11610f32017-09-21 10:23:10 +02003491 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003492 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003493 err_code |= ERR_ALERT | ERR_ABORT;
3494 goto out;
3495 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003496 ph->id = strdup(args[cur_arg]);
3497 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003498 cur_arg++;
3499 }
3500 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003501 else if (!strcmp(args[0], "groups")) {
3502 int cur_arg = 1;
3503 while (*args[cur_arg]) {
3504 struct spoe_placeholder *ph = NULL;
3505
3506 list_for_each_entry(ph, &curgphs, list) {
3507 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003508 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3509 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003510 err_code |= ERR_ALERT | ERR_FATAL;
3511 goto out;
3512 }
3513 }
3514
3515 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003517 err_code |= ERR_ALERT | ERR_ABORT;
3518 goto out;
3519 }
3520 ph->id = strdup(args[cur_arg]);
3521 LIST_ADDQ(&curgphs, &ph->list);
3522 cur_arg++;
3523 }
3524 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003525 else if (!strcmp(args[0], "timeout")) {
3526 unsigned int *tv = NULL;
3527 const char *res;
3528 unsigned timeout;
3529
3530 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003531 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3532 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003533 err_code |= ERR_ALERT | ERR_FATAL;
3534 goto out;
3535 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003536 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3537 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003538 if (!strcmp(args[1], "hello"))
3539 tv = &curagent->timeout.hello;
3540 else if (!strcmp(args[1], "idle"))
3541 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003542 else if (!strcmp(args[1], "processing"))
3543 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003544 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003545 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3546 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003547 err_code |= ERR_ALERT | ERR_FATAL;
3548 goto out;
3549 }
3550 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003551 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3552 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003553 err_code |= ERR_ALERT | ERR_FATAL;
3554 goto out;
3555 }
3556 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003557 if (res == PARSE_TIME_OVER) {
3558 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3559 file, linenum, args[2], args[0], args[1]);
3560 err_code |= ERR_ALERT | ERR_FATAL;
3561 goto out;
3562 }
3563 else if (res == PARSE_TIME_UNDER) {
3564 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3565 file, linenum, args[2], args[0], args[1]);
3566 err_code |= ERR_ALERT | ERR_FATAL;
3567 goto out;
3568 }
3569 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003570 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3571 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003572 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003573 goto out;
3574 }
3575 *tv = MS_TO_TICKS(timeout);
3576 }
3577 else if (!strcmp(args[0], "option")) {
3578 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003579 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3580 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003581 err_code |= ERR_ALERT | ERR_FATAL;
3582 goto out;
3583 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003584
Christopher Faulet305c6072017-02-23 16:17:53 +01003585 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003586 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003587 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003588 if (kwm == 1)
3589 curagent->flags &= ~SPOE_FL_PIPELINING;
3590 else
3591 curagent->flags |= SPOE_FL_PIPELINING;
3592 goto out;
3593 }
3594 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003595 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003596 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003597 if (kwm == 1)
3598 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet46c76822019-09-17 11:55:52 +02003599 else
3600 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003601 goto out;
3602 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003603 else if (!strcmp(args[1], "send-frag-payload")) {
3604 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3605 goto out;
3606 if (kwm == 1)
3607 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3608 else
3609 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3610 goto out;
3611 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003612 else if (!strcmp(args[1], "dontlog-normal")) {
3613 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3614 goto out;
3615 if (kwm == 1)
3616 curpxopts2 &= ~PR_O2_NOLOGNORM;
3617 else
3618 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003619 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003620 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003621
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003622 /* Following options does not support negation */
3623 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003624 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3625 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003626 err_code |= ERR_ALERT | ERR_FATAL;
3627 goto out;
3628 }
3629
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003630 if (!strcmp(args[1], "var-prefix")) {
3631 char *tmp;
3632
3633 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003634 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3635 file, linenum, args[0],
3636 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003637 err_code |= ERR_ALERT | ERR_FATAL;
3638 goto out;
3639 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003640 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3641 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003642 tmp = args[2];
3643 while (*tmp) {
3644 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003645 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003646 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003647 err_code |= ERR_ALERT | ERR_FATAL;
3648 goto out;
3649 }
3650 tmp++;
3651 }
3652 curagent->var_pfx = strdup(args[2]);
3653 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003654 else if (!strcmp(args[1], "force-set-var")) {
3655 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3656 goto out;
3657 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3658 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003659 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003660 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003661 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003662 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3663 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003664 else if (!strcmp(args[1], "set-on-error")) {
3665 char *tmp;
3666
3667 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003668 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3669 file, linenum, args[0],
3670 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003671 err_code |= ERR_ALERT | ERR_FATAL;
3672 goto out;
3673 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003674 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3675 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003676 tmp = args[2];
3677 while (*tmp) {
3678 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003679 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003680 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003681 err_code |= ERR_ALERT | ERR_FATAL;
3682 goto out;
3683 }
3684 tmp++;
3685 }
3686 curagent->var_on_error = strdup(args[2]);
3687 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003688 else if (!strcmp(args[1], "set-process-time")) {
3689 char *tmp;
3690
3691 if (!*args[2]) {
3692 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3693 file, linenum, args[0],
3694 args[1]);
3695 err_code |= ERR_ALERT | ERR_FATAL;
3696 goto out;
3697 }
3698 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3699 goto out;
3700 tmp = args[2];
3701 while (*tmp) {
3702 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003703 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003704 file, linenum, args[0], args[1]);
3705 err_code |= ERR_ALERT | ERR_FATAL;
3706 goto out;
3707 }
3708 tmp++;
3709 }
3710 curagent->var_t_process = strdup(args[2]);
3711 }
3712 else if (!strcmp(args[1], "set-total-time")) {
3713 char *tmp;
3714
3715 if (!*args[2]) {
3716 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3717 file, linenum, args[0],
3718 args[1]);
3719 err_code |= ERR_ALERT | ERR_FATAL;
3720 goto out;
3721 }
3722 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3723 goto out;
3724 tmp = args[2];
3725 while (*tmp) {
3726 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003727 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003728 file, linenum, args[0], args[1]);
3729 err_code |= ERR_ALERT | ERR_FATAL;
3730 goto out;
3731 }
3732 tmp++;
3733 }
3734 curagent->var_t_total = strdup(args[2]);
3735 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003736 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003737 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3738 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003739 err_code |= ERR_ALERT | ERR_FATAL;
3740 goto out;
3741 }
Christopher Faulet48026722016-11-16 15:01:12 +01003742 }
3743 else if (!strcmp(args[0], "maxconnrate")) {
3744 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003745 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3746 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003747 err_code |= ERR_ALERT | ERR_FATAL;
3748 goto out;
3749 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003750 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003751 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003752 curagent->cps_max = atol(args[1]);
3753 }
3754 else if (!strcmp(args[0], "maxerrrate")) {
3755 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003756 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3757 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003758 err_code |= ERR_ALERT | ERR_FATAL;
3759 goto out;
3760 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003761 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003762 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003763 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003764 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003765 else if (!strcmp(args[0], "max-frame-size")) {
3766 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003767 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3768 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003769 err_code |= ERR_ALERT | ERR_FATAL;
3770 goto out;
3771 }
3772 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3773 goto out;
3774 curagent->max_frame_size = atol(args[1]);
3775 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3776 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003777 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3778 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003779 err_code |= ERR_ALERT | ERR_FATAL;
3780 goto out;
3781 }
3782 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003783 else if (!strcmp(args[0], "max-waiting-frames")) {
3784 if (!*args[1]) {
3785 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3786 file, linenum, args[0]);
3787 err_code |= ERR_ALERT | ERR_FATAL;
3788 goto out;
3789 }
3790 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3791 goto out;
3792 curagent->max_fpa = atol(args[1]);
3793 if (curagent->max_fpa < 1) {
3794 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3795 file, linenum, args[0]);
3796 err_code |= ERR_ALERT | ERR_FATAL;
3797 goto out;
3798 }
3799 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003800 else if (!strcmp(args[0], "register-var-names")) {
3801 int cur_arg;
3802
3803 if (!*args[1]) {
3804 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3805 file, linenum, args[0]);
3806 err_code |= ERR_ALERT | ERR_FATAL;
3807 goto out;
3808 }
3809 cur_arg = 1;
3810 while (*args[cur_arg]) {
3811 struct spoe_var_placeholder *vph;
3812
3813 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3814 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3815 err_code |= ERR_ALERT | ERR_ABORT;
3816 goto out;
3817 }
3818 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusf818ea52019-06-23 22:10:13 +02003819 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003820 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3821 err_code |= ERR_ALERT | ERR_ABORT;
3822 goto out;
3823 }
3824 LIST_ADDQ(&curvars, &vph->list);
3825 cur_arg++;
3826 }
3827 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003828 else if (!strcmp(args[0], "log")) {
3829 char *errmsg = NULL;
3830
3831 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3832 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3833 err_code |= ERR_ALERT | ERR_FATAL;
3834 goto out;
3835 }
3836 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003837 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003838 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3839 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003840 err_code |= ERR_ALERT | ERR_FATAL;
3841 goto out;
3842 }
3843 out:
3844 return err_code;
3845}
Christopher Faulet11610f32017-09-21 10:23:10 +02003846static int
3847cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3848{
3849 struct spoe_group *grp;
3850 const char *err;
3851 int err_code = 0;
3852
3853 if ((cfg_scope == NULL && curengine != NULL) ||
3854 (cfg_scope != NULL && curengine == NULL) ||
3855 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3856 goto out;
3857
3858 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3859 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003860 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3861 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003862 err_code |= ERR_ALERT | ERR_ABORT;
3863 goto out;
3864 }
3865 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3866 err_code |= ERR_ABORT;
3867 goto out;
3868 }
3869
3870 err = invalid_char(args[1]);
3871 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003872 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3873 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003874 err_code |= ERR_ALERT | ERR_ABORT;
3875 goto out;
3876 }
3877
3878 list_for_each_entry(grp, &curgrps, list) {
3879 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003880 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3881 " name as another one declared at %s:%d.\n",
3882 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003883 err_code |= ERR_ALERT | ERR_FATAL;
3884 goto out;
3885 }
3886 }
3887
3888 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003889 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003890 err_code |= ERR_ALERT | ERR_ABORT;
3891 goto out;
3892 }
3893
3894 curgrp->id = strdup(args[1]);
3895 curgrp->conf.file = strdup(file);
3896 curgrp->conf.line = linenum;
3897 LIST_INIT(&curgrp->phs);
3898 LIST_INIT(&curgrp->messages);
3899 LIST_ADDQ(&curgrps, &curgrp->list);
3900 }
3901 else if (!strcmp(args[0], "messages")) {
3902 int cur_arg = 1;
3903 while (*args[cur_arg]) {
3904 struct spoe_placeholder *ph = NULL;
3905
3906 list_for_each_entry(ph, &curgrp->phs, list) {
3907 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003908 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3909 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003910 err_code |= ERR_ALERT | ERR_FATAL;
3911 goto out;
3912 }
3913 }
3914
3915 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003916 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003917 err_code |= ERR_ALERT | ERR_ABORT;
3918 goto out;
3919 }
3920 ph->id = strdup(args[cur_arg]);
3921 LIST_ADDQ(&curgrp->phs, &ph->list);
3922 cur_arg++;
3923 }
3924 }
3925 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003926 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3927 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003928 err_code |= ERR_ALERT | ERR_FATAL;
3929 goto out;
3930 }
3931 out:
3932 return err_code;
3933}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003934
3935static int
3936cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3937{
3938 struct spoe_message *msg;
3939 struct spoe_arg *arg;
3940 const char *err;
3941 char *errmsg = NULL;
3942 int err_code = 0;
3943
3944 if ((cfg_scope == NULL && curengine != NULL) ||
3945 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003946 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003947 goto out;
3948
3949 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3950 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003951 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3952 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003953 err_code |= ERR_ALERT | ERR_ABORT;
3954 goto out;
3955 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003956 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3957 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003958 goto out;
3959 }
3960
3961 err = invalid_char(args[1]);
3962 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003963 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3964 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003965 err_code |= ERR_ALERT | ERR_ABORT;
3966 goto out;
3967 }
3968
3969 list_for_each_entry(msg, &curmsgs, list) {
3970 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003971 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3972 " name as another one declared at %s:%d.\n",
3973 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003974 err_code |= ERR_ALERT | ERR_FATAL;
3975 goto out;
3976 }
3977 }
3978
3979 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003980 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 err_code |= ERR_ALERT | ERR_ABORT;
3982 goto out;
3983 }
3984
3985 curmsg->id = strdup(args[1]);
3986 curmsg->id_len = strlen(curmsg->id);
3987 curmsg->event = SPOE_EV_NONE;
3988 curmsg->conf.file = strdup(file);
3989 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003990 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003991 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003992 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003993 LIST_INIT(&curmsg->by_evt);
3994 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003995 LIST_ADDQ(&curmsgs, &curmsg->list);
3996 }
3997 else if (!strcmp(args[0], "args")) {
3998 int cur_arg = 1;
3999
4000 curproxy->conf.args.ctx = ARGC_SPOE;
4001 curproxy->conf.args.file = file;
4002 curproxy->conf.args.line = linenum;
4003 while (*args[cur_arg]) {
4004 char *delim = strchr(args[cur_arg], '=');
4005 int idx = 0;
4006
4007 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004008 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004009 err_code |= ERR_ALERT | ERR_ABORT;
4010 goto out;
4011 }
4012
4013 if (!delim) {
4014 arg->name = NULL;
4015 arg->name_len = 0;
4016 delim = args[cur_arg];
4017 }
4018 else {
4019 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
4020 arg->name_len = delim - args[cur_arg];
4021 delim++;
4022 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01004023 arg->expr = sample_parse_expr((char*[]){delim, NULL},
4024 &idx, file, linenum, &errmsg,
4025 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004026 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004027 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004028 err_code |= ERR_ALERT | ERR_FATAL;
4029 free(arg->name);
4030 free(arg);
4031 goto out;
4032 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004033 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004034 LIST_ADDQ(&curmsg->args, &arg->list);
4035 cur_arg++;
4036 }
4037 curproxy->conf.args.file = NULL;
4038 curproxy->conf.args.line = 0;
4039 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004040 else if (!strcmp(args[0], "acl")) {
4041 err = invalid_char(args[1]);
4042 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004043 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4044 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004045 err_code |= ERR_ALERT | ERR_FATAL;
4046 goto out;
4047 }
Tim Duesterhus6b7ce1e2020-02-05 21:00:50 +01004048 if (strcasecmp(args[1], "or") == 0) {
4049 ha_warning("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
4050 "logical disjunction within a condition.\n",
4051 file, linenum, args[1]);
4052 err_code |= ERR_WARN;
4053 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004054 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004055 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4056 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004057 err_code |= ERR_ALERT | ERR_FATAL;
4058 goto out;
4059 }
4060 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004061 else if (!strcmp(args[0], "event")) {
4062 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004063 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004064 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004065 goto out;
4066 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004067 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4068 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004069
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004070 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
4071 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
4072 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
4073 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4074
4075 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
4076 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
4077 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
4078 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
4079 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
4080 curmsg->event = SPOE_EV_ON_TCP_RSP;
4081
4082 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
4083 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
4084 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
4085 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
4086 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
4087 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4088 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004089 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004090 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004091 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004092 goto out;
4093 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004094
4095 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4096 struct acl_cond *cond;
4097
4098 cond = build_acl_cond(file, linenum, &curmsg->acls,
4099 curproxy, (const char **)args+2,
4100 &errmsg);
4101 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004102 ha_alert("parsing [%s:%d] : error detected while "
4103 "parsing an 'event %s' condition : %s.\n",
4104 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004105 err_code |= ERR_ALERT | ERR_FATAL;
4106 goto out;
4107 }
4108 curmsg->cond = cond;
4109 }
4110 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004111 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4112 "or 'unless' followed by a condition but found '%s'.\n",
4113 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004114 err_code |= ERR_ALERT | ERR_FATAL;
4115 goto out;
4116 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004117 }
4118 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004119 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4120 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004121 err_code |= ERR_ALERT | ERR_FATAL;
4122 goto out;
4123 }
4124 out:
4125 free(errmsg);
4126 return err_code;
4127}
4128
4129/* Return -1 on error, else 0 */
4130static int
4131parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4132 struct flt_conf *fconf, char **err, void *private)
4133{
4134 struct list backup_sections;
4135 struct spoe_config *conf;
4136 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004137 struct spoe_group *grp, *grpback;
4138 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004139 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004140 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004141 char *file = NULL, *engine = NULL;
4142 int ret, pos = *cur_arg + 1;
4143
Christopher Faulet84c844e2018-03-23 14:37:14 +01004144 LIST_INIT(&curmsgs);
4145 LIST_INIT(&curgrps);
4146 LIST_INIT(&curmphs);
4147 LIST_INIT(&curgphs);
4148 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004149 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004150 curpxopts = 0;
4151 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004152
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004153 conf = calloc(1, sizeof(*conf));
4154 if (conf == NULL) {
4155 memprintf(err, "%s: out of memory", args[*cur_arg]);
4156 goto error;
4157 }
4158 conf->proxy = px;
4159
4160 while (*args[pos]) {
4161 if (!strcmp(args[pos], "config")) {
4162 if (!*args[pos+1]) {
4163 memprintf(err, "'%s' : '%s' option without value",
4164 args[*cur_arg], args[pos]);
4165 goto error;
4166 }
4167 file = args[pos+1];
4168 pos += 2;
4169 }
4170 else if (!strcmp(args[pos], "engine")) {
4171 if (!*args[pos+1]) {
4172 memprintf(err, "'%s' : '%s' option without value",
4173 args[*cur_arg], args[pos]);
4174 goto error;
4175 }
4176 engine = args[pos+1];
4177 pos += 2;
4178 }
4179 else {
4180 memprintf(err, "unknown keyword '%s'", args[pos]);
4181 goto error;
4182 }
4183 }
4184 if (file == NULL) {
4185 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4186 goto error;
4187 }
4188
4189 /* backup sections and register SPOE sections */
4190 LIST_INIT(&backup_sections);
4191 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004192 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4193 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004194 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004195
4196 /* Parse SPOE filter configuration file */
4197 curengine = engine;
4198 curproxy = px;
4199 curagent = NULL;
4200 curmsg = NULL;
4201 ret = readcfgfile(file);
4202 curproxy = NULL;
4203
4204 /* unregister SPOE sections and restore previous sections */
4205 cfg_unregister_sections();
4206 cfg_restore_sections(&backup_sections);
4207
4208 if (ret == -1) {
4209 memprintf(err, "Could not open configuration file %s : %s",
4210 file, strerror(errno));
4211 goto error;
4212 }
4213 if (ret & (ERR_ABORT|ERR_FATAL)) {
4214 memprintf(err, "Error(s) found in configuration file %s", file);
4215 goto error;
4216 }
4217
4218 /* Check SPOE agent */
4219 if (curagent == NULL) {
4220 memprintf(err, "No SPOE agent found in file %s", file);
4221 goto error;
4222 }
4223 if (curagent->b.name == NULL) {
4224 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4225 curagent->id, curagent->conf.file, curagent->conf.line);
4226 goto error;
4227 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004228 if (curagent->timeout.hello == TICK_ETERNITY ||
4229 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004230 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004231 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4232 " | While not properly invalid, you will certainly encounter various problems\n"
4233 " | with such a configuration. To fix this, please ensure that all following\n"
4234 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4235 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004236 }
4237 if (curagent->var_pfx == NULL) {
4238 char *tmp = curagent->id;
4239
4240 while (*tmp) {
4241 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4242 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4243 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4244 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4245 goto error;
4246 }
4247 tmp++;
4248 }
4249 curagent->var_pfx = strdup(curagent->id);
4250 }
4251
Christopher Fauletb7426d12018-03-21 14:12:17 +01004252 if (curagent->var_on_error) {
4253 struct arg arg;
4254
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004255 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004256 curagent->var_pfx, curagent->var_on_error);
4257
4258 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004259 arg.data.str.area = trash.area;
4260 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004261 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004262 if (!vars_check_arg(&arg, err)) {
4263 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4264 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4265 goto error;
4266 }
4267 }
4268
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004269 if (curagent->var_t_process) {
4270 struct arg arg;
4271
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004272 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004273 curagent->var_pfx, curagent->var_t_process);
4274
4275 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004276 arg.data.str.area = trash.area;
4277 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004278 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004279 if (!vars_check_arg(&arg, err)) {
4280 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4281 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4282 goto error;
4283 }
4284 }
4285
4286 if (curagent->var_t_total) {
4287 struct arg arg;
4288
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004289 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004290 curagent->var_pfx, curagent->var_t_total);
4291
4292 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004293 arg.data.str.area = trash.area;
4294 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004295 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004296 if (!vars_check_arg(&arg, err)) {
4297 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4298 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4299 goto error;
4300 }
4301 }
4302
Christopher Faulet11610f32017-09-21 10:23:10 +02004303 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004304 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4305 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004306 goto finish;
4307 }
4308
Christopher Faulet11610f32017-09-21 10:23:10 +02004309 /* Replace placeholders by the corresponding messages for the SPOE
4310 * agent */
4311 list_for_each_entry(ph, &curmphs, list) {
4312 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004313 struct spoe_arg *arg;
4314 unsigned int where;
4315
Christopher Faulet11610f32017-09-21 10:23:10 +02004316 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004317 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4318 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4319 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4320 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4321 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4322 }
4323 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4324 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4325 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004326 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4327 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004328 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004329 }
4330 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004331 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4332 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004333 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004334 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004335
4336 where = 0;
4337 switch (msg->event) {
4338 case SPOE_EV_ON_CLIENT_SESS:
4339 where |= SMP_VAL_FE_CON_ACC;
4340 break;
4341
4342 case SPOE_EV_ON_TCP_REQ_FE:
4343 where |= SMP_VAL_FE_REQ_CNT;
4344 break;
4345
4346 case SPOE_EV_ON_HTTP_REQ_FE:
4347 where |= SMP_VAL_FE_HRQ_HDR;
4348 break;
4349
4350 case SPOE_EV_ON_TCP_REQ_BE:
4351 if (px->cap & PR_CAP_FE)
4352 where |= SMP_VAL_FE_REQ_CNT;
4353 if (px->cap & PR_CAP_BE)
4354 where |= SMP_VAL_BE_REQ_CNT;
4355 break;
4356
4357 case SPOE_EV_ON_HTTP_REQ_BE:
4358 if (px->cap & PR_CAP_FE)
4359 where |= SMP_VAL_FE_HRQ_HDR;
4360 if (px->cap & PR_CAP_BE)
4361 where |= SMP_VAL_BE_HRQ_HDR;
4362 break;
4363
4364 case SPOE_EV_ON_SERVER_SESS:
4365 where |= SMP_VAL_BE_SRV_CON;
4366 break;
4367
4368 case SPOE_EV_ON_TCP_RSP:
4369 if (px->cap & PR_CAP_FE)
4370 where |= SMP_VAL_FE_RES_CNT;
4371 if (px->cap & PR_CAP_BE)
4372 where |= SMP_VAL_BE_RES_CNT;
4373 break;
4374
4375 case SPOE_EV_ON_HTTP_RSP:
4376 if (px->cap & PR_CAP_FE)
4377 where |= SMP_VAL_FE_HRS_HDR;
4378 if (px->cap & PR_CAP_BE)
4379 where |= SMP_VAL_BE_HRS_HDR;
4380 break;
4381
4382 default:
4383 break;
4384 }
4385
4386 list_for_each_entry(arg, &msg->args, list) {
4387 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004388 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004389 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004390 "none of which is available here ('%s')",
4391 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004392 sample_ckp_names(arg->expr->fetch->use),
4393 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004394 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004395 }
4396 }
4397
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004398 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004399 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4400 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004401 }
4402 }
4403 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004404 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004405 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004406 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004407 continue;
4408 }
4409
Christopher Faulet11610f32017-09-21 10:23:10 +02004410 /* Replace placeholders by the corresponding groups for the SPOE
4411 * agent */
4412 list_for_each_entry(ph, &curgphs, list) {
4413 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4414 if (!strcmp(grp->id, ph->id)) {
4415 grp->agent = curagent;
4416 LIST_DEL(&grp->list);
4417 LIST_ADDQ(&curagent->groups, &grp->list);
4418 goto next_aph;
4419 }
4420 }
4421 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4422 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4423 goto error;
4424 next_aph:
4425 continue;
4426 }
4427
4428 /* Replace placeholders by the corresponding message for each SPOE
4429 * group of the SPOE agent */
4430 list_for_each_entry(grp, &curagent->groups, list) {
4431 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4432 list_for_each_entry(msg, &curmsgs, list) {
4433 if (!strcmp(msg->id, ph->id)) {
4434 if (msg->group != NULL) {
4435 memprintf(err, "SPOE message '%s' already belongs to "
4436 "the SPOE group '%s' declare at %s:%d",
4437 msg->id, msg->group->id,
4438 msg->group->conf.file,
4439 msg->group->conf.line);
4440 goto error;
4441 }
4442
4443 /* Scope for arguments are not checked for now. We will check
4444 * them only if a rule use the corresponding SPOE group. */
4445 msg->agent = curagent;
4446 msg->group = grp;
4447 LIST_DEL(&ph->list);
4448 LIST_ADDQ(&grp->messages, &msg->by_grp);
4449 goto next_mph_grp;
4450 }
4451 }
4452 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4453 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4454 goto error;
4455 next_mph_grp:
4456 continue;
4457 }
4458 }
4459
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004460 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004461 /* move curmsgs to the agent message list */
4462 curmsgs.n->p = &curagent->messages;
4463 curmsgs.p->n = &curagent->messages;
4464 curagent->messages = curmsgs;
4465 LIST_INIT(&curmsgs);
4466
Christopher Faulet7ee86672017-09-19 11:08:28 +02004467 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004468 conf->agent = curagent;
Christopher Faulet75fc5f32021-08-02 17:51:01 +02004469 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004470
4471 /* Start agent's proxy initialization here. It will be finished during
4472 * the filter init. */
4473 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4474 init_new_proxy(&conf->agent_fe);
4475 conf->agent_fe.id = conf->agent->id;
4476 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004477 conf->agent_fe.options |= curpxopts;
4478 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004479
4480 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4481 LIST_DEL(&logsrv->list);
4482 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4483 }
4484
Christopher Faulet11610f32017-09-21 10:23:10 +02004485 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4486 LIST_DEL(&ph->list);
4487 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004488 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004489 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4490 LIST_DEL(&ph->list);
4491 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004492 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004493 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4494 struct arg arg;
4495
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004496 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004497 curagent->var_pfx, vph->name);
4498
4499 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004500 arg.data.str.area = trash.area;
4501 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004502 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004503 if (!vars_check_arg(&arg, err)) {
4504 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4505 curagent->id, curagent->var_pfx, vph->name, *err);
4506 goto error;
4507 }
4508
4509 LIST_DEL(&vph->list);
4510 free(vph->name);
4511 free(vph);
4512 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004513 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4514 LIST_DEL(&grp->list);
4515 spoe_release_group(grp);
4516 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004517 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004518 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004519 fconf->ops = &spoe_ops;
4520 fconf->conf = conf;
4521 return 0;
4522
4523 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004524 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004525 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4526 LIST_DEL(&ph->list);
4527 spoe_release_placeholder(ph);
4528 }
4529 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4530 LIST_DEL(&ph->list);
4531 spoe_release_placeholder(ph);
4532 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004533 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4534 LIST_DEL(&vph->list);
4535 free(vph->name);
4536 free(vph);
4537 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004538 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4539 LIST_DEL(&grp->list);
4540 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004541 }
4542 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4543 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004544 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004545 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004546 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4547 LIST_DEL(&logsrv->list);
4548 free(logsrv);
4549 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004550 free(conf);
4551 return -1;
4552}
4553
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004554/* Send message of a SPOE group. This is the action_ptr callback of a rule
4555 * associated to a "send-spoe-group" action.
4556 *
4557 * It returns ACT_RET_CONT is processing is finished without error, it returns
4558 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4559 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004560static enum act_return
4561spoe_send_group(struct act_rule *rule, struct proxy *px,
4562 struct session *sess, struct stream *s, int flags)
4563{
4564 struct filter *filter;
4565 struct spoe_agent *agent = NULL;
4566 struct spoe_group *group = NULL;
4567 struct spoe_context *ctx = NULL;
4568 int ret, dir;
4569
4570 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4571 if (filter->config == rule->arg.act.p[0]) {
4572 agent = rule->arg.act.p[2];
4573 group = rule->arg.act.p[3];
4574 ctx = filter->ctx;
4575 break;
4576 }
4577 }
4578 if (agent == NULL || group == NULL || ctx == NULL)
4579 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004580 if (ctx->state == SPOE_CTX_ST_NONE)
4581 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004582
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004583 switch (rule->from) {
4584 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4585 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4586 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4587 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4588 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4589 default:
4590 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4591 " - internal error while execute spoe-send-group\n",
4592 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4593 __FUNCTION__, s);
4594 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4595 agent->id);
4596 return ACT_RET_CONT;
4597 }
4598
4599 ret = spoe_process_group(s, ctx, group, dir);
4600 if (ret == 1)
4601 return ACT_RET_CONT;
4602 else if (ret == 0) {
4603 if (flags & ACT_FLAG_FINAL) {
4604 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4605 " - failed to process group '%s': interrupted by caller\n",
4606 (int)now.tv_sec, (int)now.tv_usec,
4607 agent->id, __FUNCTION__, s, group->id);
4608 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004609 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004610 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004611 return ACT_RET_CONT;
4612 }
4613 return ACT_RET_YIELD;
4614 }
4615 else
4616 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004617}
4618
4619/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4620 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4621 * action should be:
4622 *
4623 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4624 *
4625 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4626 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4627 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4628 * group.
4629 *
4630 * The function returns 1 in success case, otherwise, it returns 0 and err is
4631 * filled.
4632 */
4633static int
4634check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4635{
4636 struct flt_conf *fconf;
4637 struct spoe_config *conf;
4638 struct spoe_agent *agent = NULL;
4639 struct spoe_group *group;
4640 struct spoe_message *msg;
4641 char *engine_id = rule->arg.act.p[0];
4642 char *group_id = rule->arg.act.p[1];
4643 unsigned int where = 0;
4644
4645 switch (rule->from) {
4646 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4647 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4648 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4649 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4650 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4651 default:
4652 memprintf(err,
4653 "internal error, unexpected rule->from=%d, please report this bug!",
4654 rule->from);
4655 goto error;
4656 }
4657
4658 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4659 * <px> */
4660 list_for_each_entry(fconf, &px->filter_configs, list) {
4661 conf = fconf->conf;
4662
4663 /* This is not an SPOE filter */
4664 if (fconf->id != spoe_filter_id)
4665 continue;
4666
4667 /* This is the good engine */
4668 if (!strcmp(conf->id, engine_id)) {
4669 agent = conf->agent;
4670 break;
4671 }
4672 }
4673 if (agent == NULL) {
4674 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4675 engine_id, group_id);
4676 goto error;
4677 }
4678
4679 /* Try to find the right group */
4680 list_for_each_entry(group, &agent->groups, list) {
4681 /* This is the good group */
4682 if (!strcmp(group->id, group_id))
4683 break;
4684 }
4685 if (&group->list == &agent->groups) {
4686 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4687 group_id, engine_id);
4688 goto error;
4689 }
4690
4691 /* Ok, we found the group, we need to check messages and their
4692 * arguments */
4693 list_for_each_entry(msg, &group->messages, by_grp) {
4694 struct spoe_arg *arg;
4695
4696 list_for_each_entry(arg, &msg->args, list) {
4697 if (!(arg->expr->fetch->val & where)) {
4698 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4699 "some args extract information from '%s',"
4700 "none of which is available here ('%s')",
4701 msg->id, group->id, msg->conf.file, msg->conf.line,
4702 sample_ckp_names(arg->expr->fetch->use),
4703 sample_ckp_names(where));
4704 goto error;
4705 }
4706 }
4707 }
4708
4709 free(engine_id);
4710 free(group_id);
4711 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4712 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4713 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4714 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4715 return 1;
4716
4717 error:
4718 free(engine_id);
4719 free(group_id);
4720 return 0;
4721}
4722
4723/* Parse 'send-spoe-group' action following the format:
4724 *
4725 * ... send-spoe-group <engine-id> <group-id>
4726 *
4727 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4728 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4729 * ids are saved and used later, when the rule will be checked.
4730 */
4731static enum act_parse_ret
4732parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4733 struct act_rule *rule, char **err)
4734{
4735 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4736 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4737 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4738 return ACT_RET_PRS_ERR;
4739 }
4740 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4741 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4742
4743 (*orig_arg) += 2;
4744
4745 rule->action = ACT_CUSTOM;
4746 rule->action_ptr = spoe_send_group;
4747 rule->check_ptr = check_send_spoe_group;
4748 return ACT_RET_PRS_OK;
4749}
4750
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004751
4752/* Declare the filter parser for "spoe" keyword */
4753static struct flt_kw_list flt_kws = { "SPOE", { }, {
4754 { "spoe", parse_spoe_flt, NULL },
4755 { NULL, NULL, NULL },
4756 }
4757};
4758
Willy Tarreau0108d902018-11-25 19:14:37 +01004759INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4760
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004761/* Delcate the action parser for "spoe-action" keyword */
4762static struct action_kw_list tcp_req_action_kws = { { }, {
4763 { "send-spoe-group", parse_send_spoe_group },
4764 { /* END */ },
4765 }
4766};
Willy Tarreau0108d902018-11-25 19:14:37 +01004767
4768INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4769
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004770static struct action_kw_list tcp_res_action_kws = { { }, {
4771 { "send-spoe-group", parse_send_spoe_group },
4772 { /* END */ },
4773 }
4774};
Willy Tarreau0108d902018-11-25 19:14:37 +01004775
4776INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4777
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004778static struct action_kw_list http_req_action_kws = { { }, {
4779 { "send-spoe-group", parse_send_spoe_group },
4780 { /* END */ },
4781 }
4782};
Willy Tarreau0108d902018-11-25 19:14:37 +01004783
4784INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4785
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004786static struct action_kw_list http_res_action_kws = { { }, {
4787 { "send-spoe-group", parse_send_spoe_group },
4788 { /* END */ },
4789 }
4790};
4791
Willy Tarreau0108d902018-11-25 19:14:37 +01004792INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);