blob: 8b2de432c61e281d44ba63b2eead4ef647970109 [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>
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Tim Duesterhus75e2f8d2021-09-16 17:38:26 +020020#include <haproxy/check.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020021#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020022#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020023#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020024#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020025#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020026#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020027#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020028#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020029#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020030#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020031#include <haproxy/signal.h>
Christopher Faulet1d7d0f82021-02-19 10:56:41 +010032#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020033#include <haproxy/spoe.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020036#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020037#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020038#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020039#include <haproxy/time.h>
Willy Tarreaue16ada12021-05-08 12:57:17 +020040#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020041#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020042
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020043
44#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
45#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010046#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020047#else
48#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010049#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020050#endif
51
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010052/* Reserved 4 bytes to the frame size. So a frame and its size can be written
53 * together in a buffer */
54#define MAX_FRAME_SIZE global.tune.bufsize - 4
55
56/* The minimum size for a frame */
57#define MIN_FRAME_SIZE 256
58
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010059/* Reserved for the metadata and the frame type.
60 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010061#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020062
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010063/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010064#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
65
Christopher Faulet3b386a32017-02-23 10:17:15 +010066/* SPOE filter id. Used to identify SPOE filters */
67const char *spoe_filter_id = "SPOE filter";
68
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020069/* Set if the handle on SIGUSR1 is registered */
70static int sighandler_registered = 0;
71
72/* proxy used during the parsing */
73struct proxy *curproxy = NULL;
74
75/* The name of the SPOE engine, used during the parsing */
76char *curengine = NULL;
77
78/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020079/* SPOE agent/group/message used during the parsing */
80struct spoe_agent *curagent = NULL;
81struct spoe_group *curgrp = NULL;
82struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020083
84/* list of SPOE messages and placeholders used during the parsing */
85struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020086struct list curgrps;
87struct list curmphs;
88struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010089struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020090
Christopher Faulet7250b8f2018-03-26 17:19:01 +020091/* list of log servers used during the parsing */
92struct list curlogsrvs;
93
Christopher Faulet0e0f0852018-03-26 17:20:36 +020094/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
95int curpxopts;
96int curpxopts2;
97
Christopher Faulet42bfa462017-01-04 14:14:19 +010098/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +010099DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
100DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200101
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200102struct flt_ops spoe_ops;
103
Christopher Faulet8ef75252017-02-20 22:56:03 +0100104static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200105static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
106static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Faulet6f1296b2021-08-02 17:53:56 +0200107static struct appctx *spoe_create_appctx(struct spoe_config *conf);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200108
109/********************************************************************
110 * helper functions/globals
111 ********************************************************************/
112static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200113spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114{
Christopher Faulet11610f32017-09-21 10:23:10 +0200115 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200117 free(ph->id);
118 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119}
120
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100122spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123{
Christopher Faulet57583e42017-09-04 15:41:09 +0200124 struct spoe_arg *arg, *argback;
125 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200126
127 if (!msg)
128 return;
129 free(msg->id);
130 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200131 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200132 release_sample_expr(arg->expr);
133 free(arg->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200134 LIST_DELETE(&arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200135 free(arg);
136 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200137 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200138 LIST_DELETE(&acl->list);
Christopher Faulet57583e42017-09-04 15:41:09 +0200139 prune_acl(acl);
140 free(acl);
141 }
142 if (msg->cond) {
143 prune_acl_cond(msg->cond);
144 free(msg->cond);
145 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200146 free(msg);
147}
148
149static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200150spoe_release_group(struct spoe_group *grp)
151{
152 if (!grp)
153 return;
154 free(grp->id);
155 free(grp->conf.file);
156 free(grp);
157}
158
159static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100160spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200161{
Christopher Faulet11610f32017-09-21 10:23:10 +0200162 struct spoe_message *msg, *msgback;
163 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200164 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200165
166 if (!agent)
167 return;
168 free(agent->id);
169 free(agent->conf.file);
170 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100171 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100172 free(agent->var_t_process);
173 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200174 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200175 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200176 spoe_release_message(msg);
177 }
178 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200179 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200180 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200181 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100182 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200183 for (i = 0; i < global.nbthread; ++i) {
184 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100185 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200186 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100187 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200188 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200189 free(agent);
190}
191
192static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100193 [SPOE_FRM_ERR_NONE] = "normal",
194 [SPOE_FRM_ERR_IO] = "I/O error",
195 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
196 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
197 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
198 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
199 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
200 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
201 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
202 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
203 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
204 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100205 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100206 [SPOE_FRM_ERR_RES] = "resource allocation error",
207 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200208};
209
210static const char *spoe_event_str[SPOE_EV_EVENTS] = {
211 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
212 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
213 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
214 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
215 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
216
217 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
218 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
219 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
220};
221
222
223#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
224
225static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100226 [SPOE_CTX_ST_NONE] = "NONE",
227 [SPOE_CTX_ST_READY] = "READY",
228 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
229 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
230 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
231 [SPOE_CTX_ST_DONE] = "DONE",
232 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200233};
234
235static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100236 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
237 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
238 [SPOE_APPCTX_ST_IDLE] = "IDLE",
239 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
240 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
241 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
242 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
243 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
244 [SPOE_APPCTX_ST_EXIT] = "EXIT",
245 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200246};
247
248#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100249
Christopher Faulet8ef75252017-02-20 22:56:03 +0100250/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500251 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100252 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100253static char *
254generate_pseudo_uuid()
255{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100256 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800257 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100258}
259
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100260
261static inline void
262spoe_update_stat_time(struct timeval *tv, long *t)
263{
264 if (*t == -1)
265 *t = tv_ms_elapsed(tv, &now);
266 else
267 *t += tv_ms_elapsed(tv, &now);
268 tv_zero(tv);
269}
270
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200271/********************************************************************
272 * Functions that encode/decode SPOE frames
273 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200274/* Helper to get static string length, excluding the terminating null byte */
275#define SLEN(str) (sizeof(str)-1)
276
277/* Predefined key used in HELLO/DISCONNECT frames */
278#define SUPPORTED_VERSIONS_KEY "supported-versions"
279#define VERSION_KEY "version"
280#define MAX_FRAME_SIZE_KEY "max-frame-size"
281#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100282#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100283#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200284#define STATUS_CODE_KEY "status-code"
285#define MSG_KEY "message"
286
287struct spoe_version {
288 char *str;
289 int min;
290 int max;
291};
292
293/* All supported versions */
294static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200295 /* 1.0 is now unsupported because of a bug about frame's flags*/
296 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200297 {NULL, 0, 0}
298};
299
300/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200301#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200302
Christopher Faulet8ef75252017-02-20 22:56:03 +0100303/* Convert a string to a SPOE version value. The string must follow the format
304 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
305 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100307spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309 const char *p, *end;
310 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200311
Christopher Faulet8ef75252017-02-20 22:56:03 +0100312 p = str;
313 end = str+len;
314 maj = min = 0;
315 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200316
Christopher Faulet8ef75252017-02-20 22:56:03 +0100317 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100318 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 /* parse Major number, until the '.' */
322 while (*p != '.') {
323 if (p >= end || *p < '0' || *p > '9')
324 goto out;
325 maj *= 10;
326 maj += (*p - '0');
327 p++;
328 }
329
330 /* check Major version */
331 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200332 goto out;
333
Christopher Faulet8ef75252017-02-20 22:56:03 +0100334 p++; /* skip the '.' */
335 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
336 goto out;
337
338 /* Parse Minor number */
339 while (p < end) {
340 if (*p < '0' || *p > '9')
341 break;
342 min *= 10;
343 min += (*p - '0');
344 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200345 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100346
347 /* check Minor number */
348 if (min > 999)
349 goto out;
350
351 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100352 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100353 p++;
354 if (p != end)
355 goto out;
356
357 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200358 out:
359 return vsn;
360}
361
Christopher Faulet8ef75252017-02-20 22:56:03 +0100362/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800363 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200365static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200367{
Willy Tarreau83061a82018-07-13 11:56:34 +0200368 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100369 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370 char *p, *end;
371 unsigned int flags = SPOE_FRM_FL_FIN;
372 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200373
Christopher Faulet8ef75252017-02-20 22:56:03 +0100374 p = frame;
375 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200376
Christopher Faulet8ef75252017-02-20 22:56:03 +0100377 /* Set Frame type */
378 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200379
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100380 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200381 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100382 memcpy(p, (char *)&flags, 4);
383 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384
385 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200387
388 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
389 * and "capabilities" */
390
391 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100392 sz = SLEN(SUPPORTED_VERSIONS_KEY);
393 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
394 goto too_big;
395
396 *p++ = SPOE_DATA_T_STR;
397 sz = SLEN(SUPPORTED_VERSIONS_VAL);
398 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
399 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200400
401 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100402 sz = SLEN(MAX_FRAME_SIZE_KEY);
403 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
404 goto too_big;
405
406 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200407 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200409
410 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100411 sz = SLEN(CAPABILITIES_KEY);
412 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
413 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200414
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100416 chk = get_trash_chunk();
417 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200418 memcpy(chk->area, "pipelining", 10);
419 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100420 }
421 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200422 if (chk->data) chk->area[chk->data++] = ',';
423 memcpy(chk->area+chk->data, "async", 5);
424 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100425 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100426 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200427 if (chk->data) chk->area[chk->data++] = ',';
428 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100429 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100430 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200431 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100432 goto too_big;
433
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500434 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200435 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 sz = SLEN(ENGINE_ID_KEY);
437 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
438 goto too_big;
439
440 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200441 sz = strlen(agent->rt[tid].engine_id);
442 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100444 }
445
Christopher Faulet8ef75252017-02-20 22:56:03 +0100446 return (p - frame);
447
448 too_big:
449 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
450 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200451}
452
Christopher Faulet8ef75252017-02-20 22:56:03 +0100453/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
454 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
455 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200456static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200458{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 const char *reason;
460 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100461 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200463
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 p = frame;
465 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200466
Christopher Faulet8ef75252017-02-20 22:56:03 +0100467 /* Set Frame type */
468 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200469
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100470 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200471 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100472 memcpy(p, (char *)&flags, 4);
473 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474
475 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476 *p++ = 0; *p++ = 0;
477
478 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
479 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200480
481 /* There are 2 mandatory items: "status-code" and "message" */
482
483 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100484 sz = SLEN(STATUS_CODE_KEY);
485 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
486 goto too_big;
487
488 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200489 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200491
492 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100493 sz = SLEN(MSG_KEY);
494 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
495 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200496
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 /*Get the message corresponding to the status code */
498 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
499
500 *p++ = SPOE_DATA_T_STR;
501 sz = strlen(reason);
502 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
503 goto too_big;
504
505 return (p - frame);
506
507 too_big:
508 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
509 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200510}
511
Christopher Faulet8ef75252017-02-20 22:56:03 +0100512/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
513 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
514 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100517 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200518{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100519 char *p, *end;
520 unsigned int stream_id, frame_id;
521 unsigned int flags = SPOE_FRM_FL_FIN;
522 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200523
Christopher Faulet8ef75252017-02-20 22:56:03 +0100524 p = frame;
525 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200526
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100527 stream_id = ctx->stream_id;
528 frame_id = ctx->frame_id;
529
530 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
531 /* The fragmentation is not supported by the applet */
532 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
533 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
534 return -1;
535 }
536 flags = ctx->frag_ctx.flags;
537 }
538
539 /* Set Frame type */
540 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
541
542 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200543 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100544 memcpy(p, (char *)&flags, 4);
545 p += 4;
546
547 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200548 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100549 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200550 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100551 goto too_big;
552
553 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200554 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100555 if (p + sz >= end)
556 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200557 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100558 p += sz;
559
560 return (p - frame);
561
562 too_big:
563 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
564 return 0;
565}
566
567/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
568 * returns the number of encoded bytes in the frame on success, 0 if an encoding
569 * error occurred and -1 if a fatal error occurred. */
570static int
571spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
572 char *frame, size_t size)
573{
574 char *p, *end;
575 unsigned int stream_id, frame_id;
576 unsigned int flags;
577 size_t sz;
578
579 p = frame;
580 end = frame+size;
581
Christopher Faulet8ef75252017-02-20 22:56:03 +0100582 /* <ctx> is null when the stream has aborted the processing of a
583 * fragmented frame. In this case, we must notify the corresponding
584 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100585 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100586 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100587 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
588 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
589 }
590 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100591 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100592 stream_id = ctx->stream_id;
593 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100594 }
595
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100597 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100599 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200600 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100601 memcpy(p, (char *)&flags, 4);
602 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200603
604 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200605 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100606 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200607 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100608 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200609
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100610 if (ctx == NULL)
611 goto end;
612
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200614 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 if (p + sz >= end)
616 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200617 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100619
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100620 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621 return (p - frame);
622
623 too_big:
624 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
625 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626}
627
Christopher Faulet8ef75252017-02-20 22:56:03 +0100628/* Decode and process the HELLO frame sent by an agent. It returns the number of
629 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
630 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200631static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200633{
Christopher Faulet305c6072017-02-23 16:17:53 +0100634 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
635 char *p, *end;
636 int vsn, max_frame_size;
637 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638
639 p = frame;
640 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200641
642 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
644 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200645 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200647
Christopher Faulet8ef75252017-02-20 22:56:03 +0100648 if (size < 7 /* TYPE + METADATA */) {
649 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
650 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200651 }
652
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100653 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200655 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100656 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200657
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100658 /* Fragmentation is not supported for HELLO frame */
659 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100661 return -1;
662 }
663
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100665 if (*p != 0 || *(p+1) != 0) {
666 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
667 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670
671 /* There are 3 mandatory items: "version", "max-frame-size" and
672 * "capabilities" */
673
674 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100675 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 while (p < end) {
677 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200678 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100679 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680
681 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100682 ret = spoe_decode_buffer(&p, end, &str, &sz);
683 if (ret == -1 || !sz) {
684 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
685 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200688 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200689 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 int i, type = *p++;
691
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100693 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
694 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
695 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
698 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
699 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700 }
701
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100704 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200705 return -1;
706 }
707 for (i = 0; supported_versions[i].str != NULL; ++i) {
708 if (vsn >= supported_versions[i].min &&
709 vsn <= supported_versions[i].max)
710 break;
711 }
712 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200714 return -1;
715 }
716 }
717 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200718 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 +0100719 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200720
721 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200722 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
723 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
724 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
725 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100726 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
727 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200728 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200729 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100730 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
731 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100733 if (sz < MIN_FRAME_SIZE ||
734 sz > SPOE_APPCTX(appctx)->max_frame_size) {
735 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200736 return -1;
737 }
738 max_frame_size = sz;
739 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100740 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200741 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100743
744 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100745 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
746 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
747 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100748 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
750 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
751 return 0;
752 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100753
Christopher Faulet8ef75252017-02-20 22:56:03 +0100754 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100755 char *delim;
756
757 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100758 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759
Christopher Faulet8ef75252017-02-20 22:56:03 +0100760 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
761 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100762 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100763 flags |= SPOE_APPCTX_FL_PIPELINING;
764 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 else if (sz >= 5 && !strncmp(str, "async", 5)) {
766 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100767 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100768 flags |= SPOE_APPCTX_FL_ASYNC;
769 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100770 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
771 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100772 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100773 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
774 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 /* Get the next comma or break */
777 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100778 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100779 delim++;
780 sz -= (delim - str);
781 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100782 }
783 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200784 else {
785 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100786 if (spoe_skip_data(&p, end) == -1) {
787 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
788 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200789 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200790 }
791 }
792
793 /* Final checks */
794 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200796 return -1;
797 }
798 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100799 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800 return -1;
801 }
Christopher Faulet11389012019-02-07 16:13:26 +0100802 if (!agent)
803 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
804 else {
805 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
806 flags &= ~SPOE_APPCTX_FL_PIPELINING;
807 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
808 flags &= ~SPOE_APPCTX_FL_ASYNC;
809 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200810
Christopher Faulet42bfa462017-01-04 14:14:19 +0100811 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
812 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
813 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100814
815 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200816}
817
818/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
819 * bytes on success, 0 if the frame can be ignored and -1 if an error
820 * occurred. */
821static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200823{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100825 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826
827 p = frame;
828 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200829
830 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
832 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835
Christopher Faulet8ef75252017-02-20 22:56:03 +0100836 if (size < 7 /* TYPE + METADATA */) {
837 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
838 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200839 }
840
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100841 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200843 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100844 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200845
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100846 /* Fragmentation is not supported for DISCONNECT frame */
847 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100849 return -1;
850 }
851
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 if (*p != 0 || *(p+1) != 0) {
854 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
855 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858
859 /* There are 2 mandatory items: "status-code" and "message" */
860
861 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 while (p < end) {
863 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200864 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200866
867 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100868 ret = spoe_decode_buffer(&p, end, &str, &sz);
869 if (ret == -1 || !sz) {
870 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
871 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200872 }
873
874 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200875 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877
878 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200879 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
880 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
881 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100883 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
884 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200885 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200886 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
888 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100890 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
892
893 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200894 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 int type = *p++;
896
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200897 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
899 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
900 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200901 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 ret = spoe_decode_buffer(&p, end, &str, &sz);
903 if (ret == -1 || sz > 255) {
904 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
905 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200906 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100907#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
908 SPOE_APPCTX(appctx)->reason = str;
909 SPOE_APPCTX(appctx)->rlen = sz;
910#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200911 }
912 else {
913 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100914 if (spoe_skip_data(&p, end) == -1) {
915 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
916 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200917 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
919 }
920
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200922}
923
924
Christopher Fauleta1cda022016-12-21 08:58:06 +0100925/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926 * success, 0 if the frame can be ignored and -1 if an error occurred. */
927static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100928spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100929 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936
937 p = frame;
938 end = frame + size;
939 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200940
941 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
943 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200946
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947 if (size < 7 /* TYPE + METADATA */) {
948 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
949 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200950 }
951
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100952 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200954 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200956
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100957 /* Fragmentation is not supported for now */
958 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100960 return -1;
961 }
962
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200963 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200964 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100966 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200968 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200970 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100972
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100974 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200975 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100976 if ((*ctx)->stream_id == (unsigned int)stream_id &&
977 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100978 goto found;
979 }
980 }
981 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100982 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
983 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100985 goto found;
986 }
987 }
988
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100989 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
990 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
991 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
992
993 /* ABRT bit is set for an unfinished fragmented frame */
994 if (flags & SPOE_FRM_FL_ABRT) {
995 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100996 (*ctx)->state = SPOE_CTX_ST_ERROR;
997 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
998 /* Ignore the payload */
999 goto end;
1000 }
1001 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1002 /* For now, we ignore the ack */
1003 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1004 return 0;
1005 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001006
Christopher Fauleta1cda022016-12-21 08:58:06 +01001007 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001008 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1009 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001010 " - stream-id=%u - frame-id=%u\n",
1011 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1012 __FUNCTION__, appctx,
1013 (unsigned int)stream_id, (unsigned int)frame_id);
1014
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001015 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001016 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1017 /* Report an error if we are waiting the ack for another frame,
1018 * but not if there is no longer frame waiting for a ack
1019 * (timeout)
1020 */
1021 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1022 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1023 return -1;
1024 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1025 SPOE_APPCTX(appctx)->cur_fpa = 0;
1026 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001027 return 0;
1028
1029 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001030 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1031 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001032 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001033 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001034 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001035
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001036 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001038 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1039 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001040 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001041
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001042 /* Transfer the buffer ownership to the SPOE context */
1043 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001044 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001045
Christopher Faulet8ef75252017-02-20 22:56:03 +01001046 (*ctx)->state = SPOE_CTX_ST_DONE;
1047
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001048 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001049 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 " - ACK frame received"
1051 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001052 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001053 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1054 (*ctx)->frame_id, flags);
1055 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001056}
1057
Christopher Fauletba7bc162016-11-07 21:07:38 +01001058/* This function is used in cfgparse.c and declared in proto/checks.h. It
1059 * prepare the request to send to agents during a healthcheck. It returns 0 on
1060 * success and -1 if an error occurred. */
1061int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001062spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001063{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001064 struct appctx appctx;
1065 struct spoe_appctx spoe_appctx;
1066 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1067 size_t sz;
1068 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069
Christopher Faulet42bfa462017-01-04 14:14:19 +01001070 memset(&appctx, 0, sizeof(appctx));
1071 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001072 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001073
1074 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001075 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001076
Christopher Faulet8ef75252017-02-20 22:56:03 +01001077 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1078 end = frame + MAX_FRAME_SIZE;
1079
1080 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1081 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001084
Christopher Faulet8ef75252017-02-20 22:56:03 +01001085 /* Add "healthcheck" K/V item */
1086 sz = SLEN(HEALTHCHECK_KEY);
1087 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1088 return -1;
1089 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090
Christopher Faulet8ef75252017-02-20 22:56:03 +01001091 *len = frame - buf;
1092 sz = htonl(*len - 4);
1093 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001094
Christopher Faulet8ef75252017-02-20 22:56:03 +01001095 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001098 return 0;
1099}
1100
1101/* This function is used in checks.c and declared in proto/checks.h. It decode
1102 * the response received from an agent during a healthcheck. It returns 0 on
1103 * success and -1 if an error occurred. */
1104int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001105spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001107 struct appctx appctx;
1108 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109
Christopher Faulet42bfa462017-01-04 14:14:19 +01001110 memset(&appctx, 0, sizeof(appctx));
1111 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001112
Christopher Faulet42bfa462017-01-04 14:14:19 +01001113 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001114 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001115
Christopher Faulet8ef75252017-02-20 22:56:03 +01001116 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1117 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001118 goto error;
1119 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001120 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1121 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001122
1123 return 0;
1124
1125 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1127 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1128 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001129 return -1;
1130}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001131
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001133 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134 * success. */
1135static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001137{
1138 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 int ret;
1140 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141
Christopher Faulet8ef75252017-02-20 22:56:03 +01001142 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1143 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001144 netint = htonl(framesz);
1145 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001146 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001148 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001149 si_rx_room_blk(si);
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{
1164 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001165 int ret;
1166 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167
Willy Tarreau06d80a92017-10-19 14:32:15 +02001168 ret = co_getblk(si_oc(si), (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 Tarreau06d80a92017-10-19 14:32:15 +02001175 ret = co_getblk(si_oc(si), 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 Tarreau0cd3bd62018-11-06 18:46:37 +01001193 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001194 si_rx_endp_more(appctx->owner);
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 Fauletf7e4e7e2016-10-27 22:29:49 +02001215/* Callback function that releases a SPOE applet. This happens when the
1216 * connection with the agent is closed. */
1217static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001218spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001219{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001220 struct stream_interface *si = appctx->owner;
1221 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1222 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001223 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001224
1225 if (spoe_appctx == NULL)
1226 return;
1227
1228 appctx->ctx.spoe.ptr = NULL;
1229 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001230
1231 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1232 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1233 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001234
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001236 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001237 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001238 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001239 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001240 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001241 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001242 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001245 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001246 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1247 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001248 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001249 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001250
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001251 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001252 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1253 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001254
1255 si_shutw(si);
1256 si_shutr(si);
1257 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001258 }
1259
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001261 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001262
Christopher Faulet8ef75252017-02-20 22:56:03 +01001263 /* Notify all waiting streams */
1264 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001265 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001266 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001267 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001268 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001269 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001270 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001271 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001272 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001273 }
1274
Christopher Faulet8ef75252017-02-20 22:56:03 +01001275 /* If the applet was processing a fragmented frame, notify the
1276 * corresponding stream. */
1277 if (spoe_appctx->frag_ctx.ctx) {
1278 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001279 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001280 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001281 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001282 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1283 }
1284
Willy Tarreaub042e4f2022-02-15 16:49:37 +01001285 if (!LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) {
Christopher Fauletcf181c72020-11-10 18:45:34 +01001286 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1287 if (ctx->spoe_appctx == spoe_appctx)
1288 ctx->spoe_appctx = NULL;
1289 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001290 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001291 }
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001292 else {
1293 /* It is the last running applet and the sending and waiting
1294 * queues are not empty. Try to start a new one if HAproxy is
1295 * not stopping.
1296 */
1297 if (!stopping &&
1298 (!LIST_ISEMPTY(&agent->rt[tid].sending_queue) || !LIST_ISEMPTY(&agent->rt[tid].waiting_queue)) &&
1299 spoe_create_appctx(agent->spoe_conf))
1300 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001301
Christopher Faulet6f1296b2021-08-02 17:53:56 +02001302 /* otherwise, notify all waiting streams */
1303 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
1304 LIST_DELETE(&ctx->list);
1305 LIST_INIT(&ctx->list);
1306 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
1307 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
1308 ctx->spoe_appctx = NULL;
1309 ctx->state = SPOE_CTX_ST_ERROR;
1310 ctx->status_code = (spoe_appctx->status_code + 0x100);
1311 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1312 }
1313 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1314 LIST_DELETE(&ctx->list);
1315 LIST_INIT(&ctx->list);
1316 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
1317 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1318 ctx->spoe_appctx = NULL;
1319 ctx->state = SPOE_CTX_ST_ERROR;
1320 ctx->status_code = (spoe_appctx->status_code + 0x100);
1321 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1322 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001323 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001324
1325 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001326 /* Release allocated memory */
1327 spoe_release_buffer(&spoe_appctx->buffer,
1328 &spoe_appctx->buffer_wait);
1329 pool_free(pool_head_spoe_appctx, spoe_appctx);
1330
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001331 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001332 agent->rt[tid].frame_size = agent->max_frame_size;
1333 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1334 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001335}
1336
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001337static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001338spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001340 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001341 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001343 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001344
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001345 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1346 /* closed */
1347 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1348 goto exit;
1349 }
1350
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001351 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001352 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001353 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001354 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1355 goto stop;
1356 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001357
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1360 " - Connection timed out\n",
1361 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1362 __FUNCTION__, appctx);
1363 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001365 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001366
Christopher Faulet42bfa462017-01-04 14:14:19 +01001367 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001368 SPOE_APPCTX(appctx)->task->expire =
1369 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001370
Christopher Faulet8ef75252017-02-20 22:56:03 +01001371 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1372 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001373 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001374 ret = spoe_prepare_hahello_frame(appctx, frame,
1375 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001376 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001377 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378
1379 switch (ret) {
1380 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001381 case 0: /* ignore => an error, cannot be ignored */
1382 goto exit;
1383
1384 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001385 goto stop;
1386
Christopher Faulet8ef75252017-02-20 22:56:03 +01001387 default:
1388 /* HELLO frame successfully sent, now wait for the
1389 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001390 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1391 goto next;
1392 }
1393
1394 next:
1395 return 0;
1396 stop:
1397 return 1;
1398 exit:
1399 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1400 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001401}
1402
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001405{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001407 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001408 char *frame;
1409 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001411
Christopher Fauletb067b062017-01-04 16:39:11 +01001412 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001413 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001414 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001415 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001416
Christopher Fauleta1cda022016-12-21 08:58:06 +01001417 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001418 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1419 " - Connection timed out\n",
1420 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1421 __FUNCTION__, appctx);
1422 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001423 goto exit;
1424 }
1425
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001426 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001427 ret = spoe_recv_frame(appctx, frame,
1428 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 if (ret > 1) {
1430 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1431 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1432 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001433 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001434 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001436 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001437
Christopher Fauleta1cda022016-12-21 08:58:06 +01001438 switch (ret) {
1439 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001440 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001441 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1442 goto next;
1443
1444 case 1: /* retry later */
1445 goto stop;
1446
1447 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001448 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001449 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001450 SPOE_APPCTX(appctx)->node.key = 0;
1451 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001452
1453 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001454 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001455 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001456 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457
Christopher Fauleta1cda022016-12-21 08:58:06 +01001458 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001459 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001460 if (trash.data)
1461 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001462
1463 SPOE_APPCTX(appctx)->task->expire =
1464 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001465 return 0;
1466 stop:
1467 return 1;
1468 exit:
1469 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1470 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001471}
1472
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001473
Christopher Fauleta1cda022016-12-21 08:58:06 +01001474static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001475spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001476{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001477 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1478 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001479 char *frame, *buf;
1480 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001481
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1483 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001484 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001485
1486 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1487 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1488 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1489 SPOE_APPCTX(appctx)->max_frame_size);
1490 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001491 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001492 *skip = 1;
1493 ret = 1;
1494 goto end;
1495 }
1496 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001497 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001498 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1499 SPOE_APPCTX(appctx)->max_frame_size);
1500
1501 }
1502
Christopher Faulet8ef75252017-02-20 22:56:03 +01001503 if (ret > 1)
1504 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001505
Christopher Faulet8ef75252017-02-20 22:56:03 +01001506 switch (ret) {
1507 case -1: /* error */
1508 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1509 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001510
Christopher Faulet8ef75252017-02-20 22:56:03 +01001511 case 0: /* ignore */
1512 if (ctx == NULL)
1513 goto abort_frag_frame;
1514
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001515 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001516 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001517 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001518 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001519 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001520 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001521 ctx->state = SPOE_CTX_ST_ERROR;
1522 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1523 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001524 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 break;
1526
1527 case 1: /* retry */
1528 *skip = 1;
1529 break;
1530
1531 default:
1532 if (ctx == NULL)
1533 goto abort_frag_frame;
1534
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001535 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001536 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001537 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001538 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001539 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001540 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001541 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1542 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1543 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001544 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001545 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001546 }
1547 goto end;
1548
1549 frag_frame_sent:
1550 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001551 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001552 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1553 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1554 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1556 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1557 goto end;
1558
1559 no_frag_frame_sent:
1560 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1561 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001562 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001563 }
1564 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1565 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001566 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567 }
1568 else {
1569 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001570 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001571 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001572 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001573 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001574 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001575 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1576 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1577 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001578 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579
Christopher Faulet8ef75252017-02-20 22:56:03 +01001580 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1581 goto end;
1582
1583 abort_frag_frame:
1584 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1585 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1586 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1587 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1588 goto end;
1589
1590 end:
1591 return ret;
1592}
1593
1594static int
1595spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1596{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001597 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001598 struct spoe_context *ctx = NULL;
1599 char *frame;
1600 int ret;
1601
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001602 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001603 ret = spoe_recv_frame(appctx, frame,
1604 SPOE_APPCTX(appctx)->max_frame_size);
1605 if (ret > 1) {
1606 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1607 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001608 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001609 goto end;
1610 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001611 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001612 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1613 }
1614 switch (ret) {
1615 case -1: /* error */
1616 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1617 break;
1618
1619 case 0: /* ignore */
1620 break;
1621
1622 case 1: /* retry */
1623 *skip = 1;
1624 break;
1625
1626 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001627 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001629 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001630 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1631 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001632 if (ctx->spoe_appctx) {
1633 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001634 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001635 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001636 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1637 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1638 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1639 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1640 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1641 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1642 }
1643 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1644 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001645 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1646 break;
1647 }
1648
1649 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001650 if (trash.data)
1651 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001652 end:
1653 return ret;
1654}
1655
1656static int
1657spoe_handle_processing_appctx(struct appctx *appctx)
1658{
1659 struct stream_interface *si = appctx->owner;
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001660 struct server *srv = objt_server(si_strm(si)->target);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001661 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001662 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0, close_asap = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001663
1664 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1665 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1666 goto exit;
1667 }
1668
1669 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1670 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1671 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1672 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1673 goto next;
1674 }
1675
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001676 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001677 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001678 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001679 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1680 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001681 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001682
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001683
1684 /* Close the applet ASAP because some sessions are waiting for a free
1685 * connection slot. It is only an issue in multithreaded mode.
1686 */
1687 close_asap = (global.nbthread > 1 &&
1688 (agent->b.be->queue.length ||
1689 (srv && (srv->queue.length || (srv->maxconn && srv->served >= srv_dynamic_maxconn(srv))))));
1690
1691 /* Don"t try to send new frame we are waiting for at lease a ack, in
1692 * sync mode or if applet must be closed ASAP
1693 */
1694 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK || (close_asap && SPOE_APPCTX(appctx)->cur_fpa))
Christopher Faulet8f82b202018-01-24 16:23:03 +01001695 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 /* receiving_frame loop */
1698 while (!skip_receiving) {
1699 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1700 switch (ret) {
1701 case -1: /* error */
1702 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001703
Christopher Faulet8f82b202018-01-24 16:23:03 +01001704 case 0: /* ignore */
1705 active_r = 1;
1706 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001707
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 case 1: /* retry */
1709 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001710
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 default:
1712 active_r = 1;
1713 break;
1714 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001716
Christopher Faulet8f82b202018-01-24 16:23:03 +01001717 /* send_frame loop */
1718 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1719 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1720 switch (ret) {
1721 case -1: /* error */
1722 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001723
Christopher Faulet8f82b202018-01-24 16:23:03 +01001724 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001725 if (SPOE_APPCTX(appctx)->node.key)
1726 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001727 active_s++;
1728 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001729
Christopher Faulet8f82b202018-01-24 16:23:03 +01001730 case 1: /* retry */
1731 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001732
Christopher Faulet8f82b202018-01-24 16:23:03 +01001733 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001734 if (SPOE_APPCTX(appctx)->node.key)
1735 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001736 active_s++;
1737 break;
1738 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001739
1740 /* if applet must be close ASAP, don't send more than a frame */
1741 if (close_asap)
1742 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001743 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001744
Christopher Faulet8f82b202018-01-24 16:23:03 +01001745 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001746 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1747 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1748 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001749
Christopher Faulet8f82b202018-01-24 16:23:03 +01001750 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001751 /* If applet must be closed, don't switch it in IDLE state and
1752 * close it when the last waiting frame is acknowledged.
Christopher Faulet9e647e52021-03-01 15:01:14 +01001753 */
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001754 if (close_asap) {
1755 if (SPOE_APPCTX(appctx)->cur_fpa)
1756 goto out;
Christopher Faulet9e647e52021-03-01 15:01:14 +01001757 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1758 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1759 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1760 goto next;
1761 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001762 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001763 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001764 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765 }
Christopher Fauletd7da3dd2021-08-02 18:13:27 +02001766
1767 out:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001768 return 1;
1769
Christopher Faulet8f82b202018-01-24 16:23:03 +01001770 next:
1771 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1772 return 0;
1773
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 exit:
1775 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1776 return 0;
1777}
1778
1779static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001780spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781{
1782 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001783 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001784 char *frame, *buf;
1785 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001786
Christopher Fauleta1cda022016-12-21 08:58:06 +01001787 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1788 goto exit;
1789
1790 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1791 goto exit;
1792
Christopher Faulet8ef75252017-02-20 22:56:03 +01001793 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1794 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001795 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001796 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1797 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001799 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001800
1801 switch (ret) {
1802 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001803 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001804 goto exit;
1805
1806 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807 goto stop;
1808
1809 default:
1810 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1811 " - disconnected by HAProxy (%d): %s\n",
1812 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813 __FUNCTION__, appctx,
1814 SPOE_APPCTX(appctx)->status_code,
1815 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816
1817 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1818 goto next;
1819 }
1820
1821 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001822 SPOE_APPCTX(appctx)->task->expire =
1823 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 return 0;
1825 stop:
1826 return 1;
1827 exit:
1828 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1829 return 0;
1830}
1831
1832static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001833spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001834{
1835 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836 char *frame;
1837 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838
Christopher Fauletb067b062017-01-04 16:39:11 +01001839 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001840 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001842 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001843
Christopher Fauletb067b062017-01-04 16:39:11 +01001844 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001847 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001849 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 ret = spoe_recv_frame(appctx, frame,
1851 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001852 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001853 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001854 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855 }
1856
1857 switch (ret) {
1858 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1860 " - error on frame (%s)\n",
1861 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001862 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001863 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865 goto exit;
1866
1867 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto next;
1869
1870 case 1: /* retry */
1871 goto stop;
1872
1873 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001875 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001877 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001878 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1879 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001880 goto exit;
1881 }
1882
1883 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001884 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001885 if (trash.data)
1886 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001887
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888 return 0;
1889 stop:
1890 return 1;
1891 exit:
1892 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1893 return 0;
1894}
1895
1896/* I/O Handler processing messages exchanged with the agent */
1897static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001898spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001899{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001900 struct stream_interface *si = appctx->owner;
1901 struct spoe_agent *agent;
1902
1903 if (SPOE_APPCTX(appctx) == NULL)
1904 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001905
Christopher Faulet8ef75252017-02-20 22:56:03 +01001906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1907 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001908
Christopher Fauleta1cda022016-12-21 08:58:06 +01001909 switchstate:
1910 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1911 " - appctx-state=%s\n",
1912 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1913 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1914
1915 switch (appctx->st0) {
1916 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001917 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001918 goto out;
1919 goto switchstate;
1920
1921 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001922 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001923 goto out;
1924 goto switchstate;
1925
1926 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001927 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001928 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001929 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001930 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001931 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001932 SPOE_APPCTX(appctx)->task->expire =
1933 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001934 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001935 goto switchstate;
1936 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001937 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1938 /* fall through */
1939
1940 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001941 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1942 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001943 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001944 goto out;
1945 goto switchstate;
1946
1947 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001948 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001949 goto out;
1950 goto switchstate;
1951
1952 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001953 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001954 goto out;
1955 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001956
1957 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001958 appctx->st0 = SPOE_APPCTX_ST_END;
1959 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1960
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961 si_shutw(si);
1962 si_shutr(si);
1963 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001964 /* fall through */
1965
1966 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001967 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001968 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001969 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001970 if (stopping)
1971 spoe_wakeup_appctx(appctx);
1972
Christopher Faulet42bfa462017-01-04 14:14:19 +01001973 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1974 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001975}
1976
1977struct applet spoe_applet = {
1978 .obj_type = OBJ_TYPE_APPLET,
1979 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001980 .fct = spoe_handle_appctx,
1981 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001982};
1983
1984/* Create a SPOE applet. On success, the created applet is returned, else
1985 * NULL. */
1986static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001987spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001988{
1989 struct appctx *appctx;
1990 struct session *sess;
Christopher Faulet13a35e52021-12-20 15:34:16 +01001991 struct conn_stream *cs;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001992 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001993
Willy Tarreaue6124462021-09-13 10:07:38 +02001994 if ((appctx = appctx_new(&spoe_applet)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995 goto out_error;
1996
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01001997 appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001998 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 goto out_free_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002000
Christopher Faulet42bfa462017-01-04 14:14:19 +01002001 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreaubeeabf52021-10-01 18:23:30 +02002002 if ((SPOE_APPCTX(appctx)->task = task_new_here()) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01002003 goto out_free_spoe_appctx;
2004
2005 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002006 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002007 SPOE_APPCTX(appctx)->task->context = appctx;
2008 SPOE_APPCTX(appctx)->agent = conf->agent;
2009 SPOE_APPCTX(appctx)->version = 0;
2010 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
2011 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01002012 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002013 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01002014 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01002015
Willy Tarreau90f366b2021-02-20 11:49:49 +01002016 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002017 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002018 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002019
2020 LIST_INIT(&SPOE_APPCTX(appctx)->list);
2021 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022
Willy Tarreau5820a362016-12-22 15:59:02 +01002023 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002024 if (!sess)
2025 goto out_free_spoe;
2026
Christopher Faulet13a35e52021-12-20 15:34:16 +01002027 cs = cs_new(&appctx->obj_type);
2028 if (!cs)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002029 goto out_free_sess;
2030
Christopher Faulet13a35e52021-12-20 15:34:16 +01002031 if ((strm = stream_new(sess, cs, &BUF_NULL)) == NULL)
2032 goto out_free_cs;
2033
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002034 stream_set_backend(strm, conf->agent->b.be);
2035
2036 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002037 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038 appctx_wakeup(appctx);
2039
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040 strm->do_log = NULL;
2041 strm->res.flags |= CF_READ_DONTWAIT;
2042
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002043 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau2b718102021-04-21 07:32:39 +02002044 LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002045 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau4781b152021-04-06 13:53:36 +02002046 _HA_ATOMIC_INC(&conf->agent->counters.applets);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002047
Emeric Brunc60def82017-09-27 14:59:38 +02002048 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049 return appctx;
2050
2051 /* Error unrolling */
Christopher Faulet13a35e52021-12-20 15:34:16 +01002052 out_free_cs:
2053 cs_free(cs);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002054 out_free_sess:
2055 session_free(sess);
2056 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002057 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002058 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002059 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002060 out_free_appctx:
2061 appctx_free(appctx);
2062 out_error:
2063 return NULL;
2064}
2065
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002067spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002068{
2069 struct spoe_config *conf = FLT_CONF(ctx->filter);
2070 struct spoe_agent *agent = conf->agent;
2071 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002072 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002073
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002075 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002076 (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec)))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002077 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002078
2079 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002080 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002081 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2082 ctx->strm);
2083
Christopher Fauleta1cda022016-12-21 08:58:06 +01002084 /* Do not try to create a new applet if there is no server up for the
2085 * agent's backend. */
2086 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2087 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2088 " - cannot create SPOE appctx: no server up\n",
2089 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2090 __FUNCTION__, ctx->strm);
2091 goto end;
2092 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002093
Christopher Fauleta1cda022016-12-21 08:58:06 +01002094 /* Do not try to create a new applet if we have reached the maximum of
2095 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002096 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002097 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002098 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2099 " - cannot create SPOE appctx: max CPS reached\n",
2100 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2101 __FUNCTION__, ctx->strm);
2102 goto end;
2103 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002104 }
2105
Christopher Faulet8ef75252017-02-20 22:56:03 +01002106 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002107 if (appctx == NULL) {
2108 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2109 " - failed to create SPOE appctx\n",
2110 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2111 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002112 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002113 "SPOE: [%s] failed to create SPOE applet\n",
2114 agent->id);
2115
Christopher Fauleta1cda022016-12-21 08:58:06 +01002116 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002117 }
2118
Christopher Fauleta1cda022016-12-21 08:58:06 +01002119 /* Increase the per-process number of cumulated connections */
2120 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002121 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002122
Christopher Fauleta1cda022016-12-21 08:58:06 +01002123 end:
2124 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002125 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002126 ctx->status_code = SPOE_CTX_ERR_RES;
2127 return -1;
2128 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002129
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002130 /* Add the SPOE context in the sending queue if the stream has no applet
2131 * already assigned and wakeup all idle applets. Otherwise, don't queue
2132 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002133 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002134 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2135 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002136 if (ctx->spoe_appctx)
2137 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002138 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002139
2140 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002141 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002142 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002143 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002144 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002145 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002146
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002147 /* Finally try to wakeup an IDLE applet. */
2148 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2149 struct eb32_node *node;
2150
2151 node = eb32_first(&agent->rt[tid].idle_applets);
2152 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2153 if (node && spoe_appctx) {
2154 eb32_delete(&spoe_appctx->node);
2155 spoe_appctx->node.key++;
2156 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2157 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002158 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002159 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002160 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002161}
2162
2163/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002164 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002165 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002166/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2167 * fragmented_content. If the next message can be processed, it returns 0. If
2168 * the message is too big, it returns -1.*/
2169static int
2170spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2171 struct spoe_message *msg, int dir,
2172 char **buf, char *end)
2173{
2174 struct sample *smp;
2175 struct spoe_arg *arg;
2176 int ret;
2177
2178 if (msg->cond) {
2179 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2180 ret = acl_pass(ret);
2181 if (msg->cond->pol == ACL_COND_UNLESS)
2182 ret = !ret;
2183
2184 /* the rule does not match */
2185 if (!ret)
2186 goto next;
2187 }
2188
2189 /* Resume encoding of a SPOE argument */
2190 if (ctx->frag_ctx.curarg != NULL) {
2191 arg = ctx->frag_ctx.curarg;
2192 goto encode_argument;
2193 }
2194
2195 if (ctx->frag_ctx.curoff != UINT_MAX)
2196 goto encode_msg_payload;
2197
2198 /* Check if there is enough space for the message name and the
2199 * number of arguments. It implies <msg->id_len> is encoded on 2
2200 * bytes, at most (< 2288). */
2201 if (*buf + 2 + msg->id_len + 1 > end)
2202 goto too_big;
2203
2204 /* Encode the message name */
2205 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2206 goto too_big;
2207
2208 /* Set the number of arguments for this message */
2209 **buf = msg->nargs;
2210 (*buf)++;
2211
2212 ctx->frag_ctx.curoff = 0;
2213 encode_msg_payload:
2214
2215 /* Loop on arguments */
2216 list_for_each_entry(arg, &msg->args, list) {
2217 ctx->frag_ctx.curarg = arg;
2218 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002219 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002220
2221 encode_argument:
2222 if (ctx->frag_ctx.curoff != UINT_MAX)
2223 goto encode_arg_value;
2224
Joseph Herlantf7f60312018-11-15 13:46:49 -08002225 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002226 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2227 goto too_big;
2228
2229 ctx->frag_ctx.curoff = 0;
2230 encode_arg_value:
2231
Joseph Herlantf7f60312018-11-15 13:46:49 -08002232 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002233 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002234 if (smp) {
2235 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2236 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2237 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002238 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002239 if (ret == -1 || ctx->frag_ctx.curoff)
2240 goto too_big;
2241 }
2242
2243 next:
2244 return 0;
2245
2246 too_big:
2247 return -1;
2248}
2249
Christopher Fauletc718b822017-09-21 16:50:56 +02002250/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2251 * handle fragmented content. On success it returns 1. If an error occurred, -1
2252 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2253 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002254static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002255spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002256 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002257{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002258 struct spoe_config *conf = FLT_CONF(ctx->filter);
2259 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002260 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002261 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002262
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002263 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002264 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002265
Christopher Fauletc718b822017-09-21 16:50:56 +02002266 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2267 /* Resume encoding of a SPOE message */
2268 if (ctx->frag_ctx.curmsg != NULL) {
2269 msg = ctx->frag_ctx.curmsg;
2270 goto encode_evt_message;
2271 }
2272
2273 list_for_each_entry(msg, messages, by_evt) {
2274 ctx->frag_ctx.curmsg = msg;
2275 ctx->frag_ctx.curarg = NULL;
2276 ctx->frag_ctx.curoff = UINT_MAX;
2277
2278 encode_evt_message:
2279 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2280 goto too_big;
2281 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002283 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2284 /* Resume encoding of a SPOE message */
2285 if (ctx->frag_ctx.curmsg != NULL) {
2286 msg = ctx->frag_ctx.curmsg;
2287 goto encode_grp_message;
2288 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002289
Christopher Fauletc718b822017-09-21 16:50:56 +02002290 list_for_each_entry(msg, messages, by_grp) {
2291 ctx->frag_ctx.curmsg = msg;
2292 ctx->frag_ctx.curarg = NULL;
2293 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002294
Christopher Fauletc718b822017-09-21 16:50:56 +02002295 encode_grp_message:
2296 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2297 goto too_big;
2298 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002299 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002300 else
2301 goto skip;
2302
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002303
Christopher Faulet57583e42017-09-04 15:41:09 +02002304 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002305 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002306 goto skip;
2307
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002308 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002309 " - encode %s messages - spoe_appctx=%p"
2310 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002311 (int)now.tv_sec, (int)now.tv_usec,
2312 agent->id, __FUNCTION__, s,
2313 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002314 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002315 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002316
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002317 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002318 ctx->frag_ctx.curmsg = NULL;
2319 ctx->frag_ctx.curarg = NULL;
2320 ctx->frag_ctx.curoff = 0;
2321 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002322
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002323 return 1;
2324
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002325 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002326 /* Return an error if fragmentation is unsupported or if nothing has
2327 * been encoded because its too big and not splittable. */
2328 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002329 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2330 return -1;
2331 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002332
2333 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002334 " - encode fragmented messages - spoe_appctx=%p"
2335 " - curmsg=%p - curarg=%p - curoff=%u"
2336 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002337 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002338 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002339 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002340 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002341
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002342 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002343 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2344 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2345 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002346
2347 skip:
2348 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2349 " - skip the frame because nothing has been encoded\n",
2350 (int)now.tv_sec, (int)now.tv_usec,
2351 agent->id, __FUNCTION__, s);
2352 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353}
2354
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002355
2356/***************************************************************************
2357 * Functions that handle SPOE actions
2358 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359/* Helper function to set a variable */
2360static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002361spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002362 struct sample *smp)
2363{
2364 struct spoe_config *conf = FLT_CONF(ctx->filter);
2365 struct spoe_agent *agent = conf->agent;
2366 char varname[64];
2367
2368 memset(varname, 0, sizeof(varname));
2369 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2370 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002371 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2372 vars_set_by_name(varname, len, smp);
2373 else
2374 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375}
2376
2377/* Helper function to unset a variable */
2378static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002379spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002380 struct sample *smp)
2381{
2382 struct spoe_config *conf = FLT_CONF(ctx->filter);
2383 struct spoe_agent *agent = conf->agent;
2384 char varname[64];
2385
2386 memset(varname, 0, sizeof(varname));
2387 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2388 scope, agent->var_pfx, len, name);
2389 vars_unset_by_name_ifexist(varname, len, smp);
2390}
2391
2392
Christopher Faulet8ef75252017-02-20 22:56:03 +01002393static inline int
2394spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2395 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397 char *str, *scope, *p = *buf;
2398 struct sample smp;
2399 uint64_t sz;
2400 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002401
Christopher Faulet8ef75252017-02-20 22:56:03 +01002402 if (p + 2 >= end)
2403 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002404
Christopher Faulet8ef75252017-02-20 22:56:03 +01002405 /* SET-VAR requires 3 arguments */
2406 if (*p++ != 3)
2407 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002408
Christopher Faulet8ef75252017-02-20 22:56:03 +01002409 switch (*p++) {
2410 case SPOE_SCOPE_PROC: scope = "proc"; break;
2411 case SPOE_SCOPE_SESS: scope = "sess"; break;
2412 case SPOE_SCOPE_TXN : scope = "txn"; break;
2413 case SPOE_SCOPE_REQ : scope = "req"; break;
2414 case SPOE_SCOPE_RES : scope = "res"; break;
2415 default: goto skip;
2416 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2419 goto skip;
2420 memset(&smp, 0, sizeof(smp));
2421 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422
Christopher Faulet8ef75252017-02-20 22:56:03 +01002423 if (spoe_decode_data(&p, end, &smp) == -1)
2424 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002425
Christopher Faulet8ef75252017-02-20 22:56:03 +01002426 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2427 " - set-var '%s.%s.%.*s'\n",
2428 (int)now.tv_sec, (int)now.tv_usec,
2429 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2430 __FUNCTION__, s, scope,
2431 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2432 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002433
Christopher Faulet2469eba2020-10-15 16:08:30 +02002434 if (smp.data.type == SMP_T_ANY)
2435 spoe_unset_var(ctx, scope, str, sz, &smp);
2436 else
2437 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002438
Christopher Faulet8ef75252017-02-20 22:56:03 +01002439 ret = (p - *buf);
2440 *buf = p;
2441 return ret;
2442 skip:
2443 return 0;
2444}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002445
Christopher Faulet8ef75252017-02-20 22:56:03 +01002446static inline int
2447spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2448 char **buf, char *end, int dir)
2449{
2450 char *str, *scope, *p = *buf;
2451 struct sample smp;
2452 uint64_t sz;
2453 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002454
Christopher Faulet8ef75252017-02-20 22:56:03 +01002455 if (p + 2 >= end)
2456 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002457
Christopher Faulet8ef75252017-02-20 22:56:03 +01002458 /* UNSET-VAR requires 2 arguments */
2459 if (*p++ != 2)
2460 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002461
Christopher Faulet8ef75252017-02-20 22:56:03 +01002462 switch (*p++) {
2463 case SPOE_SCOPE_PROC: scope = "proc"; break;
2464 case SPOE_SCOPE_SESS: scope = "sess"; break;
2465 case SPOE_SCOPE_TXN : scope = "txn"; break;
2466 case SPOE_SCOPE_REQ : scope = "req"; break;
2467 case SPOE_SCOPE_RES : scope = "res"; break;
2468 default: goto skip;
2469 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002470
Christopher Faulet8ef75252017-02-20 22:56:03 +01002471 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2472 goto skip;
2473 memset(&smp, 0, sizeof(smp));
2474 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002475
Christopher Faulet8ef75252017-02-20 22:56:03 +01002476 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2477 " - unset-var '%s.%s.%.*s'\n",
2478 (int)now.tv_sec, (int)now.tv_usec,
2479 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2480 __FUNCTION__, s, scope,
2481 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2482 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002483
Christopher Faulet8ef75252017-02-20 22:56:03 +01002484 spoe_unset_var(ctx, scope, str, sz, &smp);
2485
2486 ret = (p - *buf);
2487 *buf = p;
2488 return ret;
2489 skip:
2490 return 0;
2491}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002492
Christopher Faulet8ef75252017-02-20 22:56:03 +01002493/* Process SPOE actions for a specific event. It returns 1 on success. If an
2494 * error occurred, 0 is returned. */
2495static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002496spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002497{
2498 char *p, *end;
2499 int ret;
2500
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002501 p = b_head(&ctx->buffer);
2502 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002503
2504 while (p < end) {
2505 enum spoe_action_type type;
2506
2507 type = *p++;
2508 switch (type) {
2509 case SPOE_ACT_T_SET_VAR:
2510 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2511 if (!ret)
2512 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002513 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002514
Christopher Faulet8ef75252017-02-20 22:56:03 +01002515 case SPOE_ACT_T_UNSET_VAR:
2516 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2517 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002518 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002519 break;
2520
2521 default:
2522 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002523 }
2524 }
2525
2526 return 1;
2527 skip:
2528 return 0;
2529}
2530
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002531/***************************************************************************
2532 * Functions that process SPOE events
2533 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002534static void
2535spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2536 struct spoe_context *ctx, int dir)
2537{
2538 if (!tv_iszero(&ctx->stats.tv_start)) {
2539 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2540 ctx->stats.t_total += ctx->stats.t_process;
2541 tv_zero(&ctx->stats.tv_request);
2542 tv_zero(&ctx->stats.tv_queue);
2543 tv_zero(&ctx->stats.tv_wait);
2544 tv_zero(&ctx->stats.tv_response);
2545 }
2546
2547 if (agent->var_t_process) {
2548 struct sample smp;
2549
2550 memset(&smp, 0, sizeof(smp));
2551 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2552 smp.data.u.sint = ctx->stats.t_process;
2553 smp.data.type = SMP_T_SINT;
2554
2555 spoe_set_var(ctx, "txn", agent->var_t_process,
2556 strlen(agent->var_t_process), &smp);
2557 }
2558
2559 if (agent->var_t_total) {
2560 struct sample smp;
2561
2562 memset(&smp, 0, sizeof(smp));
2563 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2564 smp.data.u.sint = ctx->stats.t_total;
2565 smp.data.type = SMP_T_SINT;
2566
2567 spoe_set_var(ctx, "txn", agent->var_t_total,
2568 strlen(agent->var_t_total), &smp);
2569 }
2570}
2571
2572static void
2573spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2574 struct spoe_context *ctx, int dir)
2575{
2576 if (agent->eps_max > 0)
2577 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2578
2579 if (agent->var_on_error) {
2580 struct sample smp;
2581
2582 memset(&smp, 0, sizeof(smp));
2583 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2584 smp.data.u.sint = ctx->status_code;
2585 smp.data.type = SMP_T_BOOL;
2586
2587 spoe_set_var(ctx, "txn", agent->var_on_error,
2588 strlen(agent->var_on_error), &smp);
2589 }
2590
2591 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2592 ? SPOE_CTX_ST_READY
2593 : SPOE_CTX_ST_NONE);
2594}
2595
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002596static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002597spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002598{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002599 /* If a process is already started for this SPOE context, retry
2600 * later. */
2601 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002602 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002603
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002604 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002605 ctx->stats.tv_start = now;
2606 ctx->stats.tv_request = now;
2607 ctx->stats.t_request = -1;
2608 ctx->stats.t_queue = -1;
2609 ctx->stats.t_waiting = -1;
2610 ctx->stats.t_response = -1;
2611 ctx->stats.t_process = -1;
2612
2613 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002614
Christopher Fauleta1cda022016-12-21 08:58:06 +01002615 /* Set the right flag to prevent request and response processing
2616 * in same time. */
2617 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2618 ? SPOE_CTX_FL_REQ_PROCESS
2619 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002620 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002621}
2622
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002623static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002624spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002625{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002626 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002627
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002628 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2629 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002630 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002631 if (sa) {
2632 if (sa->frag_ctx.ctx == ctx) {
2633 sa->frag_ctx.ctx = NULL;
2634 spoe_wakeup_appctx(sa->owner);
2635 }
2636 else
2637 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002638 }
2639
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002641 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002642 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002643
2644 /* Reset processing timer */
2645 ctx->process_exp = TICK_ETERNITY;
2646
Christopher Faulet8ef75252017-02-20 22:56:03 +01002647 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002648
Christopher Fauletfce747b2018-01-24 15:59:32 +01002649 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002650 ctx->frag_ctx.curmsg = NULL;
2651 ctx->frag_ctx.curarg = NULL;
2652 ctx->frag_ctx.curoff = 0;
2653 ctx->frag_ctx.flags = 0;
2654
Christopher Fauleta1cda022016-12-21 08:58:06 +01002655 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002656 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002657 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002658 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002659 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002660
Willy Tarreau2b718102021-04-21 07:32:39 +02002661 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002662 LIST_INIT(&ctx->list);
2663 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002664}
2665
Christopher Faulet58d03682017-09-21 16:57:24 +02002666/* Process a list of SPOE messages. First, this functions will process messages
2667 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2668 * to process corresponding actions. During all the processing, it returns 0
2669 * and it returns 1 when the processing is finished. If an error occurred, -1
2670 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002671static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002672spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2673 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002674{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002675 struct spoe_config *conf = FLT_CONF(ctx->filter);
2676 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002677 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002678
2679 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002680 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002681
2682 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2683 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002684 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002685 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002686 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002687 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002688 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002689 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002690
2691 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002692 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002693 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002694 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002695 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002696 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002697 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 goto skip;
2699 }
2700 }
2701
Christopher Fauletf7a30922016-11-10 15:04:51 +01002702 if (!tick_isset(ctx->process_exp)) {
2703 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2704 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2705 ctx->process_exp);
2706 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002707 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002708 if (!ret)
2709 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002710
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002711 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002712 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002713 }
2714
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002715 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002716 if (tv_iszero(&ctx->stats.tv_request))
2717 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002718 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002719 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002720 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002721 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002722 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002723 if (!ret)
2724 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002725 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002726 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002727 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2728 }
2729
2730 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002731 if (ctx->spoe_appctx)
2732 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002733 ret = 0;
2734 goto out;
2735 }
2736
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002737 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2738 ret = 0;
2739 goto out;
2740 }
2741
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002742 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002743 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002744 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002745 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002746 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002747 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002748 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002749 }
2750
2751 out:
2752 return ret;
2753
Christopher Fauleta1cda022016-12-21 08:58:06 +01002754 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002755 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002756 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002757 spoe_stop_processing(agent, ctx);
2758 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002759
Christopher Fauleta1cda022016-12-21 08:58:06 +01002760 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002761 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002762 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002763 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002764 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002765 spoe_handle_processing_error(s, agent, ctx, dir);
2766 ret = 1;
2767 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002768 return ret;
2769}
2770
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002771/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2772 * See spoe_process_message for details. */
2773static int
2774spoe_process_group(struct stream *s, struct spoe_context *ctx,
2775 struct spoe_group *group, int dir)
2776{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002777 struct spoe_config *conf = FLT_CONF(ctx->filter);
2778 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002779 int ret;
2780
2781 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2782 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002783 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002784 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2785 group->id);
2786
2787 if (LIST_ISEMPTY(&group->messages))
2788 return 1;
2789
2790 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002791 if (ret && ctx->stats.t_process != -1) {
2792 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002793 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002794 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002795 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002796 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002797 ctx->stats.t_response, ctx->stats.t_process,
2798 agent->counters.idles, agent->counters.applets,
2799 agent->counters.nb_sending, agent->counters.nb_waiting,
2800 agent->counters.nb_errors, agent->counters.nb_processed,
2801 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2802 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2803 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2804 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2805 agent->id, group->id, s->uniq_id, ctx->status_code,
2806 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2807 ctx->stats.t_response, ctx->stats.t_process,
2808 agent->counters.idles, agent->counters.applets,
2809 agent->counters.nb_sending, agent->counters.nb_waiting,
2810 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002811 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002812 return ret;
2813}
2814
Christopher Faulet58d03682017-09-21 16:57:24 +02002815/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2816 * See spoe_process_message for details. */
2817static int
2818spoe_process_event(struct stream *s, struct spoe_context *ctx,
2819 enum spoe_event ev)
2820{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002821 struct spoe_config *conf = FLT_CONF(ctx->filter);
2822 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002823 int dir, ret;
2824
2825 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002826 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002827 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002828 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2829 spoe_event_str[ev]);
2830
2831 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2832
2833 if (LIST_ISEMPTY(&(ctx->events[ev])))
2834 return 1;
2835
2836 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002837 if (ret && ctx->stats.t_process != -1) {
2838 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002839 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002840 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2841 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2842 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002843 ctx->stats.t_response, ctx->stats.t_process,
2844 agent->counters.idles, agent->counters.applets,
2845 agent->counters.nb_sending, agent->counters.nb_waiting,
2846 agent->counters.nb_errors, agent->counters.nb_processed,
2847 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2848 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2849 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2850 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2851 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2852 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2853 ctx->stats.t_response, ctx->stats.t_process,
2854 agent->counters.idles, agent->counters.applets,
2855 agent->counters.nb_sending, agent->counters.nb_waiting,
2856 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002857 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002858 return ret;
2859}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002860
2861/***************************************************************************
2862 * Functions that create/destroy SPOE contexts
2863 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002864static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002865spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002866{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002867 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002868 return 1;
2869
Willy Tarreau2b718102021-04-21 07:32:39 +02002870 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002871 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002872
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002873 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002874 return 1;
2875
Willy Tarreaub4e34762021-09-30 19:02:18 +02002876 LIST_APPEND(&th_ctx->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002877 return 0;
2878}
2879
2880static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002881spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002882{
Willy Tarreau2b718102021-04-21 07:32:39 +02002883 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002884 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002885
2886 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002887 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002888 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002889 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002890 }
2891}
2892
Christopher Faulet4596fb72017-01-11 14:05:19 +01002893static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002894spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002895{
2896 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2897 return 1;
2898}
2899
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002900static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002901spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002902{
2903 struct spoe_config *conf = FLT_CONF(filter);
2904 struct spoe_context *ctx;
2905
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002906 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002907 if (ctx == NULL) {
2908 return NULL;
2909 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002910 ctx->filter = filter;
2911 ctx->state = SPOE_CTX_ST_NONE;
2912 ctx->status_code = SPOE_CTX_ERR_NONE;
2913 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002914 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002915 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002916 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002917 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002918 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002919 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002920 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002921
Christopher Fauletf7a30922016-11-10 15:04:51 +01002922 ctx->stream_id = 0;
2923 ctx->frame_id = 1;
2924 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002925
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002926 tv_zero(&ctx->stats.tv_start);
2927 tv_zero(&ctx->stats.tv_request);
2928 tv_zero(&ctx->stats.tv_queue);
2929 tv_zero(&ctx->stats.tv_wait);
2930 tv_zero(&ctx->stats.tv_response);
2931 ctx->stats.t_request = -1;
2932 ctx->stats.t_queue = -1;
2933 ctx->stats.t_waiting = -1;
2934 ctx->stats.t_response = -1;
2935 ctx->stats.t_process = -1;
2936 ctx->stats.t_total = 0;
2937
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002938 ctx->strm = s;
2939 ctx->state = SPOE_CTX_ST_READY;
2940 filter->ctx = ctx;
2941
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002942 return ctx;
2943}
2944
2945static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002946spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002947{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002948 struct spoe_config *conf = FLT_CONF(filter);
2949 struct spoe_context *ctx = filter->ctx;
2950
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002951 if (!ctx)
2952 return;
2953
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002954 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002955 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002956 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957}
2958
2959static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002960spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002961{
2962 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002963 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002964
2965 tv_zero(&ctx->stats.tv_start);
2966 tv_zero(&ctx->stats.tv_request);
2967 tv_zero(&ctx->stats.tv_queue);
2968 tv_zero(&ctx->stats.tv_wait);
2969 tv_zero(&ctx->stats.tv_response);
2970 ctx->stats.t_request = -1;
2971 ctx->stats.t_queue = -1;
2972 ctx->stats.t_waiting = -1;
2973 ctx->stats.t_response = -1;
2974 ctx->stats.t_process = -1;
2975 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002976}
2977
2978
2979/***************************************************************************
2980 * Hooks that manage the filter lifecycle (init/check/deinit)
2981 **************************************************************************/
2982/* Signal handler: Do a soft stop, wakeup SPOE applet */
2983static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002984spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002985{
2986 struct proxy *p;
2987
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002988 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002989 while (p) {
2990 struct flt_conf *fconf;
2991
2992 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002993 struct spoe_config *conf;
2994 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002995 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002996 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997
Christopher Faulet3b386a32017-02-23 10:17:15 +01002998 if (fconf->id != spoe_filter_id)
2999 continue;
3000
3001 conf = fconf->conf;
3002 agent = conf->agent;
3003
Christopher Faulet24289f22017-09-25 14:48:02 +02003004 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003005 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003006 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
3007 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003008 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003009 }
3010 }
3011 p = p->next;
3012 }
3013}
3014
3015
3016/* Initialize the SPOE filter. Returns -1 on error, else 0. */
3017static int
3018spoe_init(struct proxy *px, struct flt_conf *fconf)
3019{
3020 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003022 /* conf->agent_fe was already initialized during the config
3023 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003024 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003025 conf->agent_fe.cap = PR_CAP_FE;
3026 conf->agent_fe.mode = PR_MODE_TCP;
3027 conf->agent_fe.maxconn = 0;
3028 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
3029 conf->agent_fe.conn_retries = CONN_RETRIES;
3030 conf->agent_fe.accept = frontend_accept;
3031 conf->agent_fe.srv = NULL;
3032 conf->agent_fe.timeout.client = TICK_ETERNITY;
3033 conf->agent_fe.default_target = &spoe_applet.obj_type;
3034 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3035
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003036 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003037 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038 sighandler_registered = 1;
3039 }
3040
Christopher Faulet00292352019-01-09 15:05:10 +01003041 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003042 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003043}
3044
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003045/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003046static void
3047spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3048{
3049 struct spoe_config *conf = fconf->conf;
3050
3051 if (conf) {
3052 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003053
Christopher Faulet8ef75252017-02-20 22:56:03 +01003054 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003055 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003056 free(conf);
3057 }
3058 fconf->conf = NULL;
3059}
3060
3061/* Check configuration of a SPOE filter for a specified proxy.
3062 * Return 1 on error, else 0. */
3063static int
3064spoe_check(struct proxy *px, struct flt_conf *fconf)
3065{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003066 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003067 struct spoe_config *conf = fconf->conf;
3068 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003069 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003070 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003071
Christopher Faulet7ee86672017-09-19 11:08:28 +02003072 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3073 * are uniq */
3074 list_for_each_entry(f, &px->filter_configs, list) {
3075 struct spoe_config *c = f->conf;
3076
3077 /* This is not an SPOE filter */
3078 if (f->id != spoe_filter_id)
3079 continue;
3080 /* This is the current SPOE filter */
3081 if (f == fconf)
3082 continue;
3083
3084 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003085 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003086 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3087 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003088 return 1;
3089 }
3090 }
3091
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003092 target = proxy_be_by_name(conf->agent->b.name);
3093 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003094 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3095 " declared at %s:%d.\n",
3096 px->id, conf->agent->b.name, conf->agent->id,
3097 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003098 return 1;
3099 }
3100 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003101 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3102 " at %s:%d does not support HTTP mode.\n",
3103 px->id, target->id, conf->agent->id,
3104 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003105 return 1;
3106 }
3107
Christopher Fauletfe261552019-03-18 13:57:42 +01003108 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003109 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3110 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3111 return 1;
3112 }
3113 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003114 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003115 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3116 conf->agent->rt[i].processing = 0;
3117 LIST_INIT(&conf->agent->rt[i].applets);
3118 LIST_INIT(&conf->agent->rt[i].sending_queue);
3119 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3120 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003121 }
3122
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003123 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3124 if (logsrv->type == LOG_TARGET_BUFFER) {
3125 struct sink *sink = sink_find(logsrv->ring_name);
3126
3127 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3128 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003129 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003130 px->id, conf->agent->id, conf->agent->conf.file,
3131 conf->agent->conf.line, logsrv->ring_name);
3132 return 1;
3133 }
3134 logsrv->sink = sink;
3135 }
3136 }
3137
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003138 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003139 conf->agent->b.be = target;
3140 return 0;
3141}
3142
Kevin Zhud87b1a52019-09-17 15:05:45 +02003143/* Initializes the SPOE filter for a proxy for a specific thread.
3144 * Returns a negative value if an error occurs. */
3145static int
3146spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3147{
3148 struct spoe_config *conf = fconf->conf;
3149 struct spoe_agent *agent = conf->agent;
3150
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003151 agent->rt[tid].engine_id = generate_pseudo_uuid();
3152 if (agent->rt[tid].engine_id == NULL)
3153 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003154 return 0;
3155}
3156
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003157/**************************************************************************
3158 * Hooks attached to a stream
3159 *************************************************************************/
3160/* Called when a filter instance is created and attach to a stream. It creates
3161 * the context that will be used to process this stream. */
3162static int
3163spoe_start(struct stream *s, struct filter *filter)
3164{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003165 struct spoe_config *conf = FLT_CONF(filter);
3166 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003167 struct spoe_context *ctx;
3168
3169 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003170 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003171 __FUNCTION__, s);
3172
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003173 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003174 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3175 " - failed to create SPOE context\n",
3176 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003177 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003178 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003179 "SPOE: [%s] failed to create SPOE context\n",
3180 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003181 return 0;
3182 }
3183
Christopher Faulet11610f32017-09-21 10:23:10 +02003184 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3186
Christopher Faulet11610f32017-09-21 10:23:10 +02003187 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003188 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3189
Christopher Faulet11610f32017-09-21 10:23:10 +02003190 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 filter->pre_analyzers |= AN_RES_INSPECT;
3192
Christopher Faulet11610f32017-09-21 10:23:10 +02003193 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003194 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3195
Christopher Faulet11610f32017-09-21 10:23:10 +02003196 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003197 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3198
Christopher Faulet11610f32017-09-21 10:23:10 +02003199 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003200 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3201
3202 return 1;
3203}
3204
3205/* Called when a filter instance is detached from a stream. It release the
3206 * attached SPOE context. */
3207static void
3208spoe_stop(struct stream *s, struct filter *filter)
3209{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003210 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3211 (int)now.tv_sec, (int)now.tv_usec,
3212 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3213 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003214 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003215}
3216
Christopher Fauletf7a30922016-11-10 15:04:51 +01003217
3218/*
3219 * Called when the stream is woken up because of expired timer.
3220 */
3221static void
3222spoe_check_timeouts(struct stream *s, struct filter *filter)
3223{
3224 struct spoe_context *ctx = filter->ctx;
3225
Christopher Fauletac580602018-03-20 16:09:20 +01003226 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003227 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003228}
3229
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230/* Called when we are ready to filter data on a channel */
3231static int
3232spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3233{
3234 struct spoe_context *ctx = filter->ctx;
3235 int ret = 1;
3236
3237 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3238 " - ctx-flags=0x%08x\n",
3239 (int)now.tv_sec, (int)now.tv_usec,
3240 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3241 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3242
Christopher Fauletb067b062017-01-04 16:39:11 +01003243 if (ctx->state == SPOE_CTX_ST_NONE)
3244 goto out;
3245
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003246 if (!(chn->flags & CF_ISRESP)) {
3247 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3248 chn->analysers |= AN_REQ_INSPECT_FE;
3249 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3250 chn->analysers |= AN_REQ_INSPECT_BE;
3251
3252 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3253 goto out;
3254
3255 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003256 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003257 if (!ret)
3258 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003259 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3260 }
3261 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003262 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003263 chn->analysers |= AN_RES_INSPECT;
3264
3265 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3266 goto out;
3267
Christopher Faulet8ef75252017-02-20 22:56:03 +01003268 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003269 if (!ret) {
3270 channel_dont_read(chn);
3271 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003272 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003273 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003274 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003275 }
3276
3277 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003278 return ret;
3279}
3280
3281/* Called before a processing happens on a given channel */
3282static int
3283spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3284 struct channel *chn, unsigned an_bit)
3285{
3286 struct spoe_context *ctx = filter->ctx;
3287 int ret = 1;
3288
3289 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3290 " - ctx-flags=0x%08x - ana=0x%08x\n",
3291 (int)now.tv_sec, (int)now.tv_usec,
3292 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3293 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3294 ctx->flags, an_bit);
3295
Christopher Fauletb067b062017-01-04 16:39:11 +01003296 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297 goto out;
3298
3299 switch (an_bit) {
3300 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003301 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003302 break;
3303 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003304 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003305 break;
3306 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003307 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003308 break;
3309 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003310 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 break;
3312 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003313 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003314 break;
3315 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003316 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 break;
3318 }
3319
3320 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003321 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 channel_dont_read(chn);
3323 channel_dont_close(chn);
3324 }
3325 return ret;
3326}
3327
3328/* Called when the filtering on the channel ends. */
3329static int
3330spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3331{
3332 struct spoe_context *ctx = filter->ctx;
3333
3334 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3335 " - ctx-flags=0x%08x\n",
3336 (int)now.tv_sec, (int)now.tv_usec,
3337 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3338 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3339
3340 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003341 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003342 }
3343
3344 return 1;
3345}
3346
3347/********************************************************************
3348 * Functions that manage the filter initialization
3349 ********************************************************************/
3350struct flt_ops spoe_ops = {
3351 /* Manage SPOE filter, called for each filter declaration */
3352 .init = spoe_init,
3353 .deinit = spoe_deinit,
3354 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003355 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003356
3357 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003358 .attach = spoe_start,
3359 .detach = spoe_stop,
3360 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003361
3362 /* Handle channels activity */
3363 .channel_start_analyze = spoe_start_analyze,
3364 .channel_pre_analyze = spoe_chn_pre_analyze,
3365 .channel_end_analyze = spoe_end_analyze,
3366};
3367
3368
3369static int
3370cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3371{
3372 const char *err;
3373 int i, err_code = 0;
3374
3375 if ((cfg_scope == NULL && curengine != NULL) ||
3376 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003377 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 goto out;
3379
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003380 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003382 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3383 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003384 err_code |= ERR_ALERT | ERR_ABORT;
3385 goto out;
3386 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003387 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3388 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 goto out;
3390 }
3391
3392 err = invalid_char(args[1]);
3393 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003394 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3395 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003396 err_code |= ERR_ALERT | ERR_ABORT;
3397 goto out;
3398 }
3399
3400 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003401 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3402 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003403 err_code |= ERR_ALERT | ERR_ABORT;
3404 goto out;
3405 }
3406 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003407 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003408 err_code |= ERR_ALERT | ERR_ABORT;
3409 goto out;
3410 }
3411
3412 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003413
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003414 curagent->conf.file = strdup(file);
3415 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003416
3417 curagent->timeout.hello = TICK_ETERNITY;
3418 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003419 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003420
Christopher Fauleta1cda022016-12-21 08:58:06 +01003421 curagent->var_pfx = NULL;
3422 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003423 curagent->var_t_process = NULL;
3424 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003425 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003426 curagent->cps_max = 0;
3427 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003428 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003429 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003430
3431 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003432 LIST_INIT(&curagent->events[i]);
3433 LIST_INIT(&curagent->groups);
3434 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003435 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003436 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003437 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003438 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3439 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003440 err_code |= ERR_ALERT | ERR_FATAL;
3441 goto out;
3442 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003443 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 free(curagent->b.name);
3446 curagent->b.name = strdup(args[1]);
3447 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003448 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003449 int cur_arg = 1;
3450 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003451 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003452
Christopher Faulet11610f32017-09-21 10:23:10 +02003453 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003454 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003455 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3456 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003457 err_code |= ERR_ALERT | ERR_FATAL;
3458 goto out;
3459 }
3460 }
3461
Christopher Faulet11610f32017-09-21 10:23:10 +02003462 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003463 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003464 err_code |= ERR_ALERT | ERR_ABORT;
3465 goto out;
3466 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003467 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003468 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003469 cur_arg++;
3470 }
3471 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003472 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003473 int cur_arg = 1;
3474 while (*args[cur_arg]) {
3475 struct spoe_placeholder *ph = NULL;
3476
3477 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003478 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003479 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3480 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003481 err_code |= ERR_ALERT | ERR_FATAL;
3482 goto out;
3483 }
3484 }
3485
3486 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003487 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003488 err_code |= ERR_ALERT | ERR_ABORT;
3489 goto out;
3490 }
3491 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003492 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003493 cur_arg++;
3494 }
3495 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003496 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003497 unsigned int *tv = NULL;
3498 const char *res;
3499 unsigned timeout;
3500
3501 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003502 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3503 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003504 err_code |= ERR_ALERT | ERR_FATAL;
3505 goto out;
3506 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003507 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3508 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003509 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003510 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003511 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003512 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003513 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003514 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003515 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3517 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003518 err_code |= ERR_ALERT | ERR_FATAL;
3519 goto out;
3520 }
3521 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003522 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3523 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003524 err_code |= ERR_ALERT | ERR_FATAL;
3525 goto out;
3526 }
3527 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003528 if (res == PARSE_TIME_OVER) {
3529 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3530 file, linenum, args[2], args[0], args[1]);
3531 err_code |= ERR_ALERT | ERR_FATAL;
3532 goto out;
3533 }
3534 else if (res == PARSE_TIME_UNDER) {
3535 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3536 file, linenum, args[2], args[0], args[1]);
3537 err_code |= ERR_ALERT | ERR_FATAL;
3538 goto out;
3539 }
3540 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003541 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3542 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003543 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003544 goto out;
3545 }
3546 *tv = MS_TO_TICKS(timeout);
3547 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003548 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003549 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003550 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3551 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003552 err_code |= ERR_ALERT | ERR_FATAL;
3553 goto out;
3554 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003555
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003556 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003557 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003558 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003559 if (kwm == 1)
3560 curagent->flags &= ~SPOE_FL_PIPELINING;
3561 else
3562 curagent->flags |= SPOE_FL_PIPELINING;
3563 goto out;
3564 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003565 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003566 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003567 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003568 if (kwm == 1)
3569 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003570 else
3571 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003572 goto out;
3573 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003574 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003575 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3576 goto out;
3577 if (kwm == 1)
3578 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3579 else
3580 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3581 goto out;
3582 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003583 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003584 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3585 goto out;
3586 if (kwm == 1)
3587 curpxopts2 &= ~PR_O2_NOLOGNORM;
3588 else
3589 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003590 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003591 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003592
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003593 /* Following options does not support negation */
3594 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003595 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3596 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003597 err_code |= ERR_ALERT | ERR_FATAL;
3598 goto out;
3599 }
3600
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003601 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003602 char *tmp;
3603
3604 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003605 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3606 file, linenum, args[0],
3607 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003608 err_code |= ERR_ALERT | ERR_FATAL;
3609 goto out;
3610 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003611 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3612 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003613 tmp = args[2];
3614 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003615 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003616 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003617 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003618 err_code |= ERR_ALERT | ERR_FATAL;
3619 goto out;
3620 }
3621 tmp++;
3622 }
3623 curagent->var_pfx = strdup(args[2]);
3624 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003625 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003626 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3627 goto out;
3628 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3629 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003630 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003631 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003632 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003633 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3634 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003635 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003636 char *tmp;
3637
3638 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003639 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3640 file, linenum, args[0],
3641 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003642 err_code |= ERR_ALERT | ERR_FATAL;
3643 goto out;
3644 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003645 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3646 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003647 tmp = args[2];
3648 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003649 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003650 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003651 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003652 err_code |= ERR_ALERT | ERR_FATAL;
3653 goto out;
3654 }
3655 tmp++;
3656 }
3657 curagent->var_on_error = strdup(args[2]);
3658 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003659 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003660 char *tmp;
3661
3662 if (!*args[2]) {
3663 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3664 file, linenum, args[0],
3665 args[1]);
3666 err_code |= ERR_ALERT | ERR_FATAL;
3667 goto out;
3668 }
3669 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3670 goto out;
3671 tmp = args[2];
3672 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003673 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003674 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003675 file, linenum, args[0], args[1]);
3676 err_code |= ERR_ALERT | ERR_FATAL;
3677 goto out;
3678 }
3679 tmp++;
3680 }
3681 curagent->var_t_process = strdup(args[2]);
3682 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003683 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003684 char *tmp;
3685
3686 if (!*args[2]) {
3687 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3688 file, linenum, args[0],
3689 args[1]);
3690 err_code |= ERR_ALERT | ERR_FATAL;
3691 goto out;
3692 }
3693 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3694 goto out;
3695 tmp = args[2];
3696 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003697 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003698 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003699 file, linenum, args[0], args[1]);
3700 err_code |= ERR_ALERT | ERR_FATAL;
3701 goto out;
3702 }
3703 tmp++;
3704 }
3705 curagent->var_t_total = strdup(args[2]);
3706 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003707 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003708 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3709 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003710 err_code |= ERR_ALERT | ERR_FATAL;
3711 goto out;
3712 }
Christopher Faulet48026722016-11-16 15:01:12 +01003713 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003714 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003715 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003716 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3717 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003718 err_code |= ERR_ALERT | ERR_FATAL;
3719 goto out;
3720 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003721 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003722 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003723 curagent->cps_max = atol(args[1]);
3724 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003725 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003726 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003727 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3728 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003729 err_code |= ERR_ALERT | ERR_FATAL;
3730 goto out;
3731 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003732 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003733 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003734 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003735 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003736 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003737 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003738 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3739 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003740 err_code |= ERR_ALERT | ERR_FATAL;
3741 goto out;
3742 }
3743 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3744 goto out;
3745 curagent->max_frame_size = atol(args[1]);
3746 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3747 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003748 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3749 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003750 err_code |= ERR_ALERT | ERR_FATAL;
3751 goto out;
3752 }
3753 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003754 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003755 if (!*args[1]) {
3756 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3757 file, linenum, args[0]);
3758 err_code |= ERR_ALERT | ERR_FATAL;
3759 goto out;
3760 }
3761 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3762 goto out;
3763 curagent->max_fpa = atol(args[1]);
3764 if (curagent->max_fpa < 1) {
3765 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3766 file, linenum, args[0]);
3767 err_code |= ERR_ALERT | ERR_FATAL;
3768 goto out;
3769 }
3770 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003771 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003772 int cur_arg;
3773
3774 if (!*args[1]) {
3775 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3776 file, linenum, args[0]);
3777 err_code |= ERR_ALERT | ERR_FATAL;
3778 goto out;
3779 }
3780 cur_arg = 1;
3781 while (*args[cur_arg]) {
3782 struct spoe_var_placeholder *vph;
3783
3784 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3785 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3786 err_code |= ERR_ALERT | ERR_ABORT;
3787 goto out;
3788 }
3789 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003790 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003791 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3792 err_code |= ERR_ALERT | ERR_ABORT;
3793 goto out;
3794 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003795 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003796 cur_arg++;
3797 }
3798 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003799 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003800 char *errmsg = NULL;
3801
Emeric Brun9533a702021-04-02 10:13:43 +02003802 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003803 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3804 err_code |= ERR_ALERT | ERR_FATAL;
3805 goto out;
3806 }
3807 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003808 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003809 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3810 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003811 err_code |= ERR_ALERT | ERR_FATAL;
3812 goto out;
3813 }
3814 out:
3815 return err_code;
3816}
Christopher Faulet11610f32017-09-21 10:23:10 +02003817static int
3818cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3819{
3820 struct spoe_group *grp;
3821 const char *err;
3822 int err_code = 0;
3823
3824 if ((cfg_scope == NULL && curengine != NULL) ||
3825 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003826 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003827 goto out;
3828
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003829 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003830 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003831 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3832 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003833 err_code |= ERR_ALERT | ERR_ABORT;
3834 goto out;
3835 }
3836 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3837 err_code |= ERR_ABORT;
3838 goto out;
3839 }
3840
3841 err = invalid_char(args[1]);
3842 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003843 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3844 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003845 err_code |= ERR_ALERT | ERR_ABORT;
3846 goto out;
3847 }
3848
3849 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003850 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003851 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3852 " name as another one declared at %s:%d.\n",
3853 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003854 err_code |= ERR_ALERT | ERR_FATAL;
3855 goto out;
3856 }
3857 }
3858
3859 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003860 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003861 err_code |= ERR_ALERT | ERR_ABORT;
3862 goto out;
3863 }
3864
3865 curgrp->id = strdup(args[1]);
3866 curgrp->conf.file = strdup(file);
3867 curgrp->conf.line = linenum;
3868 LIST_INIT(&curgrp->phs);
3869 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003870 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003871 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003872 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003873 int cur_arg = 1;
3874 while (*args[cur_arg]) {
3875 struct spoe_placeholder *ph = NULL;
3876
3877 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003878 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003879 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3880 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003881 err_code |= ERR_ALERT | ERR_FATAL;
3882 goto out;
3883 }
3884 }
3885
3886 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003887 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003888 err_code |= ERR_ALERT | ERR_ABORT;
3889 goto out;
3890 }
3891 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003892 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003893 cur_arg++;
3894 }
3895 }
3896 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003897 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3898 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003899 err_code |= ERR_ALERT | ERR_FATAL;
3900 goto out;
3901 }
3902 out:
3903 return err_code;
3904}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003905
3906static int
3907cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3908{
3909 struct spoe_message *msg;
3910 struct spoe_arg *arg;
3911 const char *err;
3912 char *errmsg = NULL;
3913 int err_code = 0;
3914
3915 if ((cfg_scope == NULL && curengine != NULL) ||
3916 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003917 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003918 goto out;
3919
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003920 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003921 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003922 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3923 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003924 err_code |= ERR_ALERT | ERR_ABORT;
3925 goto out;
3926 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003927 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3928 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003929 goto out;
3930 }
3931
3932 err = invalid_char(args[1]);
3933 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003934 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3935 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003936 err_code |= ERR_ALERT | ERR_ABORT;
3937 goto out;
3938 }
3939
3940 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003941 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003942 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3943 " name as another one declared at %s:%d.\n",
3944 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003945 err_code |= ERR_ALERT | ERR_FATAL;
3946 goto out;
3947 }
3948 }
3949
3950 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003951 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003952 err_code |= ERR_ALERT | ERR_ABORT;
3953 goto out;
3954 }
3955
3956 curmsg->id = strdup(args[1]);
3957 curmsg->id_len = strlen(curmsg->id);
3958 curmsg->event = SPOE_EV_NONE;
3959 curmsg->conf.file = strdup(file);
3960 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003961 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003962 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003963 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003964 LIST_INIT(&curmsg->by_evt);
3965 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003966 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003967 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003968 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003969 int cur_arg = 1;
3970
3971 curproxy->conf.args.ctx = ARGC_SPOE;
3972 curproxy->conf.args.file = file;
3973 curproxy->conf.args.line = linenum;
3974 while (*args[cur_arg]) {
3975 char *delim = strchr(args[cur_arg], '=');
3976 int idx = 0;
3977
3978 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003979 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003980 err_code |= ERR_ALERT | ERR_ABORT;
3981 goto out;
3982 }
3983
3984 if (!delim) {
3985 arg->name = NULL;
3986 arg->name_len = 0;
3987 delim = args[cur_arg];
3988 }
3989 else {
3990 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3991 arg->name_len = delim - args[cur_arg];
3992 delim++;
3993 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003994 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3995 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003996 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003997 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003998 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003999 err_code |= ERR_ALERT | ERR_FATAL;
4000 free(arg->name);
4001 free(arg);
4002 goto out;
4003 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01004004 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02004005 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004006 cur_arg++;
4007 }
4008 curproxy->conf.args.file = NULL;
4009 curproxy->conf.args.line = 0;
4010 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004011 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02004012 err = invalid_char(args[1]);
4013 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004014 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
4015 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004016 err_code |= ERR_ALERT | ERR_FATAL;
4017 goto out;
4018 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004019 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01004020 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01004021 "logical disjunction within a condition.\n",
4022 file, linenum, args[1]);
4023 err_code |= ERR_ALERT | ERR_FATAL;
4024 goto out;
4025 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004026 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004027 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4028 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004029 err_code |= ERR_ALERT | ERR_FATAL;
4030 goto out;
4031 }
4032 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004033 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004034 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004035 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004036 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004037 goto out;
4038 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004039 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4040 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004041
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004042 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004043 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004044 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004045 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4046
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004047 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004048 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004049 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004050 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004051 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004052 curmsg->event = SPOE_EV_ON_TCP_RSP;
4053
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004054 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004055 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004056 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004057 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004058 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004059 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4060 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004061 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004062 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004063 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004064 goto out;
4065 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004066
4067 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4068 struct acl_cond *cond;
4069
4070 cond = build_acl_cond(file, linenum, &curmsg->acls,
4071 curproxy, (const char **)args+2,
4072 &errmsg);
4073 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004074 ha_alert("parsing [%s:%d] : error detected while "
4075 "parsing an 'event %s' condition : %s.\n",
4076 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004077 err_code |= ERR_ALERT | ERR_FATAL;
4078 goto out;
4079 }
4080 curmsg->cond = cond;
4081 }
4082 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004083 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4084 "or 'unless' followed by a condition but found '%s'.\n",
4085 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004086 err_code |= ERR_ALERT | ERR_FATAL;
4087 goto out;
4088 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004089 }
4090 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004091 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4092 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004093 err_code |= ERR_ALERT | ERR_FATAL;
4094 goto out;
4095 }
4096 out:
4097 free(errmsg);
4098 return err_code;
4099}
4100
4101/* Return -1 on error, else 0 */
4102static int
4103parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4104 struct flt_conf *fconf, char **err, void *private)
4105{
4106 struct list backup_sections;
4107 struct spoe_config *conf;
4108 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004109 struct spoe_group *grp, *grpback;
4110 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004111 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004112 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004113 char *file = NULL, *engine = NULL;
4114 int ret, pos = *cur_arg + 1;
4115
Christopher Faulet84c844e2018-03-23 14:37:14 +01004116 LIST_INIT(&curmsgs);
4117 LIST_INIT(&curgrps);
4118 LIST_INIT(&curmphs);
4119 LIST_INIT(&curgphs);
4120 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004121 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004122 curpxopts = 0;
4123 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004124
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004125 conf = calloc(1, sizeof(*conf));
4126 if (conf == NULL) {
4127 memprintf(err, "%s: out of memory", args[*cur_arg]);
4128 goto error;
4129 }
4130 conf->proxy = px;
4131
4132 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004133 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004134 if (!*args[pos+1]) {
4135 memprintf(err, "'%s' : '%s' option without value",
4136 args[*cur_arg], args[pos]);
4137 goto error;
4138 }
4139 file = args[pos+1];
4140 pos += 2;
4141 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004142 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004143 if (!*args[pos+1]) {
4144 memprintf(err, "'%s' : '%s' option without value",
4145 args[*cur_arg], args[pos]);
4146 goto error;
4147 }
4148 engine = args[pos+1];
4149 pos += 2;
4150 }
4151 else {
4152 memprintf(err, "unknown keyword '%s'", args[pos]);
4153 goto error;
4154 }
4155 }
4156 if (file == NULL) {
4157 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4158 goto error;
4159 }
4160
4161 /* backup sections and register SPOE sections */
4162 LIST_INIT(&backup_sections);
4163 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004164 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4165 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004166 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004167
4168 /* Parse SPOE filter configuration file */
4169 curengine = engine;
4170 curproxy = px;
4171 curagent = NULL;
4172 curmsg = NULL;
4173 ret = readcfgfile(file);
4174 curproxy = NULL;
4175
4176 /* unregister SPOE sections and restore previous sections */
4177 cfg_unregister_sections();
4178 cfg_restore_sections(&backup_sections);
4179
4180 if (ret == -1) {
4181 memprintf(err, "Could not open configuration file %s : %s",
4182 file, strerror(errno));
4183 goto error;
4184 }
4185 if (ret & (ERR_ABORT|ERR_FATAL)) {
4186 memprintf(err, "Error(s) found in configuration file %s", file);
4187 goto error;
4188 }
4189
4190 /* Check SPOE agent */
4191 if (curagent == NULL) {
4192 memprintf(err, "No SPOE agent found in file %s", file);
4193 goto error;
4194 }
4195 if (curagent->b.name == NULL) {
4196 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4197 curagent->id, curagent->conf.file, curagent->conf.line);
4198 goto error;
4199 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004200 if (curagent->timeout.hello == TICK_ETERNITY ||
4201 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004202 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004203 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4204 " | While not properly invalid, you will certainly encounter various problems\n"
4205 " | with such a configuration. To fix this, please ensure that all following\n"
4206 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4207 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004208 }
4209 if (curagent->var_pfx == NULL) {
4210 char *tmp = curagent->id;
4211
4212 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004213 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004214 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4215 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4216 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4217 goto error;
4218 }
4219 tmp++;
4220 }
4221 curagent->var_pfx = strdup(curagent->id);
4222 }
4223
Christopher Fauletb7426d12018-03-21 14:12:17 +01004224 if (curagent->var_on_error) {
4225 struct arg arg;
4226
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004227 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004228 curagent->var_pfx, curagent->var_on_error);
4229
4230 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004231 arg.data.str.area = trash.area;
4232 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004233 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004234 if (!vars_check_arg(&arg, err)) {
4235 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4236 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4237 goto error;
4238 }
4239 }
4240
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004241 if (curagent->var_t_process) {
4242 struct arg arg;
4243
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004244 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004245 curagent->var_pfx, curagent->var_t_process);
4246
4247 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004248 arg.data.str.area = trash.area;
4249 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004250 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004251 if (!vars_check_arg(&arg, err)) {
4252 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4253 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4254 goto error;
4255 }
4256 }
4257
4258 if (curagent->var_t_total) {
4259 struct arg arg;
4260
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004261 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004262 curagent->var_pfx, curagent->var_t_total);
4263
4264 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004265 arg.data.str.area = trash.area;
4266 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004267 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004268 if (!vars_check_arg(&arg, err)) {
4269 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4270 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4271 goto error;
4272 }
4273 }
4274
Christopher Faulet11610f32017-09-21 10:23:10 +02004275 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004276 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4277 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004278 goto finish;
4279 }
4280
Christopher Faulet11610f32017-09-21 10:23:10 +02004281 /* Replace placeholders by the corresponding messages for the SPOE
4282 * agent */
4283 list_for_each_entry(ph, &curmphs, list) {
4284 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004285 struct spoe_arg *arg;
4286 unsigned int where;
4287
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004288 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004289 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4290 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4291 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4292 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4293 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4294 }
4295 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4296 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4297 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004298 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4299 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004300 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004301 }
4302 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004303 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4304 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004305 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004306 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004307
4308 where = 0;
4309 switch (msg->event) {
4310 case SPOE_EV_ON_CLIENT_SESS:
4311 where |= SMP_VAL_FE_CON_ACC;
4312 break;
4313
4314 case SPOE_EV_ON_TCP_REQ_FE:
4315 where |= SMP_VAL_FE_REQ_CNT;
4316 break;
4317
4318 case SPOE_EV_ON_HTTP_REQ_FE:
4319 where |= SMP_VAL_FE_HRQ_HDR;
4320 break;
4321
4322 case SPOE_EV_ON_TCP_REQ_BE:
4323 if (px->cap & PR_CAP_FE)
4324 where |= SMP_VAL_FE_REQ_CNT;
4325 if (px->cap & PR_CAP_BE)
4326 where |= SMP_VAL_BE_REQ_CNT;
4327 break;
4328
4329 case SPOE_EV_ON_HTTP_REQ_BE:
4330 if (px->cap & PR_CAP_FE)
4331 where |= SMP_VAL_FE_HRQ_HDR;
4332 if (px->cap & PR_CAP_BE)
4333 where |= SMP_VAL_BE_HRQ_HDR;
4334 break;
4335
4336 case SPOE_EV_ON_SERVER_SESS:
4337 where |= SMP_VAL_BE_SRV_CON;
4338 break;
4339
4340 case SPOE_EV_ON_TCP_RSP:
4341 if (px->cap & PR_CAP_FE)
4342 where |= SMP_VAL_FE_RES_CNT;
4343 if (px->cap & PR_CAP_BE)
4344 where |= SMP_VAL_BE_RES_CNT;
4345 break;
4346
4347 case SPOE_EV_ON_HTTP_RSP:
4348 if (px->cap & PR_CAP_FE)
4349 where |= SMP_VAL_FE_HRS_HDR;
4350 if (px->cap & PR_CAP_BE)
4351 where |= SMP_VAL_BE_HRS_HDR;
4352 break;
4353
4354 default:
4355 break;
4356 }
4357
4358 list_for_each_entry(arg, &msg->args, list) {
4359 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004360 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004361 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004362 "none of which is available here ('%s')",
4363 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004364 sample_ckp_names(arg->expr->fetch->use),
4365 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004366 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004367 }
4368 }
4369
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004370 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004371 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004372 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004373 }
4374 }
4375 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004376 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004377 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004378 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004379 continue;
4380 }
4381
Christopher Faulet11610f32017-09-21 10:23:10 +02004382 /* Replace placeholders by the corresponding groups for the SPOE
4383 * agent */
4384 list_for_each_entry(ph, &curgphs, list) {
4385 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004386 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004387 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004388 LIST_DELETE(&grp->list);
4389 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004390 goto next_aph;
4391 }
4392 }
4393 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4394 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4395 goto error;
4396 next_aph:
4397 continue;
4398 }
4399
4400 /* Replace placeholders by the corresponding message for each SPOE
4401 * group of the SPOE agent */
4402 list_for_each_entry(grp, &curagent->groups, list) {
4403 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4404 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004405 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004406 if (msg->group != NULL) {
4407 memprintf(err, "SPOE message '%s' already belongs to "
4408 "the SPOE group '%s' declare at %s:%d",
4409 msg->id, msg->group->id,
4410 msg->group->conf.file,
4411 msg->group->conf.line);
4412 goto error;
4413 }
4414
4415 /* Scope for arguments are not checked for now. We will check
4416 * them only if a rule use the corresponding SPOE group. */
4417 msg->agent = curagent;
4418 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004419 LIST_DELETE(&ph->list);
4420 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004421 goto next_mph_grp;
4422 }
4423 }
4424 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4425 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4426 goto error;
4427 next_mph_grp:
4428 continue;
4429 }
4430 }
4431
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004432 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004433 /* move curmsgs to the agent message list */
4434 curmsgs.n->p = &curagent->messages;
4435 curmsgs.p->n = &curagent->messages;
4436 curagent->messages = curmsgs;
4437 LIST_INIT(&curmsgs);
4438
Christopher Faulet7ee86672017-09-19 11:08:28 +02004439 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004440 conf->agent = curagent;
Christopher Faulet434b8522021-08-02 17:51:01 +02004441 curagent->spoe_conf = conf;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004442
4443 /* Start agent's proxy initialization here. It will be finished during
4444 * the filter init. */
4445 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4446 init_new_proxy(&conf->agent_fe);
4447 conf->agent_fe.id = conf->agent->id;
4448 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004449 conf->agent_fe.options |= curpxopts;
4450 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004451
4452 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004453 LIST_DELETE(&logsrv->list);
4454 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004455 }
4456
Christopher Faulet11610f32017-09-21 10:23:10 +02004457 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004458 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004459 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004460 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004461 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004462 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004463 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004464 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004465 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4466 struct arg arg;
4467
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004468 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004469 curagent->var_pfx, vph->name);
4470
4471 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004472 arg.data.str.area = trash.area;
4473 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004474 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004475 if (!vars_check_arg(&arg, err)) {
4476 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4477 curagent->id, curagent->var_pfx, vph->name, *err);
4478 goto error;
4479 }
4480
Willy Tarreau2b718102021-04-21 07:32:39 +02004481 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004482 free(vph->name);
4483 free(vph);
4484 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004485 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004486 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004487 spoe_release_group(grp);
4488 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004489 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004490 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004491 fconf->ops = &spoe_ops;
4492 fconf->conf = conf;
4493 return 0;
4494
4495 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004496 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004497 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004498 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004499 spoe_release_placeholder(ph);
4500 }
4501 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004502 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004503 spoe_release_placeholder(ph);
4504 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004505 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004506 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004507 free(vph->name);
4508 free(vph);
4509 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004510 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004511 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004512 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004513 }
4514 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004515 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004516 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004517 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004518 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004519 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004520 free(logsrv);
4521 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004522 free(conf);
4523 return -1;
4524}
4525
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004526/* Send message of a SPOE group. This is the action_ptr callback of a rule
4527 * associated to a "send-spoe-group" action.
4528 *
Christopher Faulet13403762019-12-13 09:01:57 +01004529 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4530 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004531static enum act_return
4532spoe_send_group(struct act_rule *rule, struct proxy *px,
4533 struct session *sess, struct stream *s, int flags)
4534{
4535 struct filter *filter;
4536 struct spoe_agent *agent = NULL;
4537 struct spoe_group *group = NULL;
4538 struct spoe_context *ctx = NULL;
4539 int ret, dir;
4540
4541 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4542 if (filter->config == rule->arg.act.p[0]) {
4543 agent = rule->arg.act.p[2];
4544 group = rule->arg.act.p[3];
4545 ctx = filter->ctx;
4546 break;
4547 }
4548 }
4549 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004550 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004551 if (ctx->state == SPOE_CTX_ST_NONE)
4552 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004553
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004554 switch (rule->from) {
4555 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4556 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4557 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4558 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4559 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4560 default:
4561 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4562 " - internal error while execute spoe-send-group\n",
4563 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4564 __FUNCTION__, s);
4565 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4566 agent->id);
4567 return ACT_RET_CONT;
4568 }
4569
4570 ret = spoe_process_group(s, ctx, group, dir);
4571 if (ret == 1)
4572 return ACT_RET_CONT;
4573 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004574 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004575 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4576 " - failed to process group '%s': interrupted by caller\n",
4577 (int)now.tv_sec, (int)now.tv_usec,
4578 agent->id, __FUNCTION__, s, group->id);
4579 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004580 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004581 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004582 return ACT_RET_CONT;
4583 }
4584 return ACT_RET_YIELD;
4585 }
4586 else
Christopher Faulet13403762019-12-13 09:01:57 +01004587 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004588}
4589
4590/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4591 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4592 * action should be:
4593 *
4594 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4595 *
4596 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4597 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4598 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4599 * group.
4600 *
4601 * The function returns 1 in success case, otherwise, it returns 0 and err is
4602 * filled.
4603 */
4604static int
4605check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4606{
4607 struct flt_conf *fconf;
4608 struct spoe_config *conf;
4609 struct spoe_agent *agent = NULL;
4610 struct spoe_group *group;
4611 struct spoe_message *msg;
4612 char *engine_id = rule->arg.act.p[0];
4613 char *group_id = rule->arg.act.p[1];
4614 unsigned int where = 0;
4615
4616 switch (rule->from) {
4617 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4618 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4619 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4620 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4621 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4622 default:
4623 memprintf(err,
4624 "internal error, unexpected rule->from=%d, please report this bug!",
4625 rule->from);
4626 goto error;
4627 }
4628
4629 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4630 * <px> */
4631 list_for_each_entry(fconf, &px->filter_configs, list) {
4632 conf = fconf->conf;
4633
4634 /* This is not an SPOE filter */
4635 if (fconf->id != spoe_filter_id)
4636 continue;
4637
4638 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004639 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004640 agent = conf->agent;
4641 break;
4642 }
4643 }
4644 if (agent == NULL) {
4645 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4646 engine_id, group_id);
4647 goto error;
4648 }
4649
4650 /* Try to find the right group */
4651 list_for_each_entry(group, &agent->groups, list) {
4652 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004653 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004654 break;
4655 }
4656 if (&group->list == &agent->groups) {
4657 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4658 group_id, engine_id);
4659 goto error;
4660 }
4661
4662 /* Ok, we found the group, we need to check messages and their
4663 * arguments */
4664 list_for_each_entry(msg, &group->messages, by_grp) {
4665 struct spoe_arg *arg;
4666
4667 list_for_each_entry(arg, &msg->args, list) {
4668 if (!(arg->expr->fetch->val & where)) {
4669 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4670 "some args extract information from '%s',"
4671 "none of which is available here ('%s')",
4672 msg->id, group->id, msg->conf.file, msg->conf.line,
4673 sample_ckp_names(arg->expr->fetch->use),
4674 sample_ckp_names(where));
4675 goto error;
4676 }
4677 }
4678 }
4679
4680 free(engine_id);
4681 free(group_id);
4682 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4683 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4684 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4685 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4686 return 1;
4687
4688 error:
4689 free(engine_id);
4690 free(group_id);
4691 return 0;
4692}
4693
4694/* Parse 'send-spoe-group' action following the format:
4695 *
4696 * ... send-spoe-group <engine-id> <group-id>
4697 *
4698 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4699 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4700 * ids are saved and used later, when the rule will be checked.
4701 */
4702static enum act_parse_ret
4703parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4704 struct act_rule *rule, char **err)
4705{
4706 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4707 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4708 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4709 return ACT_RET_PRS_ERR;
4710 }
4711 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4712 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4713
4714 (*orig_arg) += 2;
4715
4716 rule->action = ACT_CUSTOM;
4717 rule->action_ptr = spoe_send_group;
4718 rule->check_ptr = check_send_spoe_group;
4719 return ACT_RET_PRS_OK;
4720}
4721
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004722
4723/* Declare the filter parser for "spoe" keyword */
4724static struct flt_kw_list flt_kws = { "SPOE", { }, {
4725 { "spoe", parse_spoe_flt, NULL },
4726 { NULL, NULL, NULL },
4727 }
4728};
4729
Willy Tarreau0108d902018-11-25 19:14:37 +01004730INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4731
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004732/* Delcate the action parser for "spoe-action" keyword */
4733static struct action_kw_list tcp_req_action_kws = { { }, {
4734 { "send-spoe-group", parse_send_spoe_group },
4735 { /* END */ },
4736 }
4737};
Willy Tarreau0108d902018-11-25 19:14:37 +01004738
4739INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4740
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004741static struct action_kw_list tcp_res_action_kws = { { }, {
4742 { "send-spoe-group", parse_send_spoe_group },
4743 { /* END */ },
4744 }
4745};
Willy Tarreau0108d902018-11-25 19:14:37 +01004746
4747INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4748
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004749static struct action_kw_list http_req_action_kws = { { }, {
4750 { "send-spoe-group", parse_send_spoe_group },
4751 { /* END */ },
4752 }
4753};
Willy Tarreau0108d902018-11-25 19:14:37 +01004754
4755INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4756
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004757static struct action_kw_list http_res_action_kws = { { }, {
4758 { "send-spoe-group", parse_send_spoe_group },
4759 { /* END */ },
4760 }
4761};
4762
Willy Tarreau0108d902018-11-25 19:14:37 +01004763INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);