blob: 65f20cec19e162a58bb2faf6415441a268f80516 [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>
Willy Tarreauc7babd82020-06-04 21:29:29 +020022#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020023#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020024#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020025#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020026#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020027#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020028#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020029#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020031#include <haproxy/sc_strm.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020032#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/signal.h>
Christopher Faulet1d7d0f82021-02-19 10:56:41 +010034#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020035#include <haproxy/spoe.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020036#include <haproxy/stconn.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 */
Willy Tarreau23a24072022-05-05 20:18:44 +020066#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->svcctx))
Christopher Faulet42bfa462017-01-04 14:14:19 +010067
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 }
Aurelien DARRAGONc6100952023-05-11 12:29:51 +0200144 free_acl_cond(msg->cond);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200145 free(msg);
146}
147
148static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200149spoe_release_group(struct spoe_group *grp)
150{
151 if (!grp)
152 return;
153 free(grp->id);
154 free(grp->conf.file);
155 free(grp);
156}
157
158static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100159spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200160{
Christopher Faulet11610f32017-09-21 10:23:10 +0200161 struct spoe_message *msg, *msgback;
162 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200163 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200164
165 if (!agent)
166 return;
167 free(agent->id);
168 free(agent->conf.file);
169 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100170 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100171 free(agent->var_t_process);
172 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200173 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200174 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200175 spoe_release_message(msg);
176 }
177 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200178 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200179 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200180 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100181 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200182 for (i = 0; i < global.nbthread; ++i) {
183 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200185 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100186 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200187 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200188 free(agent);
189}
190
191static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100192 [SPOE_FRM_ERR_NONE] = "normal",
193 [SPOE_FRM_ERR_IO] = "I/O error",
194 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
195 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
196 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
197 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
198 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
199 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
200 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
201 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
202 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
203 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100204 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100205 [SPOE_FRM_ERR_RES] = "resource allocation error",
206 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200207};
208
209static const char *spoe_event_str[SPOE_EV_EVENTS] = {
210 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
211 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
212 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
213 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
214 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
215
216 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
217 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
218 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
219};
220
221
222#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
223
224static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100225 [SPOE_CTX_ST_NONE] = "NONE",
226 [SPOE_CTX_ST_READY] = "READY",
227 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
228 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
229 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
230 [SPOE_CTX_ST_DONE] = "DONE",
231 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200232};
233
234static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100235 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
236 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
237 [SPOE_APPCTX_ST_IDLE] = "IDLE",
238 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
239 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
240 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
241 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
242 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
243 [SPOE_APPCTX_ST_EXIT] = "EXIT",
244 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200245};
246
247#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100248
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500250 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100251 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100252static char *
253generate_pseudo_uuid()
254{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100255 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800256 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100257}
258
Willy Tarreauaaebcae2023-04-27 11:54:11 +0200259/* set/add to <t> the elapsed time since <since> and now */
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100260static inline void
Willy Tarreauaaebcae2023-04-27 11:54:11 +0200261spoe_update_stat_time(ullong *since, long *t)
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100262{
263 if (*t == -1)
Willy Tarreau69530f52023-04-28 09:16:15 +0200264 *t = ns_to_ms(now_ns - *since);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100265 else
Willy Tarreau69530f52023-04-28 09:16:15 +0200266 *t += ns_to_ms(now_ns - *since);
Willy Tarreauaaebcae2023-04-27 11:54:11 +0200267 *since = 0;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100268}
269
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200270/********************************************************************
271 * Functions that encode/decode SPOE frames
272 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200273/* Helper to get static string length, excluding the terminating null byte */
274#define SLEN(str) (sizeof(str)-1)
275
276/* Predefined key used in HELLO/DISCONNECT frames */
277#define SUPPORTED_VERSIONS_KEY "supported-versions"
278#define VERSION_KEY "version"
279#define MAX_FRAME_SIZE_KEY "max-frame-size"
280#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100281#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100282#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200283#define STATUS_CODE_KEY "status-code"
284#define MSG_KEY "message"
285
286struct spoe_version {
287 char *str;
288 int min;
289 int max;
290};
291
292/* All supported versions */
293static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200294 /* 1.0 is now unsupported because of a bug about frame's flags*/
295 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200296 {NULL, 0, 0}
297};
298
299/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200300#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301
Christopher Faulet8ef75252017-02-20 22:56:03 +0100302/* Convert a string to a SPOE version value. The string must follow the format
303 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
304 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308 const char *p, *end;
309 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311 p = str;
312 end = str+len;
313 maj = min = 0;
314 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200315
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100317 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100318 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200319
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320 /* parse Major number, until the '.' */
321 while (*p != '.') {
322 if (p >= end || *p < '0' || *p > '9')
323 goto out;
324 maj *= 10;
325 maj += (*p - '0');
326 p++;
327 }
328
329 /* check Major version */
330 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200331 goto out;
332
Christopher Faulet8ef75252017-02-20 22:56:03 +0100333 p++; /* skip the '.' */
334 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
335 goto out;
336
337 /* Parse Minor number */
338 while (p < end) {
339 if (*p < '0' || *p > '9')
340 break;
341 min *= 10;
342 min += (*p - '0');
343 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200344 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100345
346 /* check Minor number */
347 if (min > 999)
348 goto out;
349
350 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100351 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100352 p++;
353 if (p != end)
354 goto out;
355
356 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200357 out:
358 return vsn;
359}
360
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800362 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100365spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366{
Willy Tarreau83061a82018-07-13 11:56:34 +0200367 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100368 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100369 char *p, *end;
370 unsigned int flags = SPOE_FRM_FL_FIN;
371 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200372
Christopher Faulet8ef75252017-02-20 22:56:03 +0100373 p = frame;
374 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200375
Christopher Faulet8ef75252017-02-20 22:56:03 +0100376 /* Set Frame type */
377 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200378
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100379 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200380 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100381 memcpy(p, (char *)&flags, 4);
382 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200383
384 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100385 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200386
387 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
388 * and "capabilities" */
389
390 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100391 sz = SLEN(SUPPORTED_VERSIONS_KEY);
392 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
393 goto too_big;
394
395 *p++ = SPOE_DATA_T_STR;
396 sz = SLEN(SUPPORTED_VERSIONS_VAL);
397 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
398 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200399
400 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100401 sz = SLEN(MAX_FRAME_SIZE_KEY);
402 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
403 goto too_big;
404
405 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200406 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100407 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200408
409 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100410 sz = SLEN(CAPABILITIES_KEY);
411 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
412 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
Christopher Faulet8ef75252017-02-20 22:56:03 +0100414 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100415 chk = get_trash_chunk();
416 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200417 memcpy(chk->area, "pipelining", 10);
418 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100419 }
420 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200421 if (chk->data) chk->area[chk->data++] = ',';
422 memcpy(chk->area+chk->data, "async", 5);
423 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100424 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100425 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 if (chk->data) chk->area[chk->data++] = ',';
427 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100428 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100429 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200430 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 goto too_big;
432
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500433 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200434 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100435 sz = SLEN(ENGINE_ID_KEY);
436 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
437 goto too_big;
438
439 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200440 sz = strlen(agent->rt[tid].engine_id);
441 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100442 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100443 }
444
Christopher Faulet8ef75252017-02-20 22:56:03 +0100445 return (p - frame);
446
447 too_big:
448 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
449 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200450}
451
Christopher Faulet8ef75252017-02-20 22:56:03 +0100452/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
453 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
454 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200455static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200457{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458 const char *reason;
459 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100460 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462
Christopher Faulet8ef75252017-02-20 22:56:03 +0100463 p = frame;
464 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200465
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 /* Set Frame type */
467 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200468
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100469 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200470 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100471 memcpy(p, (char *)&flags, 4);
472 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200473
474 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100475 *p++ = 0; *p++ = 0;
476
477 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
478 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479
480 /* There are 2 mandatory items: "status-code" and "message" */
481
482 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100483 sz = SLEN(STATUS_CODE_KEY);
484 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
485 goto too_big;
486
487 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200488 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100489 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200490
491 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100492 sz = SLEN(MSG_KEY);
493 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
494 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
Christopher Faulet8ef75252017-02-20 22:56:03 +0100496 /*Get the message corresponding to the status code */
497 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
498
499 *p++ = SPOE_DATA_T_STR;
500 sz = strlen(reason);
501 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
502 goto too_big;
503
504 return (p - frame);
505
506 too_big:
507 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
508 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200509}
510
Christopher Faulet8ef75252017-02-20 22:56:03 +0100511/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
512 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
513 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200514static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100515spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100516 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200517{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100518 char *p, *end;
519 unsigned int stream_id, frame_id;
520 unsigned int flags = SPOE_FRM_FL_FIN;
521 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 p = frame;
524 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200525
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100526 stream_id = ctx->stream_id;
527 frame_id = ctx->frame_id;
528
529 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
530 /* The fragmentation is not supported by the applet */
531 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
532 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
533 return -1;
534 }
535 flags = ctx->frag_ctx.flags;
536 }
537
538 /* Set Frame type */
539 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
540
541 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200542 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100543 memcpy(p, (char *)&flags, 4);
544 p += 4;
545
546 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200547 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200549 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100550 goto too_big;
551
552 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200553 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100554 if (p + sz >= end)
555 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200556 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100557 p += sz;
558
559 return (p - frame);
560
561 too_big:
562 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
563 return 0;
564}
565
566/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
567 * returns the number of encoded bytes in the frame on success, 0 if an encoding
568 * error occurred and -1 if a fatal error occurred. */
569static int
570spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
571 char *frame, size_t size)
572{
573 char *p, *end;
574 unsigned int stream_id, frame_id;
575 unsigned int flags;
576 size_t sz;
577
578 p = frame;
579 end = frame+size;
580
Christopher Faulet8ef75252017-02-20 22:56:03 +0100581 /* <ctx> is null when the stream has aborted the processing of a
582 * fragmented frame. In this case, we must notify the corresponding
583 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100584 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100585 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100586 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
587 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
588 }
589 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100590 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100591 stream_id = ctx->stream_id;
592 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100593 }
594
Christopher Faulet8ef75252017-02-20 22:56:03 +0100595 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100596 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100598 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200599 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600 memcpy(p, (char *)&flags, 4);
601 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200602
603 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200604 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200606 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200608
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 if (ctx == NULL)
610 goto end;
611
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200613 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 if (p + sz >= end)
615 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200616 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100618
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100619 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100620 return (p - frame);
621
622 too_big:
623 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
624 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200625}
626
Christopher Faulet8ef75252017-02-20 22:56:03 +0100627/* Decode and process the HELLO frame sent by an agent. It returns the number of
628 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
629 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632{
Christopher Faulet305c6072017-02-23 16:17:53 +0100633 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
634 char *p, *end;
635 int vsn, max_frame_size;
636 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100637
638 p = frame;
639 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200640
641 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
643 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 if (size < 7 /* TYPE + METADATA */) {
648 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
649 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200650 }
651
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100652 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200654 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100655 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200656
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100657 /* Fragmentation is not supported for HELLO frame */
658 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100659 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100660 return -1;
661 }
662
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200663 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 if (*p != 0 || *(p+1) != 0) {
665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
666 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100668 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669
670 /* There are 3 mandatory items: "version", "max-frame-size" and
671 * "capabilities" */
672
673 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100674 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100675 while (p < end) {
676 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200677 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100678 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200679
680 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100681 ret = spoe_decode_buffer(&p, end, &str, &sz);
682 if (ret == -1 || !sz) {
683 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
684 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200688 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100689 int i, type = *p++;
690
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
693 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
694 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200695 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100696 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
697 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
698 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200699 }
700
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 return -1;
705 }
706 for (i = 0; supported_versions[i].str != NULL; ++i) {
707 if (vsn >= supported_versions[i].min &&
708 vsn <= supported_versions[i].max)
709 break;
710 }
711 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100712 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200713 return -1;
714 }
715 }
716 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200717 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 +0100718 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719
720 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
722 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
723 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
724 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
726 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200727 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200728 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100729 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
730 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200731 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100732 if (sz < MIN_FRAME_SIZE ||
733 sz > SPOE_APPCTX(appctx)->max_frame_size) {
734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200735 return -1;
736 }
737 max_frame_size = sz;
738 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100739 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200740 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100741 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100742
743 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100744 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
745 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
746 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100748 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
749 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
750 return 0;
751 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752
Christopher Faulet8ef75252017-02-20 22:56:03 +0100753 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100754 char *delim;
755
756 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100757 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100758
Christopher Faulet8ef75252017-02-20 22:56:03 +0100759 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
760 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100761 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100762 flags |= SPOE_APPCTX_FL_PIPELINING;
763 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100764 else if (sz >= 5 && !strncmp(str, "async", 5)) {
765 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100766 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100767 flags |= SPOE_APPCTX_FL_ASYNC;
768 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100769 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
770 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100771 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100772 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
773 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100774
Christopher Faulet8ef75252017-02-20 22:56:03 +0100775 /* Get the next comma or break */
776 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100777 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100778 delim++;
779 sz -= (delim - str);
780 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100781 }
782 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200783 else {
784 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100785 if (spoe_skip_data(&p, end) == -1) {
786 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
787 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200788 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200789 }
790 }
791
792 /* Final checks */
793 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100794 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200795 return -1;
796 }
797 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100798 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200799 return -1;
800 }
Christopher Faulet11389012019-02-07 16:13:26 +0100801 if (!agent)
802 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
803 else {
804 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
805 flags &= ~SPOE_APPCTX_FL_PIPELINING;
806 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
807 flags &= ~SPOE_APPCTX_FL_ASYNC;
808 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200809
Christopher Faulet42bfa462017-01-04 14:14:19 +0100810 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
811 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
812 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100813
814 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200815}
816
817/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
818 * bytes on success, 0 if the frame can be ignored and -1 if an error
819 * occurred. */
820static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200822{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100824 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825
826 p = frame;
827 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200828
829 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100830 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
831 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834
Christopher Faulet8ef75252017-02-20 22:56:03 +0100835 if (size < 7 /* TYPE + METADATA */) {
836 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
837 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200838 }
839
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100840 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200842 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200844
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100845 /* Fragmentation is not supported for DISCONNECT frame */
846 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100847 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100848 return -1;
849 }
850
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200851 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852 if (*p != 0 || *(p+1) != 0) {
853 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
854 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100856 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200857
858 /* There are 2 mandatory items: "status-code" and "message" */
859
860 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 while (p < end) {
862 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200863 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100864 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200865
866 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 ret = spoe_decode_buffer(&p, end, &str, &sz);
868 if (ret == -1 || !sz) {
869 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
870 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871 }
872
873 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200874 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100875 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876
877 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
879 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
880 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
881 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100882 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
883 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200884 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200885 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100886 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
887 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100889 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 }
891
892 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200893 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 int type = *p++;
895
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100897 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
898 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
899 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200900 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100901 ret = spoe_decode_buffer(&p, end, &str, &sz);
902 if (ret == -1 || sz > 255) {
903 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
904 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
907 SPOE_APPCTX(appctx)->reason = str;
908 SPOE_APPCTX(appctx)->rlen = sz;
909#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200910 }
911 else {
912 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 if (spoe_skip_data(&p, end) == -1) {
914 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
915 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200916 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
918 }
919
Christopher Faulet8ef75252017-02-20 22:56:03 +0100920 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921}
922
923
Christopher Fauleta1cda022016-12-21 08:58:06 +0100924/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 * success, 0 if the frame can be ignored and -1 if an error occurred. */
926static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100927spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100928 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200929{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100930 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100932 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100934 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100935
936 p = frame;
937 end = frame + size;
938 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200939
940 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100941 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
942 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200945
Christopher Faulet8ef75252017-02-20 22:56:03 +0100946 if (size < 7 /* TYPE + METADATA */) {
947 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
948 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200949 }
950
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100951 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200953 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200955
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100956 /* Fragmentation is not supported for now */
957 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100958 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100959 return -1;
960 }
961
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200962 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200963 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100965 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200967 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200969 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100971
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100973 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200974 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100975 if ((*ctx)->stream_id == (unsigned int)stream_id &&
976 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100977 goto found;
978 }
979 }
980 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100981 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
982 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100983 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100984 goto found;
985 }
986 }
987
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100988 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
989 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
990 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
991
992 /* ABRT bit is set for an unfinished fragmented frame */
993 if (flags & SPOE_FRM_FL_ABRT) {
994 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100995 (*ctx)->state = SPOE_CTX_ST_ERROR;
996 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
997 /* Ignore the payload */
998 goto end;
999 }
1000 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1001 /* For now, we ignore the ack */
1002 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1003 return 0;
1004 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001005
Christopher Fauleta1cda022016-12-21 08:58:06 +01001006 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001007 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1008 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001009 " - stream-id=%u - frame-id=%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001010 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 __FUNCTION__, appctx,
1012 (unsigned int)stream_id, (unsigned int)frame_id);
1013
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001014 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001015 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1016 /* Report an error if we are waiting the ack for another frame,
1017 * but not if there is no longer frame waiting for a ack
1018 * (timeout)
1019 */
1020 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1021 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1022 return -1;
1023 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1024 SPOE_APPCTX(appctx)->cur_fpa = 0;
1025 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001026 return 0;
1027
1028 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1030 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001032 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001033 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001034
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001035 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001036 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001037 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1038 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001039 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001040
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 /* Transfer the buffer ownership to the SPOE context */
1042 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001043 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001044
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 (*ctx)->state = SPOE_CTX_ST_DONE;
1046
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001047 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001048 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001049 " - ACK frame received"
1050 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001051 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001052 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1053 (*ctx)->frame_id, flags);
1054 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001055}
1056
Christopher Fauletba7bc162016-11-07 21:07:38 +01001057/* This function is used in cfgparse.c and declared in proto/checks.h. It
1058 * prepare the request to send to agents during a healthcheck. It returns 0 on
1059 * success and -1 if an error occurred. */
1060int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001062{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 struct appctx appctx;
1064 struct spoe_appctx spoe_appctx;
1065 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1066 size_t sz;
1067 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet42bfa462017-01-04 14:14:19 +01001069 memset(&appctx, 0, sizeof(appctx));
1070 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001072
Willy Tarreau23a24072022-05-05 20:18:44 +02001073 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001074 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001075
Christopher Faulet8ef75252017-02-20 22:56:03 +01001076 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1077 end = frame + MAX_FRAME_SIZE;
1078
1079 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1080 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001083
Christopher Faulet8ef75252017-02-20 22:56:03 +01001084 /* Add "healthcheck" K/V item */
1085 sz = SLEN(HEALTHCHECK_KEY);
1086 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1087 return -1;
1088 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 *len = frame - buf;
1091 sz = htonl(*len - 4);
1092 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001096 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001097 return 0;
1098}
1099
1100/* This function is used in checks.c and declared in proto/checks.h. It decode
1101 * the response received from an agent during a healthcheck. It returns 0 on
1102 * success and -1 if an error occurred. */
1103int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001104spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001105{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001106 struct appctx appctx;
1107 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108
Christopher Faulet42bfa462017-01-04 14:14:19 +01001109 memset(&appctx, 0, sizeof(appctx));
1110 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001111
Willy Tarreau23a24072022-05-05 20:18:44 +02001112 appctx.svcctx = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001113 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001114
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1116 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117 goto error;
1118 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001119 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1120 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121
1122 return 0;
1123
1124 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001125 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1126 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1127 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 return -1;
1129}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001130
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001132 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133 * success. */
1134static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001135spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001137 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001138 int ret;
1139 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001140
Christopher Faulet8ef75252017-02-20 22:56:03 +01001141 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1142 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001143 netint = htonl(framesz);
1144 memcpy(buf, (char *)&netint, 4);
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001145 ret = applet_putblk(appctx, buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001146 if (ret <= 0) {
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001147 if (ret == -3 && b_is_null(&sc_ic(sc)->buf)) {
Willy Tarreaud0a06d52022-05-18 15:07:19 +02001148 /* WT: is this still needed for the case ret==-3 ? */
Christopher Faulet7b3d38a2023-05-05 11:28:45 +02001149 sc_need_room(sc, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001151 }
1152 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001153 return -1; /* error */
1154 }
1155 return framesz;
1156}
1157
1158/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1159 * when the frame can be ignored, 1 to retry later and the frame length on
1160 * success. */
1161static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001162spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001163{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001164 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001165 int ret;
1166 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001168 ret = co_getblk(sc_oc(sc), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001169 if (ret > 0) {
1170 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001171 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001172 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173 return -1;
1174 }
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001175 ret = co_getblk(sc_oc(sc), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001176 }
1177 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001178 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001180 }
1181 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001182 return -1; /* error */
1183 }
1184 return framesz;
1185}
1186
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001187/********************************************************************
1188 * Functions that manage the SPOE applet
1189 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001190static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001191spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001192{
Willy Tarreau75a8f8e2022-05-25 18:12:11 +02001193 applet_will_consume(appctx);
Willy Tarreau4164eb92022-05-25 15:42:03 +02001194 applet_have_more_data(appctx);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001195 appctx_wakeup(appctx);
1196 return 1;
1197}
1198
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001199/* Callback function that catches applet timeouts. If a timeout occurred, we set
1200 * <appctx->st1> flag and the SPOE applet is woken up. */
1201static struct task *
Willy Tarreau144f84a2021-03-02 16:09:26 +01001202spoe_process_appctx(struct task * task, void *context, unsigned int state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001203{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001204 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205
1206 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1207 if (tick_is_expired(task->expire, now_ms)) {
1208 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001209 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001210 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001211 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001212 return task;
1213}
1214
Christopher Faulet69ebc302022-05-12 15:28:51 +02001215static int
1216spoe_init_appctx(struct appctx *appctx)
1217{
1218 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1219 struct spoe_agent *agent = spoe_appctx->agent;
1220 struct task *task;
1221 struct stream *s;
1222
1223 if ((task = task_new_here()) == NULL)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001224 goto out_error;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001225 task->process = spoe_process_appctx;
1226 task->context = appctx;
1227
1228 if (appctx_finalize_startup(appctx, &agent->spoe_conf->agent_fe, &BUF_NULL) == -1)
Christopher Fauletfa463af2022-05-18 07:42:49 +02001229 goto out_free_task;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001230
1231 spoe_appctx->owner = appctx;
1232 spoe_appctx->task = task;
1233
1234 LIST_INIT(&spoe_appctx->buffer_wait.list);
1235 spoe_appctx->buffer_wait.target = appctx;
1236 spoe_appctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
1237
1238 s = appctx_strm(appctx);
1239 stream_set_backend(s, agent->b.be);
1240
1241 /* applet is waiting for data */
Willy Tarreau90e8b452022-05-25 18:21:43 +02001242 applet_need_more_data(appctx);
Christopher Faulet69ebc302022-05-12 15:28:51 +02001243
1244 s->do_log = NULL;
Christopher Faulet9a790f62023-03-16 14:40:03 +01001245 s->scb->flags |= SC_FL_RCV_ONCE;
Christopher Faulet69ebc302022-05-12 15:28:51 +02001246
1247 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1248 LIST_APPEND(&agent->rt[tid].applets, &spoe_appctx->list);
1249 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
1250 _HA_ATOMIC_INC(&agent->counters.applets);
1251
1252 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
1253 task_wakeup(spoe_appctx->task, TASK_WOKEN_INIT);
1254 return 0;
1255 out_free_task:
1256 task_destroy(task);
1257 out_error:
1258 return -1;
1259}
1260
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001261/* Callback function that releases a SPOE applet. This happens when the
1262 * connection with the agent is closed. */
1263static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001264spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001265{
Christopher Faulet908628c2022-03-25 16:43:49 +01001266 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1267 struct spoe_agent *agent;
1268 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269
1270 if (spoe_appctx == NULL)
1271 return;
1272
Willy Tarreau23a24072022-05-05 20:18:44 +02001273 appctx->svcctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001274 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001275
1276 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001277 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001278 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001279
Christopher Faulet8ef75252017-02-20 22:56:03 +01001280 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001281 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001282 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001284 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001285 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001286 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001287 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001290 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001291 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1292 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001293 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001294 agent->rt[tid].idles--;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001295 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001296
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001297 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1299 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300 }
1301
Christopher Faulet8ef75252017-02-20 22:56:03 +01001302 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001303 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001304
Christopher Faulet42a06622022-08-25 18:50:18 +02001305 /* Report an error to all streams in the appctx waiting queue */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001306 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001307 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001308 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001309 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001310 spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001311 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001312 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001313 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001314 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001315 }
1316
Christopher Faulet42a06622022-08-25 18:50:18 +02001317 /* If the applet was processing a fragmented frame, report an error to
1318 * the corresponding stream. */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 if (spoe_appctx->frag_ctx.ctx) {
1320 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001321 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001322 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001323 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001324 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1325 }
1326
Christopher Faulet42a06622022-08-25 18:50:18 +02001327 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1328 /* If there are still some running applets, remove reference on
1329 * the current one from streams in the async waiting queue. In
1330 * async mode, the ACK may be received from another appctx.
1331 */
Christopher Fauletcf181c72020-11-10 18:45:34 +01001332 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1333 if (ctx->spoe_appctx == spoe_appctx)
1334 ctx->spoe_appctx = NULL;
1335 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001336 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001337 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001338 else {
Christopher Faulet42a06622022-08-25 18:50:18 +02001339 /* It is the last running applet and the sending and async
1340 * waiting queues are not empty. So try to start a new applet if
1341 * HAproxy is not stopping. On success, we remove reference on
1342 * the current appctx from streams in the async waiting queue.
1343 * In async mode, the ACK may be received from another appctx.
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001344 */
1345 if (!stopping &&
1346 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
Christopher Faulet42a06622022-08-25 18:50:18 +02001347 spoe_create_appctx(agent->spoe_conf)) {
1348 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1349 if (ctx->spoe_appctx == spoe_appctx)
1350 ctx->spoe_appctx = NULL;
1351 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001352 goto end;
Christopher Faulet42a06622022-08-25 18:50:18 +02001353 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001354
Christopher Faulet42a06622022-08-25 18:50:18 +02001355 /* Otherwise, report an error to all streams in the sending and
1356 * async waiting queues.
1357 */
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001358 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1359 LIST_DELETE(&ctx->list);
1360 LIST_INIT(&ctx->list);
1361 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001362 spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue);
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001363 ctx->spoe_appctx = NULL;
1364 ctx->state = SPOE_CTX_ST_ERROR;
1365 ctx->status_code = (spoe_appctx->status_code + 0x100);
1366 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1367 }
1368 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1369 LIST_DELETE(&ctx->list);
1370 LIST_INIT(&ctx->list);
1371 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001372 spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting);
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001373 ctx->spoe_appctx = NULL;
1374 ctx->state = SPOE_CTX_ST_ERROR;
1375 ctx->status_code = (spoe_appctx->status_code + 0x100);
1376 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1377 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001379
1380 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001381 /* Release allocated memory */
1382 spoe_release_buffer(&spoe_appctx->buffer,
1383 &spoe_appctx->buffer_wait);
1384 pool_free(pool_head_spoe_appctx, spoe_appctx);
1385
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001386 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001387 agent->rt[tid].frame_size = agent->max_frame_size;
1388 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1389 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001390}
1391
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001392static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001393spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001394{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001395 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001396 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001397 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001398 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001399
Christopher Fauletd550d262023-03-31 11:04:34 +02001400 /* if the connection is not established, inform the stream that we want
1401 * to be notified whenever the connection completes.
1402 */
1403 if (sc_opposite(sc)->state < SC_ST_EST) {
1404 applet_need_more_data(appctx);
1405 se_need_remote_conn(appctx->sedesc);
1406 applet_have_more_data(appctx);
1407 goto stop;
1408 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001409
Christopher Fauleta1cda022016-12-21 08:58:06 +01001410 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001411 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1412 " - Connection timed out\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001413 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 __FUNCTION__, appctx);
1415 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001417 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001418
Christopher Faulet42bfa462017-01-04 14:14:19 +01001419 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001420 SPOE_APPCTX(appctx)->task->expire =
1421 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001422
Christopher Faulet8ef75252017-02-20 22:56:03 +01001423 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1424 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001425 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001426 ret = spoe_prepare_hahello_frame(appctx, frame,
1427 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001429 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001430
1431 switch (ret) {
1432 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001433 case 0: /* ignore => an error, cannot be ignored */
1434 goto exit;
1435
1436 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 goto stop;
1438
Christopher Faulet8ef75252017-02-20 22:56:03 +01001439 default:
1440 /* HELLO frame successfully sent, now wait for the
1441 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001442 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1443 goto next;
1444 }
1445
1446 next:
1447 return 0;
1448 stop:
1449 return 1;
1450 exit:
1451 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1452 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001453}
1454
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001455static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001456spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001458 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001459 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001460 char *frame;
1461 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001462
Christopher Fauleta1cda022016-12-21 08:58:06 +01001463 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001464 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1465 " - Connection timed out\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001466 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001467 __FUNCTION__, appctx);
1468 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001469 goto exit;
1470 }
1471
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001472 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001473 ret = spoe_recv_frame(appctx, frame,
1474 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001475 if (ret > 1) {
1476 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1477 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1478 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001479 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001480 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001481 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001482 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001483
Christopher Fauleta1cda022016-12-21 08:58:06 +01001484 switch (ret) {
1485 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001486 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001487 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1488 goto next;
1489
1490 case 1: /* retry later */
1491 goto stop;
1492
1493 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001494 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001495 agent->rt[tid].idles++;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001496 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001497 SPOE_APPCTX(appctx)->node.key = 0;
1498 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001499
1500 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001501 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001502 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001503 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001504
Christopher Fauleta1cda022016-12-21 08:58:06 +01001505 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001507 if (trash.data)
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001508 co_skip(sc_oc(sc), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001509
1510 SPOE_APPCTX(appctx)->task->expire =
1511 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001512 return 0;
1513 stop:
1514 return 1;
1515 exit:
1516 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1517 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001518}
1519
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001520
Christopher Fauleta1cda022016-12-21 08:58:06 +01001521static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001522spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001523{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001524 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1525 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001526 char *frame, *buf;
1527 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001528
Christopher Faulet8ef75252017-02-20 22:56:03 +01001529 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1530 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001531 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001532
1533 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1534 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1535 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1536 SPOE_APPCTX(appctx)->max_frame_size);
1537 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001538 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001539 *skip = 1;
1540 ret = 1;
1541 goto end;
1542 }
1543 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001544 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001545 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1546 SPOE_APPCTX(appctx)->max_frame_size);
1547
1548 }
1549
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 if (ret > 1)
1551 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001552
Christopher Faulet8ef75252017-02-20 22:56:03 +01001553 switch (ret) {
1554 case -1: /* error */
1555 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1556 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001557
Christopher Faulet8ef75252017-02-20 22:56:03 +01001558 case 0: /* ignore */
1559 if (ctx == NULL)
1560 goto abort_frag_frame;
1561
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001562 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001563 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001564 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001565 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001566 spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001567 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001568 ctx->state = SPOE_CTX_ST_ERROR;
1569 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1570 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001571 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001572 break;
1573
1574 case 1: /* retry */
1575 *skip = 1;
1576 break;
1577
1578 default:
1579 if (ctx == NULL)
1580 goto abort_frag_frame;
1581
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001582 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001583 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001584 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001585 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001586 spoe_update_stat_time(&ctx->stats.queue_ts, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001587 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001588 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1589 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1590 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001591 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001592 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001593 }
1594 goto end;
1595
1596 frag_frame_sent:
1597 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001598 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001599 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1600 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1601 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001602 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1603 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1604 goto end;
1605
1606 no_frag_frame_sent:
1607 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1608 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001609 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001610 }
1611 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1612 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001613 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001614 }
1615 else {
1616 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001617 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001618 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001619 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001620 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Willy Tarreau69530f52023-04-28 09:16:15 +02001621 ctx->stats.wait_ts = now_ns;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001622 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1623 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1624 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001625 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001626
Christopher Faulet8ef75252017-02-20 22:56:03 +01001627 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1628 goto end;
1629
1630 abort_frag_frame:
1631 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1632 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1633 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1634 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1635 goto end;
1636
1637 end:
1638 return ret;
1639}
1640
1641static int
1642spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1643{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001644 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001645 struct spoe_context *ctx = NULL;
1646 char *frame;
1647 int ret;
1648
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001649 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001650 ret = spoe_recv_frame(appctx, frame,
1651 SPOE_APPCTX(appctx)->max_frame_size);
1652 if (ret > 1) {
1653 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1654 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001655 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001656 goto end;
1657 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001658 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001659 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1660 }
1661 switch (ret) {
1662 case -1: /* error */
1663 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1664 break;
1665
1666 case 0: /* ignore */
1667 break;
1668
1669 case 1: /* retry */
1670 *skip = 1;
1671 break;
1672
1673 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001674 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001675 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001676 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02001677 spoe_update_stat_time(&ctx->stats.wait_ts, &ctx->stats.t_waiting);
Willy Tarreau69530f52023-04-28 09:16:15 +02001678 ctx->stats.response_ts = now_ns;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001679 if (ctx->spoe_appctx) {
1680 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001681 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001682 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001683 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1684 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1685 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1686 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1687 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1688 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1689 }
1690 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1691 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001692 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1693 break;
1694 }
1695
1696 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001697 if (trash.data)
Willy Tarreauc12b3212022-05-27 11:08:15 +02001698 co_skip(sc_oc(appctx_sc(appctx)), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001699 end:
1700 return ret;
1701}
1702
1703static int
1704spoe_handle_processing_appctx(struct appctx *appctx)
1705{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001706 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001707 struct server *srv = objt_server(__sc_strm(sc)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001708 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001709 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001710
Christopher Faulet8ef75252017-02-20 22:56:03 +01001711 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1712 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1713 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1714 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1715 goto next;
1716 }
1717
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001718 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001719 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001720 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001721 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1722 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001723 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001724
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001725
1726 /* Close the applet ASAP because some sessions are waiting for a free
1727 * connection slot. It is only an issue in multithreaded mode.
1728 */
1729 close_asap = (global.nbthread > 1 &&
1730 (agent->b.be->queue.length ||
1731 (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1732
Christopher Faulet8f82b202018-01-24 16:23:03 +01001733 /* receiving_frame loop */
1734 while (!skip_receiving) {
1735 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1736 switch (ret) {
1737 case -1: /* error */
1738 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001739
Christopher Faulet8f82b202018-01-24 16:23:03 +01001740 case 0: /* ignore */
1741 active_r = 1;
1742 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001743
Christopher Faulet8f82b202018-01-24 16:23:03 +01001744 case 1: /* retry */
1745 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001746
Christopher Faulet8f82b202018-01-24 16:23:03 +01001747 default:
1748 active_r = 1;
1749 break;
1750 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001751 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001752
Christopher Fauletd4349b82023-06-05 08:15:59 +02001753 /* Don"t try to send new frame we are waiting for at lease a ack, in
1754 * sync mode or if applet must be closed ASAP
1755 */
1756 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
1757 skip_sending = 1;
1758
Christopher Faulet8f82b202018-01-24 16:23:03 +01001759 /* send_frame loop */
1760 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1761 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1762 switch (ret) {
1763 case -1: /* error */
1764 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765
Christopher Faulet8f82b202018-01-24 16:23:03 +01001766 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001767 if (SPOE_APPCTX(appctx)->node.key)
1768 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001769 active_s++;
1770 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771
Christopher Faulet8f82b202018-01-24 16:23:03 +01001772 case 1: /* retry */
1773 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774
Christopher Faulet8f82b202018-01-24 16:23:03 +01001775 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001776 if (SPOE_APPCTX(appctx)->node.key)
1777 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001778 active_s++;
1779 break;
1780 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001781
1782 /* if applet must be close ASAP, don't send more than a frame */
1783 if (close_asap)
1784 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001786
Christopher Faulet8f82b202018-01-24 16:23:03 +01001787 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001788 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1789 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1790 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001791
Christopher Faulet8f82b202018-01-24 16:23:03 +01001792 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001793 /* If applet must be closed, don't switch it in IDLE state and
1794 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001795 */
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001796 if (close_asap) {
1797 if (SPOE_APPCTX(appctx)->cur_fpa)
1798 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001799 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1800 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1801 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1802 goto next;
1803 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001804 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001805 agent->rt[tid].idles++;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001806 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001807 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001808 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001809
1810 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811 return 1;
1812
Christopher Faulet8f82b202018-01-24 16:23:03 +01001813 next:
1814 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1815 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816}
1817
1818static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001819spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820{
Christopher Faulet908628c2022-03-25 16:43:49 +01001821 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001822 char *frame, *buf;
1823 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001824
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1826 goto exit;
1827
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1829 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001830 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001831 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1832 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835
1836 switch (ret) {
1837 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001838 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001839 goto exit;
1840
1841 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001842 goto stop;
1843
1844 default:
1845 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1846 " - disconnected by HAProxy (%d): %s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001847 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848 __FUNCTION__, appctx,
1849 SPOE_APPCTX(appctx)->status_code,
1850 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001851
1852 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1853 goto next;
1854 }
1855
1856 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001857 SPOE_APPCTX(appctx)->task->expire =
1858 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 return 0;
1860 stop:
1861 return 1;
1862 exit:
1863 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1864 return 0;
1865}
1866
1867static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001868spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001869{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001870 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 char *frame;
1872 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873
Christopher Fauletb067b062017-01-04 16:39:11 +01001874 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001875 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001877 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001879 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001880 ret = spoe_recv_frame(appctx, frame,
1881 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001882 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001883 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001884 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001885 }
1886
1887 switch (ret) {
1888 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001889 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1890 " - error on frame (%s)\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001891 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001892 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001893 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto exit;
1896
1897 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001898 goto next;
1899
1900 case 1: /* retry */
1901 goto stop;
1902
1903 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001905 " - disconnected by peer (%d): %.*s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001906 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001907 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1909 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 goto exit;
1911 }
1912
1913 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001914 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001915 if (trash.data)
Willy Tarreau9002a2e2022-05-27 10:34:25 +02001916 co_skip(sc_oc(sc), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001917
Christopher Fauleta1cda022016-12-21 08:58:06 +01001918 return 0;
1919 stop:
1920 return 1;
1921 exit:
1922 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1923 return 0;
1924}
1925
1926/* I/O Handler processing messages exchanged with the agent */
1927static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001928spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001930 struct stconn *sc = appctx_sc(appctx);
Christopher Faulet908628c2022-03-25 16:43:49 +01001931 struct spoe_agent *agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001932
1933 if (SPOE_APPCTX(appctx) == NULL)
1934 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001935
Christopher Fauletd550d262023-03-31 11:04:34 +02001936 if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) {
1937 co_skip(sc_oc(sc), co_data(sc_oc(sc)));
1938 goto out;
1939 }
1940
Christopher Faulet8ef75252017-02-20 22:56:03 +01001941 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1942 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001943
Christopher Fauleta1cda022016-12-21 08:58:06 +01001944 switchstate:
1945 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1946 " - appctx-state=%s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02001947 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001948 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1949
1950 switch (appctx->st0) {
1951 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001952 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001953 goto out;
1954 goto switchstate;
1955
1956 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001957 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001958 goto out;
1959 goto switchstate;
1960
1961 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001962 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulete99c4392023-04-26 15:56:59 +02001963 agent->rt[tid].idles--;
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001964 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001965 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001966 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001967 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001968 SPOE_APPCTX(appctx)->task->expire =
1969 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001970 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971 goto switchstate;
1972 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001973 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau3064f522022-11-14 07:22:14 +01001974 __fallthrough;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001975
1976 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001977 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1978 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001979 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001980 goto out;
1981 goto switchstate;
1982
1983 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001984 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001985 goto out;
1986 goto switchstate;
1987
1988 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001989 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001990 goto out;
1991 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992
1993 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001994 appctx->st0 = SPOE_APPCTX_ST_END;
1995 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
Christopher Fauletd550d262023-03-31 11:04:34 +02001996 se_fl_set(appctx->sedesc, SE_FL_EOS);
1997 if (SPOE_APPCTX(appctx)->status_code != SPOE_FRM_ERR_NONE)
1998 se_fl_set(appctx->sedesc, SE_FL_ERROR);
1999 else
2000 se_fl_set(appctx->sedesc, SE_FL_EOI);
Willy Tarreau3064f522022-11-14 07:22:14 +01002001 __fallthrough;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002
2003 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002004 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002006 out:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002007 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
2008 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002009}
2010
2011struct applet spoe_applet = {
2012 .obj_type = OBJ_TYPE_APPLET,
2013 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01002014 .fct = spoe_handle_appctx,
Christopher Faulet69ebc302022-05-12 15:28:51 +02002015 .init = spoe_init_appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002016 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002017};
2018
2019/* Create a SPOE applet. On success, the created applet is returned, else
2020 * NULL. */
2021static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01002022spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023{
Christopher Faulet69ebc302022-05-12 15:28:51 +02002024 struct spoe_appctx *spoe_appctx;
2025 struct appctx *appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026
Christopher Faulet69ebc302022-05-12 15:28:51 +02002027 spoe_appctx = pool_zalloc(pool_head_spoe_appctx);
2028 if (spoe_appctx == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029 goto out_error;
2030
Christopher Faulet69ebc302022-05-12 15:28:51 +02002031 spoe_appctx->agent = conf->agent;
2032 spoe_appctx->version = 0;
2033 spoe_appctx->max_frame_size = conf->agent->max_frame_size;
2034 spoe_appctx->flags = 0;
2035 spoe_appctx->status_code = SPOE_FRM_ERR_NONE;
2036 spoe_appctx->buffer = BUF_NULL;
2037 spoe_appctx->cur_fpa = 0;
2038 LIST_INIT(&spoe_appctx->list);
2039 LIST_INIT(&spoe_appctx->waiting_queue);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002040
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002041
Christopher Faulet6095d572022-05-16 17:09:48 +02002042 if ((appctx = appctx_new_here(&spoe_applet, NULL)) == NULL)
Christopher Faulet69ebc302022-05-12 15:28:51 +02002043 goto out_free_spoe_appctx;
Christopher Faulet13a35e52021-12-20 15:34:16 +01002044
Christopher Faulet69ebc302022-05-12 15:28:51 +02002045 appctx->svcctx = spoe_appctx;
2046 if (appctx_init(appctx) == -1)
2047 goto out_free_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002048
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049 appctx_wakeup(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002050 return appctx;
2051
2052 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002053 out_free_appctx:
Christopher Faulet69ebc302022-05-12 15:28:51 +02002054 appctx_free_on_early_error(appctx);
2055 out_free_spoe_appctx:
2056 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002057 out_error:
2058 return NULL;
2059}
2060
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002061static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002062spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002063{
2064 struct spoe_config *conf = FLT_CONF(ctx->filter);
2065 struct spoe_agent *agent = conf->agent;
2066 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002067 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068
Christopher Fauleta1cda022016-12-21 08:58:06 +01002069 /* Check if we need to create a new SPOE applet or not. */
Christopher Faulete99c4392023-04-26 15:56:59 +02002070 if (agent->rt[tid].processing < agent->rt[tid].idles ||
2071 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002072 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002073
2074 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002075 " - try to create new SPOE appctx\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002076 (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002077 ctx->strm);
2078
Christopher Fauleta1cda022016-12-21 08:58:06 +01002079 /* Do not try to create a new applet if there is no server up for the
2080 * agent's backend. */
2081 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2082 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2083 " - cannot create SPOE appctx: no server up\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002084 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01002085 __FUNCTION__, ctx->strm);
2086 goto end;
2087 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002088
Christopher Fauleta1cda022016-12-21 08:58:06 +01002089 /* Do not try to create a new applet if we have reached the maximum of
2090 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002091 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002092 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002093 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2094 " - cannot create SPOE appctx: max CPS reached\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002095 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01002096 __FUNCTION__, ctx->strm);
2097 goto end;
2098 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002099 }
2100
Christopher Faulet8ef75252017-02-20 22:56:03 +01002101 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002102 if (appctx == NULL) {
2103 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2104 " - failed to create SPOE appctx\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002105 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01002106 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002107 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002108 "SPOE: [%s] failed to create SPOE applet\n",
2109 agent->id);
2110
Christopher Fauleta1cda022016-12-21 08:58:06 +01002111 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002112 }
2113
Christopher Fauleta1cda022016-12-21 08:58:06 +01002114 /* Increase the per-process number of cumulated connections */
2115 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002116 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002117
Christopher Fauleta1cda022016-12-21 08:58:06 +01002118 end:
2119 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002120 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002121 ctx->status_code = SPOE_CTX_ERR_RES;
2122 return -1;
2123 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002124
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002125 /* Add the SPOE context in the sending queue if the stream has no applet
2126 * already assigned and wakeup all idle applets. Otherwise, don't queue
2127 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002128 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002129 spoe_update_stat_time(&ctx->stats.request_ts, &ctx->stats.t_request);
Willy Tarreau69530f52023-04-28 09:16:15 +02002130 ctx->stats.queue_ts = now_ns;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002131 if (ctx->spoe_appctx)
2132 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002133 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002134
2135 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002136 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002137 " - applets=%u - idles=%u - processing=%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002138 (int)date.tv_sec, (int)date.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002139 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002140 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002141
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002142 /* Finally try to wakeup an IDLE applet. */
2143 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2144 struct eb32_node *node;
2145
2146 node = eb32_first(&agent->rt[tid].idle_applets);
2147 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2148 if (node && spoe_appctx) {
2149 eb32_delete(&spoe_appctx->node);
2150 spoe_appctx->node.key++;
2151 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2152 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002153 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002154 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002155 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002156}
2157
2158/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002159 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002160 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002161/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2162 * fragmented_content. If the next message can be processed, it returns 0. If
2163 * the message is too big, it returns -1.*/
2164static int
2165spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2166 struct spoe_message *msg, int dir,
2167 char **buf, char *end)
2168{
2169 struct sample *smp;
2170 struct spoe_arg *arg;
2171 int ret;
2172
2173 if (msg->cond) {
2174 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2175 ret = acl_pass(ret);
2176 if (msg->cond->pol == ACL_COND_UNLESS)
2177 ret = !ret;
2178
2179 /* the rule does not match */
2180 if (!ret)
2181 goto next;
2182 }
2183
2184 /* Resume encoding of a SPOE argument */
2185 if (ctx->frag_ctx.curarg != NULL) {
2186 arg = ctx->frag_ctx.curarg;
2187 goto encode_argument;
2188 }
2189
2190 if (ctx->frag_ctx.curoff != UINT_MAX)
2191 goto encode_msg_payload;
2192
2193 /* Check if there is enough space for the message name and the
2194 * number of arguments. It implies <msg->id_len> is encoded on 2
2195 * bytes, at most (< 2288). */
2196 if (*buf + 2 + msg->id_len + 1 > end)
2197 goto too_big;
2198
2199 /* Encode the message name */
2200 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2201 goto too_big;
2202
2203 /* Set the number of arguments for this message */
2204 **buf = msg->nargs;
2205 (*buf)++;
2206
2207 ctx->frag_ctx.curoff = 0;
2208 encode_msg_payload:
2209
2210 /* Loop on arguments */
2211 list_for_each_entry(arg, &msg->args, list) {
2212 ctx->frag_ctx.curarg = arg;
2213 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002214 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002215
2216 encode_argument:
2217 if (ctx->frag_ctx.curoff != UINT_MAX)
2218 goto encode_arg_value;
2219
Joseph Herlantf7f60312018-11-15 13:46:49 -08002220 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002221 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2222 goto too_big;
2223
2224 ctx->frag_ctx.curoff = 0;
2225 encode_arg_value:
2226
Joseph Herlantf7f60312018-11-15 13:46:49 -08002227 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002228 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002229 if (smp) {
2230 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2231 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2232 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002233 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002234 if (ret == -1 || ctx->frag_ctx.curoff)
2235 goto too_big;
2236 }
2237
2238 next:
2239 return 0;
2240
2241 too_big:
2242 return -1;
2243}
2244
Christopher Fauletc718b822017-09-21 16:50:56 +02002245/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2246 * handle fragmented content. On success it returns 1. If an error occurred, -1
2247 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2248 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002249static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002250spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002251 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002252{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002253 struct spoe_config *conf = FLT_CONF(ctx->filter);
2254 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002255 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002256 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002257
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002258 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002259 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002260
Christopher Fauletc718b822017-09-21 16:50:56 +02002261 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2262 /* Resume encoding of a SPOE message */
2263 if (ctx->frag_ctx.curmsg != NULL) {
2264 msg = ctx->frag_ctx.curmsg;
2265 goto encode_evt_message;
2266 }
2267
2268 list_for_each_entry(msg, messages, by_evt) {
2269 ctx->frag_ctx.curmsg = msg;
2270 ctx->frag_ctx.curarg = NULL;
2271 ctx->frag_ctx.curoff = UINT_MAX;
2272
2273 encode_evt_message:
2274 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2275 goto too_big;
2276 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002277 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002278 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2279 /* Resume encoding of a SPOE message */
2280 if (ctx->frag_ctx.curmsg != NULL) {
2281 msg = ctx->frag_ctx.curmsg;
2282 goto encode_grp_message;
2283 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002284
Christopher Fauletc718b822017-09-21 16:50:56 +02002285 list_for_each_entry(msg, messages, by_grp) {
2286 ctx->frag_ctx.curmsg = msg;
2287 ctx->frag_ctx.curarg = NULL;
2288 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002289
Christopher Fauletc718b822017-09-21 16:50:56 +02002290 encode_grp_message:
2291 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2292 goto too_big;
2293 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002294 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002295 else
2296 goto skip;
2297
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002298
Christopher Faulet57583e42017-09-04 15:41:09 +02002299 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002300 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002301 goto skip;
2302
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002303 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002304 " - encode %s messages - spoe_appctx=%p"
2305 "- max_size=%u - encoded=%ld\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002306 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002307 agent->id, __FUNCTION__, s,
2308 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002309 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002310 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002311
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002312 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002313 ctx->frag_ctx.curmsg = NULL;
2314 ctx->frag_ctx.curarg = NULL;
2315 ctx->frag_ctx.curoff = 0;
2316 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002317
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002318 return 1;
2319
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002320 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002321 /* Return an error if fragmentation is unsupported or if nothing has
2322 * been encoded because its too big and not splittable. */
2323 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002324 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2325 return -1;
2326 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002327
2328 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002329 " - encode fragmented messages - spoe_appctx=%p"
2330 " - curmsg=%p - curarg=%p - curoff=%u"
2331 " - max_size=%u - encoded=%ld\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002332 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002333 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002334 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002335 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002336
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002337 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002338 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2339 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2340 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002341
2342 skip:
2343 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2344 " - skip the frame because nothing has been encoded\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002345 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet57583e42017-09-04 15:41:09 +02002346 agent->id, __FUNCTION__, s);
2347 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002348}
2349
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002350
2351/***************************************************************************
2352 * Functions that handle SPOE actions
2353 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002354/* Helper function to set a variable */
2355static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002357 struct sample *smp)
2358{
2359 struct spoe_config *conf = FLT_CONF(ctx->filter);
2360 struct spoe_agent *agent = conf->agent;
2361 char varname[64];
2362
2363 memset(varname, 0, sizeof(varname));
2364 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2365 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002366 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2367 vars_set_by_name(varname, len, smp);
2368 else
2369 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002370}
2371
2372/* Helper function to unset a variable */
2373static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375 struct sample *smp)
2376{
2377 struct spoe_config *conf = FLT_CONF(ctx->filter);
2378 struct spoe_agent *agent = conf->agent;
2379 char varname[64];
2380
2381 memset(varname, 0, sizeof(varname));
2382 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2383 scope, agent->var_pfx, len, name);
2384 vars_unset_by_name_ifexist(varname, len, smp);
2385}
2386
2387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388static inline int
2389spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2390 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002391{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002392 char *str, *scope, *p = *buf;
2393 struct sample smp;
2394 uint64_t sz;
2395 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397 if (p + 2 >= end)
2398 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002399
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400 /* SET-VAR requires 3 arguments */
2401 if (*p++ != 3)
2402 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 switch (*p++) {
2405 case SPOE_SCOPE_PROC: scope = "proc"; break;
2406 case SPOE_SCOPE_SESS: scope = "sess"; break;
2407 case SPOE_SCOPE_TXN : scope = "txn"; break;
2408 case SPOE_SCOPE_REQ : scope = "req"; break;
2409 case SPOE_SCOPE_RES : scope = "res"; break;
2410 default: goto skip;
2411 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2414 goto skip;
2415 memset(&smp, 0, sizeof(smp));
2416 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 if (spoe_decode_data(&p, end, &smp) == -1)
2419 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002420
Christopher Faulet8ef75252017-02-20 22:56:03 +01002421 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2422 " - set-var '%s.%s.%.*s'\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002423 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002424 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2425 __FUNCTION__, s, scope,
2426 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2427 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428
Christopher Faulet2469eba2020-10-15 16:08:30 +02002429 if (smp.data.type == SMP_T_ANY)
2430 spoe_unset_var(ctx, scope, str, sz, &smp);
2431 else
2432 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002433
Christopher Faulet8ef75252017-02-20 22:56:03 +01002434 ret = (p - *buf);
2435 *buf = p;
2436 return ret;
2437 skip:
2438 return 0;
2439}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002440
Christopher Faulet8ef75252017-02-20 22:56:03 +01002441static inline int
2442spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2443 char **buf, char *end, int dir)
2444{
2445 char *str, *scope, *p = *buf;
2446 struct sample smp;
2447 uint64_t sz;
2448 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002449
Christopher Faulet8ef75252017-02-20 22:56:03 +01002450 if (p + 2 >= end)
2451 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002452
Christopher Faulet8ef75252017-02-20 22:56:03 +01002453 /* UNSET-VAR requires 2 arguments */
2454 if (*p++ != 2)
2455 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002456
Christopher Faulet8ef75252017-02-20 22:56:03 +01002457 switch (*p++) {
2458 case SPOE_SCOPE_PROC: scope = "proc"; break;
2459 case SPOE_SCOPE_SESS: scope = "sess"; break;
2460 case SPOE_SCOPE_TXN : scope = "txn"; break;
2461 case SPOE_SCOPE_REQ : scope = "req"; break;
2462 case SPOE_SCOPE_RES : scope = "res"; break;
2463 default: goto skip;
2464 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002465
Christopher Faulet8ef75252017-02-20 22:56:03 +01002466 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2467 goto skip;
2468 memset(&smp, 0, sizeof(smp));
2469 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002470
Christopher Faulet8ef75252017-02-20 22:56:03 +01002471 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2472 " - unset-var '%s.%s.%.*s'\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002473 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet8ef75252017-02-20 22:56:03 +01002474 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2475 __FUNCTION__, s, scope,
2476 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2477 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002478
Christopher Faulet8ef75252017-02-20 22:56:03 +01002479 spoe_unset_var(ctx, scope, str, sz, &smp);
2480
2481 ret = (p - *buf);
2482 *buf = p;
2483 return ret;
2484 skip:
2485 return 0;
2486}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002487
Christopher Faulet8ef75252017-02-20 22:56:03 +01002488/* Process SPOE actions for a specific event. It returns 1 on success. If an
2489 * error occurred, 0 is returned. */
2490static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002491spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002492{
2493 char *p, *end;
2494 int ret;
2495
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002496 p = b_head(&ctx->buffer);
2497 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002498
2499 while (p < end) {
2500 enum spoe_action_type type;
2501
2502 type = *p++;
2503 switch (type) {
2504 case SPOE_ACT_T_SET_VAR:
2505 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2506 if (!ret)
2507 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002508 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002509
Christopher Faulet8ef75252017-02-20 22:56:03 +01002510 case SPOE_ACT_T_UNSET_VAR:
2511 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2512 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002513 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002514 break;
2515
2516 default:
2517 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002518 }
2519 }
2520
2521 return 1;
2522 skip:
2523 return 0;
2524}
2525
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002526/***************************************************************************
2527 * Functions that process SPOE events
2528 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002529static void
2530spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2531 struct spoe_context *ctx, int dir)
2532{
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002533 if (ctx->stats.start_ts != 0) {
2534 spoe_update_stat_time(&ctx->stats.start_ts, &ctx->stats.t_process);
2535 ctx->stats.t_total += ctx->stats.t_process;
2536 ctx->stats.request_ts = 0;
2537 ctx->stats.queue_ts = 0;
2538 ctx->stats.wait_ts = 0;
2539 ctx->stats.response_ts = 0;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002540 }
2541
2542 if (agent->var_t_process) {
2543 struct sample smp;
2544
2545 memset(&smp, 0, sizeof(smp));
2546 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2547 smp.data.u.sint = ctx->stats.t_process;
2548 smp.data.type = SMP_T_SINT;
2549
2550 spoe_set_var(ctx, "txn", agent->var_t_process,
2551 strlen(agent->var_t_process), &smp);
2552 }
2553
2554 if (agent->var_t_total) {
2555 struct sample smp;
2556
2557 memset(&smp, 0, sizeof(smp));
2558 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2559 smp.data.u.sint = ctx->stats.t_total;
2560 smp.data.type = SMP_T_SINT;
2561
2562 spoe_set_var(ctx, "txn", agent->var_t_total,
2563 strlen(agent->var_t_total), &smp);
2564 }
2565}
2566
2567static void
2568spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2569 struct spoe_context *ctx, int dir)
2570{
2571 if (agent->eps_max > 0)
2572 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2573
2574 if (agent->var_on_error) {
2575 struct sample smp;
2576
2577 memset(&smp, 0, sizeof(smp));
2578 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2579 smp.data.u.sint = ctx->status_code;
2580 smp.data.type = SMP_T_BOOL;
2581
2582 spoe_set_var(ctx, "txn", agent->var_on_error,
2583 strlen(agent->var_on_error), &smp);
2584 }
2585
2586 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2587 ? SPOE_CTX_ST_READY
2588 : SPOE_CTX_ST_NONE);
2589}
2590
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002591static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002592spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002593{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002594 /* If a process is already started for this SPOE context, retry
2595 * later. */
2596 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002597 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002598
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002599 agent->rt[tid].processing++;
Willy Tarreau69530f52023-04-28 09:16:15 +02002600 ctx->stats.start_ts = now_ns;
2601 ctx->stats.request_ts = now_ns;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002602 ctx->stats.t_request = -1;
2603 ctx->stats.t_queue = -1;
2604 ctx->stats.t_waiting = -1;
2605 ctx->stats.t_response = -1;
2606 ctx->stats.t_process = -1;
2607
2608 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002609
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610 /* Set the right flag to prevent request and response processing
2611 * in same time. */
2612 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2613 ? SPOE_CTX_FL_REQ_PROCESS
2614 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002615 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002616}
2617
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002618static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002619spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002620{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002621 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002622
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002623 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2624 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002625 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002626 if (sa) {
2627 if (sa->frag_ctx.ctx == ctx) {
2628 sa->frag_ctx.ctx = NULL;
2629 spoe_wakeup_appctx(sa->owner);
2630 }
2631 else
2632 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002633 }
2634
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002636 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002637 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638
2639 /* Reset processing timer */
2640 ctx->process_exp = TICK_ETERNITY;
2641
Christopher Faulet8ef75252017-02-20 22:56:03 +01002642 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002643
Christopher Fauletfce747b2018-01-24 15:59:32 +01002644 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002645 ctx->frag_ctx.curmsg = NULL;
2646 ctx->frag_ctx.curarg = NULL;
2647 ctx->frag_ctx.curoff = 0;
2648 ctx->frag_ctx.flags = 0;
2649
Christopher Fauleta1cda022016-12-21 08:58:06 +01002650 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002651 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002652 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002653 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002654 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002655
Willy Tarreau2b718102021-04-21 07:32:39 +02002656 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002657 LIST_INIT(&ctx->list);
2658 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002659}
2660
Christopher Faulet58d03682017-09-21 16:57:24 +02002661/* Process a list of SPOE messages. First, this functions will process messages
2662 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2663 * to process corresponding actions. During all the processing, it returns 0
2664 * and it returns 1 when the processing is finished. If an error occurred, -1
2665 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002666static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002667spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2668 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002669{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002670 struct spoe_config *conf = FLT_CONF(ctx->filter);
2671 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002672 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002673
2674 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002675 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002676
2677 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2678 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002679 " - failed to process messages: timeout\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002680 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002681 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002682 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002683 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002684 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002685
2686 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002687 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002688 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002689 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002690 " - skip processing of messages: max EPS reached\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002691 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002692 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002693 goto skip;
2694 }
2695 }
2696
Christopher Fauletf7a30922016-11-10 15:04:51 +01002697 if (!tick_isset(ctx->process_exp)) {
2698 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2699 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2700 ctx->process_exp);
2701 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002702 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002703 if (!ret)
2704 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002705
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002706 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002707 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002708 }
2709
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002710 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002711 if (ctx->stats.request_ts == 0)
Willy Tarreau69530f52023-04-28 09:16:15 +02002712 ctx->stats.request_ts = now_ns;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002713 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002714 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002715 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002716 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002717 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002718 if (!ret)
2719 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002720 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002721 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002722 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2723 }
2724
2725 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002726 if (ctx->spoe_appctx)
2727 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002728 ret = 0;
2729 goto out;
2730 }
2731
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002732 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2733 ret = 0;
2734 goto out;
2735 }
2736
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002737 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002738 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002739 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002740 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002741 ctx->state = SPOE_CTX_ST_READY;
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002742 spoe_update_stat_time(&ctx->stats.response_ts, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002743 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002744 }
2745
2746 out:
2747 return ret;
2748
Christopher Fauleta1cda022016-12-21 08:58:06 +01002749 skip:
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002750 ctx->stats.start_ts = 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002751 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002752 spoe_stop_processing(agent, ctx);
2753 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002754
Christopher Fauleta1cda022016-12-21 08:58:06 +01002755 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002756 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002757 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002758 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002759 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002760 spoe_handle_processing_error(s, agent, ctx, dir);
2761 ret = 1;
2762 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002763 return ret;
2764}
2765
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002766/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2767 * See spoe_process_message for details. */
2768static int
2769spoe_process_group(struct stream *s, struct spoe_context *ctx,
2770 struct spoe_group *group, int dir)
2771{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002772 struct spoe_config *conf = FLT_CONF(ctx->filter);
2773 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002774 int ret;
2775
2776 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2777 " - ctx-state=%s - Process messages for group=%s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002778 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002779 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2780 group->id);
2781
2782 if (LIST_ISEMPTY(&group->messages))
2783 return 1;
2784
2785 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002786 if (ret && ctx->stats.t_process != -1) {
2787 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002788 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002789 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002790 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002791 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002792 ctx->stats.t_response, ctx->stats.t_process,
2793 agent->counters.idles, agent->counters.applets,
2794 agent->counters.nb_sending, agent->counters.nb_waiting,
2795 agent->counters.nb_errors, agent->counters.nb_processed,
2796 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2797 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2798 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2799 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2800 agent->id, group->id, s->uniq_id, ctx->status_code,
2801 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2802 ctx->stats.t_response, ctx->stats.t_process,
2803 agent->counters.idles, agent->counters.applets,
2804 agent->counters.nb_sending, agent->counters.nb_waiting,
2805 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002806 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002807 return ret;
2808}
2809
Christopher Faulet58d03682017-09-21 16:57:24 +02002810/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2811 * See spoe_process_message for details. */
2812static int
2813spoe_process_event(struct stream *s, struct spoe_context *ctx,
2814 enum spoe_event ev)
2815{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002816 struct spoe_config *conf = FLT_CONF(ctx->filter);
2817 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002818 int dir, ret;
2819
2820 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002821 " - ctx-state=%s - Process messages for event=%s\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002822 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002823 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2824 spoe_event_str[ev]);
2825
2826 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2827
2828 if (LIST_ISEMPTY(&(ctx->events[ev])))
2829 return 1;
2830
2831 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002832 if (ret && ctx->stats.t_process != -1) {
2833 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002834 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02002835 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002836 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2837 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002838 ctx->stats.t_response, ctx->stats.t_process,
2839 agent->counters.idles, agent->counters.applets,
2840 agent->counters.nb_sending, agent->counters.nb_waiting,
2841 agent->counters.nb_errors, agent->counters.nb_processed,
2842 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2843 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2844 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2845 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2846 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2847 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2848 ctx->stats.t_response, ctx->stats.t_process,
2849 agent->counters.idles, agent->counters.applets,
2850 agent->counters.nb_sending, agent->counters.nb_waiting,
2851 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002852 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002853 return ret;
2854}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002855
2856/***************************************************************************
2857 * Functions that create/destroy SPOE contexts
2858 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002859static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002860spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002861{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002862 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002863 return 1;
2864
Willy Tarreau2b718102021-04-21 07:32:39 +02002865 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002866 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002867
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002868 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002869 return 1;
2870
Willy Tarreaub4e34762021-09-30 19:02:18 +02002871 LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002872 return 0;
2873}
2874
2875static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002876spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002877{
Willy Tarreau2b718102021-04-21 07:32:39 +02002878 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002879 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002880
2881 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002882 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002883 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002884 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002885 }
2886}
2887
Christopher Faulet4596fb72017-01-11 14:05:19 +01002888static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002889spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002890{
2891 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2892 return 1;
2893}
2894
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002895static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002896spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002897{
2898 struct spoe_config *conf = FLT_CONF(filter);
2899 struct spoe_context *ctx;
2900
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002901 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902 if (ctx == NULL) {
2903 return NULL;
2904 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002905 ctx->filter = filter;
2906 ctx->state = SPOE_CTX_ST_NONE;
2907 ctx->status_code = SPOE_CTX_ERR_NONE;
2908 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002909 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002910 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002911 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002912 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002913 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002914 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002915 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002916
Christopher Fauletf7a30922016-11-10 15:04:51 +01002917 ctx->stream_id = 0;
2918 ctx->frame_id = 1;
2919 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002920
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002921 ctx->stats.start_ts = 0;
2922 ctx->stats.request_ts = 0;
2923 ctx->stats.queue_ts = 0;
2924 ctx->stats.wait_ts = 0;
2925 ctx->stats.response_ts= 0;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002926 ctx->stats.t_request = -1;
2927 ctx->stats.t_queue = -1;
2928 ctx->stats.t_waiting = -1;
2929 ctx->stats.t_response = -1;
2930 ctx->stats.t_process = -1;
2931 ctx->stats.t_total = 0;
2932
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002933 ctx->strm = s;
2934 ctx->state = SPOE_CTX_ST_READY;
2935 filter->ctx = ctx;
2936
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002937 return ctx;
2938}
2939
2940static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002941spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002943 struct spoe_config *conf = FLT_CONF(filter);
2944 struct spoe_context *ctx = filter->ctx;
2945
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002946 if (!ctx)
2947 return;
2948
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002949 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002950 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002951 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952}
2953
2954static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002955spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956{
2957 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002958 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002959
Willy Tarreauaaebcae2023-04-27 11:54:11 +02002960 ctx->stats.start_ts = 0;
2961 ctx->stats.request_ts = 0;
2962 ctx->stats.queue_ts = 0;
2963 ctx->stats.wait_ts = 0;
2964 ctx->stats.response_ts= 0;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002965 ctx->stats.t_request = -1;
2966 ctx->stats.t_queue = -1;
2967 ctx->stats.t_waiting = -1;
2968 ctx->stats.t_response = -1;
2969 ctx->stats.t_process = -1;
2970 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002971}
2972
2973
2974/***************************************************************************
2975 * Hooks that manage the filter lifecycle (init/check/deinit)
2976 **************************************************************************/
2977/* Signal handler: Do a soft stop, wakeup SPOE applet */
2978static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002979spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980{
2981 struct proxy *p;
2982
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002983 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002984 while (p) {
2985 struct flt_conf *fconf;
2986
Christopher Faulet7f4ffad2023-05-11 09:08:28 +02002987 /* SPOE filter are not initialized for disabled proxoes. Move to
2988 * the next one
2989 */
2990 if (p->flags & PR_FL_DISABLED) {
2991 p = p->next;
2992 continue;
2993 }
2994
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002996 struct spoe_config *conf;
2997 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002998 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002999 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003000
Christopher Faulet3b386a32017-02-23 10:17:15 +01003001 if (fconf->id != spoe_filter_id)
3002 continue;
3003
3004 conf = fconf->conf;
3005 agent = conf->agent;
3006
Christopher Faulet24289f22017-09-25 14:48:02 +02003007 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003008 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003009 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3010 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003011 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003012 }
3013 }
3014 p = p->next;
3015 }
3016}
3017
3018
3019/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3020static int
3021spoe_init(struct proxy *px, struct flt_conf *fconf)
3022{
3023 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003024
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003025 /* conf->agent_fe was already initialized during the config
3026 * parsing. Finish initialization. */
Willy Tarreau69530f52023-04-28 09:16:15 +02003027 conf->agent_fe.last_change = ns_to_sec(now_ns);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003028 conf->agent_fe.cap = PR_CAP_FE;
3029 conf->agent_fe.mode = PR_MODE_TCP;
3030 conf->agent_fe.maxconn = 0;
3031 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3032 conf->agent_fe.conn_retries = CONN_RETRIES;
3033 conf->agent_fe.accept = frontend_accept;
3034 conf->agent_fe.srv = NULL;
3035 conf->agent_fe.timeout.client = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003036 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3037
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003039 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003040 sighandler_registered = 1;
3041 }
3042
Christopher Faulet00292352019-01-09 15:05:10 +01003043 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003044 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003045}
3046
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003047/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048static void
3049spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3050{
3051 struct spoe_config *conf = fconf->conf;
3052
3053 if (conf) {
3054 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003055
Christopher Faulet8ef75252017-02-20 22:56:03 +01003056 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003057 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003058 free(conf);
3059 }
3060 fconf->conf = NULL;
3061}
3062
3063/* Check configuration of a SPOE filter for a specified proxy.
3064 * Return 1 on error, else 0. */
3065static int
3066spoe_check(struct proxy *px, struct flt_conf *fconf)
3067{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003068 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003069 struct spoe_config *conf = fconf->conf;
3070 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003071 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003072 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003073
Christopher Faulet7ee86672017-09-19 11:08:28 +02003074 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3075 * are uniq */
3076 list_for_each_entry(f, &px->filter_configs, list) {
3077 struct spoe_config *c = f->conf;
3078
3079 /* This is not an SPOE filter */
3080 if (f->id != spoe_filter_id)
3081 continue;
3082 /* This is the current SPOE filter */
3083 if (f == fconf)
3084 continue;
3085
3086 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003087 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003088 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3089 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003090 return 1;
3091 }
3092 }
3093
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003094 target = proxy_be_by_name(conf->agent->b.name);
3095 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003096 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3097 " declared at %s:%d.\n",
3098 px->id, conf->agent->b.name, conf->agent->id,
3099 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003100 return 1;
3101 }
3102 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003103 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3104 " at %s:%d does not support HTTP mode.\n",
3105 px->id, target->id, conf->agent->id,
3106 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003107 return 1;
3108 }
3109
Christopher Fauletfe261552019-03-18 13:57:42 +01003110 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003111 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3112 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3113 return 1;
3114 }
3115 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003116 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003117 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3118 conf->agent->rt[i].processing = 0;
Christopher Faulete99c4392023-04-26 15:56:59 +02003119 conf->agent->rt[i].idles = 0;
Christopher Fauletfe261552019-03-18 13:57:42 +01003120 LIST_INIT(&conf->agent->rt[i].applets);
3121 LIST_INIT(&conf->agent->rt[i].sending_queue);
3122 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3123 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003124 }
3125
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003126 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3127 if (logsrv->type == LOG_TARGET_BUFFER) {
3128 struct sink *sink = sink_find(logsrv->ring_name);
3129
3130 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3131 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003132 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003133 px->id, conf->agent->id, conf->agent->conf.file,
3134 conf->agent->conf.line, logsrv->ring_name);
3135 return 1;
3136 }
3137 logsrv->sink = sink;
3138 }
3139 }
3140
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003141 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142 conf->agent->b.be = target;
3143 return 0;
3144}
3145
Kevin Zhud87b1a52019-09-17 15:05:45 +02003146/* Initializes the SPOE filter for a proxy for a specific thread.
3147 * Returns a negative value if an error occurs. */
3148static int
3149spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3150{
3151 struct spoe_config *conf = fconf->conf;
3152 struct spoe_agent *agent = conf->agent;
3153
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003154 agent->rt[tid].engine_id = generate_pseudo_uuid();
3155 if (agent->rt[tid].engine_id == NULL)
3156 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003157 return 0;
3158}
3159
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003160/**************************************************************************
3161 * Hooks attached to a stream
3162 *************************************************************************/
3163/* Called when a filter instance is created and attach to a stream. It creates
3164 * the context that will be used to process this stream. */
3165static int
3166spoe_start(struct stream *s, struct filter *filter)
3167{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003168 struct spoe_config *conf = FLT_CONF(filter);
3169 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003170 struct spoe_context *ctx;
3171
3172 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003173 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003174 __FUNCTION__, s);
3175
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003176 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003177 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3178 " - failed to create SPOE context\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003179 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003180 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003181 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003182 "SPOE: [%s] failed to create SPOE context\n",
3183 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003184 return 0;
3185 }
3186
Christopher Faulet11610f32017-09-21 10:23:10 +02003187 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003188 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3189
Christopher Faulet11610f32017-09-21 10:23:10 +02003190 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3192
Christopher Faulet11610f32017-09-21 10:23:10 +02003193 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003194 filter->pre_analyzers |= AN_RES_INSPECT;
3195
Christopher Faulet11610f32017-09-21 10:23:10 +02003196 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003197 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3198
Christopher Faulet11610f32017-09-21 10:23:10 +02003199 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003200 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3201
Christopher Faulet11610f32017-09-21 10:23:10 +02003202 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003203 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3204
3205 return 1;
3206}
3207
3208/* Called when a filter instance is detached from a stream. It release the
3209 * attached SPOE context. */
3210static void
3211spoe_stop(struct stream *s, struct filter *filter)
3212{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003213 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003214 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003215 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3216 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003217 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003218}
3219
Christopher Fauletf7a30922016-11-10 15:04:51 +01003220
3221/*
3222 * Called when the stream is woken up because of expired timer.
3223 */
3224static void
3225spoe_check_timeouts(struct stream *s, struct filter *filter)
3226{
3227 struct spoe_context *ctx = filter->ctx;
3228
Christopher Fauletac580602018-03-20 16:09:20 +01003229 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003230 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003231}
3232
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003233/* Called when we are ready to filter data on a channel */
3234static int
3235spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3236{
3237 struct spoe_context *ctx = filter->ctx;
3238 int ret = 1;
3239
3240 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3241 " - ctx-flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003242 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003243 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3244 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3245
Christopher Fauletb067b062017-01-04 16:39:11 +01003246 if (ctx->state == SPOE_CTX_ST_NONE)
3247 goto out;
3248
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003249 if (!(chn->flags & CF_ISRESP)) {
3250 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3251 chn->analysers |= AN_REQ_INSPECT_FE;
3252 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3253 chn->analysers |= AN_REQ_INSPECT_BE;
3254
3255 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3256 goto out;
3257
3258 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003259 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003260 if (!ret)
3261 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003262 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3263 }
3264 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003265 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003266 chn->analysers |= AN_RES_INSPECT;
3267
3268 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3269 goto out;
3270
Christopher Faulet8ef75252017-02-20 22:56:03 +01003271 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003272 if (!ret) {
3273 channel_dont_read(chn);
3274 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003275 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003276 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003277 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003278 }
3279
3280 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003281 return ret;
3282}
3283
3284/* Called before a processing happens on a given channel */
3285static int
3286spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3287 struct channel *chn, unsigned an_bit)
3288{
3289 struct spoe_context *ctx = filter->ctx;
3290 int ret = 1;
3291
3292 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3293 " - ctx-flags=0x%08x - ana=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003294 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003295 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3296 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3297 ctx->flags, an_bit);
3298
Christopher Fauletb067b062017-01-04 16:39:11 +01003299 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003300 goto out;
3301
3302 switch (an_bit) {
3303 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003304 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003305 break;
3306 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003307 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003308 break;
3309 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003310 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 break;
3312 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003313 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003314 break;
3315 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003316 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 break;
3318 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003319 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003320 break;
3321 }
3322
3323 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003324 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 channel_dont_read(chn);
3326 channel_dont_close(chn);
3327 }
3328 return ret;
3329}
3330
3331/* Called when the filtering on the channel ends. */
3332static int
3333spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3334{
3335 struct spoe_context *ctx = filter->ctx;
3336
3337 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3338 " - ctx-flags=0x%08x\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02003339 (int)date.tv_sec, (int)date.tv_usec,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003340 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3341 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3342
3343 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003344 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003345 }
3346
3347 return 1;
3348}
3349
3350/********************************************************************
3351 * Functions that manage the filter initialization
3352 ********************************************************************/
3353struct flt_ops spoe_ops = {
3354 /* Manage SPOE filter, called for each filter declaration */
3355 .init = spoe_init,
3356 .deinit = spoe_deinit,
3357 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003358 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003359
3360 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003361 .attach = spoe_start,
3362 .detach = spoe_stop,
3363 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003364
3365 /* Handle channels activity */
3366 .channel_start_analyze = spoe_start_analyze,
3367 .channel_pre_analyze = spoe_chn_pre_analyze,
3368 .channel_end_analyze = spoe_end_analyze,
3369};
3370
3371
3372static int
3373cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3374{
3375 const char *err;
3376 int i, err_code = 0;
3377
3378 if ((cfg_scope == NULL && curengine != NULL) ||
3379 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003380 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 goto out;
3382
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003383 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003384 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003385 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3386 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003387 err_code |= ERR_ALERT | ERR_ABORT;
3388 goto out;
3389 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003390 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3391 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003392 goto out;
3393 }
3394
3395 err = invalid_char(args[1]);
3396 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003397 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3398 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003399 err_code |= ERR_ALERT | ERR_ABORT;
3400 goto out;
3401 }
3402
3403 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003404 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3405 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003406 err_code |= ERR_ALERT | ERR_ABORT;
3407 goto out;
3408 }
3409 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003410 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003411 err_code |= ERR_ALERT | ERR_ABORT;
3412 goto out;
3413 }
3414
3415 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003416
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417 curagent->conf.file = strdup(file);
3418 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003419
3420 curagent->timeout.hello = TICK_ETERNITY;
3421 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003422 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003423
Christopher Fauleta1cda022016-12-21 08:58:06 +01003424 curagent->var_pfx = NULL;
3425 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003426 curagent->var_t_process = NULL;
3427 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003428 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003429 curagent->cps_max = 0;
3430 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003431 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003432 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003433
3434 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003435 LIST_INIT(&curagent->events[i]);
3436 LIST_INIT(&curagent->groups);
3437 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003438 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003439 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003440 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003441 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3442 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003443 err_code |= ERR_ALERT | ERR_FATAL;
3444 goto out;
3445 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003446 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003447 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003448 free(curagent->b.name);
3449 curagent->b.name = strdup(args[1]);
3450 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003451 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003452 int cur_arg = 1;
3453 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003454 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003455
Christopher Faulet11610f32017-09-21 10:23:10 +02003456 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003457 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003458 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3459 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003460 err_code |= ERR_ALERT | ERR_FATAL;
3461 goto out;
3462 }
3463 }
3464
Christopher Faulet11610f32017-09-21 10:23:10 +02003465 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003466 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003467 err_code |= ERR_ALERT | ERR_ABORT;
3468 goto out;
3469 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003470 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003471 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003472 cur_arg++;
3473 }
3474 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003475 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003476 int cur_arg = 1;
3477 while (*args[cur_arg]) {
3478 struct spoe_placeholder *ph = NULL;
3479
3480 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003481 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003482 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3483 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003484 err_code |= ERR_ALERT | ERR_FATAL;
3485 goto out;
3486 }
3487 }
3488
3489 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003490 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003491 err_code |= ERR_ALERT | ERR_ABORT;
3492 goto out;
3493 }
3494 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003495 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003496 cur_arg++;
3497 }
3498 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003499 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003500 unsigned int *tv = NULL;
3501 const char *res;
3502 unsigned timeout;
3503
3504 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003505 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3506 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003507 err_code |= ERR_ALERT | ERR_FATAL;
3508 goto out;
3509 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003510 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3511 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003512 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003513 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003514 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003515 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003516 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003517 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003518 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003519 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3520 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003521 err_code |= ERR_ALERT | ERR_FATAL;
3522 goto out;
3523 }
3524 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003525 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3526 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003527 err_code |= ERR_ALERT | ERR_FATAL;
3528 goto out;
3529 }
3530 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003531 if (res == PARSE_TIME_OVER) {
3532 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3533 file, linenum, args[2], args[0], args[1]);
3534 err_code |= ERR_ALERT | ERR_FATAL;
3535 goto out;
3536 }
3537 else if (res == PARSE_TIME_UNDER) {
3538 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3539 file, linenum, args[2], args[0], args[1]);
3540 err_code |= ERR_ALERT | ERR_FATAL;
3541 goto out;
3542 }
3543 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003544 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3545 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003546 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003547 goto out;
3548 }
3549 *tv = MS_TO_TICKS(timeout);
3550 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003551 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003552 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003553 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3554 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003555 err_code |= ERR_ALERT | ERR_FATAL;
3556 goto out;
3557 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003558
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003559 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003560 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003561 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003562 if (kwm == 1)
3563 curagent->flags &= ~SPOE_FL_PIPELINING;
3564 else
3565 curagent->flags |= SPOE_FL_PIPELINING;
3566 goto out;
3567 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003568 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003569 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003570 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003571 if (kwm == 1)
3572 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003573 else
3574 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003575 goto out;
3576 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003577 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003578 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3579 goto out;
3580 if (kwm == 1)
3581 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3582 else
3583 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3584 goto out;
3585 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003586 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003587 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3588 goto out;
3589 if (kwm == 1)
3590 curpxopts2 &= ~PR_O2_NOLOGNORM;
3591 else
3592 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003593 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003594 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003595
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003596 /* Following options does not support negation */
3597 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003598 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3599 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003600 err_code |= ERR_ALERT | ERR_FATAL;
3601 goto out;
3602 }
3603
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003604 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003605 char *tmp;
3606
3607 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003608 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3609 file, linenum, args[0],
3610 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003611 err_code |= ERR_ALERT | ERR_FATAL;
3612 goto out;
3613 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003614 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3615 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003616 tmp = args[2];
3617 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003618 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003619 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003620 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003621 err_code |= ERR_ALERT | ERR_FATAL;
3622 goto out;
3623 }
3624 tmp++;
3625 }
3626 curagent->var_pfx = strdup(args[2]);
3627 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003628 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003629 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3630 goto out;
3631 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3632 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003633 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003634 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003635 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003636 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3637 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003638 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003639 char *tmp;
3640
3641 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003642 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3643 file, linenum, args[0],
3644 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003645 err_code |= ERR_ALERT | ERR_FATAL;
3646 goto out;
3647 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003648 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3649 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003650 tmp = args[2];
3651 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003652 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003653 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003654 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003655 err_code |= ERR_ALERT | ERR_FATAL;
3656 goto out;
3657 }
3658 tmp++;
3659 }
3660 curagent->var_on_error = strdup(args[2]);
3661 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003662 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003663 char *tmp;
3664
3665 if (!*args[2]) {
3666 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3667 file, linenum, args[0],
3668 args[1]);
3669 err_code |= ERR_ALERT | ERR_FATAL;
3670 goto out;
3671 }
3672 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3673 goto out;
3674 tmp = args[2];
3675 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003676 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003677 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003678 file, linenum, args[0], args[1]);
3679 err_code |= ERR_ALERT | ERR_FATAL;
3680 goto out;
3681 }
3682 tmp++;
3683 }
3684 curagent->var_t_process = strdup(args[2]);
3685 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003686 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003687 char *tmp;
3688
3689 if (!*args[2]) {
3690 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3691 file, linenum, args[0],
3692 args[1]);
3693 err_code |= ERR_ALERT | ERR_FATAL;
3694 goto out;
3695 }
3696 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3697 goto out;
3698 tmp = args[2];
3699 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003700 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003701 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003702 file, linenum, args[0], args[1]);
3703 err_code |= ERR_ALERT | ERR_FATAL;
3704 goto out;
3705 }
3706 tmp++;
3707 }
3708 curagent->var_t_total = strdup(args[2]);
3709 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003710 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003711 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3712 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003713 err_code |= ERR_ALERT | ERR_FATAL;
3714 goto out;
3715 }
Christopher Faulet48026722016-11-16 15:01:12 +01003716 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003717 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003718 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003719 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3720 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003721 err_code |= ERR_ALERT | ERR_FATAL;
3722 goto out;
3723 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003724 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003725 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003726 curagent->cps_max = atol(args[1]);
3727 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003728 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003729 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003730 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3731 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003732 err_code |= ERR_ALERT | ERR_FATAL;
3733 goto out;
3734 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003735 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003736 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003737 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003738 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003739 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003740 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003741 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3742 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003743 err_code |= ERR_ALERT | ERR_FATAL;
3744 goto out;
3745 }
3746 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3747 goto out;
3748 curagent->max_frame_size = atol(args[1]);
3749 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3750 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003751 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3752 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003753 err_code |= ERR_ALERT | ERR_FATAL;
3754 goto out;
3755 }
3756 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003757 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003758 if (!*args[1]) {
3759 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3760 file, linenum, args[0]);
3761 err_code |= ERR_ALERT | ERR_FATAL;
3762 goto out;
3763 }
3764 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3765 goto out;
3766 curagent->max_fpa = atol(args[1]);
3767 if (curagent->max_fpa < 1) {
3768 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3769 file, linenum, args[0]);
3770 err_code |= ERR_ALERT | ERR_FATAL;
3771 goto out;
3772 }
3773 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003774 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003775 int cur_arg;
3776
3777 if (!*args[1]) {
3778 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3779 file, linenum, args[0]);
3780 err_code |= ERR_ALERT | ERR_FATAL;
3781 goto out;
3782 }
3783 cur_arg = 1;
3784 while (*args[cur_arg]) {
3785 struct spoe_var_placeholder *vph;
3786
3787 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3788 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3789 err_code |= ERR_ALERT | ERR_ABORT;
3790 goto out;
3791 }
3792 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003793 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003794 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3795 err_code |= ERR_ALERT | ERR_ABORT;
3796 goto out;
3797 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003798 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003799 cur_arg++;
3800 }
3801 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003802 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003803 char *errmsg = NULL;
3804
Emeric Brun9533a702021-04-02 10:13:43 +02003805 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003806 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3807 err_code |= ERR_ALERT | ERR_FATAL;
3808 goto out;
3809 }
3810 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003811 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003812 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3813 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003814 err_code |= ERR_ALERT | ERR_FATAL;
3815 goto out;
3816 }
3817 out:
3818 return err_code;
3819}
Christopher Faulet11610f32017-09-21 10:23:10 +02003820static int
3821cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3822{
3823 struct spoe_group *grp;
3824 const char *err;
3825 int err_code = 0;
3826
3827 if ((cfg_scope == NULL && curengine != NULL) ||
3828 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003829 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003830 goto out;
3831
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003832 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003833 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003834 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3835 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003836 err_code |= ERR_ALERT | ERR_ABORT;
3837 goto out;
3838 }
3839 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3840 err_code |= ERR_ABORT;
3841 goto out;
3842 }
3843
3844 err = invalid_char(args[1]);
3845 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003846 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3847 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003848 err_code |= ERR_ALERT | ERR_ABORT;
3849 goto out;
3850 }
3851
3852 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003853 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003854 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3855 " name as another one declared at %s:%d.\n",
3856 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003857 err_code |= ERR_ALERT | ERR_FATAL;
3858 goto out;
3859 }
3860 }
3861
3862 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003863 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003864 err_code |= ERR_ALERT | ERR_ABORT;
3865 goto out;
3866 }
3867
3868 curgrp->id = strdup(args[1]);
3869 curgrp->conf.file = strdup(file);
3870 curgrp->conf.line = linenum;
3871 LIST_INIT(&curgrp->phs);
3872 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003873 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003874 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003875 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003876 int cur_arg = 1;
3877 while (*args[cur_arg]) {
3878 struct spoe_placeholder *ph = NULL;
3879
3880 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003881 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003882 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3883 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003884 err_code |= ERR_ALERT | ERR_FATAL;
3885 goto out;
3886 }
3887 }
3888
3889 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003890 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003891 err_code |= ERR_ALERT | ERR_ABORT;
3892 goto out;
3893 }
3894 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003895 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003896 cur_arg++;
3897 }
3898 }
3899 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003900 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3901 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003902 err_code |= ERR_ALERT | ERR_FATAL;
3903 goto out;
3904 }
3905 out:
3906 return err_code;
3907}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003908
3909static int
3910cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3911{
3912 struct spoe_message *msg;
3913 struct spoe_arg *arg;
3914 const char *err;
3915 char *errmsg = NULL;
3916 int err_code = 0;
3917
3918 if ((cfg_scope == NULL && curengine != NULL) ||
3919 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003920 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003921 goto out;
3922
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003923 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003924 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003925 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3926 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 err_code |= ERR_ALERT | ERR_ABORT;
3928 goto out;
3929 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003930 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3931 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003932 goto out;
3933 }
3934
3935 err = invalid_char(args[1]);
3936 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003937 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3938 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003939 err_code |= ERR_ALERT | ERR_ABORT;
3940 goto out;
3941 }
3942
3943 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003944 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003945 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3946 " name as another one declared at %s:%d.\n",
3947 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003948 err_code |= ERR_ALERT | ERR_FATAL;
3949 goto out;
3950 }
3951 }
3952
3953 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003954 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003955 err_code |= ERR_ALERT | ERR_ABORT;
3956 goto out;
3957 }
3958
3959 curmsg->id = strdup(args[1]);
3960 curmsg->id_len = strlen(curmsg->id);
3961 curmsg->event = SPOE_EV_NONE;
3962 curmsg->conf.file = strdup(file);
3963 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003964 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003965 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003966 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003967 LIST_INIT(&curmsg->by_evt);
3968 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003969 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003970 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003971 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003972 int cur_arg = 1;
3973
3974 curproxy->conf.args.ctx = ARGC_SPOE;
3975 curproxy->conf.args.file = file;
3976 curproxy->conf.args.line = linenum;
3977 while (*args[cur_arg]) {
3978 char *delim = strchr(args[cur_arg], '=');
3979 int idx = 0;
3980
3981 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003982 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003983 err_code |= ERR_ALERT | ERR_ABORT;
3984 goto out;
3985 }
3986
3987 if (!delim) {
3988 arg->name = NULL;
3989 arg->name_len = 0;
3990 delim = args[cur_arg];
3991 }
3992 else {
3993 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3994 arg->name_len = delim - args[cur_arg];
3995 delim++;
3996 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003997 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3998 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003999 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004000 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004001 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004002 err_code |= ERR_ALERT | ERR_FATAL;
4003 free(arg->name);
4004 free(arg);
4005 goto out;
4006 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004007 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004008 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004009 cur_arg++;
4010 }
4011 curproxy->conf.args.file = NULL;
4012 curproxy->conf.args.line = 0;
4013 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004014 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004015 err = invalid_char(args[1]);
4016 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004017 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4018 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004019 err_code |= ERR_ALERT | ERR_FATAL;
4020 goto out;
4021 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004022 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004023 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004024 "logical disjunction within a condition.\n",
4025 file, linenum, args[1]);
4026 err_code |= ERR_ALERT | ERR_FATAL;
4027 goto out;
4028 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004029 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004030 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4031 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004032 err_code |= ERR_ALERT | ERR_FATAL;
4033 goto out;
4034 }
4035 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004036 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004037 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004038 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004039 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004040 goto out;
4041 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004042 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4043 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004044
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004045 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004046 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004047 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004048 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4049
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004050 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004051 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004052 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004053 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004054 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 curmsg->event = SPOE_EV_ON_TCP_RSP;
4056
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004057 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004058 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004059 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004060 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004061 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004062 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4063 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004064 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004065 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004066 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004067 goto out;
4068 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004069
4070 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4071 struct acl_cond *cond;
4072
4073 cond = build_acl_cond(file, linenum, &curmsg->acls,
4074 curproxy, (const char **)args+2,
4075 &errmsg);
4076 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004077 ha_alert("parsing [%s:%d] : error detected while "
4078 "parsing an 'event %s' condition : %s.\n",
4079 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004080 err_code |= ERR_ALERT | ERR_FATAL;
4081 goto out;
4082 }
4083 curmsg->cond = cond;
4084 }
4085 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004086 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4087 "or 'unless' followed by a condition but found '%s'.\n",
4088 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004089 err_code |= ERR_ALERT | ERR_FATAL;
4090 goto out;
4091 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004092 }
4093 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004094 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4095 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004096 err_code |= ERR_ALERT | ERR_FATAL;
4097 goto out;
4098 }
4099 out:
4100 free(errmsg);
4101 return err_code;
4102}
4103
4104/* Return -1 on error, else 0 */
4105static int
4106parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4107 struct flt_conf *fconf, char **err, void *private)
4108{
4109 struct list backup_sections;
4110 struct spoe_config *conf;
4111 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004112 struct spoe_group *grp, *grpback;
4113 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004114 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004115 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004116 char *file = NULL, *engine = NULL;
4117 int ret, pos = *cur_arg + 1;
4118
Christopher Faulet84c844e2018-03-23 14:37:14 +01004119 LIST_INIT(&curmsgs);
4120 LIST_INIT(&curgrps);
4121 LIST_INIT(&curmphs);
4122 LIST_INIT(&curgphs);
4123 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004124 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004125 curpxopts = 0;
4126 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004127
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004128 conf = calloc(1, sizeof(*conf));
4129 if (conf == NULL) {
4130 memprintf(err, "%s: out of memory", args[*cur_arg]);
4131 goto error;
4132 }
4133 conf->proxy = px;
4134
4135 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004136 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004137 if (!*args[pos+1]) {
4138 memprintf(err, "'%s' : '%s' option without value",
4139 args[*cur_arg], args[pos]);
4140 goto error;
4141 }
4142 file = args[pos+1];
4143 pos += 2;
4144 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004145 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004146 if (!*args[pos+1]) {
4147 memprintf(err, "'%s' : '%s' option without value",
4148 args[*cur_arg], args[pos]);
4149 goto error;
4150 }
4151 engine = args[pos+1];
4152 pos += 2;
4153 }
4154 else {
4155 memprintf(err, "unknown keyword '%s'", args[pos]);
4156 goto error;
4157 }
4158 }
4159 if (file == NULL) {
4160 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4161 goto error;
4162 }
4163
4164 /* backup sections and register SPOE sections */
4165 LIST_INIT(&backup_sections);
4166 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004167 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4168 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004169 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004170
4171 /* Parse SPOE filter configuration file */
4172 curengine = engine;
4173 curproxy = px;
4174 curagent = NULL;
4175 curmsg = NULL;
4176 ret = readcfgfile(file);
4177 curproxy = NULL;
4178
4179 /* unregister SPOE sections and restore previous sections */
4180 cfg_unregister_sections();
4181 cfg_restore_sections(&backup_sections);
4182
4183 if (ret == -1) {
4184 memprintf(err, "Could not open configuration file %s : %s",
4185 file, strerror(errno));
4186 goto error;
4187 }
4188 if (ret & (ERR_ABORT|ERR_FATAL)) {
4189 memprintf(err, "Error(s) found in configuration file %s", file);
4190 goto error;
4191 }
4192
4193 /* Check SPOE agent */
4194 if (curagent == NULL) {
4195 memprintf(err, "No SPOE agent found in file %s", file);
4196 goto error;
4197 }
4198 if (curagent->b.name == NULL) {
4199 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4200 curagent->id, curagent->conf.file, curagent->conf.line);
4201 goto error;
4202 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004203 if (curagent->timeout.hello == TICK_ETERNITY ||
4204 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004205 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004206 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4207 " | While not properly invalid, you will certainly encounter various problems\n"
4208 " | with such a configuration. To fix this, please ensure that all following\n"
4209 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4210 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004211 }
4212 if (curagent->var_pfx == NULL) {
4213 char *tmp = curagent->id;
4214
4215 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004216 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004217 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4218 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4219 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4220 goto error;
4221 }
4222 tmp++;
4223 }
4224 curagent->var_pfx = strdup(curagent->id);
4225 }
4226
Christopher Fauletb7426d12018-03-21 14:12:17 +01004227 if (curagent->var_on_error) {
4228 struct arg arg;
4229
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004230 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004231 curagent->var_pfx, curagent->var_on_error);
4232
4233 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004234 arg.data.str.area = trash.area;
4235 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004236 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004237 if (!vars_check_arg(&arg, err)) {
4238 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4239 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4240 goto error;
4241 }
4242 }
4243
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004244 if (curagent->var_t_process) {
4245 struct arg arg;
4246
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004247 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004248 curagent->var_pfx, curagent->var_t_process);
4249
4250 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004251 arg.data.str.area = trash.area;
4252 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004253 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004254 if (!vars_check_arg(&arg, err)) {
4255 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4256 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4257 goto error;
4258 }
4259 }
4260
4261 if (curagent->var_t_total) {
4262 struct arg arg;
4263
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004264 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004265 curagent->var_pfx, curagent->var_t_total);
4266
4267 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004268 arg.data.str.area = trash.area;
4269 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004270 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004271 if (!vars_check_arg(&arg, err)) {
4272 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4273 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4274 goto error;
4275 }
4276 }
4277
Christopher Faulet11610f32017-09-21 10:23:10 +02004278 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004279 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4280 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004281 goto finish;
4282 }
4283
Christopher Faulet11610f32017-09-21 10:23:10 +02004284 /* Replace placeholders by the corresponding messages for the SPOE
4285 * agent */
4286 list_for_each_entry(ph, &curmphs, list) {
4287 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004288 struct spoe_arg *arg;
4289 unsigned int where;
4290
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004291 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004292 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4293 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4294 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4295 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4296 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4297 }
4298 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4299 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4300 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004301 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4302 px->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 }
4305 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004306 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4307 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004308 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004309 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004310
4311 where = 0;
4312 switch (msg->event) {
4313 case SPOE_EV_ON_CLIENT_SESS:
4314 where |= SMP_VAL_FE_CON_ACC;
4315 break;
4316
4317 case SPOE_EV_ON_TCP_REQ_FE:
4318 where |= SMP_VAL_FE_REQ_CNT;
4319 break;
4320
4321 case SPOE_EV_ON_HTTP_REQ_FE:
4322 where |= SMP_VAL_FE_HRQ_HDR;
4323 break;
4324
4325 case SPOE_EV_ON_TCP_REQ_BE:
4326 if (px->cap & PR_CAP_FE)
4327 where |= SMP_VAL_FE_REQ_CNT;
4328 if (px->cap & PR_CAP_BE)
4329 where |= SMP_VAL_BE_REQ_CNT;
4330 break;
4331
4332 case SPOE_EV_ON_HTTP_REQ_BE:
4333 if (px->cap & PR_CAP_FE)
4334 where |= SMP_VAL_FE_HRQ_HDR;
4335 if (px->cap & PR_CAP_BE)
4336 where |= SMP_VAL_BE_HRQ_HDR;
4337 break;
4338
4339 case SPOE_EV_ON_SERVER_SESS:
4340 where |= SMP_VAL_BE_SRV_CON;
4341 break;
4342
4343 case SPOE_EV_ON_TCP_RSP:
4344 if (px->cap & PR_CAP_FE)
4345 where |= SMP_VAL_FE_RES_CNT;
4346 if (px->cap & PR_CAP_BE)
4347 where |= SMP_VAL_BE_RES_CNT;
4348 break;
4349
4350 case SPOE_EV_ON_HTTP_RSP:
4351 if (px->cap & PR_CAP_FE)
4352 where |= SMP_VAL_FE_HRS_HDR;
4353 if (px->cap & PR_CAP_BE)
4354 where |= SMP_VAL_BE_HRS_HDR;
4355 break;
4356
4357 default:
4358 break;
4359 }
4360
4361 list_for_each_entry(arg, &msg->args, list) {
4362 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004363 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004364 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004365 "none of which is available here ('%s')",
4366 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004367 sample_ckp_names(arg->expr->fetch->use),
4368 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004369 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004370 }
4371 }
4372
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004373 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004374 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004375 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004376 }
4377 }
4378 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004379 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004380 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004381 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004382 continue;
4383 }
4384
Christopher Faulet11610f32017-09-21 10:23:10 +02004385 /* Replace placeholders by the corresponding groups for the SPOE
4386 * agent */
4387 list_for_each_entry(ph, &curgphs, list) {
4388 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004389 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004390 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004391 LIST_DELETE(&grp->list);
4392 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004393 goto next_aph;
4394 }
4395 }
4396 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4397 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4398 goto error;
4399 next_aph:
4400 continue;
4401 }
4402
4403 /* Replace placeholders by the corresponding message for each SPOE
4404 * group of the SPOE agent */
4405 list_for_each_entry(grp, &curagent->groups, list) {
4406 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4407 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004408 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004409 if (msg->group != NULL) {
4410 memprintf(err, "SPOE message '%s' already belongs to "
4411 "the SPOE group '%s' declare at %s:%d",
4412 msg->id, msg->group->id,
4413 msg->group->conf.file,
4414 msg->group->conf.line);
4415 goto error;
4416 }
4417
4418 /* Scope for arguments are not checked for now. We will check
4419 * them only if a rule use the corresponding SPOE group. */
4420 msg->agent = curagent;
4421 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004422 LIST_DELETE(&ph->list);
4423 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004424 goto next_mph_grp;
4425 }
4426 }
4427 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4428 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4429 goto error;
4430 next_mph_grp:
4431 continue;
4432 }
4433 }
4434
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004435 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004436 /* move curmsgs to the agent message list */
4437 curmsgs.n->p = &curagent->messages;
4438 curmsgs.p->n = &curagent->messages;
4439 curagent->messages = curmsgs;
4440 LIST_INIT(&curmsgs);
4441
Christopher Faulet7ee86672017-09-19 11:08:28 +02004442 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004443 conf->agent = curagent;
Christopher Faulet434b8522021-08-02 17:51:01 +02004444 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004445
4446 /* Start agent's proxy initialization here. It will be finished during
4447 * the filter init. */
4448 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4449 init_new_proxy(&conf->agent_fe);
4450 conf->agent_fe.id = conf->agent->id;
4451 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004452 conf->agent_fe.options |= curpxopts;
4453 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004454
4455 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004456 LIST_DELETE(&logsrv->list);
4457 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004458 }
4459
Christopher Faulet11610f32017-09-21 10:23:10 +02004460 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004461 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004462 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004463 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004464 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004465 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004466 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004467 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004468 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4469 struct arg arg;
4470
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004471 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004472 curagent->var_pfx, vph->name);
4473
4474 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004475 arg.data.str.area = trash.area;
4476 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004477 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004478 if (!vars_check_arg(&arg, err)) {
4479 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4480 curagent->id, curagent->var_pfx, vph->name, *err);
4481 goto error;
4482 }
4483
Willy Tarreau2b718102021-04-21 07:32:39 +02004484 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004485 free(vph->name);
4486 free(vph);
4487 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004488 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004489 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004490 spoe_release_group(grp);
4491 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004492 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004493 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004494 fconf->ops = &spoe_ops;
4495 fconf->conf = conf;
4496 return 0;
4497
4498 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004499 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004500 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004501 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004502 spoe_release_placeholder(ph);
4503 }
4504 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004505 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004506 spoe_release_placeholder(ph);
4507 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004508 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004509 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004510 free(vph->name);
4511 free(vph);
4512 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004513 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004514 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004515 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004516 }
4517 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004518 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004519 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004520 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004521 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004522 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004523 free(logsrv);
4524 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004525 free(conf);
4526 return -1;
4527}
4528
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004529/* Send message of a SPOE group. This is the action_ptr callback of a rule
4530 * associated to a "send-spoe-group" action.
4531 *
Christopher Faulet13403762019-12-13 09:01:57 +01004532 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4533 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004534static enum act_return
4535spoe_send_group(struct act_rule *rule, struct proxy *px,
4536 struct session *sess, struct stream *s, int flags)
4537{
4538 struct filter *filter;
4539 struct spoe_agent *agent = NULL;
4540 struct spoe_group *group = NULL;
4541 struct spoe_context *ctx = NULL;
4542 int ret, dir;
4543
4544 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4545 if (filter->config == rule->arg.act.p[0]) {
4546 agent = rule->arg.act.p[2];
4547 group = rule->arg.act.p[3];
4548 ctx = filter->ctx;
4549 break;
4550 }
4551 }
4552 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004553 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004554 if (ctx->state == SPOE_CTX_ST_NONE)
4555 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004556
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004557 switch (rule->from) {
4558 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4559 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4560 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4561 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4562 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4563 default:
4564 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4565 " - internal error while execute spoe-send-group\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02004566 (int)date.tv_sec, (int)date.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004567 __FUNCTION__, s);
4568 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4569 agent->id);
4570 return ACT_RET_CONT;
4571 }
4572
4573 ret = spoe_process_group(s, ctx, group, dir);
4574 if (ret == 1)
4575 return ACT_RET_CONT;
4576 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004577 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004578 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4579 " - failed to process group '%s': interrupted by caller\n",
Willy Tarreaua5f0e6c2023-04-27 11:56:03 +02004580 (int)date.tv_sec, (int)date.tv_usec,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004581 agent->id, __FUNCTION__, s, group->id);
4582 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004583 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004584 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004585 return ACT_RET_CONT;
4586 }
4587 return ACT_RET_YIELD;
4588 }
4589 else
Christopher Faulet13403762019-12-13 09:01:57 +01004590 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004591}
4592
4593/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4594 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4595 * action should be:
4596 *
4597 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4598 *
4599 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4600 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4601 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4602 * group.
4603 *
4604 * The function returns 1 in success case, otherwise, it returns 0 and err is
4605 * filled.
4606 */
4607static int
4608check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4609{
4610 struct flt_conf *fconf;
4611 struct spoe_config *conf;
4612 struct spoe_agent *agent = NULL;
4613 struct spoe_group *group;
4614 struct spoe_message *msg;
4615 char *engine_id = rule->arg.act.p[0];
4616 char *group_id = rule->arg.act.p[1];
4617 unsigned int where = 0;
4618
4619 switch (rule->from) {
4620 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4621 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4622 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4623 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4624 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4625 default:
4626 memprintf(err,
4627 "internal error, unexpected rule->from=%d, please report this bug!",
4628 rule->from);
4629 goto error;
4630 }
4631
4632 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4633 * <px> */
4634 list_for_each_entry(fconf, &px->filter_configs, list) {
4635 conf = fconf->conf;
4636
4637 /* This is not an SPOE filter */
4638 if (fconf->id != spoe_filter_id)
4639 continue;
4640
4641 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004642 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004643 agent = conf->agent;
4644 break;
4645 }
4646 }
4647 if (agent == NULL) {
4648 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4649 engine_id, group_id);
4650 goto error;
4651 }
4652
4653 /* Try to find the right group */
4654 list_for_each_entry(group, &agent->groups, list) {
4655 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004656 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004657 break;
4658 }
4659 if (&group->list == &agent->groups) {
4660 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4661 group_id, engine_id);
4662 goto error;
4663 }
4664
4665 /* Ok, we found the group, we need to check messages and their
4666 * arguments */
4667 list_for_each_entry(msg, &group->messages, by_grp) {
4668 struct spoe_arg *arg;
4669
4670 list_for_each_entry(arg, &msg->args, list) {
4671 if (!(arg->expr->fetch->val & where)) {
4672 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4673 "some args extract information from '%s',"
4674 "none of which is available here ('%s')",
4675 msg->id, group->id, msg->conf.file, msg->conf.line,
4676 sample_ckp_names(arg->expr->fetch->use),
4677 sample_ckp_names(where));
4678 goto error;
4679 }
4680 }
4681 }
4682
4683 free(engine_id);
4684 free(group_id);
4685 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4686 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4687 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4688 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4689 return 1;
4690
4691 error:
4692 free(engine_id);
4693 free(group_id);
4694 return 0;
4695}
4696
4697/* Parse 'send-spoe-group' action following the format:
4698 *
4699 * ... send-spoe-group <engine-id> <group-id>
4700 *
4701 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4702 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4703 * ids are saved and used later, when the rule will be checked.
4704 */
4705static enum act_parse_ret
4706parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4707 struct act_rule *rule, char **err)
4708{
4709 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4710 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4711 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4712 return ACT_RET_PRS_ERR;
4713 }
4714 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4715 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4716
4717 (*orig_arg) += 2;
4718
4719 rule->action = ACT_CUSTOM;
4720 rule->action_ptr = spoe_send_group;
4721 rule->check_ptr = check_send_spoe_group;
4722 return ACT_RET_PRS_OK;
4723}
4724
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004725
4726/* Declare the filter parser for "spoe" keyword */
4727static struct flt_kw_list flt_kws = { "SPOE", { }, {
4728 { "spoe", parse_spoe_flt, NULL },
4729 { NULL, NULL, NULL },
4730 }
4731};
4732
Willy Tarreau0108d902018-11-25 19:14:37 +01004733INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4734
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004735/* Delcate the action parser for "spoe-action" keyword */
4736static struct action_kw_list tcp_req_action_kws = { { }, {
4737 { "send-spoe-group", parse_send_spoe_group },
4738 { /* END */ },
4739 }
4740};
Willy Tarreau0108d902018-11-25 19:14:37 +01004741
4742INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4743
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004744static struct action_kw_list tcp_res_action_kws = { { }, {
4745 { "send-spoe-group", parse_send_spoe_group },
4746 { /* END */ },
4747 }
4748};
Willy Tarreau0108d902018-11-25 19:14:37 +01004749
4750INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4751
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004752static struct action_kw_list http_req_action_kws = { { }, {
4753 { "send-spoe-group", parse_send_spoe_group },
4754 { /* END */ },
4755 }
4756};
Willy Tarreau0108d902018-11-25 19:14:37 +01004757
4758INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4759
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004760static struct action_kw_list http_res_action_kws = { { }, {
4761 { "send-spoe-group", parse_send_spoe_group },
4762 { /* END */ },
4763 }
4764};
4765
Willy Tarreau0108d902018-11-25 19:14:37 +01004766INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);