blob: 3f264872ad692c2fe8d1f3dd7d1d0cb0cdea772c [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
Willy Tarreaudcc048a2020-06-04 19:11:43 +020015#include <haproxy/acl.h>
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +020016#include <haproxy/applet.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020017#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020018#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020019#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Tim Duesterhus75e2f8d2021-09-16 17:38:26 +020021#include <haproxy/check.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010022#include <haproxy/conn_stream.h>
23#include <haproxy/cs_utils.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020024#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020025#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020026#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020027#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020028#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020029#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020030#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020033#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020034#include <haproxy/signal.h>
Christopher Faulet1d7d0f82021-02-19 10:56:41 +010035#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020036#include <haproxy/spoe.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020038#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020039#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020040#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020041#include <haproxy/time.h>
Willy Tarreaue16ada12021-05-08 12:57:17 +020042#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020043#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020044
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045
46#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
47#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010048#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020049#else
50#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010051#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020052#endif
53
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010054/* Reserved 4 bytes to the frame size. So a frame and its size can be written
55 * together in a buffer */
56#define MAX_FRAME_SIZE global.tune.bufsize - 4
57
58/* The minimum size for a frame */
59#define MIN_FRAME_SIZE 256
60
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010061/* Reserved for the metadata and the frame type.
62 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010063#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020064
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010065/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010066#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
67
Christopher Faulet3b386a32017-02-23 10:17:15 +010068/* SPOE filter id. Used to identify SPOE filters */
69const char *spoe_filter_id = "SPOE filter";
70
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020071/* Set if the handle on SIGUSR1 is registered */
72static int sighandler_registered = 0;
73
74/* proxy used during the parsing */
75struct proxy *curproxy = NULL;
76
77/* The name of the SPOE engine, used during the parsing */
78char *curengine = NULL;
79
80/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020081/* SPOE agent/group/message used during the parsing */
82struct spoe_agent *curagent = NULL;
83struct spoe_group *curgrp = NULL;
84struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020085
86/* list of SPOE messages and placeholders used during the parsing */
87struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020088struct list curgrps;
89struct list curmphs;
90struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010091struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020092
Christopher Faulet7250b8f2018-03-26 17:19:01 +020093/* list of log servers used during the parsing */
94struct list curlogsrvs;
95
Christopher Faulet0e0f0852018-03-26 17:20:36 +020096/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
97int curpxopts;
98int curpxopts2;
99
Christopher Faulet42bfa462017-01-04 14:14:19 +0100100/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100101DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
102DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200103
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104struct flt_ops spoe_ops;
105
Christopher Faulet8ef75252017-02-20 22:56:03 +0100106static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200107static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
108static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Faulet6f1296b2021-08-02 17:53:56 +0200109static struct appctx *spoe_create_appctx(struct spoe_config *conf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200110
111/********************************************************************
112 * helper functions/globals
113 ********************************************************************/
114static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200115spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116{
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 free(ph->id);
120 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121}
122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100124spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125{
Christopher Faulet57583e42017-09-04 15:41:09 +0200126 struct spoe_arg *arg, *argback;
127 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200128
129 if (!msg)
130 return;
131 free(msg->id);
132 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200133 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200134 release_sample_expr(arg->expr);
135 free(arg->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200136 LIST_DELETE(&arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200137 free(arg);
138 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200139 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200140 LIST_DELETE(&acl->list);
Christopher Faulet57583e42017-09-04 15:41:09 +0200141 prune_acl(acl);
142 free(acl);
143 }
144 if (msg->cond) {
145 prune_acl_cond(msg->cond);
146 free(msg->cond);
147 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200148 free(msg);
149}
150
151static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200152spoe_release_group(struct spoe_group *grp)
153{
154 if (!grp)
155 return;
156 free(grp->id);
157 free(grp->conf.file);
158 free(grp);
159}
160
161static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100162spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163{
Christopher Faulet11610f32017-09-21 10:23:10 +0200164 struct spoe_message *msg, *msgback;
165 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200166 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200167
168 if (!agent)
169 return;
170 free(agent->id);
171 free(agent->conf.file);
172 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100173 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100174 free(agent->var_t_process);
175 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200176 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200177 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200178 spoe_release_message(msg);
179 }
180 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200181 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200182 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200183 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200185 for (i = 0; i < global.nbthread; ++i) {
186 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200188 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100189 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200190 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200191 free(agent);
192}
193
194static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100195 [SPOE_FRM_ERR_NONE] = "normal",
196 [SPOE_FRM_ERR_IO] = "I/O error",
197 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
198 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
199 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
200 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
201 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
202 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
203 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
204 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
205 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
206 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100207 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100208 [SPOE_FRM_ERR_RES] = "resource allocation error",
209 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200210};
211
212static const char *spoe_event_str[SPOE_EV_EVENTS] = {
213 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
214 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
215 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
216 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
217 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
218
219 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
220 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
221 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
222};
223
224
225#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
226
227static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100228 [SPOE_CTX_ST_NONE] = "NONE",
229 [SPOE_CTX_ST_READY] = "READY",
230 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
231 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
232 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
233 [SPOE_CTX_ST_DONE] = "DONE",
234 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200235};
236
237static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100238 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
239 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
240 [SPOE_APPCTX_ST_IDLE] = "IDLE",
241 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
242 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
243 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
244 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
245 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
246 [SPOE_APPCTX_ST_EXIT] = "EXIT",
247 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200248};
249
250#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100251
Christopher Faulet8ef75252017-02-20 22:56:03 +0100252/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500253 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100254 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100255static char *
256generate_pseudo_uuid()
257{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100258 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800259 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100260}
261
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100262
263static inline void
264spoe_update_stat_time(struct timeval *tv, long *t)
265{
266 if (*t == -1)
267 *t = tv_ms_elapsed(tv, &now);
268 else
269 *t += tv_ms_elapsed(tv, &now);
270 tv_zero(tv);
271}
272
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200273/********************************************************************
274 * Functions that encode/decode SPOE frames
275 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200276/* Helper to get static string length, excluding the terminating null byte */
277#define SLEN(str) (sizeof(str)-1)
278
279/* Predefined key used in HELLO/DISCONNECT frames */
280#define SUPPORTED_VERSIONS_KEY "supported-versions"
281#define VERSION_KEY "version"
282#define MAX_FRAME_SIZE_KEY "max-frame-size"
283#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100284#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100285#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200286#define STATUS_CODE_KEY "status-code"
287#define MSG_KEY "message"
288
289struct spoe_version {
290 char *str;
291 int min;
292 int max;
293};
294
295/* All supported versions */
296static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200297 /* 1.0 is now unsupported because of a bug about frame's flags*/
298 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200299 {NULL, 0, 0}
300};
301
302/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200303#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304
Christopher Faulet8ef75252017-02-20 22:56:03 +0100305/* Convert a string to a SPOE version value. The string must follow the format
306 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
307 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 const char *p, *end;
312 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200313
Christopher Faulet8ef75252017-02-20 22:56:03 +0100314 p = str;
315 end = str+len;
316 maj = min = 0;
317 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200318
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100320 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200322
Christopher Faulet8ef75252017-02-20 22:56:03 +0100323 /* parse Major number, until the '.' */
324 while (*p != '.') {
325 if (p >= end || *p < '0' || *p > '9')
326 goto out;
327 maj *= 10;
328 maj += (*p - '0');
329 p++;
330 }
331
332 /* check Major version */
333 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334 goto out;
335
Christopher Faulet8ef75252017-02-20 22:56:03 +0100336 p++; /* skip the '.' */
337 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
338 goto out;
339
340 /* Parse Minor number */
341 while (p < end) {
342 if (*p < '0' || *p > '9')
343 break;
344 min *= 10;
345 min += (*p - '0');
346 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200347 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100348
349 /* check Minor number */
350 if (min > 999)
351 goto out;
352
353 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100354 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100355 p++;
356 if (p != end)
357 goto out;
358
359 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200360 out:
361 return vsn;
362}
363
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800365 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200367static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100368spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200369{
Willy Tarreau83061a82018-07-13 11:56:34 +0200370 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100371 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100372 char *p, *end;
373 unsigned int flags = SPOE_FRM_FL_FIN;
374 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 p = frame;
377 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Faulet8ef75252017-02-20 22:56:03 +0100379 /* Set Frame type */
380 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200381
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100382 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200383 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384 memcpy(p, (char *)&flags, 4);
385 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386
387 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100388 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200389
390 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
391 * and "capabilities" */
392
393 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100394 sz = SLEN(SUPPORTED_VERSIONS_KEY);
395 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
396 goto too_big;
397
398 *p++ = SPOE_DATA_T_STR;
399 sz = SLEN(SUPPORTED_VERSIONS_VAL);
400 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
401 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200402
403 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100404 sz = SLEN(MAX_FRAME_SIZE_KEY);
405 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
406 goto too_big;
407
408 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200409 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
412 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 sz = SLEN(CAPABILITIES_KEY);
414 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
415 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200416
Christopher Faulet8ef75252017-02-20 22:56:03 +0100417 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100418 chk = get_trash_chunk();
419 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200420 memcpy(chk->area, "pipelining", 10);
421 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100422 }
423 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200424 if (chk->data) chk->area[chk->data++] = ',';
425 memcpy(chk->area+chk->data, "async", 5);
426 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100427 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100428 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 if (chk->data) chk->area[chk->data++] = ',';
430 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100431 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100432 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200433 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 goto too_big;
435
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500436 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200437 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100438 sz = SLEN(ENGINE_ID_KEY);
439 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
440 goto too_big;
441
442 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200443 sz = strlen(agent->rt[tid].engine_id);
444 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100446 }
447
Christopher Faulet8ef75252017-02-20 22:56:03 +0100448 return (p - frame);
449
450 too_big:
451 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
452 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200453}
454
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
456 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
457 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200460{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 const char *reason;
462 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100463 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 p = frame;
467 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 /* Set Frame type */
470 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100472 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200473 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474 memcpy(p, (char *)&flags, 4);
475 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200476
477 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100478 *p++ = 0; *p++ = 0;
479
480 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
481 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200482
483 /* There are 2 mandatory items: "status-code" and "message" */
484
485 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100486 sz = SLEN(STATUS_CODE_KEY);
487 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
488 goto too_big;
489
490 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200491 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
494 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 sz = SLEN(MSG_KEY);
496 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
497 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200498
Christopher Faulet8ef75252017-02-20 22:56:03 +0100499 /*Get the message corresponding to the status code */
500 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
501
502 *p++ = SPOE_DATA_T_STR;
503 sz = strlen(reason);
504 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
505 goto too_big;
506
507 return (p - frame);
508
509 too_big:
510 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
511 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512}
513
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
515 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
516 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100519 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 char *p, *end;
522 unsigned int stream_id, frame_id;
523 unsigned int flags = SPOE_FRM_FL_FIN;
524 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Faulet8ef75252017-02-20 22:56:03 +0100526 p = frame;
527 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200528
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100529 stream_id = ctx->stream_id;
530 frame_id = ctx->frame_id;
531
532 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
533 /* The fragmentation is not supported by the applet */
534 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
535 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
536 return -1;
537 }
538 flags = ctx->frag_ctx.flags;
539 }
540
541 /* Set Frame type */
542 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
543
544 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200545 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100546 memcpy(p, (char *)&flags, 4);
547 p += 4;
548
549 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200550 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100551 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200552 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100553 goto too_big;
554
555 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200556 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 if (p + sz >= end)
558 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200559 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100560 p += sz;
561
562 return (p - frame);
563
564 too_big:
565 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
566 return 0;
567}
568
569/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
570 * returns the number of encoded bytes in the frame on success, 0 if an encoding
571 * error occurred and -1 if a fatal error occurred. */
572static int
573spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
574 char *frame, size_t size)
575{
576 char *p, *end;
577 unsigned int stream_id, frame_id;
578 unsigned int flags;
579 size_t sz;
580
581 p = frame;
582 end = frame+size;
583
Christopher Faulet8ef75252017-02-20 22:56:03 +0100584 /* <ctx> is null when the stream has aborted the processing of a
585 * fragmented frame. In this case, we must notify the corresponding
586 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100587 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100588 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100589 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
590 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
591 }
592 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100593 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100594 stream_id = ctx->stream_id;
595 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 }
597
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100599 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100601 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200602 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 memcpy(p, (char *)&flags, 4);
604 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200605
606 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200607 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200609 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200611
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100612 if (ctx == NULL)
613 goto end;
614
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200616 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 if (p + sz >= end)
618 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200619 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100621
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100622 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100623 return (p - frame);
624
625 too_big:
626 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
627 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628}
629
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630/* Decode and process the HELLO frame sent by an agent. It returns the number of
631 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
632 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100634spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200635{
Christopher Faulet305c6072017-02-23 16:17:53 +0100636 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
637 char *p, *end;
638 int vsn, max_frame_size;
639 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640
641 p = frame;
642 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643
644 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650 if (size < 7 /* TYPE + METADATA */) {
651 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
652 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200653 }
654
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100655 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200657 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200659
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100660 /* Fragmentation is not supported for HELLO frame */
661 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100663 return -1;
664 }
665
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 if (*p != 0 || *(p+1) != 0) {
668 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
669 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672
673 /* There are 3 mandatory items: "version", "max-frame-size" and
674 * "capabilities" */
675
676 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100677 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 while (p < end) {
679 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200680 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200682
683 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684 ret = spoe_decode_buffer(&p, end, &str, &sz);
685 if (ret == -1 || !sz) {
686 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
687 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200691 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 int i, type = *p++;
693
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
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 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
700 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
701 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 }
703
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 return -1;
708 }
709 for (i = 0; supported_versions[i].str != NULL; ++i) {
710 if (vsn >= supported_versions[i].min &&
711 vsn <= supported_versions[i].max)
712 break;
713 }
714 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100715 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200716 return -1;
717 }
718 }
719 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200720 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 +0100721 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722
723 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
725 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
726 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
727 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
729 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200731 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
733 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200734 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100735 if (sz < MIN_FRAME_SIZE ||
736 sz > SPOE_APPCTX(appctx)->max_frame_size) {
737 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200738 return -1;
739 }
740 max_frame_size = sz;
741 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200743 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100744 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100745
746 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100747 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
748 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
749 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
752 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
753 return 0;
754 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757 char *delim;
758
759 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100760 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
763 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100764 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765 flags |= SPOE_APPCTX_FL_PIPELINING;
766 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 else if (sz >= 5 && !strncmp(str, "async", 5)) {
768 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100769 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100770 flags |= SPOE_APPCTX_FL_ASYNC;
771 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
773 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100774 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100775 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
776 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 /* Get the next comma or break */
779 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 delim++;
782 sz -= (delim - str);
783 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100784 }
785 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786 else {
787 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 if (spoe_skip_data(&p, end) == -1) {
789 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
790 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200791 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200792 }
793 }
794
795 /* Final checks */
796 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200798 return -1;
799 }
800 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100801 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200802 return -1;
803 }
Christopher Faulet11389012019-02-07 16:13:26 +0100804 if (!agent)
805 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
806 else {
807 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
808 flags &= ~SPOE_APPCTX_FL_PIPELINING;
809 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
810 flags &= ~SPOE_APPCTX_FL_ASYNC;
811 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812
Christopher Faulet42bfa462017-01-04 14:14:19 +0100813 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
814 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
815 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100816
817 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200818}
819
820/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
821 * bytes on success, 0 if the frame can be ignored and -1 if an error
822 * occurred. */
823static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200825{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100827 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828
829 p = frame;
830 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831
832 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
834 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838 if (size < 7 /* TYPE + METADATA */) {
839 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
840 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200841 }
842
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100843 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200845 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200847
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 /* Fragmentation is not supported for DISCONNECT frame */
849 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100851 return -1;
852 }
853
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 if (*p != 0 || *(p+1) != 0) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860
861 /* There are 2 mandatory items: "status-code" and "message" */
862
863 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 while (p < end) {
865 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200866 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200868
869 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100870 ret = spoe_decode_buffer(&p, end, &str, &sz);
871 if (ret == -1 || !sz) {
872 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
873 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874 }
875
876 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200877 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100878 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879
880 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
883 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
884 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200888 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
890 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
894
895 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200896 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 int type = *p++;
898
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904 ret = spoe_decode_buffer(&p, end, &str, &sz);
905 if (ret == -1 || sz > 255) {
906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
910 SPOE_APPCTX(appctx)->reason = str;
911 SPOE_APPCTX(appctx)->rlen = sz;
912#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200913 }
914 else {
915 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100916 if (spoe_skip_data(&p, end) == -1) {
917 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
918 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
921 }
922
Christopher Faulet8ef75252017-02-20 22:56:03 +0100923 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924}
925
926
Christopher Fauleta1cda022016-12-21 08:58:06 +0100927/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928 * success, 0 if the frame can be ignored and -1 if an error occurred. */
929static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200932{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938
939 p = frame;
940 end = frame + size;
941 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942
943 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 if (size < 7 /* TYPE + METADATA */) {
950 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
951 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200952 }
953
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200956 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200958
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 /* Fragmentation is not supported for now */
960 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100962 return -1;
963 }
964
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200965 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200966 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100968 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200970 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200972 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100974
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100976 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200977 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100978 if ((*ctx)->stream_id == (unsigned int)stream_id &&
979 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100980 goto found;
981 }
982 }
983 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100984 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
985 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100986 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100987 goto found;
988 }
989 }
990
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100991 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
992 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
993 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
994
995 /* ABRT bit is set for an unfinished fragmented frame */
996 if (flags & SPOE_FRM_FL_ABRT) {
997 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100998 (*ctx)->state = SPOE_CTX_ST_ERROR;
999 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1000 /* Ignore the payload */
1001 goto end;
1002 }
1003 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1004 /* For now, we ignore the ack */
1005 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1006 return 0;
1007 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008
Christopher Fauleta1cda022016-12-21 08:58:06 +01001009 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001010 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1011 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001012 " - stream-id=%u - frame-id=%u\n",
1013 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1014 __FUNCTION__, appctx,
1015 (unsigned int)stream_id, (unsigned int)frame_id);
1016
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001017 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001018 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1019 /* Report an error if we are waiting the ack for another frame,
1020 * but not if there is no longer frame waiting for a ack
1021 * (timeout)
1022 */
1023 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1024 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1025 return -1;
1026 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1027 SPOE_APPCTX(appctx)->cur_fpa = 0;
1028 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001029 return 0;
1030
1031 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001032 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1033 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001034 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001035 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001036 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001037
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001038 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001040 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1041 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001043
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001044 /* Transfer the buffer ownership to the SPOE context */
1045 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001046 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001047
Christopher Faulet8ef75252017-02-20 22:56:03 +01001048 (*ctx)->state = SPOE_CTX_ST_DONE;
1049
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001050 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001051 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 " - ACK frame received"
1053 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001054 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001055 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1056 (*ctx)->frame_id, flags);
1057 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001058}
1059
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060/* This function is used in cfgparse.c and declared in proto/checks.h. It
1061 * prepare the request to send to agents during a healthcheck. It returns 0 on
1062 * success and -1 if an error occurred. */
1063int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001065{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001066 struct appctx appctx;
1067 struct spoe_appctx spoe_appctx;
1068 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1069 size_t sz;
1070 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072 memset(&appctx, 0, sizeof(appctx));
1073 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001075
1076 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001077 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001078
Christopher Faulet8ef75252017-02-20 22:56:03 +01001079 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1080 end = frame + MAX_FRAME_SIZE;
1081
1082 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1083 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001085 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 /* Add "healthcheck" K/V item */
1088 sz = SLEN(HEALTHCHECK_KEY);
1089 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1090 return -1;
1091 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 *len = frame - buf;
1094 sz = htonl(*len - 4);
1095 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001098 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001099 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100 return 0;
1101}
1102
1103/* This function is used in checks.c and declared in proto/checks.h. It decode
1104 * the response received from an agent during a healthcheck. It returns 0 on
1105 * success and -1 if an error occurred. */
1106int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 struct appctx appctx;
1110 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112 memset(&appctx, 0, sizeof(appctx));
1113 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
Christopher Faulet42bfa462017-01-04 14:14:19 +01001115 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001116 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001117
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1119 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120 goto error;
1121 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001122 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1123 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124
1125 return 0;
1126
1127 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001128 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1129 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1130 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001131 return -1;
1132}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001133
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001135 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136 * success. */
1137static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139{
Christopher Faulet908628c2022-03-25 16:43:49 +01001140 struct conn_stream *cs = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 int ret;
1142 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143
Christopher Faulet8ef75252017-02-20 22:56:03 +01001144 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1145 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 netint = htonl(framesz);
1147 memcpy(buf, (char *)&netint, 4);
Christopher Faulet908628c2022-03-25 16:43:49 +01001148 ret = ci_putblk(cs_ic(cs), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001149 if (ret <= 0) {
Christopher Faulet908628c2022-03-25 16:43:49 +01001150 if ((ret == -3 && b_is_null(&cs_ic(cs)->buf)) || ret == -1) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001151 cs_rx_room_blk(cs);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001152 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001153 }
1154 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155 return -1; /* error */
1156 }
1157 return framesz;
1158}
1159
1160/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1161 * when the frame can be ignored, 1 to retry later and the frame length on
1162 * success. */
1163static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001165{
Christopher Faulet908628c2022-03-25 16:43:49 +01001166 struct conn_stream *cs = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001167 int ret;
1168 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169
Christopher Faulet908628c2022-03-25 16:43:49 +01001170 ret = co_getblk(cs_oc(cs), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171 if (ret > 0) {
1172 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001173 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001174 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001175 return -1;
1176 }
Christopher Faulet908628c2022-03-25 16:43:49 +01001177 ret = co_getblk(cs_oc(cs), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001178 }
1179 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001180 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001181 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 }
1183 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001184 return -1; /* error */
1185 }
1186 return framesz;
1187}
1188
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001189/********************************************************************
1190 * Functions that manage the SPOE applet
1191 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001192static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001193spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001194{
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001195 cs_want_get(appctx->owner);
1196 cs_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001197 appctx_wakeup(appctx);
1198 return 1;
1199}
1200
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001201/* Callback function that catches applet timeouts. If a timeout occurred, we set
1202 * <appctx->st1> flag and the SPOE applet is woken up. */
1203static struct task *
Willy Tarreau144f84a2021-03-02 16:09:26 +01001204spoe_process_appctx(struct task * task, void *context, unsigned int state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001206 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207
1208 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1209 if (tick_is_expired(task->expire, now_ms)) {
1210 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001211 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001212 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001213 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214 return task;
1215}
1216
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001217/* Callback function that releases a SPOE applet. This happens when the
1218 * connection with the agent is closed. */
1219static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001220spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001221{
Christopher Faulet908628c2022-03-25 16:43:49 +01001222 struct conn_stream *cs = appctx->owner;
1223 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1224 struct spoe_agent *agent;
1225 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001226
1227 if (spoe_appctx == NULL)
1228 return;
1229
1230 appctx->ctx.spoe.ptr = NULL;
1231 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001232
1233 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1234 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1235 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001236
Christopher Faulet8ef75252017-02-20 22:56:03 +01001237 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001238 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001239 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001240 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001241 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001242 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001243 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001244 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001245
Christopher Faulet8ef75252017-02-20 22:56:03 +01001246 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001247 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001248 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1249 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001250 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001251 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001252
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001253 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1255 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001256
Christopher Fauletda098e62022-03-31 17:44:45 +02001257 cs_shutw(cs);
1258 cs_shutr(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +01001259 cs_ic(cs)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001260 }
1261
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001263 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001264
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 /* Notify all waiting streams */
1266 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001267 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001268 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001269 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001270 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001271 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001272 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001274 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001275 }
1276
Christopher Faulet8ef75252017-02-20 22:56:03 +01001277 /* If the applet was processing a fragmented frame, notify the
1278 * corresponding stream. */
1279 if (spoe_appctx->frag_ctx.ctx) {
1280 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001281 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001282 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001284 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1285 }
1286
Willy Tarreaub042e4f2022-02-15 16:49:37 +01001287 if (!LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) {
Christopher Fauletcf181c72020-11-10 18:45:34 +01001288 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1289 if (ctx->spoe_appctx == spoe_appctx)
1290 ctx->spoe_appctx = NULL;
1291 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001292 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001293 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001294 else {
1295 /* It is the last running applet and the sending and waiting
1296 * queues are not empty. Try to start a new one if HAproxy is
1297 * not stopping.
1298 */
1299 if (!stopping &&
1300 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
1301 spoe_create_appctx(agent->spoe_conf))
1302 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001303
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001304 /* otherwise, notify all waiting streams */
1305 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1306 LIST_DELETE(&ctx->list);
1307 LIST_INIT(&ctx->list);
1308 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
1309 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1310 ctx->spoe_appctx = NULL;
1311 ctx->state = SPOE_CTX_ST_ERROR;
1312 ctx->status_code = (spoe_appctx->status_code + 0x100);
1313 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1314 }
1315 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1316 LIST_DELETE(&ctx->list);
1317 LIST_INIT(&ctx->list);
1318 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
1319 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1320 ctx->spoe_appctx = NULL;
1321 ctx->state = SPOE_CTX_ST_ERROR;
1322 ctx->status_code = (spoe_appctx->status_code + 0x100);
1323 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1324 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001325 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001326
1327 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001328 /* Release allocated memory */
1329 spoe_release_buffer(&spoe_appctx->buffer,
1330 &spoe_appctx->buffer_wait);
1331 pool_free(pool_head_spoe_appctx, spoe_appctx);
1332
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001333 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001334 agent->rt[tid].frame_size = agent->max_frame_size;
1335 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1336 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337}
1338
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001340spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001341{
Christopher Faulet908628c2022-03-25 16:43:49 +01001342 struct conn_stream *cs = appctx->owner;
1343 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001344 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001346
Christopher Faulet62e75742022-03-31 09:16:34 +02001347 if (cs_state_in(cs->state, CS_SB_CER|CS_SB_DIS|CS_SB_CLO)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001348 /* closed */
1349 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1350 goto exit;
1351 }
1352
Christopher Faulet62e75742022-03-31 09:16:34 +02001353 if (!cs_state_in(cs->state, CS_SB_RDY|CS_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001354 /* not connected yet */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001355 cs_rx_endp_more(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +01001356 task_wakeup(__cs_strm(cs)->task, TASK_WOKEN_MSG);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001357 goto stop;
1358 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001359
Christopher Fauleta1cda022016-12-21 08:58:06 +01001360 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1362 " - Connection timed out\n",
1363 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1364 __FUNCTION__, appctx);
1365 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001367 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001368
Christopher Faulet42bfa462017-01-04 14:14:19 +01001369 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001370 SPOE_APPCTX(appctx)->task->expire =
1371 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372
Christopher Faulet8ef75252017-02-20 22:56:03 +01001373 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1374 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001375 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001376 ret = spoe_prepare_hahello_frame(appctx, frame,
1377 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001379 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380
1381 switch (ret) {
1382 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 case 0: /* ignore => an error, cannot be ignored */
1384 goto exit;
1385
1386 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001387 goto stop;
1388
Christopher Faulet8ef75252017-02-20 22:56:03 +01001389 default:
1390 /* HELLO frame successfully sent, now wait for the
1391 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001392 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1393 goto next;
1394 }
1395
1396 next:
1397 return 0;
1398 stop:
1399 return 1;
1400 exit:
1401 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1402 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403}
1404
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001405static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001406spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001407{
Christopher Faulet908628c2022-03-25 16:43:49 +01001408 struct conn_stream *cs = appctx->owner;
1409 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001410 char *frame;
1411 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001412
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413
Christopher Faulet62e75742022-03-31 09:16:34 +02001414 if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001415 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001417 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001418
Christopher Fauleta1cda022016-12-21 08:58:06 +01001419 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001420 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1421 " - Connection timed out\n",
1422 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1423 __FUNCTION__, appctx);
1424 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 goto exit;
1426 }
1427
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001428 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001429 ret = spoe_recv_frame(appctx, frame,
1430 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 if (ret > 1) {
1432 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1433 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1434 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001436 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001437 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001438 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001439
Christopher Fauleta1cda022016-12-21 08:58:06 +01001440 switch (ret) {
1441 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001442 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001443 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1444 goto next;
1445
1446 case 1: /* retry later */
1447 goto stop;
1448
1449 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001450 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001451 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001452 SPOE_APPCTX(appctx)->node.key = 0;
1453 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001454
1455 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001456 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001457 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001458 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001459
Christopher Fauleta1cda022016-12-21 08:58:06 +01001460 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001461 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001462 if (trash.data)
Christopher Faulet908628c2022-03-25 16:43:49 +01001463 co_skip(cs_oc(cs), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001464
1465 SPOE_APPCTX(appctx)->task->expire =
1466 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001467 return 0;
1468 stop:
1469 return 1;
1470 exit:
1471 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1472 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001473}
1474
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475
Christopher Fauleta1cda022016-12-21 08:58:06 +01001476static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001477spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001478{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001479 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1480 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001481 char *frame, *buf;
1482 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001483
Christopher Faulet8ef75252017-02-20 22:56:03 +01001484 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1485 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001486 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001487
1488 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1489 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1490 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1491 SPOE_APPCTX(appctx)->max_frame_size);
1492 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001493 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001494 *skip = 1;
1495 ret = 1;
1496 goto end;
1497 }
1498 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001499 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001500 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1501 SPOE_APPCTX(appctx)->max_frame_size);
1502
1503 }
1504
Christopher Faulet8ef75252017-02-20 22:56:03 +01001505 if (ret > 1)
1506 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001507
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 switch (ret) {
1509 case -1: /* error */
1510 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1511 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001512
Christopher Faulet8ef75252017-02-20 22:56:03 +01001513 case 0: /* ignore */
1514 if (ctx == NULL)
1515 goto abort_frag_frame;
1516
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001517 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001518 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001519 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001520 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001521 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001522 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523 ctx->state = SPOE_CTX_ST_ERROR;
1524 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1525 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001526 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001527 break;
1528
1529 case 1: /* retry */
1530 *skip = 1;
1531 break;
1532
1533 default:
1534 if (ctx == NULL)
1535 goto abort_frag_frame;
1536
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001537 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001538 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001539 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001540 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001541 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001542 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001543 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1544 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1545 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001546 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001547 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001548 }
1549 goto end;
1550
1551 frag_frame_sent:
1552 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001553 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001554 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1555 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1556 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001557 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1558 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1559 goto end;
1560
1561 no_frag_frame_sent:
1562 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1563 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001564 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001565 }
1566 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1567 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001568 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001569 }
1570 else {
1571 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001572 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001573 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001574 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001575 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001576 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001577 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1578 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1579 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001580 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001581
Christopher Faulet8ef75252017-02-20 22:56:03 +01001582 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1583 goto end;
1584
1585 abort_frag_frame:
1586 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1587 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1588 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1589 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1590 goto end;
1591
1592 end:
1593 return ret;
1594}
1595
1596static int
1597spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1598{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001599 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001600 struct spoe_context *ctx = NULL;
1601 char *frame;
1602 int ret;
1603
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001604 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001605 ret = spoe_recv_frame(appctx, frame,
1606 SPOE_APPCTX(appctx)->max_frame_size);
1607 if (ret > 1) {
1608 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1609 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001610 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001611 goto end;
1612 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001613 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001614 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1615 }
1616 switch (ret) {
1617 case -1: /* error */
1618 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1619 break;
1620
1621 case 0: /* ignore */
1622 break;
1623
1624 case 1: /* retry */
1625 *skip = 1;
1626 break;
1627
1628 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001629 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001630 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001631 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001632 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1633 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001634 if (ctx->spoe_appctx) {
1635 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001636 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001637 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001638 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1639 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1640 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1641 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1642 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1643 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1644 }
1645 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1646 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001647 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1648 break;
1649 }
1650
1651 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001652 if (trash.data)
Christopher Faulet908628c2022-03-25 16:43:49 +01001653 co_skip(cs_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001654 end:
1655 return ret;
1656}
1657
1658static int
1659spoe_handle_processing_appctx(struct appctx *appctx)
1660{
Christopher Faulet908628c2022-03-25 16:43:49 +01001661 struct conn_stream *cs = appctx->owner;
1662 struct server *srv = objt_server(__cs_strm(cs)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001663 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001664 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001665
Christopher Faulet62e75742022-03-31 09:16:34 +02001666 if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001667 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1668 goto exit;
1669 }
1670
1671 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1672 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1673 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1674 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1675 goto next;
1676 }
1677
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001678 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001679 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001680 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001681 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1682 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001683 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001684
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001685
1686 /* Close the applet ASAP because some sessions are waiting for a free
1687 * connection slot. It is only an issue in multithreaded mode.
1688 */
1689 close_asap = (global.nbthread > 1 &&
1690 (agent->b.be->queue.length ||
1691 (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1692
1693 /* Don"t try to send new frame we are waiting for at lease a ack, in
1694 * sync mode or if applet must be closed ASAP
1695 */
1696 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001698
Christopher Faulet8f82b202018-01-24 16:23:03 +01001699 /* receiving_frame loop */
1700 while (!skip_receiving) {
1701 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1702 switch (ret) {
1703 case -1: /* error */
1704 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001705
Christopher Faulet8f82b202018-01-24 16:23:03 +01001706 case 0: /* ignore */
1707 active_r = 1;
1708 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001709
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 case 1: /* retry */
1711 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001712
Christopher Faulet8f82b202018-01-24 16:23:03 +01001713 default:
1714 active_r = 1;
1715 break;
1716 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001718
Christopher Faulet8f82b202018-01-24 16:23:03 +01001719 /* send_frame loop */
1720 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1721 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1722 switch (ret) {
1723 case -1: /* error */
1724 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001725
Christopher Faulet8f82b202018-01-24 16:23:03 +01001726 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001727 if (SPOE_APPCTX(appctx)->node.key)
1728 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001729 active_s++;
1730 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731
Christopher Faulet8f82b202018-01-24 16:23:03 +01001732 case 1: /* retry */
1733 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001734
Christopher Faulet8f82b202018-01-24 16:23:03 +01001735 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001736 if (SPOE_APPCTX(appctx)->node.key)
1737 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001738 active_s++;
1739 break;
1740 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001741
1742 /* if applet must be close ASAP, don't send more than a frame */
1743 if (close_asap)
1744 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001745 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746
Christopher Faulet8f82b202018-01-24 16:23:03 +01001747 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001748 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1749 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1750 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001751
Christopher Faulet8f82b202018-01-24 16:23:03 +01001752 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001753 /* If applet must be closed, don't switch it in IDLE state and
1754 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001755 */
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001756 if (close_asap) {
1757 if (SPOE_APPCTX(appctx)->cur_fpa)
1758 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001759 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1760 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1761 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1762 goto next;
1763 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001764 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001765 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001766 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001767 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001768
1769 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 return 1;
1771
Christopher Faulet8f82b202018-01-24 16:23:03 +01001772 next:
1773 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1774 return 0;
1775
Christopher Fauleta1cda022016-12-21 08:58:06 +01001776 exit:
1777 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1778 return 0;
1779}
1780
1781static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001782spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001783{
Christopher Faulet908628c2022-03-25 16:43:49 +01001784 struct conn_stream *cs = appctx->owner;
1785 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 char *frame, *buf;
1787 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001788
Christopher Faulet62e75742022-03-31 09:16:34 +02001789 if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790 goto exit;
1791
1792 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1793 goto exit;
1794
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1796 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001797 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1799 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001800 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802
1803 switch (ret) {
1804 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806 goto exit;
1807
1808 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 goto stop;
1810
1811 default:
1812 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1813 " - disconnected by HAProxy (%d): %s\n",
1814 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 __FUNCTION__, appctx,
1816 SPOE_APPCTX(appctx)->status_code,
1817 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818
1819 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1820 goto next;
1821 }
1822
1823 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001824 SPOE_APPCTX(appctx)->task->expire =
1825 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 return 0;
1827 stop:
1828 return 1;
1829 exit:
1830 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1831 return 0;
1832}
1833
1834static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001835spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001836{
Christopher Faulet908628c2022-03-25 16:43:49 +01001837 struct conn_stream *cs = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001838 char *frame;
1839 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001840
Christopher Faulet62e75742022-03-31 09:16:34 +02001841 if (cs->state == CS_ST_CLO || cs_opposite(cs)->state == CS_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001842 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001843 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001844 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845
Christopher Fauletb067b062017-01-04 16:39:11 +01001846 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001849 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001850
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001851 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852 ret = spoe_recv_frame(appctx, frame,
1853 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001854 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001855 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001857 }
1858
1859 switch (ret) {
1860 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001861 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1862 " - error on frame (%s)\n",
1863 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001864 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001866 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001867 goto exit;
1868
1869 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870 goto next;
1871
1872 case 1: /* retry */
1873 goto stop;
1874
1875 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001877 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001879 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1881 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001882 goto exit;
1883 }
1884
1885 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001886 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001887 if (trash.data)
Christopher Faulet908628c2022-03-25 16:43:49 +01001888 co_skip(cs_oc(cs), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001889
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 return 0;
1891 stop:
1892 return 1;
1893 exit:
1894 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1895 return 0;
1896}
1897
1898/* I/O Handler processing messages exchanged with the agent */
1899static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001900spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001901{
Christopher Faulet908628c2022-03-25 16:43:49 +01001902 struct conn_stream *cs = appctx->owner;
1903 struct spoe_agent *agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904
1905 if (SPOE_APPCTX(appctx) == NULL)
1906 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001907
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1909 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001910
Christopher Fauleta1cda022016-12-21 08:58:06 +01001911 switchstate:
1912 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1913 " - appctx-state=%s\n",
1914 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1915 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1916
1917 switch (appctx->st0) {
1918 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001919 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001920 goto out;
1921 goto switchstate;
1922
1923 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001924 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001925 goto out;
1926 goto switchstate;
1927
1928 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001929 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001930 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001931 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001932 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001933 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001934 SPOE_APPCTX(appctx)->task->expire =
1935 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001936 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001937 goto switchstate;
1938 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001939 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1940 /* fall through */
1941
1942 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001943 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1944 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001945 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001946 goto out;
1947 goto switchstate;
1948
1949 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001950 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001951 goto out;
1952 goto switchstate;
1953
1954 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001955 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001956 goto out;
1957 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001958
1959 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001960 appctx->st0 = SPOE_APPCTX_ST_END;
1961 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1962
Christopher Fauletda098e62022-03-31 17:44:45 +02001963 cs_shutw(cs);
1964 cs_shutr(cs);
Christopher Faulet908628c2022-03-25 16:43:49 +01001965 cs_ic(cs)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001966 /* fall through */
1967
1968 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001969 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001970 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001971 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001972 if (stopping)
1973 spoe_wakeup_appctx(appctx);
1974
Christopher Faulet42bfa462017-01-04 14:14:19 +01001975 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1976 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977}
1978
1979struct applet spoe_applet = {
1980 .obj_type = OBJ_TYPE_APPLET,
1981 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001982 .fct = spoe_handle_appctx,
1983 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984};
1985
1986/* Create a SPOE applet. On success, the created applet is returned, else
1987 * NULL. */
1988static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001989spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001990{
1991 struct appctx *appctx;
1992 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01001993 struct conn_stream *cs;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01001996 if ((appctx = appctx_new(&spoe_applet, NULL)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 goto out_error;
1998
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01001999 appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002000 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002001 goto out_free_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002002
Christopher Faulet42bfa462017-01-04 14:14:19 +01002003 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002004 if ((SPOE_APPCTX(appctx)->task = task_new_here()) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002005 goto out_free_spoe_appctx;
2006
2007 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002008 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002009 SPOE_APPCTX(appctx)->task->context = appctx;
2010 SPOE_APPCTX(appctx)->agent = conf->agent;
2011 SPOE_APPCTX(appctx)->version = 0;
2012 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
2013 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01002014 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002015 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01002016 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01002017
Willy Tarreau90f366b2021-02-20 11:49:49 +01002018 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002019 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002020 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002021
2022 LIST_INIT(&SPOE_APPCTX(appctx)->list);
2023 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024
Willy Tarreau5820a362016-12-22 15:59:02 +01002025 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026 if (!sess)
2027 goto out_free_spoe;
2028
Christopher Faulet9ec2f4d2022-03-23 15:15:29 +01002029 cs = cs_new_from_applet(appctx->endp, sess, &BUF_NULL);
2030 if (!cs)
Christopher Faulet2479e5f2022-01-19 14:50:11 +01002031 goto out_free_sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01002032
Christopher Fauleta9e8b392022-03-23 11:01:09 +01002033 strm = DISGUISE(cs_strm(cs));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002034 stream_set_backend(strm, conf->agent->b.be);
2035
2036 /* applet is waiting for data */
Christopher Fauleta0bdec32022-04-04 07:51:21 +02002037 cs_cant_get(strm->csf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038 appctx_wakeup(appctx);
2039
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040 strm->do_log = NULL;
2041 strm->res.flags |= CF_READ_DONTWAIT;
2042
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002043 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau2b718102021-04-21 07:32:39 +02002044 LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002045 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau4781b152021-04-06 13:53:36 +02002046 _HA_ATOMIC_INC(&conf->agent->counters.applets);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002047
Emeric Brunc60def82017-09-27 14:59:38 +02002048 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049 return appctx;
2050
2051 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002052 out_free_sess:
2053 session_free(sess);
2054 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002055 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002056 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002057 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002058 out_free_appctx:
2059 appctx_free(appctx);
2060 out_error:
2061 return NULL;
2062}
2063
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002064static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002065spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066{
2067 struct spoe_config *conf = FLT_CONF(ctx->filter);
2068 struct spoe_agent *agent = conf->agent;
2069 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002070 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002071
Christopher Fauleta1cda022016-12-21 08:58:06 +01002072 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002073 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002074 (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 +01002075 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002076
2077 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002078 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2080 ctx->strm);
2081
Christopher Fauleta1cda022016-12-21 08:58:06 +01002082 /* Do not try to create a new applet if there is no server up for the
2083 * agent's backend. */
2084 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2085 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2086 " - cannot create SPOE appctx: no server up\n",
2087 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2088 __FUNCTION__, ctx->strm);
2089 goto end;
2090 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002091
Christopher Fauleta1cda022016-12-21 08:58:06 +01002092 /* Do not try to create a new applet if we have reached the maximum of
2093 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002094 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002095 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002096 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2097 " - cannot create SPOE appctx: max CPS reached\n",
2098 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2099 __FUNCTION__, ctx->strm);
2100 goto end;
2101 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002102 }
2103
Christopher Faulet8ef75252017-02-20 22:56:03 +01002104 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002105 if (appctx == NULL) {
2106 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2107 " - failed to create SPOE appctx\n",
2108 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2109 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002110 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002111 "SPOE: [%s] failed to create SPOE applet\n",
2112 agent->id);
2113
Christopher Fauleta1cda022016-12-21 08:58:06 +01002114 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002115 }
2116
Christopher Fauleta1cda022016-12-21 08:58:06 +01002117 /* Increase the per-process number of cumulated connections */
2118 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002119 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002120
Christopher Fauleta1cda022016-12-21 08:58:06 +01002121 end:
2122 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002123 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002124 ctx->status_code = SPOE_CTX_ERR_RES;
2125 return -1;
2126 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002127
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002128 /* Add the SPOE context in the sending queue if the stream has no applet
2129 * already assigned and wakeup all idle applets. Otherwise, don't queue
2130 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002131 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002132 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2133 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002134 if (ctx->spoe_appctx)
2135 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002136 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002137
2138 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002139 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002140 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002141 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002142 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002143 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002144
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002145 /* Finally try to wakeup an IDLE applet. */
2146 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2147 struct eb32_node *node;
2148
2149 node = eb32_first(&agent->rt[tid].idle_applets);
2150 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2151 if (node && spoe_appctx) {
2152 eb32_delete(&spoe_appctx->node);
2153 spoe_appctx->node.key++;
2154 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2155 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002156 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002157 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002158 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002159}
2160
2161/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002162 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002163 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002164/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2165 * fragmented_content. If the next message can be processed, it returns 0. If
2166 * the message is too big, it returns -1.*/
2167static int
2168spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2169 struct spoe_message *msg, int dir,
2170 char **buf, char *end)
2171{
2172 struct sample *smp;
2173 struct spoe_arg *arg;
2174 int ret;
2175
2176 if (msg->cond) {
2177 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2178 ret = acl_pass(ret);
2179 if (msg->cond->pol == ACL_COND_UNLESS)
2180 ret = !ret;
2181
2182 /* the rule does not match */
2183 if (!ret)
2184 goto next;
2185 }
2186
2187 /* Resume encoding of a SPOE argument */
2188 if (ctx->frag_ctx.curarg != NULL) {
2189 arg = ctx->frag_ctx.curarg;
2190 goto encode_argument;
2191 }
2192
2193 if (ctx->frag_ctx.curoff != UINT_MAX)
2194 goto encode_msg_payload;
2195
2196 /* Check if there is enough space for the message name and the
2197 * number of arguments. It implies <msg->id_len> is encoded on 2
2198 * bytes, at most (< 2288). */
2199 if (*buf + 2 + msg->id_len + 1 > end)
2200 goto too_big;
2201
2202 /* Encode the message name */
2203 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2204 goto too_big;
2205
2206 /* Set the number of arguments for this message */
2207 **buf = msg->nargs;
2208 (*buf)++;
2209
2210 ctx->frag_ctx.curoff = 0;
2211 encode_msg_payload:
2212
2213 /* Loop on arguments */
2214 list_for_each_entry(arg, &msg->args, list) {
2215 ctx->frag_ctx.curarg = arg;
2216 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002217 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002218
2219 encode_argument:
2220 if (ctx->frag_ctx.curoff != UINT_MAX)
2221 goto encode_arg_value;
2222
Joseph Herlantf7f60312018-11-15 13:46:49 -08002223 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002224 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2225 goto too_big;
2226
2227 ctx->frag_ctx.curoff = 0;
2228 encode_arg_value:
2229
Joseph Herlantf7f60312018-11-15 13:46:49 -08002230 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002231 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002232 if (smp) {
2233 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2234 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2235 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002236 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002237 if (ret == -1 || ctx->frag_ctx.curoff)
2238 goto too_big;
2239 }
2240
2241 next:
2242 return 0;
2243
2244 too_big:
2245 return -1;
2246}
2247
Christopher Fauletc718b822017-09-21 16:50:56 +02002248/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2249 * handle fragmented content. On success it returns 1. If an error occurred, -1
2250 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2251 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002252static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002253spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002254 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002255{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002256 struct spoe_config *conf = FLT_CONF(ctx->filter);
2257 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002258 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002259 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002260
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002261 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002262 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002263
Christopher Fauletc718b822017-09-21 16:50:56 +02002264 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2265 /* Resume encoding of a SPOE message */
2266 if (ctx->frag_ctx.curmsg != NULL) {
2267 msg = ctx->frag_ctx.curmsg;
2268 goto encode_evt_message;
2269 }
2270
2271 list_for_each_entry(msg, messages, by_evt) {
2272 ctx->frag_ctx.curmsg = msg;
2273 ctx->frag_ctx.curarg = NULL;
2274 ctx->frag_ctx.curoff = UINT_MAX;
2275
2276 encode_evt_message:
2277 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2278 goto too_big;
2279 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002280 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002281 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2282 /* Resume encoding of a SPOE message */
2283 if (ctx->frag_ctx.curmsg != NULL) {
2284 msg = ctx->frag_ctx.curmsg;
2285 goto encode_grp_message;
2286 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002287
Christopher Fauletc718b822017-09-21 16:50:56 +02002288 list_for_each_entry(msg, messages, by_grp) {
2289 ctx->frag_ctx.curmsg = msg;
2290 ctx->frag_ctx.curarg = NULL;
2291 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292
Christopher Fauletc718b822017-09-21 16:50:56 +02002293 encode_grp_message:
2294 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2295 goto too_big;
2296 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002297 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002298 else
2299 goto skip;
2300
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002301
Christopher Faulet57583e42017-09-04 15:41:09 +02002302 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002303 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002304 goto skip;
2305
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002306 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002307 " - encode %s messages - spoe_appctx=%p"
2308 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002309 (int)now.tv_sec, (int)now.tv_usec,
2310 agent->id, __FUNCTION__, s,
2311 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002312 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002313 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002314
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002315 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002316 ctx->frag_ctx.curmsg = NULL;
2317 ctx->frag_ctx.curarg = NULL;
2318 ctx->frag_ctx.curoff = 0;
2319 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002320
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002321 return 1;
2322
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002323 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002324 /* Return an error if fragmentation is unsupported or if nothing has
2325 * been encoded because its too big and not splittable. */
2326 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002327 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2328 return -1;
2329 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002330
2331 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002332 " - encode fragmented messages - spoe_appctx=%p"
2333 " - curmsg=%p - curarg=%p - curoff=%u"
2334 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002335 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002336 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002337 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002338 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002339
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002340 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002341 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2342 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2343 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002344
2345 skip:
2346 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2347 " - skip the frame because nothing has been encoded\n",
2348 (int)now.tv_sec, (int)now.tv_usec,
2349 agent->id, __FUNCTION__, s);
2350 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002351}
2352
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002353
2354/***************************************************************************
2355 * Functions that handle SPOE actions
2356 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002357/* Helper function to set a variable */
2358static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002359spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002360 struct sample *smp)
2361{
2362 struct spoe_config *conf = FLT_CONF(ctx->filter);
2363 struct spoe_agent *agent = conf->agent;
2364 char varname[64];
2365
2366 memset(varname, 0, sizeof(varname));
2367 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2368 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002369 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2370 vars_set_by_name(varname, len, smp);
2371 else
2372 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373}
2374
2375/* Helper function to unset a variable */
2376static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378 struct sample *smp)
2379{
2380 struct spoe_config *conf = FLT_CONF(ctx->filter);
2381 struct spoe_agent *agent = conf->agent;
2382 char varname[64];
2383
2384 memset(varname, 0, sizeof(varname));
2385 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2386 scope, agent->var_pfx, len, name);
2387 vars_unset_by_name_ifexist(varname, len, smp);
2388}
2389
2390
Christopher Faulet8ef75252017-02-20 22:56:03 +01002391static inline int
2392spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2393 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002394{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002395 char *str, *scope, *p = *buf;
2396 struct sample smp;
2397 uint64_t sz;
2398 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002399
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400 if (p + 2 >= end)
2401 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 /* SET-VAR requires 3 arguments */
2404 if (*p++ != 3)
2405 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002406
Christopher Faulet8ef75252017-02-20 22:56:03 +01002407 switch (*p++) {
2408 case SPOE_SCOPE_PROC: scope = "proc"; break;
2409 case SPOE_SCOPE_SESS: scope = "sess"; break;
2410 case SPOE_SCOPE_TXN : scope = "txn"; break;
2411 case SPOE_SCOPE_REQ : scope = "req"; break;
2412 case SPOE_SCOPE_RES : scope = "res"; break;
2413 default: goto skip;
2414 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002415
Christopher Faulet8ef75252017-02-20 22:56:03 +01002416 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2417 goto skip;
2418 memset(&smp, 0, sizeof(smp));
2419 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002420
Christopher Faulet8ef75252017-02-20 22:56:03 +01002421 if (spoe_decode_data(&p, end, &smp) == -1)
2422 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002423
Christopher Faulet8ef75252017-02-20 22:56:03 +01002424 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2425 " - set-var '%s.%s.%.*s'\n",
2426 (int)now.tv_sec, (int)now.tv_usec,
2427 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2428 __FUNCTION__, s, scope,
2429 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2430 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002431
Christopher Faulet2469eba2020-10-15 16:08:30 +02002432 if (smp.data.type == SMP_T_ANY)
2433 spoe_unset_var(ctx, scope, str, sz, &smp);
2434 else
2435 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002436
Christopher Faulet8ef75252017-02-20 22:56:03 +01002437 ret = (p - *buf);
2438 *buf = p;
2439 return ret;
2440 skip:
2441 return 0;
2442}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002443
Christopher Faulet8ef75252017-02-20 22:56:03 +01002444static inline int
2445spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2446 char **buf, char *end, int dir)
2447{
2448 char *str, *scope, *p = *buf;
2449 struct sample smp;
2450 uint64_t sz;
2451 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002452
Christopher Faulet8ef75252017-02-20 22:56:03 +01002453 if (p + 2 >= end)
2454 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002455
Christopher Faulet8ef75252017-02-20 22:56:03 +01002456 /* UNSET-VAR requires 2 arguments */
2457 if (*p++ != 2)
2458 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002459
Christopher Faulet8ef75252017-02-20 22:56:03 +01002460 switch (*p++) {
2461 case SPOE_SCOPE_PROC: scope = "proc"; break;
2462 case SPOE_SCOPE_SESS: scope = "sess"; break;
2463 case SPOE_SCOPE_TXN : scope = "txn"; break;
2464 case SPOE_SCOPE_REQ : scope = "req"; break;
2465 case SPOE_SCOPE_RES : scope = "res"; break;
2466 default: goto skip;
2467 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002468
Christopher Faulet8ef75252017-02-20 22:56:03 +01002469 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2470 goto skip;
2471 memset(&smp, 0, sizeof(smp));
2472 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002473
Christopher Faulet8ef75252017-02-20 22:56:03 +01002474 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2475 " - unset-var '%s.%s.%.*s'\n",
2476 (int)now.tv_sec, (int)now.tv_usec,
2477 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2478 __FUNCTION__, s, scope,
2479 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2480 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002481
Christopher Faulet8ef75252017-02-20 22:56:03 +01002482 spoe_unset_var(ctx, scope, str, sz, &smp);
2483
2484 ret = (p - *buf);
2485 *buf = p;
2486 return ret;
2487 skip:
2488 return 0;
2489}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002490
Christopher Faulet8ef75252017-02-20 22:56:03 +01002491/* Process SPOE actions for a specific event. It returns 1 on success. If an
2492 * error occurred, 0 is returned. */
2493static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002494spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002495{
2496 char *p, *end;
2497 int ret;
2498
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002499 p = b_head(&ctx->buffer);
2500 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002501
2502 while (p < end) {
2503 enum spoe_action_type type;
2504
2505 type = *p++;
2506 switch (type) {
2507 case SPOE_ACT_T_SET_VAR:
2508 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2509 if (!ret)
2510 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002511 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002512
Christopher Faulet8ef75252017-02-20 22:56:03 +01002513 case SPOE_ACT_T_UNSET_VAR:
2514 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2515 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002516 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002517 break;
2518
2519 default:
2520 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002521 }
2522 }
2523
2524 return 1;
2525 skip:
2526 return 0;
2527}
2528
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002529/***************************************************************************
2530 * Functions that process SPOE events
2531 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002532static void
2533spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2534 struct spoe_context *ctx, int dir)
2535{
2536 if (!tv_iszero(&ctx->stats.tv_start)) {
2537 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2538 ctx->stats.t_total += ctx->stats.t_process;
2539 tv_zero(&ctx->stats.tv_request);
2540 tv_zero(&ctx->stats.tv_queue);
2541 tv_zero(&ctx->stats.tv_wait);
2542 tv_zero(&ctx->stats.tv_response);
2543 }
2544
2545 if (agent->var_t_process) {
2546 struct sample smp;
2547
2548 memset(&smp, 0, sizeof(smp));
2549 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2550 smp.data.u.sint = ctx->stats.t_process;
2551 smp.data.type = SMP_T_SINT;
2552
2553 spoe_set_var(ctx, "txn", agent->var_t_process,
2554 strlen(agent->var_t_process), &smp);
2555 }
2556
2557 if (agent->var_t_total) {
2558 struct sample smp;
2559
2560 memset(&smp, 0, sizeof(smp));
2561 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2562 smp.data.u.sint = ctx->stats.t_total;
2563 smp.data.type = SMP_T_SINT;
2564
2565 spoe_set_var(ctx, "txn", agent->var_t_total,
2566 strlen(agent->var_t_total), &smp);
2567 }
2568}
2569
2570static void
2571spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2572 struct spoe_context *ctx, int dir)
2573{
2574 if (agent->eps_max > 0)
2575 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2576
2577 if (agent->var_on_error) {
2578 struct sample smp;
2579
2580 memset(&smp, 0, sizeof(smp));
2581 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2582 smp.data.u.sint = ctx->status_code;
2583 smp.data.type = SMP_T_BOOL;
2584
2585 spoe_set_var(ctx, "txn", agent->var_on_error,
2586 strlen(agent->var_on_error), &smp);
2587 }
2588
2589 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2590 ? SPOE_CTX_ST_READY
2591 : SPOE_CTX_ST_NONE);
2592}
2593
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002594static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002595spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002596{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002597 /* If a process is already started for this SPOE context, retry
2598 * later. */
2599 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002600 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002601
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002602 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002603 ctx->stats.tv_start = now;
2604 ctx->stats.tv_request = now;
2605 ctx->stats.t_request = -1;
2606 ctx->stats.t_queue = -1;
2607 ctx->stats.t_waiting = -1;
2608 ctx->stats.t_response = -1;
2609 ctx->stats.t_process = -1;
2610
2611 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002612
Christopher Fauleta1cda022016-12-21 08:58:06 +01002613 /* Set the right flag to prevent request and response processing
2614 * in same time. */
2615 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2616 ? SPOE_CTX_FL_REQ_PROCESS
2617 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002618 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002619}
2620
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002621static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002622spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002623{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002624 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002625
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002626 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2627 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002628 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002629 if (sa) {
2630 if (sa->frag_ctx.ctx == ctx) {
2631 sa->frag_ctx.ctx = NULL;
2632 spoe_wakeup_appctx(sa->owner);
2633 }
2634 else
2635 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002636 }
2637
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002639 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002640 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002641
2642 /* Reset processing timer */
2643 ctx->process_exp = TICK_ETERNITY;
2644
Christopher Faulet8ef75252017-02-20 22:56:03 +01002645 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002646
Christopher Fauletfce747b2018-01-24 15:59:32 +01002647 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002648 ctx->frag_ctx.curmsg = NULL;
2649 ctx->frag_ctx.curarg = NULL;
2650 ctx->frag_ctx.curoff = 0;
2651 ctx->frag_ctx.flags = 0;
2652
Christopher Fauleta1cda022016-12-21 08:58:06 +01002653 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002654 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002655 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002656 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002657 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002658
Willy Tarreau2b718102021-04-21 07:32:39 +02002659 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002660 LIST_INIT(&ctx->list);
2661 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002662}
2663
Christopher Faulet58d03682017-09-21 16:57:24 +02002664/* Process a list of SPOE messages. First, this functions will process messages
2665 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2666 * to process corresponding actions. During all the processing, it returns 0
2667 * and it returns 1 when the processing is finished. If an error occurred, -1
2668 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002669static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002670spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2671 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002672{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002673 struct spoe_config *conf = FLT_CONF(ctx->filter);
2674 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002675 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002676
2677 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002678 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002679
2680 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2681 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002682 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002683 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002684 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002685 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002686 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002687 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688
2689 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002690 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002691 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002692 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002693 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002694 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002695 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002696 goto skip;
2697 }
2698 }
2699
Christopher Fauletf7a30922016-11-10 15:04:51 +01002700 if (!tick_isset(ctx->process_exp)) {
2701 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2702 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2703 ctx->process_exp);
2704 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002705 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002706 if (!ret)
2707 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002708
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002709 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002710 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002711 }
2712
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002713 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002714 if (tv_iszero(&ctx->stats.tv_request))
2715 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002716 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002717 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002718 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002719 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002720 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002721 if (!ret)
2722 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002723 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002724 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002725 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2726 }
2727
2728 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002729 if (ctx->spoe_appctx)
2730 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002731 ret = 0;
2732 goto out;
2733 }
2734
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002735 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2736 ret = 0;
2737 goto out;
2738 }
2739
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002740 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002741 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002742 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002743 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002745 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002746 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002747 }
2748
2749 out:
2750 return ret;
2751
Christopher Fauleta1cda022016-12-21 08:58:06 +01002752 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002753 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002754 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002755 spoe_stop_processing(agent, ctx);
2756 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002757
Christopher Fauleta1cda022016-12-21 08:58:06 +01002758 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002759 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002760 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002761 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002762 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002763 spoe_handle_processing_error(s, agent, ctx, dir);
2764 ret = 1;
2765 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002766 return ret;
2767}
2768
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002769/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2770 * See spoe_process_message for details. */
2771static int
2772spoe_process_group(struct stream *s, struct spoe_context *ctx,
2773 struct spoe_group *group, int dir)
2774{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002775 struct spoe_config *conf = FLT_CONF(ctx->filter);
2776 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002777 int ret;
2778
2779 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2780 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002781 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002782 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2783 group->id);
2784
2785 if (LIST_ISEMPTY(&group->messages))
2786 return 1;
2787
2788 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002789 if (ret && ctx->stats.t_process != -1) {
2790 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002791 " - <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 +01002792 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002793 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002794 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002795 ctx->stats.t_response, ctx->stats.t_process,
2796 agent->counters.idles, agent->counters.applets,
2797 agent->counters.nb_sending, agent->counters.nb_waiting,
2798 agent->counters.nb_errors, agent->counters.nb_processed,
2799 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2800 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2801 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2802 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2803 agent->id, group->id, s->uniq_id, ctx->status_code,
2804 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2805 ctx->stats.t_response, ctx->stats.t_process,
2806 agent->counters.idles, agent->counters.applets,
2807 agent->counters.nb_sending, agent->counters.nb_waiting,
2808 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002809 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002810 return ret;
2811}
2812
Christopher Faulet58d03682017-09-21 16:57:24 +02002813/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2814 * See spoe_process_message for details. */
2815static int
2816spoe_process_event(struct stream *s, struct spoe_context *ctx,
2817 enum spoe_event ev)
2818{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002819 struct spoe_config *conf = FLT_CONF(ctx->filter);
2820 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002821 int dir, ret;
2822
2823 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002824 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002825 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002826 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2827 spoe_event_str[ev]);
2828
2829 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2830
2831 if (LIST_ISEMPTY(&(ctx->events[ev])))
2832 return 1;
2833
2834 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002835 if (ret && ctx->stats.t_process != -1) {
2836 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002837 " - <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 +01002838 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2839 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2840 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002841 ctx->stats.t_response, ctx->stats.t_process,
2842 agent->counters.idles, agent->counters.applets,
2843 agent->counters.nb_sending, agent->counters.nb_waiting,
2844 agent->counters.nb_errors, agent->counters.nb_processed,
2845 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2846 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2847 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2848 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2849 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2850 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2851 ctx->stats.t_response, ctx->stats.t_process,
2852 agent->counters.idles, agent->counters.applets,
2853 agent->counters.nb_sending, agent->counters.nb_waiting,
2854 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002855 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002856 return ret;
2857}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002858
2859/***************************************************************************
2860 * Functions that create/destroy SPOE contexts
2861 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002862static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002863spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002864{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002865 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002866 return 1;
2867
Willy Tarreau2b718102021-04-21 07:32:39 +02002868 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002869 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002870
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002871 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002872 return 1;
2873
Willy Tarreaub4e34762021-09-30 19:02:18 +02002874 LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002875 return 0;
2876}
2877
2878static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002879spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002880{
Willy Tarreau2b718102021-04-21 07:32:39 +02002881 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002882 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002883
2884 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002885 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002886 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002887 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002888 }
2889}
2890
Christopher Faulet4596fb72017-01-11 14:05:19 +01002891static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002892spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002893{
2894 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2895 return 1;
2896}
2897
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002898static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002899spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002900{
2901 struct spoe_config *conf = FLT_CONF(filter);
2902 struct spoe_context *ctx;
2903
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002904 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002905 if (ctx == NULL) {
2906 return NULL;
2907 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002908 ctx->filter = filter;
2909 ctx->state = SPOE_CTX_ST_NONE;
2910 ctx->status_code = SPOE_CTX_ERR_NONE;
2911 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002912 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002913 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002914 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002915 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002916 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002917 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002918 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919
Christopher Fauletf7a30922016-11-10 15:04:51 +01002920 ctx->stream_id = 0;
2921 ctx->frame_id = 1;
2922 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002923
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002924 tv_zero(&ctx->stats.tv_start);
2925 tv_zero(&ctx->stats.tv_request);
2926 tv_zero(&ctx->stats.tv_queue);
2927 tv_zero(&ctx->stats.tv_wait);
2928 tv_zero(&ctx->stats.tv_response);
2929 ctx->stats.t_request = -1;
2930 ctx->stats.t_queue = -1;
2931 ctx->stats.t_waiting = -1;
2932 ctx->stats.t_response = -1;
2933 ctx->stats.t_process = -1;
2934 ctx->stats.t_total = 0;
2935
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002936 ctx->strm = s;
2937 ctx->state = SPOE_CTX_ST_READY;
2938 filter->ctx = ctx;
2939
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940 return ctx;
2941}
2942
2943static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002944spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002945{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002946 struct spoe_config *conf = FLT_CONF(filter);
2947 struct spoe_context *ctx = filter->ctx;
2948
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002949 if (!ctx)
2950 return;
2951
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002952 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002953 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002954 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002955}
2956
2957static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002958spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002959{
2960 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002961 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002962
2963 tv_zero(&ctx->stats.tv_start);
2964 tv_zero(&ctx->stats.tv_request);
2965 tv_zero(&ctx->stats.tv_queue);
2966 tv_zero(&ctx->stats.tv_wait);
2967 tv_zero(&ctx->stats.tv_response);
2968 ctx->stats.t_request = -1;
2969 ctx->stats.t_queue = -1;
2970 ctx->stats.t_waiting = -1;
2971 ctx->stats.t_response = -1;
2972 ctx->stats.t_process = -1;
2973 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002974}
2975
2976
2977/***************************************************************************
2978 * Hooks that manage the filter lifecycle (init/check/deinit)
2979 **************************************************************************/
2980/* Signal handler: Do a soft stop, wakeup SPOE applet */
2981static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002982spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002983{
2984 struct proxy *p;
2985
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002986 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002987 while (p) {
2988 struct flt_conf *fconf;
2989
2990 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002991 struct spoe_config *conf;
2992 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002993 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002994 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995
Christopher Faulet3b386a32017-02-23 10:17:15 +01002996 if (fconf->id != spoe_filter_id)
2997 continue;
2998
2999 conf = fconf->conf;
3000 agent = conf->agent;
3001
Christopher Faulet24289f22017-09-25 14:48:02 +02003002 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003003 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003004 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3005 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003006 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007 }
3008 }
3009 p = p->next;
3010 }
3011}
3012
3013
3014/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3015static int
3016spoe_init(struct proxy *px, struct flt_conf *fconf)
3017{
3018 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003019
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003020 /* conf->agent_fe was already initialized during the config
3021 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003022 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023 conf->agent_fe.cap = PR_CAP_FE;
3024 conf->agent_fe.mode = PR_MODE_TCP;
3025 conf->agent_fe.maxconn = 0;
3026 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3027 conf->agent_fe.conn_retries = CONN_RETRIES;
3028 conf->agent_fe.accept = frontend_accept;
3029 conf->agent_fe.srv = NULL;
3030 conf->agent_fe.timeout.client = TICK_ETERNITY;
3031 conf->agent_fe.default_target = &spoe_applet.obj_type;
3032 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3033
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003034 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003035 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003036 sighandler_registered = 1;
3037 }
3038
Christopher Faulet00292352019-01-09 15:05:10 +01003039 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003040 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003041}
3042
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003043/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003044static void
3045spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3046{
3047 struct spoe_config *conf = fconf->conf;
3048
3049 if (conf) {
3050 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003051
Christopher Faulet8ef75252017-02-20 22:56:03 +01003052 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003053 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003054 free(conf);
3055 }
3056 fconf->conf = NULL;
3057}
3058
3059/* Check configuration of a SPOE filter for a specified proxy.
3060 * Return 1 on error, else 0. */
3061static int
3062spoe_check(struct proxy *px, struct flt_conf *fconf)
3063{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003064 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003065 struct spoe_config *conf = fconf->conf;
3066 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003067 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003068 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003069
Christopher Faulet7ee86672017-09-19 11:08:28 +02003070 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3071 * are uniq */
3072 list_for_each_entry(f, &px->filter_configs, list) {
3073 struct spoe_config *c = f->conf;
3074
3075 /* This is not an SPOE filter */
3076 if (f->id != spoe_filter_id)
3077 continue;
3078 /* This is the current SPOE filter */
3079 if (f == fconf)
3080 continue;
3081
3082 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003083 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003084 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3085 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003086 return 1;
3087 }
3088 }
3089
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003090 target = proxy_be_by_name(conf->agent->b.name);
3091 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003092 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3093 " declared at %s:%d.\n",
3094 px->id, conf->agent->b.name, conf->agent->id,
3095 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003096 return 1;
3097 }
3098 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003099 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3100 " at %s:%d does not support HTTP mode.\n",
3101 px->id, target->id, conf->agent->id,
3102 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103 return 1;
3104 }
3105
Christopher Fauletfe261552019-03-18 13:57:42 +01003106 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003107 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3108 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3109 return 1;
3110 }
3111 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003112 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003113 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3114 conf->agent->rt[i].processing = 0;
3115 LIST_INIT(&conf->agent->rt[i].applets);
3116 LIST_INIT(&conf->agent->rt[i].sending_queue);
3117 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3118 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003119 }
3120
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003121 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3122 if (logsrv->type == LOG_TARGET_BUFFER) {
3123 struct sink *sink = sink_find(logsrv->ring_name);
3124
3125 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3126 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003127 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003128 px->id, conf->agent->id, conf->agent->conf.file,
3129 conf->agent->conf.line, logsrv->ring_name);
3130 return 1;
3131 }
3132 logsrv->sink = sink;
3133 }
3134 }
3135
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003136 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003137 conf->agent->b.be = target;
3138 return 0;
3139}
3140
Kevin Zhud87b1a52019-09-17 15:05:45 +02003141/* Initializes the SPOE filter for a proxy for a specific thread.
3142 * Returns a negative value if an error occurs. */
3143static int
3144spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3145{
3146 struct spoe_config *conf = fconf->conf;
3147 struct spoe_agent *agent = conf->agent;
3148
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003149 agent->rt[tid].engine_id = generate_pseudo_uuid();
3150 if (agent->rt[tid].engine_id == NULL)
3151 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003152 return 0;
3153}
3154
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003155/**************************************************************************
3156 * Hooks attached to a stream
3157 *************************************************************************/
3158/* Called when a filter instance is created and attach to a stream. It creates
3159 * the context that will be used to process this stream. */
3160static int
3161spoe_start(struct stream *s, struct filter *filter)
3162{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003163 struct spoe_config *conf = FLT_CONF(filter);
3164 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003165 struct spoe_context *ctx;
3166
3167 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003168 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169 __FUNCTION__, s);
3170
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003171 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003172 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3173 " - failed to create SPOE context\n",
3174 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003175 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003176 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003177 "SPOE: [%s] failed to create SPOE context\n",
3178 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003179 return 0;
3180 }
3181
Christopher Faulet11610f32017-09-21 10:23:10 +02003182 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003183 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3184
Christopher Faulet11610f32017-09-21 10:23:10 +02003185 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003186 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3187
Christopher Faulet11610f32017-09-21 10:23:10 +02003188 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003189 filter->pre_analyzers |= AN_RES_INSPECT;
3190
Christopher Faulet11610f32017-09-21 10:23:10 +02003191 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003192 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3193
Christopher Faulet11610f32017-09-21 10:23:10 +02003194 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3196
Christopher Faulet11610f32017-09-21 10:23:10 +02003197 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003198 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3199
3200 return 1;
3201}
3202
3203/* Called when a filter instance is detached from a stream. It release the
3204 * attached SPOE context. */
3205static void
3206spoe_stop(struct stream *s, struct filter *filter)
3207{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003208 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3209 (int)now.tv_sec, (int)now.tv_usec,
3210 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3211 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003212 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003213}
3214
Christopher Fauletf7a30922016-11-10 15:04:51 +01003215
3216/*
3217 * Called when the stream is woken up because of expired timer.
3218 */
3219static void
3220spoe_check_timeouts(struct stream *s, struct filter *filter)
3221{
3222 struct spoe_context *ctx = filter->ctx;
3223
Christopher Fauletac580602018-03-20 16:09:20 +01003224 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003225 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003226}
3227
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003228/* Called when we are ready to filter data on a channel */
3229static int
3230spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3231{
3232 struct spoe_context *ctx = filter->ctx;
3233 int ret = 1;
3234
3235 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3236 " - ctx-flags=0x%08x\n",
3237 (int)now.tv_sec, (int)now.tv_usec,
3238 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3239 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3240
Christopher Fauletb067b062017-01-04 16:39:11 +01003241 if (ctx->state == SPOE_CTX_ST_NONE)
3242 goto out;
3243
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003244 if (!(chn->flags & CF_ISRESP)) {
3245 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3246 chn->analysers |= AN_REQ_INSPECT_FE;
3247 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3248 chn->analysers |= AN_REQ_INSPECT_BE;
3249
3250 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3251 goto out;
3252
3253 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003254 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003255 if (!ret)
3256 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003257 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3258 }
3259 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003260 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003261 chn->analysers |= AN_RES_INSPECT;
3262
3263 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3264 goto out;
3265
Christopher Faulet8ef75252017-02-20 22:56:03 +01003266 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003267 if (!ret) {
3268 channel_dont_read(chn);
3269 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003270 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003271 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003272 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273 }
3274
3275 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003276 return ret;
3277}
3278
3279/* Called before a processing happens on a given channel */
3280static int
3281spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3282 struct channel *chn, unsigned an_bit)
3283{
3284 struct spoe_context *ctx = filter->ctx;
3285 int ret = 1;
3286
3287 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3288 " - ctx-flags=0x%08x - ana=0x%08x\n",
3289 (int)now.tv_sec, (int)now.tv_usec,
3290 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3291 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3292 ctx->flags, an_bit);
3293
Christopher Fauletb067b062017-01-04 16:39:11 +01003294 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003295 goto out;
3296
3297 switch (an_bit) {
3298 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003299 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003300 break;
3301 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003302 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003303 break;
3304 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003305 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003306 break;
3307 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003308 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003309 break;
3310 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003311 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003312 break;
3313 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003314 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003315 break;
3316 }
3317
3318 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003319 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003320 channel_dont_read(chn);
3321 channel_dont_close(chn);
3322 }
3323 return ret;
3324}
3325
3326/* Called when the filtering on the channel ends. */
3327static int
3328spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3329{
3330 struct spoe_context *ctx = filter->ctx;
3331
3332 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3333 " - ctx-flags=0x%08x\n",
3334 (int)now.tv_sec, (int)now.tv_usec,
3335 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3336 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3337
3338 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003339 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003340 }
3341
3342 return 1;
3343}
3344
3345/********************************************************************
3346 * Functions that manage the filter initialization
3347 ********************************************************************/
3348struct flt_ops spoe_ops = {
3349 /* Manage SPOE filter, called for each filter declaration */
3350 .init = spoe_init,
3351 .deinit = spoe_deinit,
3352 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003353 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003354
3355 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003356 .attach = spoe_start,
3357 .detach = spoe_stop,
3358 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003359
3360 /* Handle channels activity */
3361 .channel_start_analyze = spoe_start_analyze,
3362 .channel_pre_analyze = spoe_chn_pre_analyze,
3363 .channel_end_analyze = spoe_end_analyze,
3364};
3365
3366
3367static int
3368cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3369{
3370 const char *err;
3371 int i, err_code = 0;
3372
3373 if ((cfg_scope == NULL && curengine != NULL) ||
3374 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003375 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003376 goto out;
3377
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003378 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003379 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003380 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3381 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003382 err_code |= ERR_ALERT | ERR_ABORT;
3383 goto out;
3384 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003385 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3386 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003387 goto out;
3388 }
3389
3390 err = invalid_char(args[1]);
3391 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003392 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3393 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003394 err_code |= ERR_ALERT | ERR_ABORT;
3395 goto out;
3396 }
3397
3398 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003399 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3400 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003401 err_code |= ERR_ALERT | ERR_ABORT;
3402 goto out;
3403 }
3404 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003405 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003406 err_code |= ERR_ALERT | ERR_ABORT;
3407 goto out;
3408 }
3409
3410 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003411
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003412 curagent->conf.file = strdup(file);
3413 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003414
3415 curagent->timeout.hello = TICK_ETERNITY;
3416 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003417 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003418
Christopher Fauleta1cda022016-12-21 08:58:06 +01003419 curagent->var_pfx = NULL;
3420 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003421 curagent->var_t_process = NULL;
3422 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003423 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003424 curagent->cps_max = 0;
3425 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003426 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003427 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003428
3429 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003430 LIST_INIT(&curagent->events[i]);
3431 LIST_INIT(&curagent->groups);
3432 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003433 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003434 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003435 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003436 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3437 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003438 err_code |= ERR_ALERT | ERR_FATAL;
3439 goto out;
3440 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003441 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003442 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 free(curagent->b.name);
3444 curagent->b.name = strdup(args[1]);
3445 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003446 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003447 int cur_arg = 1;
3448 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003449 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003450
Christopher Faulet11610f32017-09-21 10:23:10 +02003451 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003452 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003453 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3454 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003455 err_code |= ERR_ALERT | ERR_FATAL;
3456 goto out;
3457 }
3458 }
3459
Christopher Faulet11610f32017-09-21 10:23:10 +02003460 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003461 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003462 err_code |= ERR_ALERT | ERR_ABORT;
3463 goto out;
3464 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003465 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003466 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003467 cur_arg++;
3468 }
3469 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003470 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003471 int cur_arg = 1;
3472 while (*args[cur_arg]) {
3473 struct spoe_placeholder *ph = NULL;
3474
3475 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003476 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003477 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3478 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003479 err_code |= ERR_ALERT | ERR_FATAL;
3480 goto out;
3481 }
3482 }
3483
3484 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003485 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003486 err_code |= ERR_ALERT | ERR_ABORT;
3487 goto out;
3488 }
3489 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003490 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003491 cur_arg++;
3492 }
3493 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003494 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003495 unsigned int *tv = NULL;
3496 const char *res;
3497 unsigned timeout;
3498
3499 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003500 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3501 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003502 err_code |= ERR_ALERT | ERR_FATAL;
3503 goto out;
3504 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003505 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3506 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003507 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003508 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003509 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003510 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003511 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003512 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003513 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003514 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3515 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003516 err_code |= ERR_ALERT | ERR_FATAL;
3517 goto out;
3518 }
3519 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003520 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3521 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003522 err_code |= ERR_ALERT | ERR_FATAL;
3523 goto out;
3524 }
3525 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003526 if (res == PARSE_TIME_OVER) {
3527 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3528 file, linenum, args[2], args[0], args[1]);
3529 err_code |= ERR_ALERT | ERR_FATAL;
3530 goto out;
3531 }
3532 else if (res == PARSE_TIME_UNDER) {
3533 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3534 file, linenum, args[2], args[0], args[1]);
3535 err_code |= ERR_ALERT | ERR_FATAL;
3536 goto out;
3537 }
3538 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003539 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3540 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003541 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003542 goto out;
3543 }
3544 *tv = MS_TO_TICKS(timeout);
3545 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003546 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003547 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003548 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3549 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003550 err_code |= ERR_ALERT | ERR_FATAL;
3551 goto out;
3552 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003553
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003554 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003555 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003556 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003557 if (kwm == 1)
3558 curagent->flags &= ~SPOE_FL_PIPELINING;
3559 else
3560 curagent->flags |= SPOE_FL_PIPELINING;
3561 goto out;
3562 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003563 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003564 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003565 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003566 if (kwm == 1)
3567 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003568 else
3569 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003570 goto out;
3571 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003572 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003573 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3574 goto out;
3575 if (kwm == 1)
3576 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3577 else
3578 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3579 goto out;
3580 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003581 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003582 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3583 goto out;
3584 if (kwm == 1)
3585 curpxopts2 &= ~PR_O2_NOLOGNORM;
3586 else
3587 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003588 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003589 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003590
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003591 /* Following options does not support negation */
3592 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003593 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3594 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003595 err_code |= ERR_ALERT | ERR_FATAL;
3596 goto out;
3597 }
3598
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003599 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003600 char *tmp;
3601
3602 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003603 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3604 file, linenum, args[0],
3605 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003606 err_code |= ERR_ALERT | ERR_FATAL;
3607 goto out;
3608 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003609 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3610 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003611 tmp = args[2];
3612 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003613 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003614 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003615 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003616 err_code |= ERR_ALERT | ERR_FATAL;
3617 goto out;
3618 }
3619 tmp++;
3620 }
3621 curagent->var_pfx = strdup(args[2]);
3622 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003623 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003624 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3625 goto out;
3626 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3627 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003628 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003629 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003630 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003631 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3632 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003633 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003634 char *tmp;
3635
3636 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003637 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3638 file, linenum, args[0],
3639 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003640 err_code |= ERR_ALERT | ERR_FATAL;
3641 goto out;
3642 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003643 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3644 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003645 tmp = args[2];
3646 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003647 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003648 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003649 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003650 err_code |= ERR_ALERT | ERR_FATAL;
3651 goto out;
3652 }
3653 tmp++;
3654 }
3655 curagent->var_on_error = strdup(args[2]);
3656 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003657 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003658 char *tmp;
3659
3660 if (!*args[2]) {
3661 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3662 file, linenum, args[0],
3663 args[1]);
3664 err_code |= ERR_ALERT | ERR_FATAL;
3665 goto out;
3666 }
3667 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3668 goto out;
3669 tmp = args[2];
3670 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003671 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003672 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003673 file, linenum, args[0], args[1]);
3674 err_code |= ERR_ALERT | ERR_FATAL;
3675 goto out;
3676 }
3677 tmp++;
3678 }
3679 curagent->var_t_process = strdup(args[2]);
3680 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003681 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003682 char *tmp;
3683
3684 if (!*args[2]) {
3685 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3686 file, linenum, args[0],
3687 args[1]);
3688 err_code |= ERR_ALERT | ERR_FATAL;
3689 goto out;
3690 }
3691 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3692 goto out;
3693 tmp = args[2];
3694 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003695 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003696 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003697 file, linenum, args[0], args[1]);
3698 err_code |= ERR_ALERT | ERR_FATAL;
3699 goto out;
3700 }
3701 tmp++;
3702 }
3703 curagent->var_t_total = strdup(args[2]);
3704 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003705 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003706 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3707 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003708 err_code |= ERR_ALERT | ERR_FATAL;
3709 goto out;
3710 }
Christopher Faulet48026722016-11-16 15:01:12 +01003711 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003712 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003713 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003714 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3715 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003716 err_code |= ERR_ALERT | ERR_FATAL;
3717 goto out;
3718 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003719 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003720 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003721 curagent->cps_max = atol(args[1]);
3722 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003723 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003724 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003725 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3726 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003727 err_code |= ERR_ALERT | ERR_FATAL;
3728 goto out;
3729 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003730 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003731 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003732 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003733 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003734 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003735 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003736 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3737 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003738 err_code |= ERR_ALERT | ERR_FATAL;
3739 goto out;
3740 }
3741 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3742 goto out;
3743 curagent->max_frame_size = atol(args[1]);
3744 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3745 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003746 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3747 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003748 err_code |= ERR_ALERT | ERR_FATAL;
3749 goto out;
3750 }
3751 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003752 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003753 if (!*args[1]) {
3754 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3755 file, linenum, args[0]);
3756 err_code |= ERR_ALERT | ERR_FATAL;
3757 goto out;
3758 }
3759 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3760 goto out;
3761 curagent->max_fpa = atol(args[1]);
3762 if (curagent->max_fpa < 1) {
3763 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3764 file, linenum, args[0]);
3765 err_code |= ERR_ALERT | ERR_FATAL;
3766 goto out;
3767 }
3768 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003769 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003770 int cur_arg;
3771
3772 if (!*args[1]) {
3773 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3774 file, linenum, args[0]);
3775 err_code |= ERR_ALERT | ERR_FATAL;
3776 goto out;
3777 }
3778 cur_arg = 1;
3779 while (*args[cur_arg]) {
3780 struct spoe_var_placeholder *vph;
3781
3782 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3783 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3784 err_code |= ERR_ALERT | ERR_ABORT;
3785 goto out;
3786 }
3787 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003788 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003789 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3790 err_code |= ERR_ALERT | ERR_ABORT;
3791 goto out;
3792 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003793 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003794 cur_arg++;
3795 }
3796 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003797 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003798 char *errmsg = NULL;
3799
Emeric Brun9533a702021-04-02 10:13:43 +02003800 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003801 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3802 err_code |= ERR_ALERT | ERR_FATAL;
3803 goto out;
3804 }
3805 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003806 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003807 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3808 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003809 err_code |= ERR_ALERT | ERR_FATAL;
3810 goto out;
3811 }
3812 out:
3813 return err_code;
3814}
Christopher Faulet11610f32017-09-21 10:23:10 +02003815static int
3816cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3817{
3818 struct spoe_group *grp;
3819 const char *err;
3820 int err_code = 0;
3821
3822 if ((cfg_scope == NULL && curengine != NULL) ||
3823 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003824 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003825 goto out;
3826
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003827 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003828 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003829 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3830 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003831 err_code |= ERR_ALERT | ERR_ABORT;
3832 goto out;
3833 }
3834 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3835 err_code |= ERR_ABORT;
3836 goto out;
3837 }
3838
3839 err = invalid_char(args[1]);
3840 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003841 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3842 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003843 err_code |= ERR_ALERT | ERR_ABORT;
3844 goto out;
3845 }
3846
3847 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003848 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003849 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3850 " name as another one declared at %s:%d.\n",
3851 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003852 err_code |= ERR_ALERT | ERR_FATAL;
3853 goto out;
3854 }
3855 }
3856
3857 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003858 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003859 err_code |= ERR_ALERT | ERR_ABORT;
3860 goto out;
3861 }
3862
3863 curgrp->id = strdup(args[1]);
3864 curgrp->conf.file = strdup(file);
3865 curgrp->conf.line = linenum;
3866 LIST_INIT(&curgrp->phs);
3867 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003868 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003869 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003870 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003871 int cur_arg = 1;
3872 while (*args[cur_arg]) {
3873 struct spoe_placeholder *ph = NULL;
3874
3875 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003876 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003877 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3878 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003879 err_code |= ERR_ALERT | ERR_FATAL;
3880 goto out;
3881 }
3882 }
3883
3884 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003885 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003886 err_code |= ERR_ALERT | ERR_ABORT;
3887 goto out;
3888 }
3889 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003890 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003891 cur_arg++;
3892 }
3893 }
3894 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003895 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3896 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003897 err_code |= ERR_ALERT | ERR_FATAL;
3898 goto out;
3899 }
3900 out:
3901 return err_code;
3902}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003903
3904static int
3905cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3906{
3907 struct spoe_message *msg;
3908 struct spoe_arg *arg;
3909 const char *err;
3910 char *errmsg = NULL;
3911 int err_code = 0;
3912
3913 if ((cfg_scope == NULL && curengine != NULL) ||
3914 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003915 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003916 goto out;
3917
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003918 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003919 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003920 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3921 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003922 err_code |= ERR_ALERT | ERR_ABORT;
3923 goto out;
3924 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003925 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3926 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 goto out;
3928 }
3929
3930 err = invalid_char(args[1]);
3931 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003932 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3933 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003934 err_code |= ERR_ALERT | ERR_ABORT;
3935 goto out;
3936 }
3937
3938 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003939 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003940 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3941 " name as another one declared at %s:%d.\n",
3942 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003943 err_code |= ERR_ALERT | ERR_FATAL;
3944 goto out;
3945 }
3946 }
3947
3948 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003949 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003950 err_code |= ERR_ALERT | ERR_ABORT;
3951 goto out;
3952 }
3953
3954 curmsg->id = strdup(args[1]);
3955 curmsg->id_len = strlen(curmsg->id);
3956 curmsg->event = SPOE_EV_NONE;
3957 curmsg->conf.file = strdup(file);
3958 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003959 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003960 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003961 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003962 LIST_INIT(&curmsg->by_evt);
3963 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003964 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003965 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003966 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003967 int cur_arg = 1;
3968
3969 curproxy->conf.args.ctx = ARGC_SPOE;
3970 curproxy->conf.args.file = file;
3971 curproxy->conf.args.line = linenum;
3972 while (*args[cur_arg]) {
3973 char *delim = strchr(args[cur_arg], '=');
3974 int idx = 0;
3975
3976 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003977 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003978 err_code |= ERR_ALERT | ERR_ABORT;
3979 goto out;
3980 }
3981
3982 if (!delim) {
3983 arg->name = NULL;
3984 arg->name_len = 0;
3985 delim = args[cur_arg];
3986 }
3987 else {
3988 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3989 arg->name_len = delim - args[cur_arg];
3990 delim++;
3991 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003992 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3993 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003994 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003995 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003996 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003997 err_code |= ERR_ALERT | ERR_FATAL;
3998 free(arg->name);
3999 free(arg);
4000 goto out;
4001 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004002 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004003 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004004 cur_arg++;
4005 }
4006 curproxy->conf.args.file = NULL;
4007 curproxy->conf.args.line = 0;
4008 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004009 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004010 err = invalid_char(args[1]);
4011 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004012 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4013 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004014 err_code |= ERR_ALERT | ERR_FATAL;
4015 goto out;
4016 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004017 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004018 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004019 "logical disjunction within a condition.\n",
4020 file, linenum, args[1]);
4021 err_code |= ERR_ALERT | ERR_FATAL;
4022 goto out;
4023 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004024 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004025 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4026 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004027 err_code |= ERR_ALERT | ERR_FATAL;
4028 goto out;
4029 }
4030 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004031 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004032 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004033 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004034 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004035 goto out;
4036 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004037 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4038 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004039
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004040 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004041 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004042 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004043 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4044
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004045 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004046 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004047 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004048 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004049 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004050 curmsg->event = SPOE_EV_ON_TCP_RSP;
4051
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004052 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004053 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004054 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004056 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004057 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4058 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004059 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004060 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004061 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004062 goto out;
4063 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004064
4065 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4066 struct acl_cond *cond;
4067
4068 cond = build_acl_cond(file, linenum, &curmsg->acls,
4069 curproxy, (const char **)args+2,
4070 &errmsg);
4071 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004072 ha_alert("parsing [%s:%d] : error detected while "
4073 "parsing an 'event %s' condition : %s.\n",
4074 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004075 err_code |= ERR_ALERT | ERR_FATAL;
4076 goto out;
4077 }
4078 curmsg->cond = cond;
4079 }
4080 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004081 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4082 "or 'unless' followed by a condition but found '%s'.\n",
4083 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004084 err_code |= ERR_ALERT | ERR_FATAL;
4085 goto out;
4086 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004087 }
4088 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004089 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4090 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004091 err_code |= ERR_ALERT | ERR_FATAL;
4092 goto out;
4093 }
4094 out:
4095 free(errmsg);
4096 return err_code;
4097}
4098
4099/* Return -1 on error, else 0 */
4100static int
4101parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4102 struct flt_conf *fconf, char **err, void *private)
4103{
4104 struct list backup_sections;
4105 struct spoe_config *conf;
4106 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004107 struct spoe_group *grp, *grpback;
4108 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004109 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004110 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004111 char *file = NULL, *engine = NULL;
4112 int ret, pos = *cur_arg + 1;
4113
Christopher Faulet84c844e2018-03-23 14:37:14 +01004114 LIST_INIT(&curmsgs);
4115 LIST_INIT(&curgrps);
4116 LIST_INIT(&curmphs);
4117 LIST_INIT(&curgphs);
4118 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004119 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004120 curpxopts = 0;
4121 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004122
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004123 conf = calloc(1, sizeof(*conf));
4124 if (conf == NULL) {
4125 memprintf(err, "%s: out of memory", args[*cur_arg]);
4126 goto error;
4127 }
4128 conf->proxy = px;
4129
4130 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004131 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004132 if (!*args[pos+1]) {
4133 memprintf(err, "'%s' : '%s' option without value",
4134 args[*cur_arg], args[pos]);
4135 goto error;
4136 }
4137 file = args[pos+1];
4138 pos += 2;
4139 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004140 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004141 if (!*args[pos+1]) {
4142 memprintf(err, "'%s' : '%s' option without value",
4143 args[*cur_arg], args[pos]);
4144 goto error;
4145 }
4146 engine = args[pos+1];
4147 pos += 2;
4148 }
4149 else {
4150 memprintf(err, "unknown keyword '%s'", args[pos]);
4151 goto error;
4152 }
4153 }
4154 if (file == NULL) {
4155 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4156 goto error;
4157 }
4158
4159 /* backup sections and register SPOE sections */
4160 LIST_INIT(&backup_sections);
4161 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004162 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4163 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004164 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004165
4166 /* Parse SPOE filter configuration file */
4167 curengine = engine;
4168 curproxy = px;
4169 curagent = NULL;
4170 curmsg = NULL;
4171 ret = readcfgfile(file);
4172 curproxy = NULL;
4173
4174 /* unregister SPOE sections and restore previous sections */
4175 cfg_unregister_sections();
4176 cfg_restore_sections(&backup_sections);
4177
4178 if (ret == -1) {
4179 memprintf(err, "Could not open configuration file %s : %s",
4180 file, strerror(errno));
4181 goto error;
4182 }
4183 if (ret & (ERR_ABORT|ERR_FATAL)) {
4184 memprintf(err, "Error(s) found in configuration file %s", file);
4185 goto error;
4186 }
4187
4188 /* Check SPOE agent */
4189 if (curagent == NULL) {
4190 memprintf(err, "No SPOE agent found in file %s", file);
4191 goto error;
4192 }
4193 if (curagent->b.name == NULL) {
4194 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4195 curagent->id, curagent->conf.file, curagent->conf.line);
4196 goto error;
4197 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004198 if (curagent->timeout.hello == TICK_ETERNITY ||
4199 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004200 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004201 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4202 " | While not properly invalid, you will certainly encounter various problems\n"
4203 " | with such a configuration. To fix this, please ensure that all following\n"
4204 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4205 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004206 }
4207 if (curagent->var_pfx == NULL) {
4208 char *tmp = curagent->id;
4209
4210 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004211 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004212 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4213 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4214 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4215 goto error;
4216 }
4217 tmp++;
4218 }
4219 curagent->var_pfx = strdup(curagent->id);
4220 }
4221
Christopher Fauletb7426d12018-03-21 14:12:17 +01004222 if (curagent->var_on_error) {
4223 struct arg arg;
4224
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004225 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004226 curagent->var_pfx, curagent->var_on_error);
4227
4228 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004229 arg.data.str.area = trash.area;
4230 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004231 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004232 if (!vars_check_arg(&arg, err)) {
4233 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4234 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4235 goto error;
4236 }
4237 }
4238
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004239 if (curagent->var_t_process) {
4240 struct arg arg;
4241
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004242 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004243 curagent->var_pfx, curagent->var_t_process);
4244
4245 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004246 arg.data.str.area = trash.area;
4247 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004248 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004249 if (!vars_check_arg(&arg, err)) {
4250 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4251 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4252 goto error;
4253 }
4254 }
4255
4256 if (curagent->var_t_total) {
4257 struct arg arg;
4258
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004259 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004260 curagent->var_pfx, curagent->var_t_total);
4261
4262 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004263 arg.data.str.area = trash.area;
4264 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004265 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004266 if (!vars_check_arg(&arg, err)) {
4267 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4268 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4269 goto error;
4270 }
4271 }
4272
Christopher Faulet11610f32017-09-21 10:23:10 +02004273 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004274 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4275 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004276 goto finish;
4277 }
4278
Christopher Faulet11610f32017-09-21 10:23:10 +02004279 /* Replace placeholders by the corresponding messages for the SPOE
4280 * agent */
4281 list_for_each_entry(ph, &curmphs, list) {
4282 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004283 struct spoe_arg *arg;
4284 unsigned int where;
4285
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004286 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004287 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4288 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4289 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4290 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4291 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4292 }
4293 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4294 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4295 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004296 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4297 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004298 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004299 }
4300 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004301 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4302 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004303 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004304 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004305
4306 where = 0;
4307 switch (msg->event) {
4308 case SPOE_EV_ON_CLIENT_SESS:
4309 where |= SMP_VAL_FE_CON_ACC;
4310 break;
4311
4312 case SPOE_EV_ON_TCP_REQ_FE:
4313 where |= SMP_VAL_FE_REQ_CNT;
4314 break;
4315
4316 case SPOE_EV_ON_HTTP_REQ_FE:
4317 where |= SMP_VAL_FE_HRQ_HDR;
4318 break;
4319
4320 case SPOE_EV_ON_TCP_REQ_BE:
4321 if (px->cap & PR_CAP_FE)
4322 where |= SMP_VAL_FE_REQ_CNT;
4323 if (px->cap & PR_CAP_BE)
4324 where |= SMP_VAL_BE_REQ_CNT;
4325 break;
4326
4327 case SPOE_EV_ON_HTTP_REQ_BE:
4328 if (px->cap & PR_CAP_FE)
4329 where |= SMP_VAL_FE_HRQ_HDR;
4330 if (px->cap & PR_CAP_BE)
4331 where |= SMP_VAL_BE_HRQ_HDR;
4332 break;
4333
4334 case SPOE_EV_ON_SERVER_SESS:
4335 where |= SMP_VAL_BE_SRV_CON;
4336 break;
4337
4338 case SPOE_EV_ON_TCP_RSP:
4339 if (px->cap & PR_CAP_FE)
4340 where |= SMP_VAL_FE_RES_CNT;
4341 if (px->cap & PR_CAP_BE)
4342 where |= SMP_VAL_BE_RES_CNT;
4343 break;
4344
4345 case SPOE_EV_ON_HTTP_RSP:
4346 if (px->cap & PR_CAP_FE)
4347 where |= SMP_VAL_FE_HRS_HDR;
4348 if (px->cap & PR_CAP_BE)
4349 where |= SMP_VAL_BE_HRS_HDR;
4350 break;
4351
4352 default:
4353 break;
4354 }
4355
4356 list_for_each_entry(arg, &msg->args, list) {
4357 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004358 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004359 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004360 "none of which is available here ('%s')",
4361 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004362 sample_ckp_names(arg->expr->fetch->use),
4363 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004364 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004365 }
4366 }
4367
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004368 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004369 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004370 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004371 }
4372 }
4373 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004374 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004375 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004376 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004377 continue;
4378 }
4379
Christopher Faulet11610f32017-09-21 10:23:10 +02004380 /* Replace placeholders by the corresponding groups for the SPOE
4381 * agent */
4382 list_for_each_entry(ph, &curgphs, list) {
4383 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004384 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004385 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004386 LIST_DELETE(&grp->list);
4387 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004388 goto next_aph;
4389 }
4390 }
4391 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4392 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4393 goto error;
4394 next_aph:
4395 continue;
4396 }
4397
4398 /* Replace placeholders by the corresponding message for each SPOE
4399 * group of the SPOE agent */
4400 list_for_each_entry(grp, &curagent->groups, list) {
4401 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4402 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004403 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004404 if (msg->group != NULL) {
4405 memprintf(err, "SPOE message '%s' already belongs to "
4406 "the SPOE group '%s' declare at %s:%d",
4407 msg->id, msg->group->id,
4408 msg->group->conf.file,
4409 msg->group->conf.line);
4410 goto error;
4411 }
4412
4413 /* Scope for arguments are not checked for now. We will check
4414 * them only if a rule use the corresponding SPOE group. */
4415 msg->agent = curagent;
4416 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004417 LIST_DELETE(&ph->list);
4418 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004419 goto next_mph_grp;
4420 }
4421 }
4422 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4423 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4424 goto error;
4425 next_mph_grp:
4426 continue;
4427 }
4428 }
4429
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004430 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004431 /* move curmsgs to the agent message list */
4432 curmsgs.n->p = &curagent->messages;
4433 curmsgs.p->n = &curagent->messages;
4434 curagent->messages = curmsgs;
4435 LIST_INIT(&curmsgs);
4436
Christopher Faulet7ee86672017-09-19 11:08:28 +02004437 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004438 conf->agent = curagent;
Christopher Faulet434b8522021-08-02 17:51:01 +02004439 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004440
4441 /* Start agent's proxy initialization here. It will be finished during
4442 * the filter init. */
4443 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4444 init_new_proxy(&conf->agent_fe);
4445 conf->agent_fe.id = conf->agent->id;
4446 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004447 conf->agent_fe.options |= curpxopts;
4448 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004449
4450 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004451 LIST_DELETE(&logsrv->list);
4452 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004453 }
4454
Christopher Faulet11610f32017-09-21 10:23:10 +02004455 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004456 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004457 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004458 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004459 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004460 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004461 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004462 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004463 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4464 struct arg arg;
4465
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004466 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004467 curagent->var_pfx, vph->name);
4468
4469 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004470 arg.data.str.area = trash.area;
4471 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004472 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004473 if (!vars_check_arg(&arg, err)) {
4474 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4475 curagent->id, curagent->var_pfx, vph->name, *err);
4476 goto error;
4477 }
4478
Willy Tarreau2b718102021-04-21 07:32:39 +02004479 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004480 free(vph->name);
4481 free(vph);
4482 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004483 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004484 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004485 spoe_release_group(grp);
4486 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004487 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004488 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004489 fconf->ops = &spoe_ops;
4490 fconf->conf = conf;
4491 return 0;
4492
4493 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004494 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004495 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004496 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004497 spoe_release_placeholder(ph);
4498 }
4499 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004500 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004501 spoe_release_placeholder(ph);
4502 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004503 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004504 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004505 free(vph->name);
4506 free(vph);
4507 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004508 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004509 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004510 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004511 }
4512 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004513 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004514 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004515 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004516 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004517 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004518 free(logsrv);
4519 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004520 free(conf);
4521 return -1;
4522}
4523
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004524/* Send message of a SPOE group. This is the action_ptr callback of a rule
4525 * associated to a "send-spoe-group" action.
4526 *
Christopher Faulet13403762019-12-13 09:01:57 +01004527 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4528 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004529static enum act_return
4530spoe_send_group(struct act_rule *rule, struct proxy *px,
4531 struct session *sess, struct stream *s, int flags)
4532{
4533 struct filter *filter;
4534 struct spoe_agent *agent = NULL;
4535 struct spoe_group *group = NULL;
4536 struct spoe_context *ctx = NULL;
4537 int ret, dir;
4538
4539 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4540 if (filter->config == rule->arg.act.p[0]) {
4541 agent = rule->arg.act.p[2];
4542 group = rule->arg.act.p[3];
4543 ctx = filter->ctx;
4544 break;
4545 }
4546 }
4547 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004548 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004549 if (ctx->state == SPOE_CTX_ST_NONE)
4550 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004551
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004552 switch (rule->from) {
4553 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4554 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4555 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4556 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4557 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4558 default:
4559 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4560 " - internal error while execute spoe-send-group\n",
4561 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4562 __FUNCTION__, s);
4563 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4564 agent->id);
4565 return ACT_RET_CONT;
4566 }
4567
4568 ret = spoe_process_group(s, ctx, group, dir);
4569 if (ret == 1)
4570 return ACT_RET_CONT;
4571 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004572 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004573 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4574 " - failed to process group '%s': interrupted by caller\n",
4575 (int)now.tv_sec, (int)now.tv_usec,
4576 agent->id, __FUNCTION__, s, group->id);
4577 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004578 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004579 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004580 return ACT_RET_CONT;
4581 }
4582 return ACT_RET_YIELD;
4583 }
4584 else
Christopher Faulet13403762019-12-13 09:01:57 +01004585 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004586}
4587
4588/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4589 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4590 * action should be:
4591 *
4592 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4593 *
4594 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4595 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4596 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4597 * group.
4598 *
4599 * The function returns 1 in success case, otherwise, it returns 0 and err is
4600 * filled.
4601 */
4602static int
4603check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4604{
4605 struct flt_conf *fconf;
4606 struct spoe_config *conf;
4607 struct spoe_agent *agent = NULL;
4608 struct spoe_group *group;
4609 struct spoe_message *msg;
4610 char *engine_id = rule->arg.act.p[0];
4611 char *group_id = rule->arg.act.p[1];
4612 unsigned int where = 0;
4613
4614 switch (rule->from) {
4615 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4616 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4617 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4618 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4619 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4620 default:
4621 memprintf(err,
4622 "internal error, unexpected rule->from=%d, please report this bug!",
4623 rule->from);
4624 goto error;
4625 }
4626
4627 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4628 * <px> */
4629 list_for_each_entry(fconf, &px->filter_configs, list) {
4630 conf = fconf->conf;
4631
4632 /* This is not an SPOE filter */
4633 if (fconf->id != spoe_filter_id)
4634 continue;
4635
4636 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004637 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004638 agent = conf->agent;
4639 break;
4640 }
4641 }
4642 if (agent == NULL) {
4643 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4644 engine_id, group_id);
4645 goto error;
4646 }
4647
4648 /* Try to find the right group */
4649 list_for_each_entry(group, &agent->groups, list) {
4650 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004651 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004652 break;
4653 }
4654 if (&group->list == &agent->groups) {
4655 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4656 group_id, engine_id);
4657 goto error;
4658 }
4659
4660 /* Ok, we found the group, we need to check messages and their
4661 * arguments */
4662 list_for_each_entry(msg, &group->messages, by_grp) {
4663 struct spoe_arg *arg;
4664
4665 list_for_each_entry(arg, &msg->args, list) {
4666 if (!(arg->expr->fetch->val & where)) {
4667 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4668 "some args extract information from '%s',"
4669 "none of which is available here ('%s')",
4670 msg->id, group->id, msg->conf.file, msg->conf.line,
4671 sample_ckp_names(arg->expr->fetch->use),
4672 sample_ckp_names(where));
4673 goto error;
4674 }
4675 }
4676 }
4677
4678 free(engine_id);
4679 free(group_id);
4680 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4681 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4682 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4683 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4684 return 1;
4685
4686 error:
4687 free(engine_id);
4688 free(group_id);
4689 return 0;
4690}
4691
4692/* Parse 'send-spoe-group' action following the format:
4693 *
4694 * ... send-spoe-group <engine-id> <group-id>
4695 *
4696 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4697 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4698 * ids are saved and used later, when the rule will be checked.
4699 */
4700static enum act_parse_ret
4701parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4702 struct act_rule *rule, char **err)
4703{
4704 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4705 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4706 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4707 return ACT_RET_PRS_ERR;
4708 }
4709 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4710 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4711
4712 (*orig_arg) += 2;
4713
4714 rule->action = ACT_CUSTOM;
4715 rule->action_ptr = spoe_send_group;
4716 rule->check_ptr = check_send_spoe_group;
4717 return ACT_RET_PRS_OK;
4718}
4719
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004720
4721/* Declare the filter parser for "spoe" keyword */
4722static struct flt_kw_list flt_kws = { "SPOE", { }, {
4723 { "spoe", parse_spoe_flt, NULL },
4724 { NULL, NULL, NULL },
4725 }
4726};
4727
Willy Tarreau0108d902018-11-25 19:14:37 +01004728INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4729
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004730/* Delcate the action parser for "spoe-action" keyword */
4731static struct action_kw_list tcp_req_action_kws = { { }, {
4732 { "send-spoe-group", parse_send_spoe_group },
4733 { /* END */ },
4734 }
4735};
Willy Tarreau0108d902018-11-25 19:14:37 +01004736
4737INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4738
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004739static struct action_kw_list tcp_res_action_kws = { { }, {
4740 { "send-spoe-group", parse_send_spoe_group },
4741 { /* END */ },
4742 }
4743};
Willy Tarreau0108d902018-11-25 19:14:37 +01004744
4745INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4746
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004747static struct action_kw_list http_req_action_kws = { { }, {
4748 { "send-spoe-group", parse_send_spoe_group },
4749 { /* END */ },
4750 }
4751};
Willy Tarreau0108d902018-11-25 19:14:37 +01004752
4753INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4754
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004755static struct action_kw_list http_res_action_kws = { { }, {
4756 { "send-spoe-group", parse_send_spoe_group },
4757 { /* END */ },
4758 }
4759};
4760
Willy Tarreau0108d902018-11-25 19:14:37 +01004761INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);