blob: a68f7b9141025963e8f4ad79c0d1617a4c59774e [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>
Willy Tarreauc7babd82020-06-04 21:29:29 +020020#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020021#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020023#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020024#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020025#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020027#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020029#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/signal.h>
Christopher Faulet1d7d0f82021-02-19 10:56:41 +010031#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020032#include <haproxy/spoe.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020035#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020036#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaue16ada12021-05-08 12:57:17 +020039#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020040#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020042
43#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
44#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010045#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020046#else
47#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010048#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020049#endif
50
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010051/* Reserved 4 bytes to the frame size. So a frame and its size can be written
52 * together in a buffer */
53#define MAX_FRAME_SIZE global.tune.bufsize - 4
54
55/* The minimum size for a frame */
56#define MIN_FRAME_SIZE 256
57
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010058/* Reserved for the metadata and the frame type.
59 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010060#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020061
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010062/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010063#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
64
Christopher Faulet3b386a32017-02-23 10:17:15 +010065/* SPOE filter id. Used to identify SPOE filters */
66const char *spoe_filter_id = "SPOE filter";
67
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020068/* Set if the handle on SIGUSR1 is registered */
69static int sighandler_registered = 0;
70
71/* proxy used during the parsing */
72struct proxy *curproxy = NULL;
73
74/* The name of the SPOE engine, used during the parsing */
75char *curengine = NULL;
76
77/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020078/* SPOE agent/group/message used during the parsing */
79struct spoe_agent *curagent = NULL;
80struct spoe_group *curgrp = NULL;
81struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020082
83/* list of SPOE messages and placeholders used during the parsing */
84struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020085struct list curgrps;
86struct list curmphs;
87struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010088struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020089
Christopher Faulet7250b8f2018-03-26 17:19:01 +020090/* list of log servers used during the parsing */
91struct list curlogsrvs;
92
Christopher Faulet0e0f0852018-03-26 17:20:36 +020093/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
94int curpxopts;
95int curpxopts2;
96
Christopher Faulet42bfa462017-01-04 14:14:19 +010097/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +010098DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
99DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200101struct flt_ops spoe_ops;
102
Christopher Faulet8ef75252017-02-20 22:56:03 +0100103static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200104static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
105static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106
107/********************************************************************
108 * helper functions/globals
109 ********************************************************************/
110static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200111spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112{
Christopher Faulet11610f32017-09-21 10:23:10 +0200113 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200114 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200115 free(ph->id);
116 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200117}
118
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100120spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200121{
Christopher Faulet57583e42017-09-04 15:41:09 +0200122 struct spoe_arg *arg, *argback;
123 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200124
125 if (!msg)
126 return;
127 free(msg->id);
128 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200129 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200130 release_sample_expr(arg->expr);
131 free(arg->name);
Willy Tarreau2b718102021-04-21 07:32:39 +0200132 LIST_DELETE(&arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200133 free(arg);
134 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200135 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200136 LIST_DELETE(&acl->list);
Christopher Faulet57583e42017-09-04 15:41:09 +0200137 prune_acl(acl);
138 free(acl);
139 }
140 if (msg->cond) {
141 prune_acl_cond(msg->cond);
142 free(msg->cond);
143 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200144 free(msg);
145}
146
147static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200148spoe_release_group(struct spoe_group *grp)
149{
150 if (!grp)
151 return;
152 free(grp->id);
153 free(grp->conf.file);
154 free(grp);
155}
156
157static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100158spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200159{
Christopher Faulet11610f32017-09-21 10:23:10 +0200160 struct spoe_message *msg, *msgback;
161 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200162 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200163
164 if (!agent)
165 return;
166 free(agent->id);
167 free(agent->conf.file);
168 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100169 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100170 free(agent->var_t_process);
171 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200172 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200173 LIST_DELETE(&msg->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200174 spoe_release_message(msg);
175 }
176 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +0200177 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +0200178 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200179 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100180 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200181 for (i = 0; i < global.nbthread; ++i) {
182 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100183 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200184 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100185 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200186 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200187 free(agent);
188}
189
190static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100191 [SPOE_FRM_ERR_NONE] = "normal",
192 [SPOE_FRM_ERR_IO] = "I/O error",
193 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
194 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
195 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
196 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
197 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
198 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
199 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
200 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
201 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
202 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100203 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100204 [SPOE_FRM_ERR_RES] = "resource allocation error",
205 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200206};
207
208static const char *spoe_event_str[SPOE_EV_EVENTS] = {
209 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
210 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
211 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
212 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
213 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
214
215 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
216 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
217 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
218};
219
220
221#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
222
223static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100224 [SPOE_CTX_ST_NONE] = "NONE",
225 [SPOE_CTX_ST_READY] = "READY",
226 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
227 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
228 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
229 [SPOE_CTX_ST_DONE] = "DONE",
230 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200231};
232
233static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100234 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
235 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
236 [SPOE_APPCTX_ST_IDLE] = "IDLE",
237 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
238 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
239 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
240 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
241 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
242 [SPOE_APPCTX_ST_EXIT] = "EXIT",
243 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200244};
245
246#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100247
Christopher Faulet8ef75252017-02-20 22:56:03 +0100248/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500249 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100250 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100251static char *
252generate_pseudo_uuid()
253{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100254 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800255 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100256}
257
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100258
259static inline void
260spoe_update_stat_time(struct timeval *tv, long *t)
261{
262 if (*t == -1)
263 *t = tv_ms_elapsed(tv, &now);
264 else
265 *t += tv_ms_elapsed(tv, &now);
266 tv_zero(tv);
267}
268
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200269/********************************************************************
270 * Functions that encode/decode SPOE frames
271 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200272/* Helper to get static string length, excluding the terminating null byte */
273#define SLEN(str) (sizeof(str)-1)
274
275/* Predefined key used in HELLO/DISCONNECT frames */
276#define SUPPORTED_VERSIONS_KEY "supported-versions"
277#define VERSION_KEY "version"
278#define MAX_FRAME_SIZE_KEY "max-frame-size"
279#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100280#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100281#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200282#define STATUS_CODE_KEY "status-code"
283#define MSG_KEY "message"
284
285struct spoe_version {
286 char *str;
287 int min;
288 int max;
289};
290
291/* All supported versions */
292static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200293 /* 1.0 is now unsupported because of a bug about frame's flags*/
294 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200295 {NULL, 0, 0}
296};
297
298/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200299#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200300
Christopher Faulet8ef75252017-02-20 22:56:03 +0100301/* Convert a string to a SPOE version value. The string must follow the format
302 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
303 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200304static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100305spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100307 const char *p, *end;
308 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200309
Christopher Faulet8ef75252017-02-20 22:56:03 +0100310 p = str;
311 end = str+len;
312 maj = min = 0;
313 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200314
Christopher Faulet8ef75252017-02-20 22:56:03 +0100315 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100316 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100317 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200318
Christopher Faulet8ef75252017-02-20 22:56:03 +0100319 /* parse Major number, until the '.' */
320 while (*p != '.') {
321 if (p >= end || *p < '0' || *p > '9')
322 goto out;
323 maj *= 10;
324 maj += (*p - '0');
325 p++;
326 }
327
328 /* check Major version */
329 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200330 goto out;
331
Christopher Faulet8ef75252017-02-20 22:56:03 +0100332 p++; /* skip the '.' */
333 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
334 goto out;
335
336 /* Parse Minor number */
337 while (p < end) {
338 if (*p < '0' || *p > '9')
339 break;
340 min *= 10;
341 min += (*p - '0');
342 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200343 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100344
345 /* check Minor number */
346 if (min > 999)
347 goto out;
348
349 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100350 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100351 p++;
352 if (p != end)
353 goto out;
354
355 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200356 out:
357 return vsn;
358}
359
Christopher Faulet8ef75252017-02-20 22:56:03 +0100360/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800361 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100362 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200363static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100364spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200365{
Willy Tarreau83061a82018-07-13 11:56:34 +0200366 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100367 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100368 char *p, *end;
369 unsigned int flags = SPOE_FRM_FL_FIN;
370 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200371
Christopher Faulet8ef75252017-02-20 22:56:03 +0100372 p = frame;
373 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200374
Christopher Faulet8ef75252017-02-20 22:56:03 +0100375 /* Set Frame type */
376 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200377
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100378 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200379 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100380 memcpy(p, (char *)&flags, 4);
381 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200382
383 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100384 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200385
386 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
387 * and "capabilities" */
388
389 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100390 sz = SLEN(SUPPORTED_VERSIONS_KEY);
391 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
392 goto too_big;
393
394 *p++ = SPOE_DATA_T_STR;
395 sz = SLEN(SUPPORTED_VERSIONS_VAL);
396 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
397 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200398
399 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 sz = SLEN(MAX_FRAME_SIZE_KEY);
401 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
402 goto too_big;
403
404 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200405 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200407
408 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100409 sz = SLEN(CAPABILITIES_KEY);
410 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
411 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200412
Christopher Faulet8ef75252017-02-20 22:56:03 +0100413 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100414 chk = get_trash_chunk();
415 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200416 memcpy(chk->area, "pipelining", 10);
417 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100418 }
419 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200420 if (chk->data) chk->area[chk->data++] = ',';
421 memcpy(chk->area+chk->data, "async", 5);
422 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100423 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100424 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200425 if (chk->data) chk->area[chk->data++] = ',';
426 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100427 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100428 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200429 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100430 goto too_big;
431
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500432 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200433 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 sz = SLEN(ENGINE_ID_KEY);
435 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
436 goto too_big;
437
438 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200439 sz = strlen(agent->rt[tid].engine_id);
440 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100441 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100442 }
443
Christopher Faulet8ef75252017-02-20 22:56:03 +0100444 return (p - frame);
445
446 too_big:
447 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
448 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200449}
450
Christopher Faulet8ef75252017-02-20 22:56:03 +0100451/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
452 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
453 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200454static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200456{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457 const char *reason;
458 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100459 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100460 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200461
Christopher Faulet8ef75252017-02-20 22:56:03 +0100462 p = frame;
463 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200464
Christopher Faulet8ef75252017-02-20 22:56:03 +0100465 /* Set Frame type */
466 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100468 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200469 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100470 memcpy(p, (char *)&flags, 4);
471 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200472
473 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100474 *p++ = 0; *p++ = 0;
475
476 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
477 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200478
479 /* There are 2 mandatory items: "status-code" and "message" */
480
481 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100482 sz = SLEN(STATUS_CODE_KEY);
483 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
484 goto too_big;
485
486 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200487 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
490 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 sz = SLEN(MSG_KEY);
492 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
493 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200494
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 /*Get the message corresponding to the status code */
496 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
497
498 *p++ = SPOE_DATA_T_STR;
499 sz = strlen(reason);
500 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
501 goto too_big;
502
503 return (p - frame);
504
505 too_big:
506 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
507 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200508}
509
Christopher Faulet8ef75252017-02-20 22:56:03 +0100510/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
511 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
512 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200513static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100514spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100515 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200516{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100517 char *p, *end;
518 unsigned int stream_id, frame_id;
519 unsigned int flags = SPOE_FRM_FL_FIN;
520 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200521
Christopher Faulet8ef75252017-02-20 22:56:03 +0100522 p = frame;
523 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200524
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100525 stream_id = ctx->stream_id;
526 frame_id = ctx->frame_id;
527
528 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
529 /* The fragmentation is not supported by the applet */
530 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
531 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
532 return -1;
533 }
534 flags = ctx->frag_ctx.flags;
535 }
536
537 /* Set Frame type */
538 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
539
540 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200541 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100542 memcpy(p, (char *)&flags, 4);
543 p += 4;
544
545 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200546 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100547 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200548 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100549 goto too_big;
550
551 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200552 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100553 if (p + sz >= end)
554 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200555 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100556 p += sz;
557
558 return (p - frame);
559
560 too_big:
561 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
562 return 0;
563}
564
565/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
566 * returns the number of encoded bytes in the frame on success, 0 if an encoding
567 * error occurred and -1 if a fatal error occurred. */
568static int
569spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
570 char *frame, size_t size)
571{
572 char *p, *end;
573 unsigned int stream_id, frame_id;
574 unsigned int flags;
575 size_t sz;
576
577 p = frame;
578 end = frame+size;
579
Christopher Faulet8ef75252017-02-20 22:56:03 +0100580 /* <ctx> is null when the stream has aborted the processing of a
581 * fragmented frame. In this case, we must notify the corresponding
582 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100583 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100584 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100585 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
586 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
587 }
588 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100589 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100590 stream_id = ctx->stream_id;
591 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100592 }
593
Christopher Faulet8ef75252017-02-20 22:56:03 +0100594 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100595 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100596
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100597 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200598 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100599 memcpy(p, (char *)&flags, 4);
600 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200601
602 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200603 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100604 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200605 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100606 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200607
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100608 if (ctx == NULL)
609 goto end;
610
Christopher Faulet8ef75252017-02-20 22:56:03 +0100611 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200612 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100613 if (p + sz >= end)
614 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200615 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100617
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100618 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 return (p - frame);
620
621 too_big:
622 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
623 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200624}
625
Christopher Faulet8ef75252017-02-20 22:56:03 +0100626/* Decode and process the HELLO frame sent by an agent. It returns the number of
627 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
628 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200629static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100630spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200631{
Christopher Faulet305c6072017-02-23 16:17:53 +0100632 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
633 char *p, *end;
634 int vsn, max_frame_size;
635 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636
637 p = frame;
638 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200639
640 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
642 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200643 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200645
Christopher Faulet8ef75252017-02-20 22:56:03 +0100646 if (size < 7 /* TYPE + METADATA */) {
647 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
648 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649 }
650
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100651 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100652 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200653 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100654 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100656 /* Fragmentation is not supported for HELLO frame */
657 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100659 return -1;
660 }
661
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200662 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100663 if (*p != 0 || *(p+1) != 0) {
664 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
665 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200666 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100667 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668
669 /* There are 3 mandatory items: "version", "max-frame-size" and
670 * "capabilities" */
671
672 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100673 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100674 while (p < end) {
675 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200676 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200678
679 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100680 ret = spoe_decode_buffer(&p, end, &str, &sz);
681 if (ret == -1 || !sz) {
682 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
683 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200684 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100685
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200686 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200687 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688 int i, type = *p++;
689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
692 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
693 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200694 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100695 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
696 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
697 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200698 }
699
Christopher Faulet8ef75252017-02-20 22:56:03 +0100700 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703 return -1;
704 }
705 for (i = 0; supported_versions[i].str != NULL; ++i) {
706 if (vsn >= supported_versions[i].min &&
707 vsn <= supported_versions[i].max)
708 break;
709 }
710 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100711 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200712 return -1;
713 }
714 }
715 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200716 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 +0100717 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718
719 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200720 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
721 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
722 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
723 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100724 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
725 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200727 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100728 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
729 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200730 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100731 if (sz < MIN_FRAME_SIZE ||
732 sz > SPOE_APPCTX(appctx)->max_frame_size) {
733 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200734 return -1;
735 }
736 max_frame_size = sz;
737 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100738 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200739 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100740 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100741
742 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
744 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
745 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100746 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100747 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
748 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
749 return 0;
750 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100751
Christopher Faulet8ef75252017-02-20 22:56:03 +0100752 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100753 char *delim;
754
755 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100756 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757
Christopher Faulet8ef75252017-02-20 22:56:03 +0100758 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
759 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100760 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100761 flags |= SPOE_APPCTX_FL_PIPELINING;
762 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100763 else if (sz >= 5 && !strncmp(str, "async", 5)) {
764 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100765 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766 flags |= SPOE_APPCTX_FL_ASYNC;
767 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100768 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
769 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100770 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100771 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
772 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100773
Christopher Faulet8ef75252017-02-20 22:56:03 +0100774 /* Get the next comma or break */
775 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 delim++;
778 sz -= (delim - str);
779 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100780 }
781 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200782 else {
783 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100784 if (spoe_skip_data(&p, end) == -1) {
785 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
786 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200787 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200788 }
789 }
790
791 /* Final checks */
792 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200794 return -1;
795 }
796 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100797 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200798 return -1;
799 }
Christopher Faulet11389012019-02-07 16:13:26 +0100800 if (!agent)
801 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
802 else {
803 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
804 flags &= ~SPOE_APPCTX_FL_PIPELINING;
805 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
806 flags &= ~SPOE_APPCTX_FL_ASYNC;
807 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200808
Christopher Faulet42bfa462017-01-04 14:14:19 +0100809 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
810 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
811 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100812
813 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200814}
815
816/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
817 * bytes on success, 0 if the frame can be ignored and -1 if an error
818 * occurred. */
819static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100820spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200821{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100823 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100824
825 p = frame;
826 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200827
828 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100829 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
830 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200831 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833
Christopher Faulet8ef75252017-02-20 22:56:03 +0100834 if (size < 7 /* TYPE + METADATA */) {
835 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
836 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837 }
838
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100839 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200841 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100842 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200843
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100844 /* Fragmentation is not supported for DISCONNECT frame */
845 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100847 return -1;
848 }
849
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200850 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100851 if (*p != 0 || *(p+1) != 0) {
852 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
853 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856
857 /* There are 2 mandatory items: "status-code" and "message" */
858
859 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100860 while (p < end) {
861 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200862 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100863 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200864
865 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 ret = spoe_decode_buffer(&p, end, &str, &sz);
867 if (ret == -1 || !sz) {
868 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
869 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200870 }
871
872 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200873 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100874 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875
876 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
878 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
879 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
880 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100881 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
882 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200884 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100885 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
886 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200887 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100888 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
890
891 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200892 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100893 int type = *p++;
894
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200895 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100896 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
897 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
898 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200899 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100900 ret = spoe_decode_buffer(&p, end, &str, &sz);
901 if (ret == -1 || sz > 255) {
902 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
903 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200904 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100905#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
906 SPOE_APPCTX(appctx)->reason = str;
907 SPOE_APPCTX(appctx)->rlen = sz;
908#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200909 }
910 else {
911 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100912 if (spoe_skip_data(&p, end) == -1) {
913 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
914 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200916 }
917 }
918
Christopher Faulet8ef75252017-02-20 22:56:03 +0100919 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920}
921
922
Christopher Fauleta1cda022016-12-21 08:58:06 +0100923/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200924 * success, 0 if the frame can be ignored and -1 if an error occurred. */
925static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100926spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100927 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200928{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100929 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100930 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100931 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100934
935 p = frame;
936 end = frame + size;
937 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200938
939 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
941 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200942 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944
Christopher Faulet8ef75252017-02-20 22:56:03 +0100945 if (size < 7 /* TYPE + METADATA */) {
946 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
947 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948 }
949
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100950 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200952 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200954
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100955 /* Fragmentation is not supported for now */
956 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100958 return -1;
959 }
960
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200961 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200962 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100963 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100964 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100965 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200966 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100967 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200968 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100970
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100972 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200973 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100974 if ((*ctx)->stream_id == (unsigned int)stream_id &&
975 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100976 goto found;
977 }
978 }
979 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100980 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
981 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100982 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100983 goto found;
984 }
985 }
986
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100987 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
988 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
989 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
990
991 /* ABRT bit is set for an unfinished fragmented frame */
992 if (flags & SPOE_FRM_FL_ABRT) {
993 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100994 (*ctx)->state = SPOE_CTX_ST_ERROR;
995 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
996 /* Ignore the payload */
997 goto end;
998 }
999 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1000 /* For now, we ignore the ack */
1001 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1002 return 0;
1003 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001004
Christopher Fauleta1cda022016-12-21 08:58:06 +01001005 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001006 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1007 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001008 " - stream-id=%u - frame-id=%u\n",
1009 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1010 __FUNCTION__, appctx,
1011 (unsigned int)stream_id, (unsigned int)frame_id);
1012
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001013 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletc7ba9102020-11-10 14:31:39 +01001014 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1015 /* Report an error if we are waiting the ack for another frame,
1016 * but not if there is no longer frame waiting for a ack
1017 * (timeout)
1018 */
1019 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1020 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1021 return -1;
1022 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1023 SPOE_APPCTX(appctx)->cur_fpa = 0;
1024 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001025 return 0;
1026
1027 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001028 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1029 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001030 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001031 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001032 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001033
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001034 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001035 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001036 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1037 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001038 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001039
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001040 /* Transfer the buffer ownership to the SPOE context */
1041 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001042 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001043
Christopher Faulet8ef75252017-02-20 22:56:03 +01001044 (*ctx)->state = SPOE_CTX_ST_DONE;
1045
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001046 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001047 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001048 " - ACK frame received"
1049 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001050 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001051 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1052 (*ctx)->frame_id, flags);
1053 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001054}
1055
Christopher Fauletba7bc162016-11-07 21:07:38 +01001056/* This function is used in cfgparse.c and declared in proto/checks.h. It
1057 * prepare the request to send to agents during a healthcheck. It returns 0 on
1058 * success and -1 if an error occurred. */
1059int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001061{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001062 struct appctx appctx;
1063 struct spoe_appctx spoe_appctx;
1064 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1065 size_t sz;
1066 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001067
Christopher Faulet42bfa462017-01-04 14:14:19 +01001068 memset(&appctx, 0, sizeof(appctx));
1069 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001070 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001071
1072 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001073 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1076 end = frame + MAX_FRAME_SIZE;
1077
1078 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1079 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001080 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001081 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 /* Add "healthcheck" K/V item */
1084 sz = SLEN(HEALTHCHECK_KEY);
1085 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1086 return -1;
1087 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088
Christopher Faulet8ef75252017-02-20 22:56:03 +01001089 *len = frame - buf;
1090 sz = htonl(*len - 4);
1091 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001094 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001095 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001096 return 0;
1097}
1098
1099/* This function is used in checks.c and declared in proto/checks.h. It decode
1100 * the response received from an agent during a healthcheck. It returns 0 on
1101 * success and -1 if an error occurred. */
1102int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001103spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001105 struct appctx appctx;
1106 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107
Christopher Faulet42bfa462017-01-04 14:14:19 +01001108 memset(&appctx, 0, sizeof(appctx));
1109 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001110
Christopher Faulet42bfa462017-01-04 14:14:19 +01001111 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001112 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001113
Christopher Faulet8ef75252017-02-20 22:56:03 +01001114 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1115 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001116 goto error;
1117 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1119 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001120
1121 return 0;
1122
1123 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001124 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1125 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1126 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001127 return -1;
1128}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001129
Christopher Fauleta1cda022016-12-21 08:58:06 +01001130/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001131 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132 * success. */
1133static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001134spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001135{
1136 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001137 int ret;
1138 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139
Christopher Faulet8ef75252017-02-20 22:56:03 +01001140 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1141 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142 netint = htonl(framesz);
1143 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001144 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001146 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001147 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001148 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001149 }
1150 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001151 return -1; /* error */
1152 }
1153 return framesz;
1154}
1155
1156/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1157 * when the frame can be ignored, 1 to retry later and the frame length on
1158 * success. */
1159static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001160spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161{
1162 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001163 int ret;
1164 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001165
Willy Tarreau06d80a92017-10-19 14:32:15 +02001166 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001167 if (ret > 0) {
1168 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001169 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171 return -1;
1172 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001173 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174 }
1175 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001176 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001178 }
1179 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001180 return -1; /* error */
1181 }
1182 return framesz;
1183}
1184
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001185/********************************************************************
1186 * Functions that manage the SPOE applet
1187 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001188static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001189spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001190{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001191 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001192 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001193 appctx_wakeup(appctx);
1194 return 1;
1195}
1196
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001197/* Callback function that catches applet timeouts. If a timeout occurred, we set
1198 * <appctx->st1> flag and the SPOE applet is woken up. */
1199static struct task *
Willy Tarreau144f84a2021-03-02 16:09:26 +01001200spoe_process_appctx(struct task * task, void *context, unsigned int state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001201{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001202 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001203
1204 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1205 if (tick_is_expired(task->expire, now_ms)) {
1206 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001207 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001208 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001209 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001210 return task;
1211}
1212
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001213/* Callback function that releases a SPOE applet. This happens when the
1214 * connection with the agent is closed. */
1215static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001216spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001217{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001218 struct stream_interface *si = appctx->owner;
1219 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1220 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001221 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001222
1223 if (spoe_appctx == NULL)
1224 return;
1225
1226 appctx->ctx.spoe.ptr = NULL;
1227 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001228
1229 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1230 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1231 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001232
Christopher Faulet8ef75252017-02-20 22:56:03 +01001233 /* Remove applet from the list of running applets */
Willy Tarreau4781b152021-04-06 13:53:36 +02001234 _HA_ATOMIC_DEC(&agent->counters.applets);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001235 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001236 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001237 LIST_DELETE(&spoe_appctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001238 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001239 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001240 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001241
Christopher Faulet8ef75252017-02-20 22:56:03 +01001242 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001244 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1245 eb32_delete(&spoe_appctx->node);
Willy Tarreau4781b152021-04-06 13:53:36 +02001246 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001247 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001248
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001249 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001250 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1251 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001252
1253 si_shutw(si);
1254 si_shutr(si);
1255 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001256 }
1257
Christopher Faulet8ef75252017-02-20 22:56:03 +01001258 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001259 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260
Christopher Faulet8ef75252017-02-20 22:56:03 +01001261 /* Notify all waiting streams */
1262 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001263 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001264 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001265 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001266 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001267 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001268 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001270 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001271 }
1272
Christopher Faulet8ef75252017-02-20 22:56:03 +01001273 /* If the applet was processing a fragmented frame, notify the
1274 * corresponding stream. */
1275 if (spoe_appctx->frag_ctx.ctx) {
1276 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001277 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001278 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001279 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001280 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1281 }
1282
Christopher Fauletcf181c72020-11-10 18:45:34 +01001283 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1284 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1285 if (ctx->spoe_appctx == spoe_appctx)
1286 ctx->spoe_appctx = NULL;
1287 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001288 goto end;
Christopher Fauletcf181c72020-11-10 18:45:34 +01001289 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001290
Christopher Faulet8ef75252017-02-20 22:56:03 +01001291 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001292 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001293 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001294 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001295 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001296 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001297 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001298 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001299 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001300 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001301 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001302 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02001303 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001304 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001305 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001306 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletcf181c72020-11-10 18:45:34 +01001307 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001308 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001309 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001310 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1311 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001312
1313 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001314 /* Release allocated memory */
1315 spoe_release_buffer(&spoe_appctx->buffer,
1316 &spoe_appctx->buffer_wait);
1317 pool_free(pool_head_spoe_appctx, spoe_appctx);
1318
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001319 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001320 agent->rt[tid].frame_size = agent->max_frame_size;
1321 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1322 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001323}
1324
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001325static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001326spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001327{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001328 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001329 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001330 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001332
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001333 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1334 /* closed */
1335 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1336 goto exit;
1337 }
1338
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001339 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001340 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001341 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1343 goto stop;
1344 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001345
Christopher Fauleta1cda022016-12-21 08:58:06 +01001346 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001347 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1348 " - Connection timed out\n",
1349 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1350 __FUNCTION__, appctx);
1351 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001353 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001354
Christopher Faulet42bfa462017-01-04 14:14:19 +01001355 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001356 SPOE_APPCTX(appctx)->task->expire =
1357 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358
Christopher Faulet8ef75252017-02-20 22:56:03 +01001359 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1360 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001361 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001362 ret = spoe_prepare_hahello_frame(appctx, frame,
1363 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001365 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001366
1367 switch (ret) {
1368 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001369 case 0: /* ignore => an error, cannot be ignored */
1370 goto exit;
1371
1372 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373 goto stop;
1374
Christopher Faulet8ef75252017-02-20 22:56:03 +01001375 default:
1376 /* HELLO frame successfully sent, now wait for the
1377 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001378 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1379 goto next;
1380 }
1381
1382 next:
1383 return 0;
1384 stop:
1385 return 1;
1386 exit:
1387 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1388 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001389}
1390
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001391static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001392spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001395 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001396 char *frame;
1397 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001399
Christopher Fauletb067b062017-01-04 16:39:11 +01001400 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001402 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001403 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001404
Christopher Fauleta1cda022016-12-21 08:58:06 +01001405 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001406 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1407 " - Connection timed out\n",
1408 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1409 __FUNCTION__, appctx);
1410 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001411 goto exit;
1412 }
1413
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001414 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001415 ret = spoe_recv_frame(appctx, frame,
1416 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001417 if (ret > 1) {
1418 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1419 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1420 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001421 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001422 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001423 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001424 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001425
Christopher Fauleta1cda022016-12-21 08:58:06 +01001426 switch (ret) {
1427 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001428 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001429 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1430 goto next;
1431
1432 case 1: /* retry later */
1433 goto stop;
1434
1435 default:
Willy Tarreau4781b152021-04-06 13:53:36 +02001436 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001438 SPOE_APPCTX(appctx)->node.key = 0;
1439 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001440
1441 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001442 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001443 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001444 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001445
Christopher Fauleta1cda022016-12-21 08:58:06 +01001446 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001447 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001448 if (trash.data)
1449 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001450
1451 SPOE_APPCTX(appctx)->task->expire =
1452 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453 return 0;
1454 stop:
1455 return 1;
1456 exit:
1457 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1458 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001459}
1460
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001461
Christopher Fauleta1cda022016-12-21 08:58:06 +01001462static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001463spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001464{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001465 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1466 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001467 char *frame, *buf;
1468 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001469
Christopher Faulet8ef75252017-02-20 22:56:03 +01001470 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1471 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001472 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001473
1474 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1475 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1476 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1477 SPOE_APPCTX(appctx)->max_frame_size);
1478 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001479 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001480 *skip = 1;
1481 ret = 1;
1482 goto end;
1483 }
1484 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001485 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001486 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1487 SPOE_APPCTX(appctx)->max_frame_size);
1488
1489 }
1490
Christopher Faulet8ef75252017-02-20 22:56:03 +01001491 if (ret > 1)
1492 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001493
Christopher Faulet8ef75252017-02-20 22:56:03 +01001494 switch (ret) {
1495 case -1: /* error */
1496 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1497 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001498
Christopher Faulet8ef75252017-02-20 22:56:03 +01001499 case 0: /* ignore */
1500 if (ctx == NULL)
1501 goto abort_frag_frame;
1502
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001503 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001504 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001505 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001506 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001507 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001508 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001509 ctx->state = SPOE_CTX_ST_ERROR;
1510 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1511 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001512 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001513 break;
1514
1515 case 1: /* retry */
1516 *skip = 1;
1517 break;
1518
1519 default:
1520 if (ctx == NULL)
1521 goto abort_frag_frame;
1522
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001523 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Willy Tarreau2b718102021-04-21 07:32:39 +02001524 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001526 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001527 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001528 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001529 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1530 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1531 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001532 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 }
1535 goto end;
1536
1537 frag_frame_sent:
1538 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001539 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1541 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1542 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001543 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1544 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1545 goto end;
1546
1547 no_frag_frame_sent:
1548 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1549 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001550 LIST_APPEND(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001551 }
1552 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1553 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Willy Tarreau2b718102021-04-21 07:32:39 +02001554 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555 }
1556 else {
1557 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001558 *skip = 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02001559 LIST_APPEND(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001560 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001561 _HA_ATOMIC_INC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001562 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001563 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1564 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1565 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001566 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567
Christopher Faulet8ef75252017-02-20 22:56:03 +01001568 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1569 goto end;
1570
1571 abort_frag_frame:
1572 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1573 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1574 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1575 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1576 goto end;
1577
1578 end:
1579 return ret;
1580}
1581
1582static int
1583spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1584{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001585 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001586 struct spoe_context *ctx = NULL;
1587 char *frame;
1588 int ret;
1589
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001590 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591 ret = spoe_recv_frame(appctx, frame,
1592 SPOE_APPCTX(appctx)->max_frame_size);
1593 if (ret > 1) {
1594 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1595 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001596 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001597 goto end;
1598 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001599 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001600 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1601 }
1602 switch (ret) {
1603 case -1: /* error */
1604 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1605 break;
1606
1607 case 0: /* ignore */
1608 break;
1609
1610 case 1: /* retry */
1611 *skip = 1;
1612 break;
1613
1614 default:
Willy Tarreau2b718102021-04-21 07:32:39 +02001615 LIST_DELETE(&ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001616 LIST_INIT(&ctx->list);
Willy Tarreau4781b152021-04-06 13:53:36 +02001617 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001618 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1619 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001620 if (ctx->spoe_appctx) {
1621 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001622 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001623 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001624 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1625 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1626 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1627 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1628 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1629 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1630 }
1631 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1632 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001633 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1634 break;
1635 }
1636
1637 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001638 if (trash.data)
1639 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001640 end:
1641 return ret;
1642}
1643
1644static int
1645spoe_handle_processing_appctx(struct appctx *appctx)
1646{
1647 struct stream_interface *si = appctx->owner;
1648 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001649 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001650
1651 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1652 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1653 goto exit;
1654 }
1655
1656 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1658 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1659 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1660 goto next;
1661 }
1662
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001663 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001664 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001665 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001666 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1667 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001668 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001669
Christopher Faulet8f82b202018-01-24 16:23:03 +01001670 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1671 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001672
Christopher Faulet8f82b202018-01-24 16:23:03 +01001673 /* receiving_frame loop */
1674 while (!skip_receiving) {
1675 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1676 switch (ret) {
1677 case -1: /* error */
1678 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001679
Christopher Faulet8f82b202018-01-24 16:23:03 +01001680 case 0: /* ignore */
1681 active_r = 1;
1682 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001683
Christopher Faulet8f82b202018-01-24 16:23:03 +01001684 case 1: /* retry */
1685 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001686
Christopher Faulet8f82b202018-01-24 16:23:03 +01001687 default:
1688 active_r = 1;
1689 break;
1690 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001691 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001692
Christopher Faulet8f82b202018-01-24 16:23:03 +01001693 /* send_frame loop */
1694 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1695 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1696 switch (ret) {
1697 case -1: /* error */
1698 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001699
Christopher Faulet8f82b202018-01-24 16:23:03 +01001700 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001701 if (SPOE_APPCTX(appctx)->node.key)
1702 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001703 active_s++;
1704 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001705
Christopher Faulet8f82b202018-01-24 16:23:03 +01001706 case 1: /* retry */
1707 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001708
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001710 if (SPOE_APPCTX(appctx)->node.key)
1711 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001712 active_s++;
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 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001718 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1719 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1720 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001721
Christopher Faulet8f82b202018-01-24 16:23:03 +01001722 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Faulet9e647e52021-03-01 15:01:14 +01001723 struct server *srv = objt_server(si_strm(si)->target);
1724
1725 /* With several threads, close the applet if there are pending
1726 * connections or if the server is full. Otherwise, add the
1727 * applet in the idle list.
1728 */
1729 if (global.nbthread > 1 &&
1730 (agent->b.be->nbpend ||
1731 (srv && (srv->nbpend || (srv->maxconn && srv->served >=srv_dynamic_maxconn(srv)))))) {
1732 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1733 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1734 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1735 goto next;
1736 }
Willy Tarreau4781b152021-04-06 13:53:36 +02001737 _HA_ATOMIC_INC(&agent->counters.idles);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001738 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001739 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001740 }
1741 return 1;
1742
Christopher Faulet8f82b202018-01-24 16:23:03 +01001743 next:
1744 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1745 return 0;
1746
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747 exit:
1748 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1749 return 0;
1750}
1751
1752static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001753spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001754{
1755 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001756 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001757 char *frame, *buf;
1758 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001759
Christopher Fauleta1cda022016-12-21 08:58:06 +01001760 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1761 goto exit;
1762
1763 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1764 goto exit;
1765
Christopher Faulet8ef75252017-02-20 22:56:03 +01001766 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1767 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001768 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001769 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1770 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001773
1774 switch (ret) {
1775 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001776 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001777 goto exit;
1778
1779 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001780 goto stop;
1781
1782 default:
1783 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1784 " - disconnected by HAProxy (%d): %s\n",
1785 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 __FUNCTION__, appctx,
1787 SPOE_APPCTX(appctx)->status_code,
1788 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789
1790 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1791 goto next;
1792 }
1793
1794 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 SPOE_APPCTX(appctx)->task->expire =
1796 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001797 return 0;
1798 stop:
1799 return 1;
1800 exit:
1801 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1802 return 0;
1803}
1804
1805static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001806spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001807{
1808 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001809 char *frame;
1810 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811
Christopher Fauletb067b062017-01-04 16:39:11 +01001812 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001813 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001814 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001815 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001816
Christopher Fauletb067b062017-01-04 16:39:11 +01001817 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001818 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001819 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001820 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001822 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001823 ret = spoe_recv_frame(appctx, frame,
1824 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001825 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001826 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001827 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 }
1829
1830 switch (ret) {
1831 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001832 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1833 " - error on frame (%s)\n",
1834 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001835 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001836 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 goto exit;
1839
1840 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 goto next;
1842
1843 case 1: /* retry */
1844 goto stop;
1845
1846 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001847 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001850 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001851 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1852 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853 goto exit;
1854 }
1855
1856 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001857 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001858 if (trash.data)
1859 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001860
Christopher Fauleta1cda022016-12-21 08:58:06 +01001861 return 0;
1862 stop:
1863 return 1;
1864 exit:
1865 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1866 return 0;
1867}
1868
1869/* I/O Handler processing messages exchanged with the agent */
1870static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001872{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001873 struct stream_interface *si = appctx->owner;
1874 struct spoe_agent *agent;
1875
1876 if (SPOE_APPCTX(appctx) == NULL)
1877 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001878
Christopher Faulet8ef75252017-02-20 22:56:03 +01001879 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1880 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001881
Christopher Fauleta1cda022016-12-21 08:58:06 +01001882 switchstate:
1883 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1884 " - appctx-state=%s\n",
1885 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1886 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1887
1888 switch (appctx->st0) {
1889 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001890 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001891 goto out;
1892 goto switchstate;
1893
1894 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001895 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001896 goto out;
1897 goto switchstate;
1898
1899 case SPOE_APPCTX_ST_IDLE:
Willy Tarreau4781b152021-04-06 13:53:36 +02001900 _HA_ATOMIC_DEC(&agent->counters.idles);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001901 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001902 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001903 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001904 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001905 SPOE_APPCTX(appctx)->task->expire =
1906 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001907 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001908 goto switchstate;
1909 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001910 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1911 /* fall through */
1912
1913 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001914 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1915 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001916 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001917 goto out;
1918 goto switchstate;
1919
1920 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001921 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001922 goto out;
1923 goto switchstate;
1924
1925 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001926 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001927 goto out;
1928 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001929
1930 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001931 appctx->st0 = SPOE_APPCTX_ST_END;
1932 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1933
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001934 si_shutw(si);
1935 si_shutr(si);
1936 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001937 /* fall through */
1938
1939 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001940 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001942 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001943 if (stopping)
1944 spoe_wakeup_appctx(appctx);
1945
Christopher Faulet42bfa462017-01-04 14:14:19 +01001946 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1947 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948}
1949
1950struct applet spoe_applet = {
1951 .obj_type = OBJ_TYPE_APPLET,
1952 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001953 .fct = spoe_handle_appctx,
1954 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001955};
1956
1957/* Create a SPOE applet. On success, the created applet is returned, else
1958 * NULL. */
1959static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001960spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961{
1962 struct appctx *appctx;
1963 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001964 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965
Emeric Brun1138fd02017-06-19 12:38:55 +02001966 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001967 goto out_error;
1968
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01001969 appctx->ctx.spoe.ptr = pool_zalloc(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001970 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971 goto out_free_appctx;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001972
Christopher Faulet42bfa462017-01-04 14:14:19 +01001973 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001974 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001975 goto out_free_spoe_appctx;
1976
1977 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001978 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001979 SPOE_APPCTX(appctx)->task->context = appctx;
1980 SPOE_APPCTX(appctx)->agent = conf->agent;
1981 SPOE_APPCTX(appctx)->version = 0;
1982 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1983 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001984 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001985 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001986 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001987
Willy Tarreau90f366b2021-02-20 11:49:49 +01001988 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001989 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001990 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001991
1992 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1993 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994
Willy Tarreau5820a362016-12-22 15:59:02 +01001995 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 if (!sess)
1997 goto out_free_spoe;
1998
Christopher Faulet26256f82020-09-14 11:40:13 +02001999 if ((strm = stream_new(sess, &appctx->obj_type, &BUF_NULL)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002000 goto out_free_sess;
2001
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002 stream_set_backend(strm, conf->agent->b.be);
2003
2004 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002005 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006 appctx_wakeup(appctx);
2007
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002008 strm->do_log = NULL;
2009 strm->res.flags |= CF_READ_DONTWAIT;
2010
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002011 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau2b718102021-04-21 07:32:39 +02002012 LIST_APPEND(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002013 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Willy Tarreau4781b152021-04-06 13:53:36 +02002014 _HA_ATOMIC_INC(&conf->agent->counters.applets);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002015
Emeric Brunc60def82017-09-27 14:59:38 +02002016 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002017 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018 return appctx;
2019
2020 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021 out_free_sess:
2022 session_free(sess);
2023 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002024 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002025 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002026 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002027 out_free_appctx:
2028 appctx_free(appctx);
2029 out_error:
2030 return NULL;
2031}
2032
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002034spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035{
2036 struct spoe_config *conf = FLT_CONF(ctx->filter);
2037 struct spoe_agent *agent = conf->agent;
2038 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002039 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002042 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002043 (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 +01002044 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045
2046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002047 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002048 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2049 ctx->strm);
2050
Christopher Fauleta1cda022016-12-21 08:58:06 +01002051 /* Do not try to create a new applet if there is no server up for the
2052 * agent's backend. */
2053 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2054 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2055 " - cannot create SPOE appctx: no server up\n",
2056 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2057 __FUNCTION__, ctx->strm);
2058 goto end;
2059 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002060
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 /* Do not try to create a new applet if we have reached the maximum of
2062 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002063 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002064 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2066 " - cannot create SPOE appctx: max CPS reached\n",
2067 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2068 __FUNCTION__, ctx->strm);
2069 goto end;
2070 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002071 }
2072
Christopher Faulet8ef75252017-02-20 22:56:03 +01002073 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 if (appctx == NULL) {
2075 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2076 " - failed to create SPOE appctx\n",
2077 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2078 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002079 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002080 "SPOE: [%s] failed to create SPOE applet\n",
2081 agent->id);
2082
Christopher Fauleta1cda022016-12-21 08:58:06 +01002083 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084 }
2085
Christopher Fauleta1cda022016-12-21 08:58:06 +01002086 /* Increase the per-process number of cumulated connections */
2087 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002088 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002089
Christopher Fauleta1cda022016-12-21 08:58:06 +01002090 end:
2091 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002092 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002093 ctx->status_code = SPOE_CTX_ERR_RES;
2094 return -1;
2095 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002096
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002097 /* Add the SPOE context in the sending queue if the stream has no applet
2098 * already assigned and wakeup all idle applets. Otherwise, don't queue
2099 * it. */
Willy Tarreau4781b152021-04-06 13:53:36 +02002100 _HA_ATOMIC_INC(&agent->counters.nb_sending);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002101 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2102 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002103 if (ctx->spoe_appctx)
2104 return 1;
Willy Tarreau2b718102021-04-21 07:32:39 +02002105 LIST_APPEND(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002106
2107 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002108 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002109 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002110 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002111 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002112 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002113
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002114 /* Finally try to wakeup an IDLE applet. */
2115 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2116 struct eb32_node *node;
2117
2118 node = eb32_first(&agent->rt[tid].idle_applets);
2119 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2120 if (node && spoe_appctx) {
2121 eb32_delete(&spoe_appctx->node);
2122 spoe_appctx->node.key++;
2123 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2124 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002125 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002126 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002127 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002128}
2129
2130/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002131 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002132 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002133/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2134 * fragmented_content. If the next message can be processed, it returns 0. If
2135 * the message is too big, it returns -1.*/
2136static int
2137spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2138 struct spoe_message *msg, int dir,
2139 char **buf, char *end)
2140{
2141 struct sample *smp;
2142 struct spoe_arg *arg;
2143 int ret;
2144
2145 if (msg->cond) {
2146 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2147 ret = acl_pass(ret);
2148 if (msg->cond->pol == ACL_COND_UNLESS)
2149 ret = !ret;
2150
2151 /* the rule does not match */
2152 if (!ret)
2153 goto next;
2154 }
2155
2156 /* Resume encoding of a SPOE argument */
2157 if (ctx->frag_ctx.curarg != NULL) {
2158 arg = ctx->frag_ctx.curarg;
2159 goto encode_argument;
2160 }
2161
2162 if (ctx->frag_ctx.curoff != UINT_MAX)
2163 goto encode_msg_payload;
2164
2165 /* Check if there is enough space for the message name and the
2166 * number of arguments. It implies <msg->id_len> is encoded on 2
2167 * bytes, at most (< 2288). */
2168 if (*buf + 2 + msg->id_len + 1 > end)
2169 goto too_big;
2170
2171 /* Encode the message name */
2172 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2173 goto too_big;
2174
2175 /* Set the number of arguments for this message */
2176 **buf = msg->nargs;
2177 (*buf)++;
2178
2179 ctx->frag_ctx.curoff = 0;
2180 encode_msg_payload:
2181
2182 /* Loop on arguments */
2183 list_for_each_entry(arg, &msg->args, list) {
2184 ctx->frag_ctx.curarg = arg;
2185 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002186 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002187
2188 encode_argument:
2189 if (ctx->frag_ctx.curoff != UINT_MAX)
2190 goto encode_arg_value;
2191
Joseph Herlantf7f60312018-11-15 13:46:49 -08002192 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002193 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2194 goto too_big;
2195
2196 ctx->frag_ctx.curoff = 0;
2197 encode_arg_value:
2198
Joseph Herlantf7f60312018-11-15 13:46:49 -08002199 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002200 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002201 if (smp) {
2202 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2203 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2204 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002205 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002206 if (ret == -1 || ctx->frag_ctx.curoff)
2207 goto too_big;
2208 }
2209
2210 next:
2211 return 0;
2212
2213 too_big:
2214 return -1;
2215}
2216
Christopher Fauletc718b822017-09-21 16:50:56 +02002217/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2218 * handle fragmented content. On success it returns 1. If an error occurred, -1
2219 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2220 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002221static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002222spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002223 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002224{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002225 struct spoe_config *conf = FLT_CONF(ctx->filter);
2226 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002227 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002228 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002229
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002230 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002231 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002232
Christopher Fauletc718b822017-09-21 16:50:56 +02002233 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2234 /* Resume encoding of a SPOE message */
2235 if (ctx->frag_ctx.curmsg != NULL) {
2236 msg = ctx->frag_ctx.curmsg;
2237 goto encode_evt_message;
2238 }
2239
2240 list_for_each_entry(msg, messages, by_evt) {
2241 ctx->frag_ctx.curmsg = msg;
2242 ctx->frag_ctx.curarg = NULL;
2243 ctx->frag_ctx.curoff = UINT_MAX;
2244
2245 encode_evt_message:
2246 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2247 goto too_big;
2248 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002250 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2251 /* Resume encoding of a SPOE message */
2252 if (ctx->frag_ctx.curmsg != NULL) {
2253 msg = ctx->frag_ctx.curmsg;
2254 goto encode_grp_message;
2255 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002256
Christopher Fauletc718b822017-09-21 16:50:56 +02002257 list_for_each_entry(msg, messages, by_grp) {
2258 ctx->frag_ctx.curmsg = msg;
2259 ctx->frag_ctx.curarg = NULL;
2260 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002261
Christopher Fauletc718b822017-09-21 16:50:56 +02002262 encode_grp_message:
2263 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2264 goto too_big;
2265 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002266 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002267 else
2268 goto skip;
2269
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002270
Christopher Faulet57583e42017-09-04 15:41:09 +02002271 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002272 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002273 goto skip;
2274
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002275 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002276 " - encode %s messages - spoe_appctx=%p"
2277 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002278 (int)now.tv_sec, (int)now.tv_usec,
2279 agent->id, __FUNCTION__, s,
2280 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002281 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002282 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002283
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002284 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002285 ctx->frag_ctx.curmsg = NULL;
2286 ctx->frag_ctx.curarg = NULL;
2287 ctx->frag_ctx.curoff = 0;
2288 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002289
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002290 return 1;
2291
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002293 /* Return an error if fragmentation is unsupported or if nothing has
2294 * been encoded because its too big and not splittable. */
2295 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002296 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2297 return -1;
2298 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002299
2300 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002301 " - encode fragmented messages - spoe_appctx=%p"
2302 " - curmsg=%p - curarg=%p - curoff=%u"
2303 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002304 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002305 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002306 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002307 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002308
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002309 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002310 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2311 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2312 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002313
2314 skip:
2315 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2316 " - skip the frame because nothing has been encoded\n",
2317 (int)now.tv_sec, (int)now.tv_usec,
2318 agent->id, __FUNCTION__, s);
2319 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320}
2321
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002322
2323/***************************************************************************
2324 * Functions that handle SPOE actions
2325 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326/* Helper function to set a variable */
2327static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002328spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002329 struct sample *smp)
2330{
2331 struct spoe_config *conf = FLT_CONF(ctx->filter);
2332 struct spoe_agent *agent = conf->agent;
2333 char varname[64];
2334
2335 memset(varname, 0, sizeof(varname));
2336 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2337 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002338 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2339 vars_set_by_name(varname, len, smp);
2340 else
2341 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002342}
2343
2344/* Helper function to unset a variable */
2345static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002346spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347 struct sample *smp)
2348{
2349 struct spoe_config *conf = FLT_CONF(ctx->filter);
2350 struct spoe_agent *agent = conf->agent;
2351 char varname[64];
2352
2353 memset(varname, 0, sizeof(varname));
2354 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2355 scope, agent->var_pfx, len, name);
2356 vars_unset_by_name_ifexist(varname, len, smp);
2357}
2358
2359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360static inline int
2361spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2362 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002363{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002364 char *str, *scope, *p = *buf;
2365 struct sample smp;
2366 uint64_t sz;
2367 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368
Christopher Faulet8ef75252017-02-20 22:56:03 +01002369 if (p + 2 >= end)
2370 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 /* SET-VAR requires 3 arguments */
2373 if (*p++ != 3)
2374 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 switch (*p++) {
2377 case SPOE_SCOPE_PROC: scope = "proc"; break;
2378 case SPOE_SCOPE_SESS: scope = "sess"; break;
2379 case SPOE_SCOPE_TXN : scope = "txn"; break;
2380 case SPOE_SCOPE_REQ : scope = "req"; break;
2381 case SPOE_SCOPE_RES : scope = "res"; break;
2382 default: goto skip;
2383 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002384
Christopher Faulet8ef75252017-02-20 22:56:03 +01002385 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2386 goto skip;
2387 memset(&smp, 0, sizeof(smp));
2388 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 if (spoe_decode_data(&p, end, &smp) == -1)
2391 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002392
Christopher Faulet8ef75252017-02-20 22:56:03 +01002393 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2394 " - set-var '%s.%s.%.*s'\n",
2395 (int)now.tv_sec, (int)now.tv_usec,
2396 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2397 __FUNCTION__, s, scope,
2398 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2399 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400
Christopher Faulet2469eba2020-10-15 16:08:30 +02002401 if (smp.data.type == SMP_T_ANY)
2402 spoe_unset_var(ctx, scope, str, sz, &smp);
2403 else
2404 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406 ret = (p - *buf);
2407 *buf = p;
2408 return ret;
2409 skip:
2410 return 0;
2411}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413static inline int
2414spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2415 char **buf, char *end, int dir)
2416{
2417 char *str, *scope, *p = *buf;
2418 struct sample smp;
2419 uint64_t sz;
2420 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 if (p + 2 >= end)
2423 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002424
Christopher Faulet8ef75252017-02-20 22:56:03 +01002425 /* UNSET-VAR requires 2 arguments */
2426 if (*p++ != 2)
2427 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428
Christopher Faulet8ef75252017-02-20 22:56:03 +01002429 switch (*p++) {
2430 case SPOE_SCOPE_PROC: scope = "proc"; break;
2431 case SPOE_SCOPE_SESS: scope = "sess"; break;
2432 case SPOE_SCOPE_TXN : scope = "txn"; break;
2433 case SPOE_SCOPE_REQ : scope = "req"; break;
2434 case SPOE_SCOPE_RES : scope = "res"; break;
2435 default: goto skip;
2436 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437
Christopher Faulet8ef75252017-02-20 22:56:03 +01002438 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2439 goto skip;
2440 memset(&smp, 0, sizeof(smp));
2441 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2444 " - unset-var '%s.%s.%.*s'\n",
2445 (int)now.tv_sec, (int)now.tv_usec,
2446 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2447 __FUNCTION__, s, scope,
2448 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2449 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002450
Christopher Faulet8ef75252017-02-20 22:56:03 +01002451 spoe_unset_var(ctx, scope, str, sz, &smp);
2452
2453 ret = (p - *buf);
2454 *buf = p;
2455 return ret;
2456 skip:
2457 return 0;
2458}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002459
Christopher Faulet8ef75252017-02-20 22:56:03 +01002460/* Process SPOE actions for a specific event. It returns 1 on success. If an
2461 * error occurred, 0 is returned. */
2462static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002463spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464{
2465 char *p, *end;
2466 int ret;
2467
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002468 p = b_head(&ctx->buffer);
2469 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002470
2471 while (p < end) {
2472 enum spoe_action_type type;
2473
2474 type = *p++;
2475 switch (type) {
2476 case SPOE_ACT_T_SET_VAR:
2477 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2478 if (!ret)
2479 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002480 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002481
Christopher Faulet8ef75252017-02-20 22:56:03 +01002482 case SPOE_ACT_T_UNSET_VAR:
2483 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2484 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002485 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002486 break;
2487
2488 default:
2489 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002490 }
2491 }
2492
2493 return 1;
2494 skip:
2495 return 0;
2496}
2497
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002498/***************************************************************************
2499 * Functions that process SPOE events
2500 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002501static void
2502spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2503 struct spoe_context *ctx, int dir)
2504{
2505 if (!tv_iszero(&ctx->stats.tv_start)) {
2506 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2507 ctx->stats.t_total += ctx->stats.t_process;
2508 tv_zero(&ctx->stats.tv_request);
2509 tv_zero(&ctx->stats.tv_queue);
2510 tv_zero(&ctx->stats.tv_wait);
2511 tv_zero(&ctx->stats.tv_response);
2512 }
2513
2514 if (agent->var_t_process) {
2515 struct sample smp;
2516
2517 memset(&smp, 0, sizeof(smp));
2518 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2519 smp.data.u.sint = ctx->stats.t_process;
2520 smp.data.type = SMP_T_SINT;
2521
2522 spoe_set_var(ctx, "txn", agent->var_t_process,
2523 strlen(agent->var_t_process), &smp);
2524 }
2525
2526 if (agent->var_t_total) {
2527 struct sample smp;
2528
2529 memset(&smp, 0, sizeof(smp));
2530 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2531 smp.data.u.sint = ctx->stats.t_total;
2532 smp.data.type = SMP_T_SINT;
2533
2534 spoe_set_var(ctx, "txn", agent->var_t_total,
2535 strlen(agent->var_t_total), &smp);
2536 }
2537}
2538
2539static void
2540spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2541 struct spoe_context *ctx, int dir)
2542{
2543 if (agent->eps_max > 0)
2544 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2545
2546 if (agent->var_on_error) {
2547 struct sample smp;
2548
2549 memset(&smp, 0, sizeof(smp));
2550 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2551 smp.data.u.sint = ctx->status_code;
2552 smp.data.type = SMP_T_BOOL;
2553
2554 spoe_set_var(ctx, "txn", agent->var_on_error,
2555 strlen(agent->var_on_error), &smp);
2556 }
2557
2558 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2559 ? SPOE_CTX_ST_READY
2560 : SPOE_CTX_ST_NONE);
2561}
2562
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002563static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002564spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002565{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002566 /* If a process is already started for this SPOE context, retry
2567 * later. */
2568 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002571 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002572 ctx->stats.tv_start = now;
2573 ctx->stats.tv_request = now;
2574 ctx->stats.t_request = -1;
2575 ctx->stats.t_queue = -1;
2576 ctx->stats.t_waiting = -1;
2577 ctx->stats.t_response = -1;
2578 ctx->stats.t_process = -1;
2579
2580 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002581
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582 /* Set the right flag to prevent request and response processing
2583 * in same time. */
2584 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2585 ? SPOE_CTX_FL_REQ_PROCESS
2586 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002588}
2589
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002590static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002591spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002593 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002594
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002595 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2596 return;
Willy Tarreau4781b152021-04-06 13:53:36 +02002597 _HA_ATOMIC_INC(&agent->counters.nb_processed);
Christopher Faulet879dca92018-03-23 11:53:24 +01002598 if (sa) {
2599 if (sa->frag_ctx.ctx == ctx) {
2600 sa->frag_ctx.ctx = NULL;
2601 spoe_wakeup_appctx(sa->owner);
2602 }
2603 else
2604 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002605 }
2606
Christopher Fauleta1cda022016-12-21 08:58:06 +01002607 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002608 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002609 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610
2611 /* Reset processing timer */
2612 ctx->process_exp = TICK_ETERNITY;
2613
Christopher Faulet8ef75252017-02-20 22:56:03 +01002614 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002615
Christopher Fauletfce747b2018-01-24 15:59:32 +01002616 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002617 ctx->frag_ctx.curmsg = NULL;
2618 ctx->frag_ctx.curarg = NULL;
2619 ctx->frag_ctx.curoff = 0;
2620 ctx->frag_ctx.flags = 0;
2621
Christopher Fauleta1cda022016-12-21 08:58:06 +01002622 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002623 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Willy Tarreau4781b152021-04-06 13:53:36 +02002624 _HA_ATOMIC_DEC(&agent->counters.nb_sending);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002625 else
Willy Tarreau4781b152021-04-06 13:53:36 +02002626 _HA_ATOMIC_DEC(&agent->counters.nb_waiting);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002627
Willy Tarreau2b718102021-04-21 07:32:39 +02002628 LIST_DELETE(&ctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002629 LIST_INIT(&ctx->list);
2630 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002631}
2632
Christopher Faulet58d03682017-09-21 16:57:24 +02002633/* Process a list of SPOE messages. First, this functions will process messages
2634 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2635 * to process corresponding actions. During all the processing, it returns 0
2636 * and it returns 1 when the processing is finished. If an error occurred, -1
2637 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002638static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002639spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2640 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002641{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002642 struct spoe_config *conf = FLT_CONF(ctx->filter);
2643 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002644 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002645
2646 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002647 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002648
2649 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2650 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002651 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002652 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002653 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002654 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002655 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002656 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002657
2658 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002659 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002660 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002662 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002663 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665 goto skip;
2666 }
2667 }
2668
Christopher Fauletf7a30922016-11-10 15:04:51 +01002669 if (!tick_isset(ctx->process_exp)) {
2670 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2671 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2672 ctx->process_exp);
2673 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002674 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002675 if (!ret)
2676 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002677
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002678 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002679 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002680 }
2681
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002682 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002683 if (tv_iszero(&ctx->stats.tv_request))
2684 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002685 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002686 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002687 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002688 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002689 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002690 if (!ret)
2691 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002692 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002693 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002694 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2695 }
2696
2697 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002698 if (ctx->spoe_appctx)
2699 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700 ret = 0;
2701 goto out;
2702 }
2703
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002704 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2705 ret = 0;
2706 goto out;
2707 }
2708
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002709 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002710 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002711 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002712 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002713 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002714 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002715 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002716 }
2717
2718 out:
2719 return ret;
2720
Christopher Fauleta1cda022016-12-21 08:58:06 +01002721 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002722 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002723 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002724 spoe_stop_processing(agent, ctx);
2725 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002726
Christopher Fauleta1cda022016-12-21 08:58:06 +01002727 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002728 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002729 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002730 if (ctx->status_code) {
Willy Tarreau4781b152021-04-06 13:53:36 +02002731 _HA_ATOMIC_INC(&agent->counters.nb_errors);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002732 spoe_handle_processing_error(s, agent, ctx, dir);
2733 ret = 1;
2734 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002735 return ret;
2736}
2737
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002738/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2739 * See spoe_process_message for details. */
2740static int
2741spoe_process_group(struct stream *s, struct spoe_context *ctx,
2742 struct spoe_group *group, int dir)
2743{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002744 struct spoe_config *conf = FLT_CONF(ctx->filter);
2745 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002746 int ret;
2747
2748 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2749 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002750 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002751 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2752 group->id);
2753
2754 if (LIST_ISEMPTY(&group->messages))
2755 return 1;
2756
2757 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002758 if (ret && ctx->stats.t_process != -1) {
2759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002760 " - <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 +01002761 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002762 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002763 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002764 ctx->stats.t_response, ctx->stats.t_process,
2765 agent->counters.idles, agent->counters.applets,
2766 agent->counters.nb_sending, agent->counters.nb_waiting,
2767 agent->counters.nb_errors, agent->counters.nb_processed,
2768 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2769 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2770 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2771 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2772 agent->id, group->id, s->uniq_id, ctx->status_code,
2773 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2774 ctx->stats.t_response, ctx->stats.t_process,
2775 agent->counters.idles, agent->counters.applets,
2776 agent->counters.nb_sending, agent->counters.nb_waiting,
2777 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002778 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002779 return ret;
2780}
2781
Christopher Faulet58d03682017-09-21 16:57:24 +02002782/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2783 * See spoe_process_message for details. */
2784static int
2785spoe_process_event(struct stream *s, struct spoe_context *ctx,
2786 enum spoe_event ev)
2787{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002788 struct spoe_config *conf = FLT_CONF(ctx->filter);
2789 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002790 int dir, ret;
2791
2792 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002793 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002794 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002795 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2796 spoe_event_str[ev]);
2797
2798 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2799
2800 if (LIST_ISEMPTY(&(ctx->events[ev])))
2801 return 1;
2802
2803 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002804 if (ret && ctx->stats.t_process != -1) {
2805 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002806 " - <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 +01002807 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2808 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2809 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002810 ctx->stats.t_response, ctx->stats.t_process,
2811 agent->counters.idles, agent->counters.applets,
2812 agent->counters.nb_sending, agent->counters.nb_waiting,
2813 agent->counters.nb_errors, agent->counters.nb_processed,
2814 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2815 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2816 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2817 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2818 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2819 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2820 ctx->stats.t_response, ctx->stats.t_process,
2821 agent->counters.idles, agent->counters.applets,
2822 agent->counters.nb_sending, agent->counters.nb_waiting,
2823 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002824 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002825 return ret;
2826}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002827
2828/***************************************************************************
2829 * Functions that create/destroy SPOE contexts
2830 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002831static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002832spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002833{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002835 return 1;
2836
Willy Tarreau2b718102021-04-21 07:32:39 +02002837 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002838 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002839
Willy Tarreaud68d4f12021-03-22 14:44:31 +01002840 if (b_alloc(buf))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002841 return 1;
2842
Willy Tarreau2b718102021-04-21 07:32:39 +02002843 LIST_APPEND(&ti->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002844 return 0;
2845}
2846
2847static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002849{
Willy Tarreau2b718102021-04-21 07:32:39 +02002850 if (LIST_INLIST(&buffer_wait->list))
Willy Tarreau90f366b2021-02-20 11:49:49 +01002851 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002852
2853 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002854 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002855 b_free(buf);
Willy Tarreau4d77bbf2021-02-20 12:02:46 +01002856 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002857 }
2858}
2859
Christopher Faulet4596fb72017-01-11 14:05:19 +01002860static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002861spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002862{
2863 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2864 return 1;
2865}
2866
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002867static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002868spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002869{
2870 struct spoe_config *conf = FLT_CONF(filter);
2871 struct spoe_context *ctx;
2872
Willy Tarreauc9ef9bc2021-03-22 21:04:50 +01002873 ctx = pool_zalloc(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002874 if (ctx == NULL) {
2875 return NULL;
2876 }
Christopher Fauletb067b062017-01-04 16:39:11 +01002877 ctx->filter = filter;
2878 ctx->state = SPOE_CTX_ST_NONE;
2879 ctx->status_code = SPOE_CTX_ERR_NONE;
2880 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002881 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002882 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002883 ctx->buffer = BUF_NULL;
Willy Tarreau90f366b2021-02-20 11:49:49 +01002884 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002885 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002886 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002887 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002888
Christopher Fauletf7a30922016-11-10 15:04:51 +01002889 ctx->stream_id = 0;
2890 ctx->frame_id = 1;
2891 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002892
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002893 tv_zero(&ctx->stats.tv_start);
2894 tv_zero(&ctx->stats.tv_request);
2895 tv_zero(&ctx->stats.tv_queue);
2896 tv_zero(&ctx->stats.tv_wait);
2897 tv_zero(&ctx->stats.tv_response);
2898 ctx->stats.t_request = -1;
2899 ctx->stats.t_queue = -1;
2900 ctx->stats.t_waiting = -1;
2901 ctx->stats.t_response = -1;
2902 ctx->stats.t_process = -1;
2903 ctx->stats.t_total = 0;
2904
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002905 ctx->strm = s;
2906 ctx->state = SPOE_CTX_ST_READY;
2907 filter->ctx = ctx;
2908
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002909 return ctx;
2910}
2911
2912static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002913spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002914{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002915 struct spoe_config *conf = FLT_CONF(filter);
2916 struct spoe_context *ctx = filter->ctx;
2917
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002918 if (!ctx)
2919 return;
2920
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002921 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002922 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002923 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002924}
2925
2926static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002927spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002928{
2929 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002930 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002931
2932 tv_zero(&ctx->stats.tv_start);
2933 tv_zero(&ctx->stats.tv_request);
2934 tv_zero(&ctx->stats.tv_queue);
2935 tv_zero(&ctx->stats.tv_wait);
2936 tv_zero(&ctx->stats.tv_response);
2937 ctx->stats.t_request = -1;
2938 ctx->stats.t_queue = -1;
2939 ctx->stats.t_waiting = -1;
2940 ctx->stats.t_response = -1;
2941 ctx->stats.t_process = -1;
2942 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002943}
2944
2945
2946/***************************************************************************
2947 * Hooks that manage the filter lifecycle (init/check/deinit)
2948 **************************************************************************/
2949/* Signal handler: Do a soft stop, wakeup SPOE applet */
2950static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002951spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952{
2953 struct proxy *p;
2954
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002955 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956 while (p) {
2957 struct flt_conf *fconf;
2958
2959 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002960 struct spoe_config *conf;
2961 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002962 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002963 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964
Christopher Faulet3b386a32017-02-23 10:17:15 +01002965 if (fconf->id != spoe_filter_id)
2966 continue;
2967
2968 conf = fconf->conf;
2969 agent = conf->agent;
2970
Christopher Faulet24289f22017-09-25 14:48:02 +02002971 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002972 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002973 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2974 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002975 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002976 }
2977 }
2978 p = p->next;
2979 }
2980}
2981
2982
2983/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2984static int
2985spoe_init(struct proxy *px, struct flt_conf *fconf)
2986{
2987 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002988
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002989 /* conf->agent_fe was already initialized during the config
2990 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002991 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002992 conf->agent_fe.cap = PR_CAP_FE;
2993 conf->agent_fe.mode = PR_MODE_TCP;
2994 conf->agent_fe.maxconn = 0;
2995 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2996 conf->agent_fe.conn_retries = CONN_RETRIES;
2997 conf->agent_fe.accept = frontend_accept;
2998 conf->agent_fe.srv = NULL;
2999 conf->agent_fe.timeout.client = TICK_ETERNITY;
3000 conf->agent_fe.default_target = &spoe_applet.obj_type;
3001 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3002
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003003 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003004 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003005 sighandler_registered = 1;
3006 }
3007
Christopher Faulet00292352019-01-09 15:05:10 +01003008 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003009 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010}
3011
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003012/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003013static void
3014spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3015{
3016 struct spoe_config *conf = fconf->conf;
3017
3018 if (conf) {
3019 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003020
Christopher Faulet8ef75252017-02-20 22:56:03 +01003021 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003022 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003023 free(conf);
3024 }
3025 fconf->conf = NULL;
3026}
3027
3028/* Check configuration of a SPOE filter for a specified proxy.
3029 * Return 1 on error, else 0. */
3030static int
3031spoe_check(struct proxy *px, struct flt_conf *fconf)
3032{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003033 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003034 struct spoe_config *conf = fconf->conf;
3035 struct proxy *target;
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003036 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003037 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003038
Christopher Faulet7ee86672017-09-19 11:08:28 +02003039 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3040 * are uniq */
3041 list_for_each_entry(f, &px->filter_configs, list) {
3042 struct spoe_config *c = f->conf;
3043
3044 /* This is not an SPOE filter */
3045 if (f->id != spoe_filter_id)
3046 continue;
3047 /* This is the current SPOE filter */
3048 if (f == fconf)
3049 continue;
3050
3051 /* Check engine Id. It should be uniq */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003052 if (strcmp(conf->id, c->id) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003053 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3054 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003055 return 1;
3056 }
3057 }
3058
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003059 target = proxy_be_by_name(conf->agent->b.name);
3060 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003061 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3062 " declared at %s:%d.\n",
3063 px->id, conf->agent->b.name, conf->agent->id,
3064 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003065 return 1;
3066 }
3067 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003068 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3069 " at %s:%d does not support HTTP mode.\n",
3070 px->id, target->id, conf->agent->id,
3071 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003072 return 1;
3073 }
3074
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003075 if (px->bind_proc & ~target->bind_proc) {
3076 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3077 " at %s:%d does not cover all of its processes.\n",
3078 px->id, target->id, conf->agent->id,
3079 conf->agent->conf.file, conf->agent->conf.line);
3080 return 1;
3081 }
3082
Christopher Fauletfe261552019-03-18 13:57:42 +01003083 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003084 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3085 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3086 return 1;
3087 }
3088 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003089 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003090 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3091 conf->agent->rt[i].processing = 0;
3092 LIST_INIT(&conf->agent->rt[i].applets);
3093 LIST_INIT(&conf->agent->rt[i].sending_queue);
3094 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3095 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003096 }
3097
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003098 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3099 if (logsrv->type == LOG_TARGET_BUFFER) {
3100 struct sink *sink = sink_find(logsrv->ring_name);
3101
3102 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3103 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
Ilya Shipitsind7a988c2021-03-04 23:26:15 +05003104 " at %s:%d uses unknown ring named '%s'.\n",
Christopher Faulet1d7d0f82021-02-19 10:56:41 +01003105 px->id, conf->agent->id, conf->agent->conf.file,
3106 conf->agent->conf.line, logsrv->ring_name);
3107 return 1;
3108 }
3109 logsrv->sink = sink;
3110 }
3111 }
3112
Willy Tarreau61cfdf42021-02-20 10:46:51 +01003113 ha_free(&conf->agent->b.name);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003114 conf->agent->b.be = target;
3115 return 0;
3116}
3117
Kevin Zhud87b1a52019-09-17 15:05:45 +02003118/* Initializes the SPOE filter for a proxy for a specific thread.
3119 * Returns a negative value if an error occurs. */
3120static int
3121spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3122{
3123 struct spoe_config *conf = fconf->conf;
3124 struct spoe_agent *agent = conf->agent;
3125
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003126 agent->rt[tid].engine_id = generate_pseudo_uuid();
3127 if (agent->rt[tid].engine_id == NULL)
3128 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003129 return 0;
3130}
3131
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003132/**************************************************************************
3133 * Hooks attached to a stream
3134 *************************************************************************/
3135/* Called when a filter instance is created and attach to a stream. It creates
3136 * the context that will be used to process this stream. */
3137static int
3138spoe_start(struct stream *s, struct filter *filter)
3139{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003140 struct spoe_config *conf = FLT_CONF(filter);
3141 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003142 struct spoe_context *ctx;
3143
3144 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003145 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003146 __FUNCTION__, s);
3147
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003148 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003149 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3150 " - failed to create SPOE context\n",
3151 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003152 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003153 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003154 "SPOE: [%s] failed to create SPOE context\n",
3155 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003156 return 0;
3157 }
3158
Christopher Faulet11610f32017-09-21 10:23:10 +02003159 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003160 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3161
Christopher Faulet11610f32017-09-21 10:23:10 +02003162 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3164
Christopher Faulet11610f32017-09-21 10:23:10 +02003165 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003166 filter->pre_analyzers |= AN_RES_INSPECT;
3167
Christopher Faulet11610f32017-09-21 10:23:10 +02003168 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003169 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3170
Christopher Faulet11610f32017-09-21 10:23:10 +02003171 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003172 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3173
Christopher Faulet11610f32017-09-21 10:23:10 +02003174 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003175 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3176
3177 return 1;
3178}
3179
3180/* Called when a filter instance is detached from a stream. It release the
3181 * attached SPOE context. */
3182static void
3183spoe_stop(struct stream *s, struct filter *filter)
3184{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003185 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3186 (int)now.tv_sec, (int)now.tv_usec,
3187 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3188 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003189 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003190}
3191
Christopher Fauletf7a30922016-11-10 15:04:51 +01003192
3193/*
3194 * Called when the stream is woken up because of expired timer.
3195 */
3196static void
3197spoe_check_timeouts(struct stream *s, struct filter *filter)
3198{
3199 struct spoe_context *ctx = filter->ctx;
3200
Christopher Fauletac580602018-03-20 16:09:20 +01003201 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003202 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003203}
3204
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003205/* Called when we are ready to filter data on a channel */
3206static int
3207spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3208{
3209 struct spoe_context *ctx = filter->ctx;
3210 int ret = 1;
3211
3212 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3213 " - ctx-flags=0x%08x\n",
3214 (int)now.tv_sec, (int)now.tv_usec,
3215 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3216 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3217
Christopher Fauletb067b062017-01-04 16:39:11 +01003218 if (ctx->state == SPOE_CTX_ST_NONE)
3219 goto out;
3220
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 if (!(chn->flags & CF_ISRESP)) {
3222 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3223 chn->analysers |= AN_REQ_INSPECT_FE;
3224 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3225 chn->analysers |= AN_REQ_INSPECT_BE;
3226
3227 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3228 goto out;
3229
3230 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003231 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003232 if (!ret)
3233 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003234 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3235 }
3236 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003237 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003238 chn->analysers |= AN_RES_INSPECT;
3239
3240 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3241 goto out;
3242
Christopher Faulet8ef75252017-02-20 22:56:03 +01003243 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003244 if (!ret) {
3245 channel_dont_read(chn);
3246 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003247 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003248 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003249 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 }
3251
3252 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003253 return ret;
3254}
3255
3256/* Called before a processing happens on a given channel */
3257static int
3258spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3259 struct channel *chn, unsigned an_bit)
3260{
3261 struct spoe_context *ctx = filter->ctx;
3262 int ret = 1;
3263
3264 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3265 " - ctx-flags=0x%08x - ana=0x%08x\n",
3266 (int)now.tv_sec, (int)now.tv_usec,
3267 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3268 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3269 ctx->flags, an_bit);
3270
Christopher Fauletb067b062017-01-04 16:39:11 +01003271 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003272 goto out;
3273
3274 switch (an_bit) {
3275 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003276 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003277 break;
3278 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003279 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003280 break;
3281 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003282 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003283 break;
3284 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003285 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003286 break;
3287 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003288 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289 break;
3290 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003291 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292 break;
3293 }
3294
3295 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003296 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297 channel_dont_read(chn);
3298 channel_dont_close(chn);
3299 }
3300 return ret;
3301}
3302
3303/* Called when the filtering on the channel ends. */
3304static int
3305spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3306{
3307 struct spoe_context *ctx = filter->ctx;
3308
3309 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3310 " - ctx-flags=0x%08x\n",
3311 (int)now.tv_sec, (int)now.tv_usec,
3312 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3313 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3314
3315 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003316 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 }
3318
3319 return 1;
3320}
3321
3322/********************************************************************
3323 * Functions that manage the filter initialization
3324 ********************************************************************/
3325struct flt_ops spoe_ops = {
3326 /* Manage SPOE filter, called for each filter declaration */
3327 .init = spoe_init,
3328 .deinit = spoe_deinit,
3329 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003330 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003331
3332 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003333 .attach = spoe_start,
3334 .detach = spoe_stop,
3335 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003336
3337 /* Handle channels activity */
3338 .channel_start_analyze = spoe_start_analyze,
3339 .channel_pre_analyze = spoe_chn_pre_analyze,
3340 .channel_end_analyze = spoe_end_analyze,
3341};
3342
3343
3344static int
3345cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3346{
3347 const char *err;
3348 int i, err_code = 0;
3349
3350 if ((cfg_scope == NULL && curengine != NULL) ||
3351 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003352 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003353 goto out;
3354
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003355 if (strcmp(args[0], "spoe-agent") == 0) { /* new spoe-agent section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003356 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003357 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3358 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003359 err_code |= ERR_ALERT | ERR_ABORT;
3360 goto out;
3361 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003362 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3363 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003364 goto out;
3365 }
3366
3367 err = invalid_char(args[1]);
3368 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003369 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3370 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 err_code |= ERR_ALERT | ERR_ABORT;
3372 goto out;
3373 }
3374
3375 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003376 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3377 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 err_code |= ERR_ALERT | ERR_ABORT;
3379 goto out;
3380 }
3381 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003382 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003383 err_code |= ERR_ALERT | ERR_ABORT;
3384 goto out;
3385 }
3386
3387 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003388
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003389 curagent->conf.file = strdup(file);
3390 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003391
3392 curagent->timeout.hello = TICK_ETERNITY;
3393 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003394 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003395
Christopher Fauleta1cda022016-12-21 08:58:06 +01003396 curagent->var_pfx = NULL;
3397 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003398 curagent->var_t_process = NULL;
3399 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003400 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003401 curagent->cps_max = 0;
3402 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003403 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003404 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405
3406 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003407 LIST_INIT(&curagent->events[i]);
3408 LIST_INIT(&curagent->groups);
3409 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003410 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003411 else if (strcmp(args[0], "use-backend") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003412 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003413 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3414 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003415 err_code |= ERR_ALERT | ERR_FATAL;
3416 goto out;
3417 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003418 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003419 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003420 free(curagent->b.name);
3421 curagent->b.name = strdup(args[1]);
3422 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003423 else if (strcmp(args[0], "messages") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003424 int cur_arg = 1;
3425 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003426 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003427
Christopher Faulet11610f32017-09-21 10:23:10 +02003428 list_for_each_entry(ph, &curmphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003429 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003430 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3431 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 err_code |= ERR_ALERT | ERR_FATAL;
3433 goto out;
3434 }
3435 }
3436
Christopher Faulet11610f32017-09-21 10:23:10 +02003437 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003438 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003439 err_code |= ERR_ALERT | ERR_ABORT;
3440 goto out;
3441 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003442 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003443 LIST_APPEND(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444 cur_arg++;
3445 }
3446 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003447 else if (strcmp(args[0], "groups") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003448 int cur_arg = 1;
3449 while (*args[cur_arg]) {
3450 struct spoe_placeholder *ph = NULL;
3451
3452 list_for_each_entry(ph, &curgphs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003453 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003454 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3455 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003456 err_code |= ERR_ALERT | ERR_FATAL;
3457 goto out;
3458 }
3459 }
3460
3461 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003462 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003463 err_code |= ERR_ALERT | ERR_ABORT;
3464 goto out;
3465 }
3466 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003467 LIST_APPEND(&curgphs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003468 cur_arg++;
3469 }
3470 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003471 else if (strcmp(args[0], "timeout") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003472 unsigned int *tv = NULL;
3473 const char *res;
3474 unsigned timeout;
3475
3476 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003477 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3478 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003479 err_code |= ERR_ALERT | ERR_FATAL;
3480 goto out;
3481 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003482 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3483 goto out;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003484 if (strcmp(args[1], "hello") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003485 tv = &curagent->timeout.hello;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003486 else if (strcmp(args[1], "idle") == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003487 tv = &curagent->timeout.idle;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003488 else if (strcmp(args[1], "processing") == 0)
Christopher Fauletf7a30922016-11-10 15:04:51 +01003489 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003490 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003491 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3492 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003493 err_code |= ERR_ALERT | ERR_FATAL;
3494 goto out;
3495 }
3496 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003497 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3498 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003499 err_code |= ERR_ALERT | ERR_FATAL;
3500 goto out;
3501 }
3502 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003503 if (res == PARSE_TIME_OVER) {
3504 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3505 file, linenum, args[2], args[0], args[1]);
3506 err_code |= ERR_ALERT | ERR_FATAL;
3507 goto out;
3508 }
3509 else if (res == PARSE_TIME_UNDER) {
3510 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3511 file, linenum, args[2], args[0], args[1]);
3512 err_code |= ERR_ALERT | ERR_FATAL;
3513 goto out;
3514 }
3515 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003516 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3517 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003518 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003519 goto out;
3520 }
3521 *tv = MS_TO_TICKS(timeout);
3522 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003523 else if (strcmp(args[0], "option") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003524 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003525 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3526 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003527 err_code |= ERR_ALERT | ERR_FATAL;
3528 goto out;
3529 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003530
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003531 if (strcmp(args[1], "pipelining") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003532 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003533 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003534 if (kwm == 1)
3535 curagent->flags &= ~SPOE_FL_PIPELINING;
3536 else
3537 curagent->flags |= SPOE_FL_PIPELINING;
3538 goto out;
3539 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003540 else if (strcmp(args[1], "async") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003541 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003542 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003543 if (kwm == 1)
3544 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003545 else
3546 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003547 goto out;
3548 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003549 else if (strcmp(args[1], "send-frag-payload") == 0) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01003550 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3551 goto out;
3552 if (kwm == 1)
3553 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3554 else
3555 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3556 goto out;
3557 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003558 else if (strcmp(args[1], "dontlog-normal") == 0) {
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003559 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3560 goto out;
3561 if (kwm == 1)
3562 curpxopts2 &= ~PR_O2_NOLOGNORM;
3563 else
3564 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003565 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003566 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003567
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003568 /* Following options does not support negation */
3569 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003570 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3571 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003572 err_code |= ERR_ALERT | ERR_FATAL;
3573 goto out;
3574 }
3575
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003576 if (strcmp(args[1], "var-prefix") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003577 char *tmp;
3578
3579 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003580 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3581 file, linenum, args[0],
3582 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003583 err_code |= ERR_ALERT | ERR_FATAL;
3584 goto out;
3585 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003586 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3587 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003588 tmp = args[2];
3589 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003590 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003591 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003592 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003593 err_code |= ERR_ALERT | ERR_FATAL;
3594 goto out;
3595 }
3596 tmp++;
3597 }
3598 curagent->var_pfx = strdup(args[2]);
3599 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003600 else if (strcmp(args[1], "force-set-var") == 0) {
Etienne Carriereaec89892017-12-14 09:36:40 +00003601 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3602 goto out;
3603 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3604 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003605 else if (strcmp(args[1], "continue-on-error") == 0) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003606 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003607 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003608 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3609 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003610 else if (strcmp(args[1], "set-on-error") == 0) {
Christopher Faulet985532d2016-11-16 15:36:19 +01003611 char *tmp;
3612
3613 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003614 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3615 file, linenum, args[0],
3616 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003617 err_code |= ERR_ALERT | ERR_FATAL;
3618 goto out;
3619 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003620 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3621 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003622 tmp = args[2];
3623 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003624 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003625 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003626 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003627 err_code |= ERR_ALERT | ERR_FATAL;
3628 goto out;
3629 }
3630 tmp++;
3631 }
3632 curagent->var_on_error = strdup(args[2]);
3633 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003634 else if (strcmp(args[1], "set-process-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003635 char *tmp;
3636
3637 if (!*args[2]) {
3638 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3639 file, linenum, args[0],
3640 args[1]);
3641 err_code |= ERR_ALERT | ERR_FATAL;
3642 goto out;
3643 }
3644 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3645 goto out;
3646 tmp = args[2];
3647 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003648 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003649 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003650 file, linenum, args[0], args[1]);
3651 err_code |= ERR_ALERT | ERR_FATAL;
3652 goto out;
3653 }
3654 tmp++;
3655 }
3656 curagent->var_t_process = strdup(args[2]);
3657 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003658 else if (strcmp(args[1], "set-total-time") == 0) {
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003659 char *tmp;
3660
3661 if (!*args[2]) {
3662 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3663 file, linenum, args[0],
3664 args[1]);
3665 err_code |= ERR_ALERT | ERR_FATAL;
3666 goto out;
3667 }
3668 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3669 goto out;
3670 tmp = args[2];
3671 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003672 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003673 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003674 file, linenum, args[0], args[1]);
3675 err_code |= ERR_ALERT | ERR_FATAL;
3676 goto out;
3677 }
3678 tmp++;
3679 }
3680 curagent->var_t_total = strdup(args[2]);
3681 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003682 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003683 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3684 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003685 err_code |= ERR_ALERT | ERR_FATAL;
3686 goto out;
3687 }
Christopher Faulet48026722016-11-16 15:01:12 +01003688 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003689 else if (strcmp(args[0], "maxconnrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003690 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003691 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3692 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003693 err_code |= ERR_ALERT | ERR_FATAL;
3694 goto out;
3695 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003696 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003697 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003698 curagent->cps_max = atol(args[1]);
3699 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003700 else if (strcmp(args[0], "maxerrrate") == 0) {
Christopher Faulet48026722016-11-16 15:01:12 +01003701 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003702 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3703 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003704 err_code |= ERR_ALERT | ERR_FATAL;
3705 goto out;
3706 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003707 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003708 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003709 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003710 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003711 else if (strcmp(args[0], "max-frame-size") == 0) {
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003712 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003713 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3714 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003715 err_code |= ERR_ALERT | ERR_FATAL;
3716 goto out;
3717 }
3718 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3719 goto out;
3720 curagent->max_frame_size = atol(args[1]);
3721 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3722 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003723 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3724 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003725 err_code |= ERR_ALERT | ERR_FATAL;
3726 goto out;
3727 }
3728 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003729 else if (strcmp(args[0], "max-waiting-frames") == 0) {
Christopher Faulete8ade382018-01-25 15:32:22 +01003730 if (!*args[1]) {
3731 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3732 file, linenum, args[0]);
3733 err_code |= ERR_ALERT | ERR_FATAL;
3734 goto out;
3735 }
3736 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3737 goto out;
3738 curagent->max_fpa = atol(args[1]);
3739 if (curagent->max_fpa < 1) {
3740 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3741 file, linenum, args[0]);
3742 err_code |= ERR_ALERT | ERR_FATAL;
3743 goto out;
3744 }
3745 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003746 else if (strcmp(args[0], "register-var-names") == 0) {
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003747 int cur_arg;
3748
3749 if (!*args[1]) {
3750 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3751 file, linenum, args[0]);
3752 err_code |= ERR_ALERT | ERR_FATAL;
3753 goto out;
3754 }
3755 cur_arg = 1;
3756 while (*args[cur_arg]) {
3757 struct spoe_var_placeholder *vph;
3758
3759 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3760 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3761 err_code |= ERR_ALERT | ERR_ABORT;
3762 goto out;
3763 }
3764 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003765 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003766 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3767 err_code |= ERR_ALERT | ERR_ABORT;
3768 goto out;
3769 }
Willy Tarreau2b718102021-04-21 07:32:39 +02003770 LIST_APPEND(&curvars, &vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003771 cur_arg++;
3772 }
3773 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003774 else if (strcmp(args[0], "log") == 0) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003775 char *errmsg = NULL;
3776
Emeric Brun9533a702021-04-02 10:13:43 +02003777 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), file, linenum, &errmsg)) {
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003778 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3779 err_code |= ERR_ALERT | ERR_FATAL;
3780 goto out;
3781 }
3782 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003783 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003784 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3785 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003786 err_code |= ERR_ALERT | ERR_FATAL;
3787 goto out;
3788 }
3789 out:
3790 return err_code;
3791}
Christopher Faulet11610f32017-09-21 10:23:10 +02003792static int
3793cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3794{
3795 struct spoe_group *grp;
3796 const char *err;
3797 int err_code = 0;
3798
3799 if ((cfg_scope == NULL && curengine != NULL) ||
3800 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003801 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Faulet11610f32017-09-21 10:23:10 +02003802 goto out;
3803
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003804 if (strcmp(args[0], "spoe-group") == 0) { /* new spoe-group section */
Christopher Faulet11610f32017-09-21 10:23:10 +02003805 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003806 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3807 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003808 err_code |= ERR_ALERT | ERR_ABORT;
3809 goto out;
3810 }
3811 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3812 err_code |= ERR_ABORT;
3813 goto out;
3814 }
3815
3816 err = invalid_char(args[1]);
3817 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003818 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3819 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003820 err_code |= ERR_ALERT | ERR_ABORT;
3821 goto out;
3822 }
3823
3824 list_for_each_entry(grp, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003825 if (strcmp(grp->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003826 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3827 " name as another one declared at %s:%d.\n",
3828 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003829 err_code |= ERR_ALERT | ERR_FATAL;
3830 goto out;
3831 }
3832 }
3833
3834 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003835 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003836 err_code |= ERR_ALERT | ERR_ABORT;
3837 goto out;
3838 }
3839
3840 curgrp->id = strdup(args[1]);
3841 curgrp->conf.file = strdup(file);
3842 curgrp->conf.line = linenum;
3843 LIST_INIT(&curgrp->phs);
3844 LIST_INIT(&curgrp->messages);
Willy Tarreau2b718102021-04-21 07:32:39 +02003845 LIST_APPEND(&curgrps, &curgrp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003846 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003847 else if (strcmp(args[0], "messages") == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003848 int cur_arg = 1;
3849 while (*args[cur_arg]) {
3850 struct spoe_placeholder *ph = NULL;
3851
3852 list_for_each_entry(ph, &curgrp->phs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003853 if (strcmp(ph->id, args[cur_arg]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003854 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3855 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003856 err_code |= ERR_ALERT | ERR_FATAL;
3857 goto out;
3858 }
3859 }
3860
3861 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003862 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003863 err_code |= ERR_ALERT | ERR_ABORT;
3864 goto out;
3865 }
3866 ph->id = strdup(args[cur_arg]);
Willy Tarreau2b718102021-04-21 07:32:39 +02003867 LIST_APPEND(&curgrp->phs, &ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02003868 cur_arg++;
3869 }
3870 }
3871 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003872 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3873 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003874 err_code |= ERR_ALERT | ERR_FATAL;
3875 goto out;
3876 }
3877 out:
3878 return err_code;
3879}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003880
3881static int
3882cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3883{
3884 struct spoe_message *msg;
3885 struct spoe_arg *arg;
3886 const char *err;
3887 char *errmsg = NULL;
3888 int err_code = 0;
3889
3890 if ((cfg_scope == NULL && curengine != NULL) ||
3891 (cfg_scope != NULL && curengine == NULL) ||
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003892 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope) != 0))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003893 goto out;
3894
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003895 if (strcmp(args[0], "spoe-message") == 0) { /* new spoe-message section */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003896 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003897 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3898 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003899 err_code |= ERR_ALERT | ERR_ABORT;
3900 goto out;
3901 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003902 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3903 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003904 goto out;
3905 }
3906
3907 err = invalid_char(args[1]);
3908 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003909 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3910 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003911 err_code |= ERR_ALERT | ERR_ABORT;
3912 goto out;
3913 }
3914
3915 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003916 if (strcmp(msg->id, args[1]) == 0) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003917 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3918 " name as another one declared at %s:%d.\n",
3919 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003920 err_code |= ERR_ALERT | ERR_FATAL;
3921 goto out;
3922 }
3923 }
3924
3925 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003926 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003927 err_code |= ERR_ALERT | ERR_ABORT;
3928 goto out;
3929 }
3930
3931 curmsg->id = strdup(args[1]);
3932 curmsg->id_len = strlen(curmsg->id);
3933 curmsg->event = SPOE_EV_NONE;
3934 curmsg->conf.file = strdup(file);
3935 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003936 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003937 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003938 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003939 LIST_INIT(&curmsg->by_evt);
3940 LIST_INIT(&curmsg->by_grp);
Willy Tarreau2b718102021-04-21 07:32:39 +02003941 LIST_APPEND(&curmsgs, &curmsg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003942 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003943 else if (strcmp(args[0], "args") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003944 int cur_arg = 1;
3945
3946 curproxy->conf.args.ctx = ARGC_SPOE;
3947 curproxy->conf.args.file = file;
3948 curproxy->conf.args.line = linenum;
3949 while (*args[cur_arg]) {
3950 char *delim = strchr(args[cur_arg], '=');
3951 int idx = 0;
3952
3953 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003954 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003955 err_code |= ERR_ALERT | ERR_ABORT;
3956 goto out;
3957 }
3958
3959 if (!delim) {
3960 arg->name = NULL;
3961 arg->name_len = 0;
3962 delim = args[cur_arg];
3963 }
3964 else {
3965 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3966 arg->name_len = delim - args[cur_arg];
3967 delim++;
3968 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003969 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3970 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003971 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003972 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003973 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003974 err_code |= ERR_ALERT | ERR_FATAL;
3975 free(arg->name);
3976 free(arg);
3977 goto out;
3978 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003979 curmsg->nargs++;
Willy Tarreau2b718102021-04-21 07:32:39 +02003980 LIST_APPEND(&curmsg->args, &arg->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003981 cur_arg++;
3982 }
3983 curproxy->conf.args.file = NULL;
3984 curproxy->conf.args.line = 0;
3985 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01003986 else if (strcmp(args[0], "acl") == 0) {
Christopher Faulet57583e42017-09-04 15:41:09 +02003987 err = invalid_char(args[1]);
3988 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003989 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3990 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003991 err_code |= ERR_ALERT | ERR_FATAL;
3992 goto out;
3993 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003994 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01003995 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003996 "logical disjunction within a condition.\n",
3997 file, linenum, args[1]);
3998 err_code |= ERR_ALERT | ERR_FATAL;
3999 goto out;
4000 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004001 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004002 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4003 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004004 err_code |= ERR_ALERT | ERR_FATAL;
4005 goto out;
4006 }
4007 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004008 else if (strcmp(args[0], "event") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004009 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004010 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004011 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004012 goto out;
4013 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004014 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4015 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004016
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004017 if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004018 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004019 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004020 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4021
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004022 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004023 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004024 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004025 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004026 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004027 curmsg->event = SPOE_EV_ON_TCP_RSP;
4028
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004029 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004030 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004031 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004032 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004033 else if (strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]) == 0)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004034 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4035 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004036 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004037 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004038 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004039 goto out;
4040 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004041
4042 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4043 struct acl_cond *cond;
4044
4045 cond = build_acl_cond(file, linenum, &curmsg->acls,
4046 curproxy, (const char **)args+2,
4047 &errmsg);
4048 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004049 ha_alert("parsing [%s:%d] : error detected while "
4050 "parsing an 'event %s' condition : %s.\n",
4051 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004052 err_code |= ERR_ALERT | ERR_FATAL;
4053 goto out;
4054 }
4055 curmsg->cond = cond;
4056 }
4057 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004058 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4059 "or 'unless' followed by a condition but found '%s'.\n",
4060 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004061 err_code |= ERR_ALERT | ERR_FATAL;
4062 goto out;
4063 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004064 }
4065 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004066 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4067 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004068 err_code |= ERR_ALERT | ERR_FATAL;
4069 goto out;
4070 }
4071 out:
4072 free(errmsg);
4073 return err_code;
4074}
4075
4076/* Return -1 on error, else 0 */
4077static int
4078parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4079 struct flt_conf *fconf, char **err, void *private)
4080{
4081 struct list backup_sections;
4082 struct spoe_config *conf;
4083 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004084 struct spoe_group *grp, *grpback;
4085 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004086 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004087 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004088 char *file = NULL, *engine = NULL;
4089 int ret, pos = *cur_arg + 1;
4090
Christopher Faulet84c844e2018-03-23 14:37:14 +01004091 LIST_INIT(&curmsgs);
4092 LIST_INIT(&curgrps);
4093 LIST_INIT(&curmphs);
4094 LIST_INIT(&curgphs);
4095 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004096 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004097 curpxopts = 0;
4098 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004099
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004100 conf = calloc(1, sizeof(*conf));
4101 if (conf == NULL) {
4102 memprintf(err, "%s: out of memory", args[*cur_arg]);
4103 goto error;
4104 }
4105 conf->proxy = px;
4106
4107 while (*args[pos]) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004108 if (strcmp(args[pos], "config") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004109 if (!*args[pos+1]) {
4110 memprintf(err, "'%s' : '%s' option without value",
4111 args[*cur_arg], args[pos]);
4112 goto error;
4113 }
4114 file = args[pos+1];
4115 pos += 2;
4116 }
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004117 else if (strcmp(args[pos], "engine") == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004118 if (!*args[pos+1]) {
4119 memprintf(err, "'%s' : '%s' option without value",
4120 args[*cur_arg], args[pos]);
4121 goto error;
4122 }
4123 engine = args[pos+1];
4124 pos += 2;
4125 }
4126 else {
4127 memprintf(err, "unknown keyword '%s'", args[pos]);
4128 goto error;
4129 }
4130 }
4131 if (file == NULL) {
4132 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4133 goto error;
4134 }
4135
4136 /* backup sections and register SPOE sections */
4137 LIST_INIT(&backup_sections);
4138 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004139 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4140 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004141 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004142
4143 /* Parse SPOE filter configuration file */
4144 curengine = engine;
4145 curproxy = px;
4146 curagent = NULL;
4147 curmsg = NULL;
4148 ret = readcfgfile(file);
4149 curproxy = NULL;
4150
4151 /* unregister SPOE sections and restore previous sections */
4152 cfg_unregister_sections();
4153 cfg_restore_sections(&backup_sections);
4154
4155 if (ret == -1) {
4156 memprintf(err, "Could not open configuration file %s : %s",
4157 file, strerror(errno));
4158 goto error;
4159 }
4160 if (ret & (ERR_ABORT|ERR_FATAL)) {
4161 memprintf(err, "Error(s) found in configuration file %s", file);
4162 goto error;
4163 }
4164
4165 /* Check SPOE agent */
4166 if (curagent == NULL) {
4167 memprintf(err, "No SPOE agent found in file %s", file);
4168 goto error;
4169 }
4170 if (curagent->b.name == NULL) {
4171 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4172 curagent->id, curagent->conf.file, curagent->conf.line);
4173 goto error;
4174 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004175 if (curagent->timeout.hello == TICK_ETERNITY ||
4176 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004177 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004178 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4179 " | While not properly invalid, you will certainly encounter various problems\n"
4180 " | with such a configuration. To fix this, please ensure that all following\n"
4181 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4182 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004183 }
4184 if (curagent->var_pfx == NULL) {
4185 char *tmp = curagent->id;
4186
4187 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004188 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004189 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4190 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4191 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4192 goto error;
4193 }
4194 tmp++;
4195 }
4196 curagent->var_pfx = strdup(curagent->id);
4197 }
4198
Christopher Fauletb7426d12018-03-21 14:12:17 +01004199 if (curagent->var_on_error) {
4200 struct arg arg;
4201
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004202 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004203 curagent->var_pfx, curagent->var_on_error);
4204
4205 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004206 arg.data.str.area = trash.area;
4207 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004208 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004209 if (!vars_check_arg(&arg, err)) {
4210 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4211 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4212 goto error;
4213 }
4214 }
4215
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004216 if (curagent->var_t_process) {
4217 struct arg arg;
4218
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004219 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004220 curagent->var_pfx, curagent->var_t_process);
4221
4222 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004223 arg.data.str.area = trash.area;
4224 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004225 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004226 if (!vars_check_arg(&arg, err)) {
4227 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4228 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4229 goto error;
4230 }
4231 }
4232
4233 if (curagent->var_t_total) {
4234 struct arg arg;
4235
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004236 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004237 curagent->var_pfx, curagent->var_t_total);
4238
4239 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004240 arg.data.str.area = trash.area;
4241 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004242 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004243 if (!vars_check_arg(&arg, err)) {
4244 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4245 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4246 goto error;
4247 }
4248 }
4249
Christopher Faulet11610f32017-09-21 10:23:10 +02004250 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004251 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4252 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004253 goto finish;
4254 }
4255
Christopher Faulet11610f32017-09-21 10:23:10 +02004256 /* Replace placeholders by the corresponding messages for the SPOE
4257 * agent */
4258 list_for_each_entry(ph, &curmphs, list) {
4259 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004260 struct spoe_arg *arg;
4261 unsigned int where;
4262
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004263 if (strcmp(msg->id, ph->id) == 0) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004264 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4265 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4266 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4267 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4268 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4269 }
4270 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4271 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4272 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004273 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4274 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004275 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004276 }
4277 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004278 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4279 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004280 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004281 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004282
4283 where = 0;
4284 switch (msg->event) {
4285 case SPOE_EV_ON_CLIENT_SESS:
4286 where |= SMP_VAL_FE_CON_ACC;
4287 break;
4288
4289 case SPOE_EV_ON_TCP_REQ_FE:
4290 where |= SMP_VAL_FE_REQ_CNT;
4291 break;
4292
4293 case SPOE_EV_ON_HTTP_REQ_FE:
4294 where |= SMP_VAL_FE_HRQ_HDR;
4295 break;
4296
4297 case SPOE_EV_ON_TCP_REQ_BE:
4298 if (px->cap & PR_CAP_FE)
4299 where |= SMP_VAL_FE_REQ_CNT;
4300 if (px->cap & PR_CAP_BE)
4301 where |= SMP_VAL_BE_REQ_CNT;
4302 break;
4303
4304 case SPOE_EV_ON_HTTP_REQ_BE:
4305 if (px->cap & PR_CAP_FE)
4306 where |= SMP_VAL_FE_HRQ_HDR;
4307 if (px->cap & PR_CAP_BE)
4308 where |= SMP_VAL_BE_HRQ_HDR;
4309 break;
4310
4311 case SPOE_EV_ON_SERVER_SESS:
4312 where |= SMP_VAL_BE_SRV_CON;
4313 break;
4314
4315 case SPOE_EV_ON_TCP_RSP:
4316 if (px->cap & PR_CAP_FE)
4317 where |= SMP_VAL_FE_RES_CNT;
4318 if (px->cap & PR_CAP_BE)
4319 where |= SMP_VAL_BE_RES_CNT;
4320 break;
4321
4322 case SPOE_EV_ON_HTTP_RSP:
4323 if (px->cap & PR_CAP_FE)
4324 where |= SMP_VAL_FE_HRS_HDR;
4325 if (px->cap & PR_CAP_BE)
4326 where |= SMP_VAL_BE_HRS_HDR;
4327 break;
4328
4329 default:
4330 break;
4331 }
4332
4333 list_for_each_entry(arg, &msg->args, list) {
4334 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004335 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004336 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004337 "none of which is available here ('%s')",
4338 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004339 sample_ckp_names(arg->expr->fetch->use),
4340 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004341 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004342 }
4343 }
4344
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004345 msg->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004346 LIST_APPEND(&curagent->events[msg->event], &msg->by_evt);
Christopher Faulet11610f32017-09-21 10:23:10 +02004347 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004348 }
4349 }
4350 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004351 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004352 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004353 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004354 continue;
4355 }
4356
Christopher Faulet11610f32017-09-21 10:23:10 +02004357 /* Replace placeholders by the corresponding groups for the SPOE
4358 * agent */
4359 list_for_each_entry(ph, &curgphs, list) {
4360 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004361 if (strcmp(grp->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004362 grp->agent = curagent;
Willy Tarreau2b718102021-04-21 07:32:39 +02004363 LIST_DELETE(&grp->list);
4364 LIST_APPEND(&curagent->groups, &grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004365 goto next_aph;
4366 }
4367 }
4368 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4369 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4370 goto error;
4371 next_aph:
4372 continue;
4373 }
4374
4375 /* Replace placeholders by the corresponding message for each SPOE
4376 * group of the SPOE agent */
4377 list_for_each_entry(grp, &curagent->groups, list) {
4378 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4379 list_for_each_entry(msg, &curmsgs, list) {
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004380 if (strcmp(msg->id, ph->id) == 0) {
Christopher Faulet11610f32017-09-21 10:23:10 +02004381 if (msg->group != NULL) {
4382 memprintf(err, "SPOE message '%s' already belongs to "
4383 "the SPOE group '%s' declare at %s:%d",
4384 msg->id, msg->group->id,
4385 msg->group->conf.file,
4386 msg->group->conf.line);
4387 goto error;
4388 }
4389
4390 /* Scope for arguments are not checked for now. We will check
4391 * them only if a rule use the corresponding SPOE group. */
4392 msg->agent = curagent;
4393 msg->group = grp;
Willy Tarreau2b718102021-04-21 07:32:39 +02004394 LIST_DELETE(&ph->list);
4395 LIST_APPEND(&grp->messages, &msg->by_grp);
Christopher Faulet11610f32017-09-21 10:23:10 +02004396 goto next_mph_grp;
4397 }
4398 }
4399 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4400 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4401 goto error;
4402 next_mph_grp:
4403 continue;
4404 }
4405 }
4406
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004407 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004408 /* move curmsgs to the agent message list */
4409 curmsgs.n->p = &curagent->messages;
4410 curmsgs.p->n = &curagent->messages;
4411 curagent->messages = curmsgs;
4412 LIST_INIT(&curmsgs);
4413
Christopher Faulet7ee86672017-09-19 11:08:28 +02004414 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004415 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004416
4417 /* Start agent's proxy initialization here. It will be finished during
4418 * the filter init. */
4419 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4420 init_new_proxy(&conf->agent_fe);
4421 conf->agent_fe.id = conf->agent->id;
4422 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004423 conf->agent_fe.options |= curpxopts;
4424 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004425
4426 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004427 LIST_DELETE(&logsrv->list);
4428 LIST_APPEND(&conf->agent_fe.logsrvs, &logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004429 }
4430
Christopher Faulet11610f32017-09-21 10:23:10 +02004431 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004432 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004433 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004434 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004435 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004436 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004437 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004438 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004439 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4440 struct arg arg;
4441
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004442 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004443 curagent->var_pfx, vph->name);
4444
4445 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004446 arg.data.str.area = trash.area;
4447 arg.data.str.data = trash.data;
William Dauchyafb93682021-01-05 11:14:58 +01004448 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_arg() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004449 if (!vars_check_arg(&arg, err)) {
4450 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4451 curagent->id, curagent->var_pfx, vph->name, *err);
4452 goto error;
4453 }
4454
Willy Tarreau2b718102021-04-21 07:32:39 +02004455 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004456 free(vph->name);
4457 free(vph);
4458 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004459 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004460 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004461 spoe_release_group(grp);
4462 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004463 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004464 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004465 fconf->ops = &spoe_ops;
4466 fconf->conf = conf;
4467 return 0;
4468
4469 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004470 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004471 list_for_each_entry_safe(ph, phback, &curmphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004472 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004473 spoe_release_placeholder(ph);
4474 }
4475 list_for_each_entry_safe(ph, phback, &curgphs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004476 LIST_DELETE(&ph->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004477 spoe_release_placeholder(ph);
4478 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004479 list_for_each_entry_safe(vph, vphback, &curvars, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004480 LIST_DELETE(&vph->list);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004481 free(vph->name);
4482 free(vph);
4483 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004484 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004485 LIST_DELETE(&grp->list);
Christopher Faulet11610f32017-09-21 10:23:10 +02004486 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004487 }
4488 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004489 LIST_DELETE(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004490 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004491 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004492 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
Willy Tarreau2b718102021-04-21 07:32:39 +02004493 LIST_DELETE(&logsrv->list);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004494 free(logsrv);
4495 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004496 free(conf);
4497 return -1;
4498}
4499
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004500/* Send message of a SPOE group. This is the action_ptr callback of a rule
4501 * associated to a "send-spoe-group" action.
4502 *
Christopher Faulet13403762019-12-13 09:01:57 +01004503 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4504 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004505static enum act_return
4506spoe_send_group(struct act_rule *rule, struct proxy *px,
4507 struct session *sess, struct stream *s, int flags)
4508{
4509 struct filter *filter;
4510 struct spoe_agent *agent = NULL;
4511 struct spoe_group *group = NULL;
4512 struct spoe_context *ctx = NULL;
4513 int ret, dir;
4514
4515 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4516 if (filter->config == rule->arg.act.p[0]) {
4517 agent = rule->arg.act.p[2];
4518 group = rule->arg.act.p[3];
4519 ctx = filter->ctx;
4520 break;
4521 }
4522 }
4523 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004524 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004525 if (ctx->state == SPOE_CTX_ST_NONE)
4526 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004527
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004528 switch (rule->from) {
4529 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4530 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4531 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4532 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4533 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4534 default:
4535 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4536 " - internal error while execute spoe-send-group\n",
4537 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4538 __FUNCTION__, s);
4539 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4540 agent->id);
4541 return ACT_RET_CONT;
4542 }
4543
4544 ret = spoe_process_group(s, ctx, group, dir);
4545 if (ret == 1)
4546 return ACT_RET_CONT;
4547 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004548 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004549 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4550 " - failed to process group '%s': interrupted by caller\n",
4551 (int)now.tv_sec, (int)now.tv_usec,
4552 agent->id, __FUNCTION__, s, group->id);
4553 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004554 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004555 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004556 return ACT_RET_CONT;
4557 }
4558 return ACT_RET_YIELD;
4559 }
4560 else
Christopher Faulet13403762019-12-13 09:01:57 +01004561 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004562}
4563
4564/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4565 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4566 * action should be:
4567 *
4568 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4569 *
4570 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4571 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4572 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4573 * group.
4574 *
4575 * The function returns 1 in success case, otherwise, it returns 0 and err is
4576 * filled.
4577 */
4578static int
4579check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4580{
4581 struct flt_conf *fconf;
4582 struct spoe_config *conf;
4583 struct spoe_agent *agent = NULL;
4584 struct spoe_group *group;
4585 struct spoe_message *msg;
4586 char *engine_id = rule->arg.act.p[0];
4587 char *group_id = rule->arg.act.p[1];
4588 unsigned int where = 0;
4589
4590 switch (rule->from) {
4591 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4592 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4593 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4594 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4595 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4596 default:
4597 memprintf(err,
4598 "internal error, unexpected rule->from=%d, please report this bug!",
4599 rule->from);
4600 goto error;
4601 }
4602
4603 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4604 * <px> */
4605 list_for_each_entry(fconf, &px->filter_configs, list) {
4606 conf = fconf->conf;
4607
4608 /* This is not an SPOE filter */
4609 if (fconf->id != spoe_filter_id)
4610 continue;
4611
4612 /* This is the good engine */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004613 if (strcmp(conf->id, engine_id) == 0) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004614 agent = conf->agent;
4615 break;
4616 }
4617 }
4618 if (agent == NULL) {
4619 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4620 engine_id, group_id);
4621 goto error;
4622 }
4623
4624 /* Try to find the right group */
4625 list_for_each_entry(group, &agent->groups, list) {
4626 /* This is the good group */
Tim Duesterhuse5ff1412021-01-02 22:31:53 +01004627 if (strcmp(group->id, group_id) == 0)
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004628 break;
4629 }
4630 if (&group->list == &agent->groups) {
4631 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4632 group_id, engine_id);
4633 goto error;
4634 }
4635
4636 /* Ok, we found the group, we need to check messages and their
4637 * arguments */
4638 list_for_each_entry(msg, &group->messages, by_grp) {
4639 struct spoe_arg *arg;
4640
4641 list_for_each_entry(arg, &msg->args, list) {
4642 if (!(arg->expr->fetch->val & where)) {
4643 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4644 "some args extract information from '%s',"
4645 "none of which is available here ('%s')",
4646 msg->id, group->id, msg->conf.file, msg->conf.line,
4647 sample_ckp_names(arg->expr->fetch->use),
4648 sample_ckp_names(where));
4649 goto error;
4650 }
4651 }
4652 }
4653
4654 free(engine_id);
4655 free(group_id);
4656 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4657 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4658 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4659 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4660 return 1;
4661
4662 error:
4663 free(engine_id);
4664 free(group_id);
4665 return 0;
4666}
4667
4668/* Parse 'send-spoe-group' action following the format:
4669 *
4670 * ... send-spoe-group <engine-id> <group-id>
4671 *
4672 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4673 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4674 * ids are saved and used later, when the rule will be checked.
4675 */
4676static enum act_parse_ret
4677parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4678 struct act_rule *rule, char **err)
4679{
4680 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4681 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4682 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4683 return ACT_RET_PRS_ERR;
4684 }
4685 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4686 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4687
4688 (*orig_arg) += 2;
4689
4690 rule->action = ACT_CUSTOM;
4691 rule->action_ptr = spoe_send_group;
4692 rule->check_ptr = check_send_spoe_group;
4693 return ACT_RET_PRS_OK;
4694}
4695
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004696
4697/* Declare the filter parser for "spoe" keyword */
4698static struct flt_kw_list flt_kws = { "SPOE", { }, {
4699 { "spoe", parse_spoe_flt, NULL },
4700 { NULL, NULL, NULL },
4701 }
4702};
4703
Willy Tarreau0108d902018-11-25 19:14:37 +01004704INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4705
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004706/* Delcate the action parser for "spoe-action" keyword */
4707static struct action_kw_list tcp_req_action_kws = { { }, {
4708 { "send-spoe-group", parse_send_spoe_group },
4709 { /* END */ },
4710 }
4711};
Willy Tarreau0108d902018-11-25 19:14:37 +01004712
4713INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4714
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004715static struct action_kw_list tcp_res_action_kws = { { }, {
4716 { "send-spoe-group", parse_send_spoe_group },
4717 { /* END */ },
4718 }
4719};
Willy Tarreau0108d902018-11-25 19:14:37 +01004720
4721INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4722
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004723static struct action_kw_list http_req_action_kws = { { }, {
4724 { "send-spoe-group", parse_send_spoe_group },
4725 { /* END */ },
4726 }
4727};
Willy Tarreau0108d902018-11-25 19:14:37 +01004728
4729INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4730
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004731static struct action_kw_list http_res_action_kws = { { }, {
4732 { "send-spoe-group", parse_send_spoe_group },
4733 { /* END */ },
4734 }
4735};
4736
Willy Tarreau0108d902018-11-25 19:14:37 +01004737INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);