blob: 9db2baa15dcf54ea72f930fac0d7468dbadc63b4 [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
Willy Tarreau0108d902018-11-25 19:14:37 +010019#include <common/hathreads.h>
20#include <common/initcall.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020021#include <common/memory.h>
22#include <common/time.h>
23
24#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020025#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010026#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020027
Christopher Faulet57583e42017-09-04 15:41:09 +020028#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020029#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020030#include <proto/arg.h>
31#include <proto/backend.h>
32#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010033#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020034#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020035#include <proto/http_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020036#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020037#include <proto/http_ana.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020038#include <proto/proxy.h>
39#include <proto/sample.h>
40#include <proto/session.h>
41#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010042#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020043#include <proto/stream.h>
44#include <proto/stream_interface.h>
45#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020046#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020047#include <proto/vars.h>
48
49#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
50#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010051#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020052#else
53#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010054#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020055#endif
56
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010057/* Reserved 4 bytes to the frame size. So a frame and its size can be written
58 * together in a buffer */
59#define MAX_FRAME_SIZE global.tune.bufsize - 4
60
61/* The minimum size for a frame */
62#define MIN_FRAME_SIZE 256
63
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010064/* Reserved for the metadata and the frame type.
65 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010066#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020067
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010068/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010069#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
70
Christopher Faulet3b386a32017-02-23 10:17:15 +010071/* SPOE filter id. Used to identify SPOE filters */
72const char *spoe_filter_id = "SPOE filter";
73
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020074/* Set if the handle on SIGUSR1 is registered */
75static int sighandler_registered = 0;
76
77/* proxy used during the parsing */
78struct proxy *curproxy = NULL;
79
80/* The name of the SPOE engine, used during the parsing */
81char *curengine = NULL;
82
83/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020084/* SPOE agent/group/message used during the parsing */
85struct spoe_agent *curagent = NULL;
86struct spoe_group *curgrp = NULL;
87struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
89/* list of SPOE messages and placeholders used during the parsing */
90struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020091struct list curgrps;
92struct list curmphs;
93struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010094struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020095
Christopher Faulet7250b8f2018-03-26 17:19:01 +020096/* list of log servers used during the parsing */
97struct list curlogsrvs;
98
Christopher Faulet0e0f0852018-03-26 17:20:36 +020099/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
100int curpxopts;
101int curpxopts2;
102
Christopher Faulet42bfa462017-01-04 14:14:19 +0100103/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100104DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
105DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200107struct flt_ops spoe_ops;
108
Christopher Faulet8ef75252017-02-20 22:56:03 +0100109static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200110static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
111static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200112
113/********************************************************************
114 * helper functions/globals
115 ********************************************************************/
116static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200117spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118{
Christopher Faulet11610f32017-09-21 10:23:10 +0200119 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200121 free(ph->id);
122 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123}
124
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200125static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100126spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127{
Christopher Faulet57583e42017-09-04 15:41:09 +0200128 struct spoe_arg *arg, *argback;
129 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200130
131 if (!msg)
132 return;
133 free(msg->id);
134 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200135 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200136 release_sample_expr(arg->expr);
137 free(arg->name);
138 LIST_DEL(&arg->list);
139 free(arg);
140 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200141 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
142 LIST_DEL(&acl->list);
143 prune_acl(acl);
144 free(acl);
145 }
146 if (msg->cond) {
147 prune_acl_cond(msg->cond);
148 free(msg->cond);
149 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200150 free(msg);
151}
152
153static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200154spoe_release_group(struct spoe_group *grp)
155{
156 if (!grp)
157 return;
158 free(grp->id);
159 free(grp->conf.file);
160 free(grp);
161}
162
163static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100164spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200165{
Christopher Faulet11610f32017-09-21 10:23:10 +0200166 struct spoe_message *msg, *msgback;
167 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200168 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200169
170 if (!agent)
171 return;
172 free(agent->id);
173 free(agent->conf.file);
174 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100175 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100176 free(agent->var_t_process);
177 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200178 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
179 LIST_DEL(&msg->list);
180 spoe_release_message(msg);
181 }
182 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
183 LIST_DEL(&grp->list);
184 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200185 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100186 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200187 for (i = 0; i < global.nbthread; ++i) {
188 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100189 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200190 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100191 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200192 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200193 free(agent);
194}
195
196static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100197 [SPOE_FRM_ERR_NONE] = "normal",
198 [SPOE_FRM_ERR_IO] = "I/O error",
199 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
200 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
201 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
202 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
203 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
204 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
205 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
206 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
207 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
208 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100209 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100210 [SPOE_FRM_ERR_RES] = "resource allocation error",
211 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200212};
213
214static const char *spoe_event_str[SPOE_EV_EVENTS] = {
215 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
216 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
217 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
218 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
219 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
220
221 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
222 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
223 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
224};
225
226
227#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
228
229static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100230 [SPOE_CTX_ST_NONE] = "NONE",
231 [SPOE_CTX_ST_READY] = "READY",
232 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
233 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
234 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
235 [SPOE_CTX_ST_DONE] = "DONE",
236 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200237};
238
239static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100240 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
241 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
242 [SPOE_APPCTX_ST_IDLE] = "IDLE",
243 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
244 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
245 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
246 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
247 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
248 [SPOE_APPCTX_ST_EXIT] = "EXIT",
249 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200250};
251
252#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100253
Christopher Faulet8ef75252017-02-20 22:56:03 +0100254/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500255 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100256 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100257static char *
258generate_pseudo_uuid()
259{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100260 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800261 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100262}
263
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100264
265static inline void
266spoe_update_stat_time(struct timeval *tv, long *t)
267{
268 if (*t == -1)
269 *t = tv_ms_elapsed(tv, &now);
270 else
271 *t += tv_ms_elapsed(tv, &now);
272 tv_zero(tv);
273}
274
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200275/********************************************************************
276 * Functions that encode/decode SPOE frames
277 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200278/* Helper to get static string length, excluding the terminating null byte */
279#define SLEN(str) (sizeof(str)-1)
280
281/* Predefined key used in HELLO/DISCONNECT frames */
282#define SUPPORTED_VERSIONS_KEY "supported-versions"
283#define VERSION_KEY "version"
284#define MAX_FRAME_SIZE_KEY "max-frame-size"
285#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100286#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100287#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200288#define STATUS_CODE_KEY "status-code"
289#define MSG_KEY "message"
290
291struct spoe_version {
292 char *str;
293 int min;
294 int max;
295};
296
297/* All supported versions */
298static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200299 /* 1.0 is now unsupported because of a bug about frame's flags*/
300 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200301 {NULL, 0, 0}
302};
303
304/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200305#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200306
Christopher Faulet8ef75252017-02-20 22:56:03 +0100307/* Convert a string to a SPOE version value. The string must follow the format
308 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
309 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200310static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100311spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200312{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100313 const char *p, *end;
314 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200315
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 p = str;
317 end = str+len;
318 maj = min = 0;
319 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320
Christopher Faulet8ef75252017-02-20 22:56:03 +0100321 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100322 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100323 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200324
Christopher Faulet8ef75252017-02-20 22:56:03 +0100325 /* parse Major number, until the '.' */
326 while (*p != '.') {
327 if (p >= end || *p < '0' || *p > '9')
328 goto out;
329 maj *= 10;
330 maj += (*p - '0');
331 p++;
332 }
333
334 /* check Major version */
335 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200336 goto out;
337
Christopher Faulet8ef75252017-02-20 22:56:03 +0100338 p++; /* skip the '.' */
339 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
340 goto out;
341
342 /* Parse Minor number */
343 while (p < end) {
344 if (*p < '0' || *p > '9')
345 break;
346 min *= 10;
347 min += (*p - '0');
348 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200349 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100350
351 /* check Minor number */
352 if (min > 999)
353 goto out;
354
355 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100356 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100357 p++;
358 if (p != end)
359 goto out;
360
361 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200362 out:
363 return vsn;
364}
365
Christopher Faulet8ef75252017-02-20 22:56:03 +0100366/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800367 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100368 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200369static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100370spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200371{
Willy Tarreau83061a82018-07-13 11:56:34 +0200372 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100373 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100374 char *p, *end;
375 unsigned int flags = SPOE_FRM_FL_FIN;
376 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200377
Christopher Faulet8ef75252017-02-20 22:56:03 +0100378 p = frame;
379 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200380
Christopher Faulet8ef75252017-02-20 22:56:03 +0100381 /* Set Frame type */
382 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200383
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100384 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200385 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100386 memcpy(p, (char *)&flags, 4);
387 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388
389 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100390 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200391
392 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
393 * and "capabilities" */
394
395 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100396 sz = SLEN(SUPPORTED_VERSIONS_KEY);
397 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
398 goto too_big;
399
400 *p++ = SPOE_DATA_T_STR;
401 sz = SLEN(SUPPORTED_VERSIONS_VAL);
402 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
403 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200404
405 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100406 sz = SLEN(MAX_FRAME_SIZE_KEY);
407 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
408 goto too_big;
409
410 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200411 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200413
414 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 sz = SLEN(CAPABILITIES_KEY);
416 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
417 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200418
Christopher Faulet8ef75252017-02-20 22:56:03 +0100419 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100420 chk = get_trash_chunk();
421 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200422 memcpy(chk->area, "pipelining", 10);
423 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100424 }
425 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200426 if (chk->data) chk->area[chk->data++] = ',';
427 memcpy(chk->area+chk->data, "async", 5);
428 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100429 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100430 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200431 if (chk->data) chk->area[chk->data++] = ',';
432 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100433 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100434 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200435 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100436 goto too_big;
437
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500438 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200439 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100440 sz = SLEN(ENGINE_ID_KEY);
441 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
442 goto too_big;
443
444 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200445 sz = strlen(agent->rt[tid].engine_id);
446 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100447 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100448 }
449
Christopher Faulet8ef75252017-02-20 22:56:03 +0100450 return (p - frame);
451
452 too_big:
453 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
454 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200455}
456
Christopher Faulet8ef75252017-02-20 22:56:03 +0100457/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
458 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
459 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200460static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200462{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100463 const char *reason;
464 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100465 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100466 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467
Christopher Faulet8ef75252017-02-20 22:56:03 +0100468 p = frame;
469 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200470
Christopher Faulet8ef75252017-02-20 22:56:03 +0100471 /* Set Frame type */
472 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200473
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100474 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200475 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476 memcpy(p, (char *)&flags, 4);
477 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200478
479 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480 *p++ = 0; *p++ = 0;
481
482 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
483 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200484
485 /* There are 2 mandatory items: "status-code" and "message" */
486
487 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100488 sz = SLEN(STATUS_CODE_KEY);
489 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
490 goto too_big;
491
492 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200493 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200495
496 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100497 sz = SLEN(MSG_KEY);
498 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
499 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200500
Christopher Faulet8ef75252017-02-20 22:56:03 +0100501 /*Get the message corresponding to the status code */
502 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
503
504 *p++ = SPOE_DATA_T_STR;
505 sz = strlen(reason);
506 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
507 goto too_big;
508
509 return (p - frame);
510
511 too_big:
512 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
513 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200514}
515
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
517 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
518 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200519static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100520spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100521 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200522{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100523 char *p, *end;
524 unsigned int stream_id, frame_id;
525 unsigned int flags = SPOE_FRM_FL_FIN;
526 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200527
Christopher Faulet8ef75252017-02-20 22:56:03 +0100528 p = frame;
529 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200530
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100531 stream_id = ctx->stream_id;
532 frame_id = ctx->frame_id;
533
534 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
535 /* The fragmentation is not supported by the applet */
536 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
537 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
538 return -1;
539 }
540 flags = ctx->frag_ctx.flags;
541 }
542
543 /* Set Frame type */
544 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
545
546 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200547 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 memcpy(p, (char *)&flags, 4);
549 p += 4;
550
551 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200552 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100553 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200554 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100555 goto too_big;
556
557 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200558 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100559 if (p + sz >= end)
560 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200561 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100562 p += sz;
563
564 return (p - frame);
565
566 too_big:
567 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
568 return 0;
569}
570
571/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
572 * returns the number of encoded bytes in the frame on success, 0 if an encoding
573 * error occurred and -1 if a fatal error occurred. */
574static int
575spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
576 char *frame, size_t size)
577{
578 char *p, *end;
579 unsigned int stream_id, frame_id;
580 unsigned int flags;
581 size_t sz;
582
583 p = frame;
584 end = frame+size;
585
Christopher Faulet8ef75252017-02-20 22:56:03 +0100586 /* <ctx> is null when the stream has aborted the processing of a
587 * fragmented frame. In this case, we must notify the corresponding
588 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100589 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100590 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100591 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
592 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
593 }
594 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100595 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 stream_id = ctx->stream_id;
597 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100598 }
599
Christopher Faulet8ef75252017-02-20 22:56:03 +0100600 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100601 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100602
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100603 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200604 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 memcpy(p, (char *)&flags, 4);
606 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200607
608 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200609 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200611 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200613
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100614 if (ctx == NULL)
615 goto end;
616
Christopher Faulet8ef75252017-02-20 22:56:03 +0100617 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200618 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 if (p + sz >= end)
620 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200621 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100623
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100624 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625 return (p - frame);
626
627 too_big:
628 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
629 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630}
631
Christopher Faulet8ef75252017-02-20 22:56:03 +0100632/* Decode and process the HELLO frame sent by an agent. It returns the number of
633 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
634 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200635static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200637{
Christopher Faulet305c6072017-02-23 16:17:53 +0100638 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
639 char *p, *end;
640 int vsn, max_frame_size;
641 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100642
643 p = frame;
644 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200645
646 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
648 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100650 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200651
Christopher Faulet8ef75252017-02-20 22:56:03 +0100652 if (size < 7 /* TYPE + METADATA */) {
653 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
654 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200655 }
656
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100657 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100658 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200659 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100660 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100662 /* Fragmentation is not supported for HELLO frame */
663 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100664 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100665 return -1;
666 }
667
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 if (*p != 0 || *(p+1) != 0) {
670 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
671 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200672 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200674
675 /* There are 3 mandatory items: "version", "max-frame-size" and
676 * "capabilities" */
677
678 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100679 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100680 while (p < end) {
681 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200682 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200684
685 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100686 ret = spoe_decode_buffer(&p, end, &str, &sz);
687 if (ret == -1 || !sz) {
688 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
689 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200690 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200692 /* Check "version" K/V item */
693 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 int i, type = *p++;
695
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200696 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100697 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
698 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
699 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
702 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
703 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 }
705
Christopher Faulet8ef75252017-02-20 22:56:03 +0100706 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200707 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100708 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709 return -1;
710 }
711 for (i = 0; supported_versions[i].str != NULL; ++i) {
712 if (vsn >= supported_versions[i].min &&
713 vsn <= supported_versions[i].max)
714 break;
715 }
716 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100717 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200718 return -1;
719 }
720 }
721 /* Check "max-frame-size" K/V item */
722 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200724
725 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
727 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
728 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
729 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100730 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
731 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200732 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200733 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
735 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200736 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100737 if (sz < MIN_FRAME_SIZE ||
738 sz > SPOE_APPCTX(appctx)->max_frame_size) {
739 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200740 return -1;
741 }
742 max_frame_size = sz;
743 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100744 /* Check "capabilities" K/V item */
745 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100747
748 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
750 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
751 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100753 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
754 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
755 return 0;
756 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100757
Christopher Faulet8ef75252017-02-20 22:56:03 +0100758 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100759 char *delim;
760
761 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100762 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100763
Christopher Faulet8ef75252017-02-20 22:56:03 +0100764 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
765 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100766 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100767 flags |= SPOE_APPCTX_FL_PIPELINING;
768 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100769 else if (sz >= 5 && !strncmp(str, "async", 5)) {
770 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100771 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772 flags |= SPOE_APPCTX_FL_ASYNC;
773 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100774 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
775 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100776 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100777 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
778 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779
Christopher Faulet8ef75252017-02-20 22:56:03 +0100780 /* Get the next comma or break */
781 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100782 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 delim++;
784 sz -= (delim - str);
785 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100786 }
787 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200788 else {
789 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100790 if (spoe_skip_data(&p, end) == -1) {
791 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
792 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200793 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200794 }
795 }
796
797 /* Final checks */
798 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100799 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200800 return -1;
801 }
802 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100803 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200804 return -1;
805 }
Christopher Faulet11389012019-02-07 16:13:26 +0100806 if (!agent)
807 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
808 else {
809 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
810 flags &= ~SPOE_APPCTX_FL_PIPELINING;
811 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
812 flags &= ~SPOE_APPCTX_FL_ASYNC;
813 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200814
Christopher Faulet42bfa462017-01-04 14:14:19 +0100815 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
816 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
817 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100818
819 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820}
821
822/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
823 * bytes on success, 0 if the frame can be ignored and -1 if an error
824 * occurred. */
825static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100826spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200827{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100829 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100830
831 p = frame;
832 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200833
834 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100835 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
836 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200837 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100838 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200839
Christopher Faulet8ef75252017-02-20 22:56:03 +0100840 if (size < 7 /* TYPE + METADATA */) {
841 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
842 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200843 }
844
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100845 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100846 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200847 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100848 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100850 /* Fragmentation is not supported for DISCONNECT frame */
851 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100852 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100853 return -1;
854 }
855
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200856 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100857 if (*p != 0 || *(p+1) != 0) {
858 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
859 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200860 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200862
863 /* There are 2 mandatory items: "status-code" and "message" */
864
865 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 while (p < end) {
867 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200868 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100869 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200870
871 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 ret = spoe_decode_buffer(&p, end, &str, &sz);
873 if (ret == -1 || !sz) {
874 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
875 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 }
877
878 /* Check "status-code" K/V item */
879 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100880 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200881
882 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200883 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
884 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
885 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
886 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
888 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200889 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200890 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
892 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200893 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100894 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200895 }
896
897 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100898 else if (!memcmp(str, MSG_KEY, sz)) {
899 int type = *p++;
900
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200901 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
903 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
904 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200905 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906 ret = spoe_decode_buffer(&p, end, &str, &sz);
907 if (ret == -1 || sz > 255) {
908 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
909 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200910 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
912 SPOE_APPCTX(appctx)->reason = str;
913 SPOE_APPCTX(appctx)->rlen = sz;
914#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
916 else {
917 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918 if (spoe_skip_data(&p, end) == -1) {
919 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
920 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200921 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200922 }
923 }
924
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200926}
927
928
Christopher Fauleta1cda022016-12-21 08:58:06 +0100929/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930 * success, 0 if the frame can be ignored and -1 if an error occurred. */
931static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100932spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100933 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200934{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100935 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100936 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100937 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100938 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100939 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940
941 p = frame;
942 end = frame + size;
943 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200944
945 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100946 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
947 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200948 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100949 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200950
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 if (size < 7 /* TYPE + METADATA */) {
952 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
953 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200954 }
955
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100956 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100957 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200958 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100959 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200960
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 /* Fragmentation is not supported for now */
962 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100963 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100964 return -1;
965 }
966
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200967 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200968 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100969 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100970 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100971 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200972 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100973 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200974 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100975 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100976
Christopher Faulet8ef75252017-02-20 22:56:03 +0100977 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100978 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200979 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100980 if ((*ctx)->stream_id == (unsigned int)stream_id &&
981 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100982 goto found;
983 }
984 }
985 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100986 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
987 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100988 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100989 goto found;
990 }
991 }
992
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100993 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
994 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
995 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
996
997 /* ABRT bit is set for an unfinished fragmented frame */
998 if (flags & SPOE_FRM_FL_ABRT) {
999 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001000 (*ctx)->state = SPOE_CTX_ST_ERROR;
1001 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1002 /* Ignore the payload */
1003 goto end;
1004 }
1005 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1006 /* For now, we ignore the ack */
1007 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1008 return 0;
1009 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001010
Christopher Fauleta1cda022016-12-21 08:58:06 +01001011 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001012 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1013 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001014 " - stream-id=%u - frame-id=%u\n",
1015 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1016 __FUNCTION__, appctx,
1017 (unsigned int)stream_id, (unsigned int)frame_id);
1018
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001019 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001020 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1021 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001022 return 0;
1023
1024 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001025 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1026 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001027 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001028 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001029 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001030
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001031 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001032 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001033 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1034 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001035 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001036
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001037 /* Transfer the buffer ownership to the SPOE context */
1038 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001039 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001040
Christopher Faulet8ef75252017-02-20 22:56:03 +01001041 (*ctx)->state = SPOE_CTX_ST_DONE;
1042
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001043 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001044 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 " - ACK frame received"
1046 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001047 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001048 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1049 (*ctx)->frame_id, flags);
1050 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001051}
1052
Christopher Fauletba7bc162016-11-07 21:07:38 +01001053/* This function is used in cfgparse.c and declared in proto/checks.h. It
1054 * prepare the request to send to agents during a healthcheck. It returns 0 on
1055 * success and -1 if an error occurred. */
1056int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001057spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001058{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059 struct appctx appctx;
1060 struct spoe_appctx spoe_appctx;
1061 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1062 size_t sz;
1063 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001064
Christopher Faulet42bfa462017-01-04 14:14:19 +01001065 memset(&appctx, 0, sizeof(appctx));
1066 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001067 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001068
1069 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001070 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001071
Christopher Faulet8ef75252017-02-20 22:56:03 +01001072 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1073 end = frame + MAX_FRAME_SIZE;
1074
1075 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1076 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001077 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001078 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001079
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 /* Add "healthcheck" K/V item */
1081 sz = SLEN(HEALTHCHECK_KEY);
1082 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1083 return -1;
1084 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001085
Christopher Faulet8ef75252017-02-20 22:56:03 +01001086 *len = frame - buf;
1087 sz = htonl(*len - 4);
1088 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001089
Christopher Faulet8ef75252017-02-20 22:56:03 +01001090 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001091 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093 return 0;
1094}
1095
1096/* This function is used in checks.c and declared in proto/checks.h. It decode
1097 * the response received from an agent during a healthcheck. It returns 0 on
1098 * success and -1 if an error occurred. */
1099int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001100spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001101{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001102 struct appctx appctx;
1103 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104
Christopher Faulet42bfa462017-01-04 14:14:19 +01001105 memset(&appctx, 0, sizeof(appctx));
1106 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001107
Christopher Faulet42bfa462017-01-04 14:14:19 +01001108 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001109 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001110
Christopher Faulet8ef75252017-02-20 22:56:03 +01001111 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1112 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001113 goto error;
1114 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1116 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001117
1118 return 0;
1119
1120 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001121 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1122 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1123 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001124 return -1;
1125}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001126
Christopher Fauleta1cda022016-12-21 08:58:06 +01001127/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001128 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129 * success. */
1130static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001131spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001132{
1133 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001134 int ret;
1135 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136
Christopher Faulet8ef75252017-02-20 22:56:03 +01001137 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1138 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139 netint = htonl(framesz);
1140 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001141 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001143 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001144 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001146 }
1147 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001148 return -1; /* error */
1149 }
1150 return framesz;
1151}
1152
1153/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1154 * when the frame can be ignored, 1 to retry later and the frame length on
1155 * success. */
1156static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001158{
1159 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001160 int ret;
1161 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001162
Willy Tarreau06d80a92017-10-19 14:32:15 +02001163 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164 if (ret > 0) {
1165 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001166 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001167 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168 return -1;
1169 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001170 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171 }
1172 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001173 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175 }
1176 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177 return -1; /* error */
1178 }
1179 return framesz;
1180}
1181
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001182/********************************************************************
1183 * Functions that manage the SPOE applet
1184 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001185static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001186spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001187{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001188 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001189 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001190 appctx_wakeup(appctx);
1191 return 1;
1192}
1193
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001194/* Callback function that catches applet timeouts. If a timeout occurred, we set
1195 * <appctx->st1> flag and the SPOE applet is woken up. */
1196static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001197spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001198{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001199 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001200
1201 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1202 if (tick_is_expired(task->expire, now_ms)) {
1203 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001204 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001205 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001206 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207 return task;
1208}
1209
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001210/* Callback function that releases a SPOE applet. This happens when the
1211 * connection with the agent is closed. */
1212static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001213spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001214{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215 struct stream_interface *si = appctx->owner;
1216 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1217 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001218 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219
1220 if (spoe_appctx == NULL)
1221 return;
1222
1223 appctx->ctx.spoe.ptr = NULL;
1224 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001225
1226 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1227 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1228 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001229
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001231 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001232 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001233 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1234 LIST_DEL(&spoe_appctx->list);
1235 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001236 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001237 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001238
Christopher Faulet8ef75252017-02-20 22:56:03 +01001239 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001241 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1242 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001243 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001244 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001245
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001246 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001247 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1248 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001249
1250 si_shutw(si);
1251 si_shutr(si);
1252 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001253 }
1254
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001256 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001257
Christopher Faulet8ef75252017-02-20 22:56:03 +01001258 /* Notify all waiting streams */
1259 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260 LIST_DEL(&ctx->list);
1261 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001262 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001263 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001264 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001265 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001266 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001267 }
1268
Christopher Faulet8ef75252017-02-20 22:56:03 +01001269 /* If the applet was processing a fragmented frame, notify the
1270 * corresponding stream. */
1271 if (spoe_appctx->frag_ctx.ctx) {
1272 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001273 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001274 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001275 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001276 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1277 }
1278
Christopher Faulet24289f22017-09-25 14:48:02 +02001279 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001280 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001281
Christopher Faulet8ef75252017-02-20 22:56:03 +01001282 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001283 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001284 LIST_DEL(&ctx->list);
1285 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001286 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001287 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001288 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001289 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001290 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001291 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001292 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001293 LIST_DEL(&ctx->list);
1294 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001295 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001296 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001297 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001298 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001299 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1300 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001301
1302 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001303 /* Release allocated memory */
1304 spoe_release_buffer(&spoe_appctx->buffer,
1305 &spoe_appctx->buffer_wait);
1306 pool_free(pool_head_spoe_appctx, spoe_appctx);
1307
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001308 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001309 agent->rt[tid].frame_size = agent->max_frame_size;
1310 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1311 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001312}
1313
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001314static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001315spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001316{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001318 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001319 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001321
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001322 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1323 /* closed */
1324 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1325 goto exit;
1326 }
1327
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001328 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001329 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001330 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001331 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1332 goto stop;
1333 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001334
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001336 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1337 " - Connection timed out\n",
1338 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1339 __FUNCTION__, appctx);
1340 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001342 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001343
Christopher Faulet42bfa462017-01-04 14:14:19 +01001344 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001345 SPOE_APPCTX(appctx)->task->expire =
1346 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347
Christopher Faulet8ef75252017-02-20 22:56:03 +01001348 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1349 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001350 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001351 ret = spoe_prepare_hahello_frame(appctx, frame,
1352 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001353 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001354 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355
1356 switch (ret) {
1357 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001358 case 0: /* ignore => an error, cannot be ignored */
1359 goto exit;
1360
1361 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001362 goto stop;
1363
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 default:
1365 /* HELLO frame successfully sent, now wait for the
1366 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001367 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1368 goto next;
1369 }
1370
1371 next:
1372 return 0;
1373 stop:
1374 return 1;
1375 exit:
1376 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1377 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001378}
1379
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001380static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001381spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001382{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001383 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001384 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001385 char *frame;
1386 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001387
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388
Christopher Fauletb067b062017-01-04 16:39:11 +01001389 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001390 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001391 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001392 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393
Christopher Fauleta1cda022016-12-21 08:58:06 +01001394 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1396 " - Connection timed out\n",
1397 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1398 __FUNCTION__, appctx);
1399 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001400 goto exit;
1401 }
1402
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001403 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001404 ret = spoe_recv_frame(appctx, frame,
1405 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 if (ret > 1) {
1407 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1408 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1409 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001411 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001412 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001413 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001414
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 switch (ret) {
1416 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001417 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001418 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1419 goto next;
1420
1421 case 1: /* retry later */
1422 goto stop;
1423
1424 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001425 /* HELLO handshake is finished, set the idle timeout and
1426 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001427 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001429 SPOE_APPCTX(appctx)->node.key = 0;
1430 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001431
1432 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001433 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001435 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001436
Christopher Fauleta1cda022016-12-21 08:58:06 +01001437 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001439 if (trash.data)
1440 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001441
1442 SPOE_APPCTX(appctx)->task->expire =
1443 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001444 return 0;
1445 stop:
1446 return 1;
1447 exit:
1448 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1449 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001450}
1451
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001452
Christopher Fauleta1cda022016-12-21 08:58:06 +01001453static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001454spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001455{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001456 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1457 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001458 char *frame, *buf;
1459 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001460
Christopher Faulet8ef75252017-02-20 22:56:03 +01001461 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1462 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001463 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001464
1465 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1466 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1467 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1468 SPOE_APPCTX(appctx)->max_frame_size);
1469 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001470 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001471 *skip = 1;
1472 ret = 1;
1473 goto end;
1474 }
1475 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001476 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001477 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1478 SPOE_APPCTX(appctx)->max_frame_size);
1479
1480 }
1481
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 if (ret > 1)
1483 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001484
Christopher Faulet8ef75252017-02-20 22:56:03 +01001485 switch (ret) {
1486 case -1: /* error */
1487 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1488 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001489
Christopher Faulet8ef75252017-02-20 22:56:03 +01001490 case 0: /* ignore */
1491 if (ctx == NULL)
1492 goto abort_frag_frame;
1493
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001494 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001495 LIST_DEL(&ctx->list);
1496 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001497 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001498 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001499 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001500 ctx->state = SPOE_CTX_ST_ERROR;
1501 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1502 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001503 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001504 break;
1505
1506 case 1: /* retry */
1507 *skip = 1;
1508 break;
1509
1510 default:
1511 if (ctx == NULL)
1512 goto abort_frag_frame;
1513
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001514 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 LIST_DEL(&ctx->list);
1516 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001517 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001518 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001519 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001520 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1521 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1522 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001523 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001524 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001525 }
1526 goto end;
1527
1528 frag_frame_sent:
1529 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001530 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1532 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1533 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001534 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1535 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1536 goto end;
1537
1538 no_frag_frame_sent:
1539 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1540 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001541 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001542 }
1543 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1544 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1545 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1546 }
1547 else {
1548 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001549 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1551 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001552 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001553 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001554 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1555 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1556 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001557 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001558
Christopher Faulet8ef75252017-02-20 22:56:03 +01001559 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1560 goto end;
1561
1562 abort_frag_frame:
1563 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1564 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1565 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1566 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1567 goto end;
1568
1569 end:
1570 return ret;
1571}
1572
1573static int
1574spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1575{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001576 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001577 struct spoe_context *ctx = NULL;
1578 char *frame;
1579 int ret;
1580
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001581 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001582 ret = spoe_recv_frame(appctx, frame,
1583 SPOE_APPCTX(appctx)->max_frame_size);
1584 if (ret > 1) {
1585 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1586 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001587 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001588 goto end;
1589 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001590 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001591 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1592 }
1593 switch (ret) {
1594 case -1: /* error */
1595 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1596 break;
1597
1598 case 0: /* ignore */
1599 break;
1600
1601 case 1: /* retry */
1602 *skip = 1;
1603 break;
1604
1605 default:
1606 LIST_DEL(&ctx->list);
1607 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001608 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001609 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1610 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001611 if (ctx->spoe_appctx) {
1612 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001613 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001614 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001615 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1616 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1617 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1618 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1619 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1620 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1621 }
1622 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1623 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001624 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1625 break;
1626 }
1627
1628 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001629 if (trash.data)
1630 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001631 end:
1632 return ret;
1633}
1634
1635static int
1636spoe_handle_processing_appctx(struct appctx *appctx)
1637{
1638 struct stream_interface *si = appctx->owner;
1639 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001640 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001641
1642 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1643 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1644 goto exit;
1645 }
1646
1647 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1648 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1649 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1650 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1651 goto next;
1652 }
1653
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001654 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001655 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001656 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001657 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1658 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001659 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001660
Christopher Faulet8f82b202018-01-24 16:23:03 +01001661 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1662 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001663
Christopher Faulet8f82b202018-01-24 16:23:03 +01001664 /* receiving_frame loop */
1665 while (!skip_receiving) {
1666 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1667 switch (ret) {
1668 case -1: /* error */
1669 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001670
Christopher Faulet8f82b202018-01-24 16:23:03 +01001671 case 0: /* ignore */
1672 active_r = 1;
1673 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001674
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 case 1: /* retry */
1676 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001677
Christopher Faulet8f82b202018-01-24 16:23:03 +01001678 default:
1679 active_r = 1;
1680 break;
1681 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001682 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001683
Christopher Faulet8f82b202018-01-24 16:23:03 +01001684 /* send_frame loop */
1685 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1686 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1687 switch (ret) {
1688 case -1: /* error */
1689 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001690
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001692 if (SPOE_APPCTX(appctx)->node.key)
1693 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 active_s++;
1695 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 case 1: /* retry */
1698 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001699
Christopher Faulet8f82b202018-01-24 16:23:03 +01001700 default:
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;
1705 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001706 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1710 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1711 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001712
Christopher Faulet8f82b202018-01-24 16:23:03 +01001713 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001714 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001715 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001716 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001717 }
1718 return 1;
1719
Christopher Faulet8f82b202018-01-24 16:23:03 +01001720 next:
1721 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1722 return 0;
1723
Christopher Fauleta1cda022016-12-21 08:58:06 +01001724 exit:
1725 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1726 return 0;
1727}
1728
1729static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001730spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001731{
1732 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001733 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001734 char *frame, *buf;
1735 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001736
Christopher Fauleta1cda022016-12-21 08:58:06 +01001737 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1738 goto exit;
1739
1740 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1741 goto exit;
1742
Christopher Faulet8ef75252017-02-20 22:56:03 +01001743 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1744 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001745 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001746 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1747 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001748 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001750
1751 switch (ret) {
1752 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001753 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001754 goto exit;
1755
1756 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001757 goto stop;
1758
1759 default:
1760 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1761 " - disconnected by HAProxy (%d): %s\n",
1762 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001763 __FUNCTION__, appctx,
1764 SPOE_APPCTX(appctx)->status_code,
1765 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001766
1767 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1768 goto next;
1769 }
1770
1771 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001772 SPOE_APPCTX(appctx)->task->expire =
1773 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001774 return 0;
1775 stop:
1776 return 1;
1777 exit:
1778 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1779 return 0;
1780}
1781
1782static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001784{
1785 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001786 char *frame;
1787 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788
Christopher Fauletb067b062017-01-04 16:39:11 +01001789 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001790 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001791 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001792 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793
Christopher Fauletb067b062017-01-04 16:39:11 +01001794 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001795 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001796 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001797 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001798
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001799 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001800 ret = spoe_recv_frame(appctx, frame,
1801 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001803 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001804 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001805 }
1806
1807 switch (ret) {
1808 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001809 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1810 " - error on frame (%s)\n",
1811 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001812 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001814 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 goto exit;
1816
1817 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818 goto next;
1819
1820 case 1: /* retry */
1821 goto stop;
1822
1823 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001826 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001827 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001828 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1829 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 goto exit;
1831 }
1832
1833 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001835 if (trash.data)
1836 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001837
Christopher Fauleta1cda022016-12-21 08:58:06 +01001838 return 0;
1839 stop:
1840 return 1;
1841 exit:
1842 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1843 return 0;
1844}
1845
1846/* I/O Handler processing messages exchanged with the agent */
1847static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001848spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001849{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 struct stream_interface *si = appctx->owner;
1851 struct spoe_agent *agent;
1852
1853 if (SPOE_APPCTX(appctx) == NULL)
1854 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001855
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1857 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001858
Christopher Fauleta1cda022016-12-21 08:58:06 +01001859 switchstate:
1860 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1861 " - appctx-state=%s\n",
1862 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1863 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1864
1865 switch (appctx->st0) {
1866 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001867 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001868 goto out;
1869 goto switchstate;
1870
1871 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001873 goto out;
1874 goto switchstate;
1875
1876 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001877 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001878 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001879 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001880 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001881 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001882 SPOE_APPCTX(appctx)->task->expire =
1883 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001884 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001885 goto switchstate;
1886 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001887 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1888 /* fall through */
1889
1890 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001891 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1892 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001893 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001894 goto out;
1895 goto switchstate;
1896
1897 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001898 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001899 goto out;
1900 goto switchstate;
1901
1902 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001903 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001904 goto out;
1905 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001906
1907 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908 appctx->st0 = SPOE_APPCTX_ST_END;
1909 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1910
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001911 si_shutw(si);
1912 si_shutr(si);
1913 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001914 /* fall through */
1915
1916 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001917 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001918 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001919 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001920 if (stopping)
1921 spoe_wakeup_appctx(appctx);
1922
Christopher Faulet42bfa462017-01-04 14:14:19 +01001923 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1924 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001925}
1926
1927struct applet spoe_applet = {
1928 .obj_type = OBJ_TYPE_APPLET,
1929 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 .fct = spoe_handle_appctx,
1931 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001932};
1933
1934/* Create a SPOE applet. On success, the created applet is returned, else
1935 * NULL. */
1936static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001937spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938{
1939 struct appctx *appctx;
1940 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942
Emeric Brun1138fd02017-06-19 12:38:55 +02001943 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001944 goto out_error;
1945
Willy Tarreaubafbe012017-11-24 17:34:44 +01001946 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001947 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001948 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001949 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001950
Christopher Faulet42bfa462017-01-04 14:14:19 +01001951 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001952 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001953 goto out_free_spoe_appctx;
1954
1955 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001956 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001957 SPOE_APPCTX(appctx)->task->context = appctx;
1958 SPOE_APPCTX(appctx)->agent = conf->agent;
1959 SPOE_APPCTX(appctx)->version = 0;
1960 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1961 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001963 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001964 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001965
Willy Tarreau21046592020-02-26 10:39:36 +01001966 MT_LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001967 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001968 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969
1970 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1971 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001972
Willy Tarreau5820a362016-12-22 15:59:02 +01001973 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001974 if (!sess)
1975 goto out_free_spoe;
1976
Willy Tarreau87787ac2017-08-28 16:22:54 +02001977 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001978 goto out_free_sess;
1979
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001980 stream_set_backend(strm, conf->agent->b.be);
1981
1982 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001983 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001984 appctx_wakeup(appctx);
1985
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001986 strm->do_log = NULL;
1987 strm->res.flags |= CF_READ_DONTWAIT;
1988
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001989 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001990 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001991 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001992 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001993
Emeric Brunc60def82017-09-27 14:59:38 +02001994 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001995 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 return appctx;
1997
1998 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001999 out_free_sess:
2000 session_free(sess);
2001 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002002 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002003 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002004 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002005 out_free_appctx:
2006 appctx_free(appctx);
2007 out_error:
2008 return NULL;
2009}
2010
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002011static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002012spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013{
2014 struct spoe_config *conf = FLT_CONF(ctx->filter);
2015 struct spoe_agent *agent = conf->agent;
2016 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002017 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018
Christopher Fauleta1cda022016-12-21 08:58:06 +01002019 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002020 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002021 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002022 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023
2024 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002025 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002026 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2027 ctx->strm);
2028
Christopher Fauleta1cda022016-12-21 08:58:06 +01002029 /* Do not try to create a new applet if there is no server up for the
2030 * agent's backend. */
2031 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2032 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2033 " - cannot create SPOE appctx: no server up\n",
2034 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2035 __FUNCTION__, ctx->strm);
2036 goto end;
2037 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002038
Christopher Fauleta1cda022016-12-21 08:58:06 +01002039 /* Do not try to create a new applet if we have reached the maximum of
2040 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002041 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002042 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002043 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2044 " - cannot create SPOE appctx: max CPS reached\n",
2045 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2046 __FUNCTION__, ctx->strm);
2047 goto end;
2048 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002049 }
2050
Christopher Faulet8ef75252017-02-20 22:56:03 +01002051 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002052 if (appctx == NULL) {
2053 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2054 " - failed to create SPOE appctx\n",
2055 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2056 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002057 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002058 "SPOE: [%s] failed to create SPOE applet\n",
2059 agent->id);
2060
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002062 }
2063
Christopher Fauleta1cda022016-12-21 08:58:06 +01002064 /* Increase the per-process number of cumulated connections */
2065 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002066 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002067
Christopher Fauleta1cda022016-12-21 08:58:06 +01002068 end:
2069 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002070 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002071 ctx->status_code = SPOE_CTX_ERR_RES;
2072 return -1;
2073 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002074
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002075 /* Add the SPOE context in the sending queue if the stream has no applet
2076 * already assigned and wakeup all idle applets. Otherwise, don't queue
2077 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002078 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002079 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2080 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002081 if (ctx->spoe_appctx)
2082 return 1;
2083 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084
2085 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002086 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002087 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002088 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002089 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002090 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002091
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002092 /* Finally try to wakeup an IDLE applet. */
2093 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2094 struct eb32_node *node;
2095
2096 node = eb32_first(&agent->rt[tid].idle_applets);
2097 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2098 if (node && spoe_appctx) {
2099 eb32_delete(&spoe_appctx->node);
2100 spoe_appctx->node.key++;
2101 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2102 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002103 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002104 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002105 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002106}
2107
2108/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002109 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002110 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002111/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2112 * fragmented_content. If the next message can be processed, it returns 0. If
2113 * the message is too big, it returns -1.*/
2114static int
2115spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2116 struct spoe_message *msg, int dir,
2117 char **buf, char *end)
2118{
2119 struct sample *smp;
2120 struct spoe_arg *arg;
2121 int ret;
2122
2123 if (msg->cond) {
2124 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2125 ret = acl_pass(ret);
2126 if (msg->cond->pol == ACL_COND_UNLESS)
2127 ret = !ret;
2128
2129 /* the rule does not match */
2130 if (!ret)
2131 goto next;
2132 }
2133
2134 /* Resume encoding of a SPOE argument */
2135 if (ctx->frag_ctx.curarg != NULL) {
2136 arg = ctx->frag_ctx.curarg;
2137 goto encode_argument;
2138 }
2139
2140 if (ctx->frag_ctx.curoff != UINT_MAX)
2141 goto encode_msg_payload;
2142
2143 /* Check if there is enough space for the message name and the
2144 * number of arguments. It implies <msg->id_len> is encoded on 2
2145 * bytes, at most (< 2288). */
2146 if (*buf + 2 + msg->id_len + 1 > end)
2147 goto too_big;
2148
2149 /* Encode the message name */
2150 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2151 goto too_big;
2152
2153 /* Set the number of arguments for this message */
2154 **buf = msg->nargs;
2155 (*buf)++;
2156
2157 ctx->frag_ctx.curoff = 0;
2158 encode_msg_payload:
2159
2160 /* Loop on arguments */
2161 list_for_each_entry(arg, &msg->args, list) {
2162 ctx->frag_ctx.curarg = arg;
2163 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002164 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002165
2166 encode_argument:
2167 if (ctx->frag_ctx.curoff != UINT_MAX)
2168 goto encode_arg_value;
2169
Joseph Herlantf7f60312018-11-15 13:46:49 -08002170 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002171 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2172 goto too_big;
2173
2174 ctx->frag_ctx.curoff = 0;
2175 encode_arg_value:
2176
Joseph Herlantf7f60312018-11-15 13:46:49 -08002177 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002178 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002179 if (smp) {
2180 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2181 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2182 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002183 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002184 if (ret == -1 || ctx->frag_ctx.curoff)
2185 goto too_big;
2186 }
2187
2188 next:
2189 return 0;
2190
2191 too_big:
2192 return -1;
2193}
2194
Christopher Fauletc718b822017-09-21 16:50:56 +02002195/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2196 * handle fragmented content. On success it returns 1. If an error occurred, -1
2197 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2198 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002199static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002200spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002201 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002202{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002203 struct spoe_config *conf = FLT_CONF(ctx->filter);
2204 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002205 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002206 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002207
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002208 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002209 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002210
Christopher Fauletc718b822017-09-21 16:50:56 +02002211 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2212 /* Resume encoding of a SPOE message */
2213 if (ctx->frag_ctx.curmsg != NULL) {
2214 msg = ctx->frag_ctx.curmsg;
2215 goto encode_evt_message;
2216 }
2217
2218 list_for_each_entry(msg, messages, by_evt) {
2219 ctx->frag_ctx.curmsg = msg;
2220 ctx->frag_ctx.curarg = NULL;
2221 ctx->frag_ctx.curoff = UINT_MAX;
2222
2223 encode_evt_message:
2224 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2225 goto too_big;
2226 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002227 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002228 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2229 /* Resume encoding of a SPOE message */
2230 if (ctx->frag_ctx.curmsg != NULL) {
2231 msg = ctx->frag_ctx.curmsg;
2232 goto encode_grp_message;
2233 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002234
Christopher Fauletc718b822017-09-21 16:50:56 +02002235 list_for_each_entry(msg, messages, by_grp) {
2236 ctx->frag_ctx.curmsg = msg;
2237 ctx->frag_ctx.curarg = NULL;
2238 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002239
Christopher Fauletc718b822017-09-21 16:50:56 +02002240 encode_grp_message:
2241 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2242 goto too_big;
2243 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002244 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002245 else
2246 goto skip;
2247
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002248
Christopher Faulet57583e42017-09-04 15:41:09 +02002249 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002250 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002251 goto skip;
2252
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002253 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002254 " - encode %s messages - spoe_appctx=%p"
2255 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002256 (int)now.tv_sec, (int)now.tv_usec,
2257 agent->id, __FUNCTION__, s,
2258 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002259 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002260 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002261
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002262 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002263 ctx->frag_ctx.curmsg = NULL;
2264 ctx->frag_ctx.curarg = NULL;
2265 ctx->frag_ctx.curoff = 0;
2266 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002267
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002268 return 1;
2269
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002270 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002271 /* Return an error if fragmentation is unsupported or if nothing has
2272 * been encoded because its too big and not splittable. */
2273 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002274 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2275 return -1;
2276 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002277
2278 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002279 " - encode fragmented messages - spoe_appctx=%p"
2280 " - curmsg=%p - curarg=%p - curoff=%u"
2281 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002282 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002283 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002284 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002285 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002286
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002287 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2289 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2290 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002291
2292 skip:
2293 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2294 " - skip the frame because nothing has been encoded\n",
2295 (int)now.tv_sec, (int)now.tv_usec,
2296 agent->id, __FUNCTION__, s);
2297 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002298}
2299
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002300
2301/***************************************************************************
2302 * Functions that handle SPOE actions
2303 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002304/* Helper function to set a variable */
2305static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002306spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002307 struct sample *smp)
2308{
2309 struct spoe_config *conf = FLT_CONF(ctx->filter);
2310 struct spoe_agent *agent = conf->agent;
2311 char varname[64];
2312
2313 memset(varname, 0, sizeof(varname));
2314 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2315 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002316 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2317 vars_set_by_name(varname, len, smp);
2318 else
2319 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320}
2321
2322/* Helper function to unset a variable */
2323static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002324spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002325 struct sample *smp)
2326{
2327 struct spoe_config *conf = FLT_CONF(ctx->filter);
2328 struct spoe_agent *agent = conf->agent;
2329 char varname[64];
2330
2331 memset(varname, 0, sizeof(varname));
2332 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2333 scope, agent->var_pfx, len, name);
2334 vars_unset_by_name_ifexist(varname, len, smp);
2335}
2336
2337
Christopher Faulet8ef75252017-02-20 22:56:03 +01002338static inline int
2339spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2340 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002341{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002342 char *str, *scope, *p = *buf;
2343 struct sample smp;
2344 uint64_t sz;
2345 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002346
Christopher Faulet8ef75252017-02-20 22:56:03 +01002347 if (p + 2 >= end)
2348 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002349
Christopher Faulet8ef75252017-02-20 22:56:03 +01002350 /* SET-VAR requires 3 arguments */
2351 if (*p++ != 3)
2352 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002353
Christopher Faulet8ef75252017-02-20 22:56:03 +01002354 switch (*p++) {
2355 case SPOE_SCOPE_PROC: scope = "proc"; break;
2356 case SPOE_SCOPE_SESS: scope = "sess"; break;
2357 case SPOE_SCOPE_TXN : scope = "txn"; break;
2358 case SPOE_SCOPE_REQ : scope = "req"; break;
2359 case SPOE_SCOPE_RES : scope = "res"; break;
2360 default: goto skip;
2361 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002362
Christopher Faulet8ef75252017-02-20 22:56:03 +01002363 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2364 goto skip;
2365 memset(&smp, 0, sizeof(smp));
2366 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002367
Christopher Faulet8ef75252017-02-20 22:56:03 +01002368 if (spoe_decode_data(&p, end, &smp) == -1)
2369 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002370
Christopher Faulet8ef75252017-02-20 22:56:03 +01002371 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2372 " - set-var '%s.%s.%.*s'\n",
2373 (int)now.tv_sec, (int)now.tv_usec,
2374 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2375 __FUNCTION__, s, scope,
2376 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2377 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002378
Christopher Faulet8ef75252017-02-20 22:56:03 +01002379 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002380
Christopher Faulet8ef75252017-02-20 22:56:03 +01002381 ret = (p - *buf);
2382 *buf = p;
2383 return ret;
2384 skip:
2385 return 0;
2386}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002387
Christopher Faulet8ef75252017-02-20 22:56:03 +01002388static inline int
2389spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2390 char **buf, char *end, int dir)
2391{
2392 char *str, *scope, *p = *buf;
2393 struct sample smp;
2394 uint64_t sz;
2395 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397 if (p + 2 >= end)
2398 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002399
Christopher Faulet8ef75252017-02-20 22:56:03 +01002400 /* UNSET-VAR requires 2 arguments */
2401 if (*p++ != 2)
2402 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002403
Christopher Faulet8ef75252017-02-20 22:56:03 +01002404 switch (*p++) {
2405 case SPOE_SCOPE_PROC: scope = "proc"; break;
2406 case SPOE_SCOPE_SESS: scope = "sess"; break;
2407 case SPOE_SCOPE_TXN : scope = "txn"; break;
2408 case SPOE_SCOPE_REQ : scope = "req"; break;
2409 case SPOE_SCOPE_RES : scope = "res"; break;
2410 default: goto skip;
2411 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2414 goto skip;
2415 memset(&smp, 0, sizeof(smp));
2416 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002417
Christopher Faulet8ef75252017-02-20 22:56:03 +01002418 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2419 " - unset-var '%s.%s.%.*s'\n",
2420 (int)now.tv_sec, (int)now.tv_usec,
2421 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2422 __FUNCTION__, s, scope,
2423 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2424 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002425
Christopher Faulet8ef75252017-02-20 22:56:03 +01002426 spoe_unset_var(ctx, scope, str, sz, &smp);
2427
2428 ret = (p - *buf);
2429 *buf = p;
2430 return ret;
2431 skip:
2432 return 0;
2433}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002434
Christopher Faulet8ef75252017-02-20 22:56:03 +01002435/* Process SPOE actions for a specific event. It returns 1 on success. If an
2436 * error occurred, 0 is returned. */
2437static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002438spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002439{
2440 char *p, *end;
2441 int ret;
2442
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002443 p = b_head(&ctx->buffer);
2444 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002445
2446 while (p < end) {
2447 enum spoe_action_type type;
2448
2449 type = *p++;
2450 switch (type) {
2451 case SPOE_ACT_T_SET_VAR:
2452 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2453 if (!ret)
2454 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002455 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002456
Christopher Faulet8ef75252017-02-20 22:56:03 +01002457 case SPOE_ACT_T_UNSET_VAR:
2458 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2459 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002460 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002461 break;
2462
2463 default:
2464 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002465 }
2466 }
2467
2468 return 1;
2469 skip:
2470 return 0;
2471}
2472
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002473/***************************************************************************
2474 * Functions that process SPOE events
2475 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002476static void
2477spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2478 struct spoe_context *ctx, int dir)
2479{
2480 if (!tv_iszero(&ctx->stats.tv_start)) {
2481 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2482 ctx->stats.t_total += ctx->stats.t_process;
2483 tv_zero(&ctx->stats.tv_request);
2484 tv_zero(&ctx->stats.tv_queue);
2485 tv_zero(&ctx->stats.tv_wait);
2486 tv_zero(&ctx->stats.tv_response);
2487 }
2488
2489 if (agent->var_t_process) {
2490 struct sample smp;
2491
2492 memset(&smp, 0, sizeof(smp));
2493 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2494 smp.data.u.sint = ctx->stats.t_process;
2495 smp.data.type = SMP_T_SINT;
2496
2497 spoe_set_var(ctx, "txn", agent->var_t_process,
2498 strlen(agent->var_t_process), &smp);
2499 }
2500
2501 if (agent->var_t_total) {
2502 struct sample smp;
2503
2504 memset(&smp, 0, sizeof(smp));
2505 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2506 smp.data.u.sint = ctx->stats.t_total;
2507 smp.data.type = SMP_T_SINT;
2508
2509 spoe_set_var(ctx, "txn", agent->var_t_total,
2510 strlen(agent->var_t_total), &smp);
2511 }
2512}
2513
2514static void
2515spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2516 struct spoe_context *ctx, int dir)
2517{
2518 if (agent->eps_max > 0)
2519 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2520
2521 if (agent->var_on_error) {
2522 struct sample smp;
2523
2524 memset(&smp, 0, sizeof(smp));
2525 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2526 smp.data.u.sint = ctx->status_code;
2527 smp.data.type = SMP_T_BOOL;
2528
2529 spoe_set_var(ctx, "txn", agent->var_on_error,
2530 strlen(agent->var_on_error), &smp);
2531 }
2532
2533 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2534 ? SPOE_CTX_ST_READY
2535 : SPOE_CTX_ST_NONE);
2536}
2537
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002538static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002539spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002540{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002541 /* If a process is already started for this SPOE context, retry
2542 * later. */
2543 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002544 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002545
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002546 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002547 ctx->stats.tv_start = now;
2548 ctx->stats.tv_request = now;
2549 ctx->stats.t_request = -1;
2550 ctx->stats.t_queue = -1;
2551 ctx->stats.t_waiting = -1;
2552 ctx->stats.t_response = -1;
2553 ctx->stats.t_process = -1;
2554
2555 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002556
Christopher Fauleta1cda022016-12-21 08:58:06 +01002557 /* Set the right flag to prevent request and response processing
2558 * in same time. */
2559 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2560 ? SPOE_CTX_FL_REQ_PROCESS
2561 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002562 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563}
2564
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002565static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002566spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002567{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002568 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002570 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2571 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002572 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002573 if (sa) {
2574 if (sa->frag_ctx.ctx == ctx) {
2575 sa->frag_ctx.ctx = NULL;
2576 spoe_wakeup_appctx(sa->owner);
2577 }
2578 else
2579 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002580 }
2581
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002583 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002584 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002585
2586 /* Reset processing timer */
2587 ctx->process_exp = TICK_ETERNITY;
2588
Christopher Faulet8ef75252017-02-20 22:56:03 +01002589 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002590
Christopher Fauletfce747b2018-01-24 15:59:32 +01002591 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002592 ctx->frag_ctx.curmsg = NULL;
2593 ctx->frag_ctx.curarg = NULL;
2594 ctx->frag_ctx.curoff = 0;
2595 ctx->frag_ctx.flags = 0;
2596
Christopher Fauleta1cda022016-12-21 08:58:06 +01002597 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002598 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002599 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002600 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002601 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002602
Christopher Fauleta1cda022016-12-21 08:58:06 +01002603 LIST_DEL(&ctx->list);
2604 LIST_INIT(&ctx->list);
2605 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002606}
2607
Christopher Faulet58d03682017-09-21 16:57:24 +02002608/* Process a list of SPOE messages. First, this functions will process messages
2609 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2610 * to process corresponding actions. During all the processing, it returns 0
2611 * and it returns 1 when the processing is finished. If an error occurred, -1
2612 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002613static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002614spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2615 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002616{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002617 struct spoe_config *conf = FLT_CONF(ctx->filter);
2618 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002619 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002620
2621 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002622 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002623
2624 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2625 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002626 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002627 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002628 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002629 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002630 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002631 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002632
2633 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002634 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002635 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002636 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002637 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002638 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002639 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640 goto skip;
2641 }
2642 }
2643
Christopher Fauletf7a30922016-11-10 15:04:51 +01002644 if (!tick_isset(ctx->process_exp)) {
2645 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2646 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2647 ctx->process_exp);
2648 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002649 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002650 if (!ret)
2651 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002652
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002653 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002654 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002655 }
2656
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002657 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002658 if (tv_iszero(&ctx->stats.tv_request))
2659 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002660 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002661 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002662 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002663 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002664 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002665 if (!ret)
2666 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002667 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002668 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002669 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2670 }
2671
2672 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002673 if (ctx->spoe_appctx)
2674 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002675 ret = 0;
2676 goto out;
2677 }
2678
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002679 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2680 ret = 0;
2681 goto out;
2682 }
2683
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002685 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002686 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002687 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002689 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002690 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002691 }
2692
2693 out:
2694 return ret;
2695
Christopher Fauleta1cda022016-12-21 08:58:06 +01002696 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002697 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002698 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002699 spoe_stop_processing(agent, ctx);
2700 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002701
Christopher Fauleta1cda022016-12-21 08:58:06 +01002702 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002703 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002704 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002705 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002706 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002707 spoe_handle_processing_error(s, agent, ctx, dir);
2708 ret = 1;
2709 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002710 return ret;
2711}
2712
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002713/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2714 * See spoe_process_message for details. */
2715static int
2716spoe_process_group(struct stream *s, struct spoe_context *ctx,
2717 struct spoe_group *group, int dir)
2718{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002719 struct spoe_config *conf = FLT_CONF(ctx->filter);
2720 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002721 int ret;
2722
2723 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2724 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002725 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002726 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2727 group->id);
2728
2729 if (LIST_ISEMPTY(&group->messages))
2730 return 1;
2731
2732 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002733 if (ret && ctx->stats.t_process != -1) {
2734 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002735 " - <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 +01002736 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002737 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002738 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002739 ctx->stats.t_response, ctx->stats.t_process,
2740 agent->counters.idles, agent->counters.applets,
2741 agent->counters.nb_sending, agent->counters.nb_waiting,
2742 agent->counters.nb_errors, agent->counters.nb_processed,
2743 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2744 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2745 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2746 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2747 agent->id, group->id, s->uniq_id, ctx->status_code,
2748 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2749 ctx->stats.t_response, ctx->stats.t_process,
2750 agent->counters.idles, agent->counters.applets,
2751 agent->counters.nb_sending, agent->counters.nb_waiting,
2752 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002753 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002754 return ret;
2755}
2756
Christopher Faulet58d03682017-09-21 16:57:24 +02002757/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2758 * See spoe_process_message for details. */
2759static int
2760spoe_process_event(struct stream *s, struct spoe_context *ctx,
2761 enum spoe_event ev)
2762{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002763 struct spoe_config *conf = FLT_CONF(ctx->filter);
2764 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002765 int dir, ret;
2766
2767 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002768 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002769 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002770 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2771 spoe_event_str[ev]);
2772
2773 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2774
2775 if (LIST_ISEMPTY(&(ctx->events[ev])))
2776 return 1;
2777
2778 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002779 if (ret && ctx->stats.t_process != -1) {
2780 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002781 " - <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 +01002782 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2783 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2784 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002785 ctx->stats.t_response, ctx->stats.t_process,
2786 agent->counters.idles, agent->counters.applets,
2787 agent->counters.nb_sending, agent->counters.nb_waiting,
2788 agent->counters.nb_errors, agent->counters.nb_processed,
2789 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2790 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2791 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2792 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2793 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2794 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2795 ctx->stats.t_response, ctx->stats.t_process,
2796 agent->counters.idles, agent->counters.applets,
2797 agent->counters.nb_sending, agent->counters.nb_waiting,
2798 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002799 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002800 return ret;
2801}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002802
2803/***************************************************************************
2804 * Functions that create/destroy SPOE contexts
2805 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002806static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002807spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002808{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002809 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002810 return 1;
2811
Willy Tarreau21046592020-02-26 10:39:36 +01002812 if (MT_LIST_ADDED(&buffer_wait->list))
2813 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002814
Christopher Faulet4596fb72017-01-11 14:05:19 +01002815 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002816 return 1;
2817
Willy Tarreau21046592020-02-26 10:39:36 +01002818 MT_LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002819 return 0;
2820}
2821
2822static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002823spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002824{
Willy Tarreau21046592020-02-26 10:39:36 +01002825 if (MT_LIST_ADDED(&buffer_wait->list))
2826 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002827
2828 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002829 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002830 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002831 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002832 }
2833}
2834
Christopher Faulet4596fb72017-01-11 14:05:19 +01002835static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002836spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002837{
2838 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2839 return 1;
2840}
2841
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002842static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002843spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002844{
2845 struct spoe_config *conf = FLT_CONF(filter);
2846 struct spoe_context *ctx;
2847
Willy Tarreaubafbe012017-11-24 17:34:44 +01002848 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002849 if (ctx == NULL) {
2850 return NULL;
2851 }
2852 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002853 ctx->filter = filter;
2854 ctx->state = SPOE_CTX_ST_NONE;
2855 ctx->status_code = SPOE_CTX_ERR_NONE;
2856 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002857 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002858 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002859 ctx->buffer = BUF_NULL;
Willy Tarreau21046592020-02-26 10:39:36 +01002860 MT_LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002861 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002862 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002863 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002864
Christopher Fauletf7a30922016-11-10 15:04:51 +01002865 ctx->stream_id = 0;
2866 ctx->frame_id = 1;
2867 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002868
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002869 tv_zero(&ctx->stats.tv_start);
2870 tv_zero(&ctx->stats.tv_request);
2871 tv_zero(&ctx->stats.tv_queue);
2872 tv_zero(&ctx->stats.tv_wait);
2873 tv_zero(&ctx->stats.tv_response);
2874 ctx->stats.t_request = -1;
2875 ctx->stats.t_queue = -1;
2876 ctx->stats.t_waiting = -1;
2877 ctx->stats.t_response = -1;
2878 ctx->stats.t_process = -1;
2879 ctx->stats.t_total = 0;
2880
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002881 ctx->strm = s;
2882 ctx->state = SPOE_CTX_ST_READY;
2883 filter->ctx = ctx;
2884
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002885 return ctx;
2886}
2887
2888static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002889spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002890{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002891 struct spoe_config *conf = FLT_CONF(filter);
2892 struct spoe_context *ctx = filter->ctx;
2893
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002894 if (!ctx)
2895 return;
2896
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002897 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002898 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002899 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002900}
2901
2902static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002903spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002904{
2905 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002906 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002907
2908 tv_zero(&ctx->stats.tv_start);
2909 tv_zero(&ctx->stats.tv_request);
2910 tv_zero(&ctx->stats.tv_queue);
2911 tv_zero(&ctx->stats.tv_wait);
2912 tv_zero(&ctx->stats.tv_response);
2913 ctx->stats.t_request = -1;
2914 ctx->stats.t_queue = -1;
2915 ctx->stats.t_waiting = -1;
2916 ctx->stats.t_response = -1;
2917 ctx->stats.t_process = -1;
2918 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919}
2920
2921
2922/***************************************************************************
2923 * Hooks that manage the filter lifecycle (init/check/deinit)
2924 **************************************************************************/
2925/* Signal handler: Do a soft stop, wakeup SPOE applet */
2926static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002927spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002928{
2929 struct proxy *p;
2930
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002931 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002932 while (p) {
2933 struct flt_conf *fconf;
2934
2935 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002936 struct spoe_config *conf;
2937 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002938 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002939 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002940
Christopher Faulet3b386a32017-02-23 10:17:15 +01002941 if (fconf->id != spoe_filter_id)
2942 continue;
2943
2944 conf = fconf->conf;
2945 agent = conf->agent;
2946
Christopher Faulet24289f22017-09-25 14:48:02 +02002947 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002948 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002949 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2950 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002951 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002952 }
2953 }
2954 p = p->next;
2955 }
2956}
2957
2958
2959/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2960static int
2961spoe_init(struct proxy *px, struct flt_conf *fconf)
2962{
2963 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002965 /* conf->agent_fe was already initialized during the config
2966 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002967 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002968 conf->agent_fe.cap = PR_CAP_FE;
2969 conf->agent_fe.mode = PR_MODE_TCP;
2970 conf->agent_fe.maxconn = 0;
2971 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2972 conf->agent_fe.conn_retries = CONN_RETRIES;
2973 conf->agent_fe.accept = frontend_accept;
2974 conf->agent_fe.srv = NULL;
2975 conf->agent_fe.timeout.client = TICK_ETERNITY;
2976 conf->agent_fe.default_target = &spoe_applet.obj_type;
2977 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2978
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002979 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002980 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002981 sighandler_registered = 1;
2982 }
2983
Christopher Faulet00292352019-01-09 15:05:10 +01002984 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002985 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986}
2987
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002988/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002989static void
2990spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2991{
2992 struct spoe_config *conf = fconf->conf;
2993
2994 if (conf) {
2995 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002996
Christopher Faulet8ef75252017-02-20 22:56:03 +01002997 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002998 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002999 free(conf);
3000 }
3001 fconf->conf = NULL;
3002}
3003
3004/* Check configuration of a SPOE filter for a specified proxy.
3005 * Return 1 on error, else 0. */
3006static int
3007spoe_check(struct proxy *px, struct flt_conf *fconf)
3008{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003009 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 struct spoe_config *conf = fconf->conf;
3011 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003012 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003013
Christopher Faulet7ee86672017-09-19 11:08:28 +02003014 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3015 * are uniq */
3016 list_for_each_entry(f, &px->filter_configs, list) {
3017 struct spoe_config *c = f->conf;
3018
3019 /* This is not an SPOE filter */
3020 if (f->id != spoe_filter_id)
3021 continue;
3022 /* This is the current SPOE filter */
3023 if (f == fconf)
3024 continue;
3025
3026 /* Check engine Id. It should be uniq */
3027 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003028 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3029 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003030 return 1;
3031 }
3032 }
3033
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003034 target = proxy_be_by_name(conf->agent->b.name);
3035 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003036 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3037 " declared at %s:%d.\n",
3038 px->id, conf->agent->b.name, conf->agent->id,
3039 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003040 return 1;
3041 }
3042 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003043 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3044 " at %s:%d does not support HTTP mode.\n",
3045 px->id, target->id, conf->agent->id,
3046 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003047 return 1;
3048 }
3049
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003050 if (px->bind_proc & ~target->bind_proc) {
3051 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3052 " at %s:%d does not cover all of its processes.\n",
3053 px->id, target->id, conf->agent->id,
3054 conf->agent->conf.file, conf->agent->conf.line);
3055 return 1;
3056 }
3057
Christopher Fauletfe261552019-03-18 13:57:42 +01003058 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003059 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3060 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3061 return 1;
3062 }
3063 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003064 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003065 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3066 conf->agent->rt[i].processing = 0;
3067 LIST_INIT(&conf->agent->rt[i].applets);
3068 LIST_INIT(&conf->agent->rt[i].sending_queue);
3069 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3070 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003071 }
3072
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003073 free(conf->agent->b.name);
3074 conf->agent->b.name = NULL;
3075 conf->agent->b.be = target;
3076 return 0;
3077}
3078
Kevin Zhud87b1a52019-09-17 15:05:45 +02003079/* Initializes the SPOE filter for a proxy for a specific thread.
3080 * Returns a negative value if an error occurs. */
3081static int
3082spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3083{
3084 struct spoe_config *conf = fconf->conf;
3085 struct spoe_agent *agent = conf->agent;
3086
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003087 agent->rt[tid].engine_id = generate_pseudo_uuid();
3088 if (agent->rt[tid].engine_id == NULL)
3089 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003090 return 0;
3091}
3092
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003093/**************************************************************************
3094 * Hooks attached to a stream
3095 *************************************************************************/
3096/* Called when a filter instance is created and attach to a stream. It creates
3097 * the context that will be used to process this stream. */
3098static int
3099spoe_start(struct stream *s, struct filter *filter)
3100{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003101 struct spoe_config *conf = FLT_CONF(filter);
3102 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003103 struct spoe_context *ctx;
3104
3105 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003106 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003107 __FUNCTION__, s);
3108
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003109 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003110 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3111 " - failed to create SPOE context\n",
3112 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003113 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003114 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003115 "SPOE: [%s] failed to create SPOE context\n",
3116 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003117 return 0;
3118 }
3119
Christopher Faulet11610f32017-09-21 10:23:10 +02003120 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003121 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3122
Christopher Faulet11610f32017-09-21 10:23:10 +02003123 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3125
Christopher Faulet11610f32017-09-21 10:23:10 +02003126 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003127 filter->pre_analyzers |= AN_RES_INSPECT;
3128
Christopher Faulet11610f32017-09-21 10:23:10 +02003129 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003130 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3131
Christopher Faulet11610f32017-09-21 10:23:10 +02003132 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003133 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3134
Christopher Faulet11610f32017-09-21 10:23:10 +02003135 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003136 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3137
3138 return 1;
3139}
3140
3141/* Called when a filter instance is detached from a stream. It release the
3142 * attached SPOE context. */
3143static void
3144spoe_stop(struct stream *s, struct filter *filter)
3145{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003146 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3147 (int)now.tv_sec, (int)now.tv_usec,
3148 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3149 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003150 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003151}
3152
Christopher Fauletf7a30922016-11-10 15:04:51 +01003153
3154/*
3155 * Called when the stream is woken up because of expired timer.
3156 */
3157static void
3158spoe_check_timeouts(struct stream *s, struct filter *filter)
3159{
3160 struct spoe_context *ctx = filter->ctx;
3161
Christopher Fauletac580602018-03-20 16:09:20 +01003162 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003163 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003164}
3165
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003166/* Called when we are ready to filter data on a channel */
3167static int
3168spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3169{
3170 struct spoe_context *ctx = filter->ctx;
3171 int ret = 1;
3172
3173 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3174 " - ctx-flags=0x%08x\n",
3175 (int)now.tv_sec, (int)now.tv_usec,
3176 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3177 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3178
Christopher Fauletb067b062017-01-04 16:39:11 +01003179 if (ctx->state == SPOE_CTX_ST_NONE)
3180 goto out;
3181
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003182 if (!(chn->flags & CF_ISRESP)) {
3183 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3184 chn->analysers |= AN_REQ_INSPECT_FE;
3185 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3186 chn->analysers |= AN_REQ_INSPECT_BE;
3187
3188 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3189 goto out;
3190
3191 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003192 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003193 if (!ret)
3194 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003195 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3196 }
3197 else {
3198 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3199 chn->analysers |= AN_RES_INSPECT;
3200
3201 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3202 goto out;
3203
Christopher Faulet8ef75252017-02-20 22:56:03 +01003204 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003205 if (!ret) {
3206 channel_dont_read(chn);
3207 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003208 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003209 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003210 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 }
3212
3213 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003214 return ret;
3215}
3216
3217/* Called before a processing happens on a given channel */
3218static int
3219spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3220 struct channel *chn, unsigned an_bit)
3221{
3222 struct spoe_context *ctx = filter->ctx;
3223 int ret = 1;
3224
3225 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3226 " - ctx-flags=0x%08x - ana=0x%08x\n",
3227 (int)now.tv_sec, (int)now.tv_usec,
3228 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3229 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3230 ctx->flags, an_bit);
3231
Christopher Fauletb067b062017-01-04 16:39:11 +01003232 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003233 goto out;
3234
3235 switch (an_bit) {
3236 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003237 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003238 break;
3239 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003240 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003241 break;
3242 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003243 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003244 break;
3245 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003246 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003247 break;
3248 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003249 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 break;
3251 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003252 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003253 break;
3254 }
3255
3256 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003257 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003258 channel_dont_read(chn);
3259 channel_dont_close(chn);
3260 }
3261 return ret;
3262}
3263
3264/* Called when the filtering on the channel ends. */
3265static int
3266spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3267{
3268 struct spoe_context *ctx = filter->ctx;
3269
3270 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3271 " - ctx-flags=0x%08x\n",
3272 (int)now.tv_sec, (int)now.tv_usec,
3273 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3274 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3275
3276 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003277 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003278 }
3279
3280 return 1;
3281}
3282
3283/********************************************************************
3284 * Functions that manage the filter initialization
3285 ********************************************************************/
3286struct flt_ops spoe_ops = {
3287 /* Manage SPOE filter, called for each filter declaration */
3288 .init = spoe_init,
3289 .deinit = spoe_deinit,
3290 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003291 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003292
3293 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003294 .attach = spoe_start,
3295 .detach = spoe_stop,
3296 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003297
3298 /* Handle channels activity */
3299 .channel_start_analyze = spoe_start_analyze,
3300 .channel_pre_analyze = spoe_chn_pre_analyze,
3301 .channel_end_analyze = spoe_end_analyze,
3302};
3303
3304
3305static int
3306cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3307{
3308 const char *err;
3309 int i, err_code = 0;
3310
3311 if ((cfg_scope == NULL && curengine != NULL) ||
3312 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003313 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003314 goto out;
3315
3316 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3317 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003318 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3319 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003320 err_code |= ERR_ALERT | ERR_ABORT;
3321 goto out;
3322 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003323 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3324 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003325 goto out;
3326 }
3327
3328 err = invalid_char(args[1]);
3329 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003330 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3331 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003332 err_code |= ERR_ALERT | ERR_ABORT;
3333 goto out;
3334 }
3335
3336 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003337 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3338 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003339 err_code |= ERR_ALERT | ERR_ABORT;
3340 goto out;
3341 }
3342 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003343 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003344 err_code |= ERR_ALERT | ERR_ABORT;
3345 goto out;
3346 }
3347
3348 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003349
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003350 curagent->conf.file = strdup(file);
3351 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003352
3353 curagent->timeout.hello = TICK_ETERNITY;
3354 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003355 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003356
Christopher Fauleta1cda022016-12-21 08:58:06 +01003357 curagent->var_pfx = NULL;
3358 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003359 curagent->var_t_process = NULL;
3360 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003361 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003362 curagent->cps_max = 0;
3363 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003364 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003365 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003366
3367 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003368 LIST_INIT(&curagent->events[i]);
3369 LIST_INIT(&curagent->groups);
3370 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003371 }
3372 else if (!strcmp(args[0], "use-backend")) {
3373 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003374 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3375 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003376 err_code |= ERR_ALERT | ERR_FATAL;
3377 goto out;
3378 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003379 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003380 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381 free(curagent->b.name);
3382 curagent->b.name = strdup(args[1]);
3383 }
3384 else if (!strcmp(args[0], "messages")) {
3385 int cur_arg = 1;
3386 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003387 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003388
Christopher Faulet11610f32017-09-21 10:23:10 +02003389 list_for_each_entry(ph, &curmphs, list) {
3390 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003391 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3392 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003393 err_code |= ERR_ALERT | ERR_FATAL;
3394 goto out;
3395 }
3396 }
3397
Christopher Faulet11610f32017-09-21 10:23:10 +02003398 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003399 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003400 err_code |= ERR_ALERT | ERR_ABORT;
3401 goto out;
3402 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003403 ph->id = strdup(args[cur_arg]);
3404 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003405 cur_arg++;
3406 }
3407 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003408 else if (!strcmp(args[0], "groups")) {
3409 int cur_arg = 1;
3410 while (*args[cur_arg]) {
3411 struct spoe_placeholder *ph = NULL;
3412
3413 list_for_each_entry(ph, &curgphs, list) {
3414 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3416 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003417 err_code |= ERR_ALERT | ERR_FATAL;
3418 goto out;
3419 }
3420 }
3421
3422 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003423 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003424 err_code |= ERR_ALERT | ERR_ABORT;
3425 goto out;
3426 }
3427 ph->id = strdup(args[cur_arg]);
3428 LIST_ADDQ(&curgphs, &ph->list);
3429 cur_arg++;
3430 }
3431 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003432 else if (!strcmp(args[0], "timeout")) {
3433 unsigned int *tv = NULL;
3434 const char *res;
3435 unsigned timeout;
3436
3437 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003438 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3439 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003440 err_code |= ERR_ALERT | ERR_FATAL;
3441 goto out;
3442 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003443 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3444 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003445 if (!strcmp(args[1], "hello"))
3446 tv = &curagent->timeout.hello;
3447 else if (!strcmp(args[1], "idle"))
3448 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003449 else if (!strcmp(args[1], "processing"))
3450 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003451 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003452 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3453 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003454 err_code |= ERR_ALERT | ERR_FATAL;
3455 goto out;
3456 }
3457 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003458 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3459 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003460 err_code |= ERR_ALERT | ERR_FATAL;
3461 goto out;
3462 }
3463 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003464 if (res == PARSE_TIME_OVER) {
3465 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3466 file, linenum, args[2], args[0], args[1]);
3467 err_code |= ERR_ALERT | ERR_FATAL;
3468 goto out;
3469 }
3470 else if (res == PARSE_TIME_UNDER) {
3471 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3472 file, linenum, args[2], args[0], args[1]);
3473 err_code |= ERR_ALERT | ERR_FATAL;
3474 goto out;
3475 }
3476 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003477 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3478 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003479 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003480 goto out;
3481 }
3482 *tv = MS_TO_TICKS(timeout);
3483 }
3484 else if (!strcmp(args[0], "option")) {
3485 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003486 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3487 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003488 err_code |= ERR_ALERT | ERR_FATAL;
3489 goto out;
3490 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003491
Christopher Faulet305c6072017-02-23 16:17:53 +01003492 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003493 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003494 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003495 if (kwm == 1)
3496 curagent->flags &= ~SPOE_FL_PIPELINING;
3497 else
3498 curagent->flags |= SPOE_FL_PIPELINING;
3499 goto out;
3500 }
3501 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003502 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003503 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003504 if (kwm == 1)
3505 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003506 else
3507 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003508 goto out;
3509 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003510 else if (!strcmp(args[1], "send-frag-payload")) {
3511 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3512 goto out;
3513 if (kwm == 1)
3514 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3515 else
3516 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3517 goto out;
3518 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003519 else if (!strcmp(args[1], "dontlog-normal")) {
3520 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3521 goto out;
3522 if (kwm == 1)
3523 curpxopts2 &= ~PR_O2_NOLOGNORM;
3524 else
3525 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003526 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003527 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003528
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003529 /* Following options does not support negation */
3530 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003531 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3532 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003533 err_code |= ERR_ALERT | ERR_FATAL;
3534 goto out;
3535 }
3536
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003537 if (!strcmp(args[1], "var-prefix")) {
3538 char *tmp;
3539
3540 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003541 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3542 file, linenum, args[0],
3543 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003544 err_code |= ERR_ALERT | ERR_FATAL;
3545 goto out;
3546 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003547 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3548 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003549 tmp = args[2];
3550 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003551 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003552 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003553 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003554 err_code |= ERR_ALERT | ERR_FATAL;
3555 goto out;
3556 }
3557 tmp++;
3558 }
3559 curagent->var_pfx = strdup(args[2]);
3560 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003561 else if (!strcmp(args[1], "force-set-var")) {
3562 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3563 goto out;
3564 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3565 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003566 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003567 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003568 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003569 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3570 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003571 else if (!strcmp(args[1], "set-on-error")) {
3572 char *tmp;
3573
3574 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003575 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3576 file, linenum, args[0],
3577 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003578 err_code |= ERR_ALERT | ERR_FATAL;
3579 goto out;
3580 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003581 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3582 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003583 tmp = args[2];
3584 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003585 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003586 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003587 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003588 err_code |= ERR_ALERT | ERR_FATAL;
3589 goto out;
3590 }
3591 tmp++;
3592 }
3593 curagent->var_on_error = strdup(args[2]);
3594 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003595 else if (!strcmp(args[1], "set-process-time")) {
3596 char *tmp;
3597
3598 if (!*args[2]) {
3599 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3600 file, linenum, args[0],
3601 args[1]);
3602 err_code |= ERR_ALERT | ERR_FATAL;
3603 goto out;
3604 }
3605 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3606 goto out;
3607 tmp = args[2];
3608 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003609 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003610 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003611 file, linenum, args[0], args[1]);
3612 err_code |= ERR_ALERT | ERR_FATAL;
3613 goto out;
3614 }
3615 tmp++;
3616 }
3617 curagent->var_t_process = strdup(args[2]);
3618 }
3619 else if (!strcmp(args[1], "set-total-time")) {
3620 char *tmp;
3621
3622 if (!*args[2]) {
3623 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3624 file, linenum, args[0],
3625 args[1]);
3626 err_code |= ERR_ALERT | ERR_FATAL;
3627 goto out;
3628 }
3629 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3630 goto out;
3631 tmp = args[2];
3632 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003633 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003634 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003635 file, linenum, args[0], args[1]);
3636 err_code |= ERR_ALERT | ERR_FATAL;
3637 goto out;
3638 }
3639 tmp++;
3640 }
3641 curagent->var_t_total = strdup(args[2]);
3642 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003643 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003644 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3645 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003646 err_code |= ERR_ALERT | ERR_FATAL;
3647 goto out;
3648 }
Christopher Faulet48026722016-11-16 15:01:12 +01003649 }
3650 else if (!strcmp(args[0], "maxconnrate")) {
3651 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003652 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3653 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003654 err_code |= ERR_ALERT | ERR_FATAL;
3655 goto out;
3656 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003657 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003658 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003659 curagent->cps_max = atol(args[1]);
3660 }
3661 else if (!strcmp(args[0], "maxerrrate")) {
3662 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003663 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3664 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003665 err_code |= ERR_ALERT | ERR_FATAL;
3666 goto out;
3667 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003668 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003669 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003670 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003671 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003672 else if (!strcmp(args[0], "max-frame-size")) {
3673 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003674 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3675 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003676 err_code |= ERR_ALERT | ERR_FATAL;
3677 goto out;
3678 }
3679 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3680 goto out;
3681 curagent->max_frame_size = atol(args[1]);
3682 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3683 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003684 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3685 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003686 err_code |= ERR_ALERT | ERR_FATAL;
3687 goto out;
3688 }
3689 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003690 else if (!strcmp(args[0], "max-waiting-frames")) {
3691 if (!*args[1]) {
3692 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3693 file, linenum, args[0]);
3694 err_code |= ERR_ALERT | ERR_FATAL;
3695 goto out;
3696 }
3697 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3698 goto out;
3699 curagent->max_fpa = atol(args[1]);
3700 if (curagent->max_fpa < 1) {
3701 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3702 file, linenum, args[0]);
3703 err_code |= ERR_ALERT | ERR_FATAL;
3704 goto out;
3705 }
3706 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003707 else if (!strcmp(args[0], "register-var-names")) {
3708 int cur_arg;
3709
3710 if (!*args[1]) {
3711 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3712 file, linenum, args[0]);
3713 err_code |= ERR_ALERT | ERR_FATAL;
3714 goto out;
3715 }
3716 cur_arg = 1;
3717 while (*args[cur_arg]) {
3718 struct spoe_var_placeholder *vph;
3719
3720 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3721 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3722 err_code |= ERR_ALERT | ERR_ABORT;
3723 goto out;
3724 }
3725 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003726 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003727 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3728 err_code |= ERR_ALERT | ERR_ABORT;
3729 goto out;
3730 }
3731 LIST_ADDQ(&curvars, &vph->list);
3732 cur_arg++;
3733 }
3734 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003735 else if (!strcmp(args[0], "log")) {
3736 char *errmsg = NULL;
3737
3738 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3739 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3740 err_code |= ERR_ALERT | ERR_FATAL;
3741 goto out;
3742 }
3743 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003744 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003745 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3746 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003747 err_code |= ERR_ALERT | ERR_FATAL;
3748 goto out;
3749 }
3750 out:
3751 return err_code;
3752}
Christopher Faulet11610f32017-09-21 10:23:10 +02003753static int
3754cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3755{
3756 struct spoe_group *grp;
3757 const char *err;
3758 int err_code = 0;
3759
3760 if ((cfg_scope == NULL && curengine != NULL) ||
3761 (cfg_scope != NULL && curengine == NULL) ||
3762 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3763 goto out;
3764
3765 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3766 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003767 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3768 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003769 err_code |= ERR_ALERT | ERR_ABORT;
3770 goto out;
3771 }
3772 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3773 err_code |= ERR_ABORT;
3774 goto out;
3775 }
3776
3777 err = invalid_char(args[1]);
3778 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003779 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3780 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003781 err_code |= ERR_ALERT | ERR_ABORT;
3782 goto out;
3783 }
3784
3785 list_for_each_entry(grp, &curgrps, list) {
3786 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003787 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3788 " name as another one declared at %s:%d.\n",
3789 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003790 err_code |= ERR_ALERT | ERR_FATAL;
3791 goto out;
3792 }
3793 }
3794
3795 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003796 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003797 err_code |= ERR_ALERT | ERR_ABORT;
3798 goto out;
3799 }
3800
3801 curgrp->id = strdup(args[1]);
3802 curgrp->conf.file = strdup(file);
3803 curgrp->conf.line = linenum;
3804 LIST_INIT(&curgrp->phs);
3805 LIST_INIT(&curgrp->messages);
3806 LIST_ADDQ(&curgrps, &curgrp->list);
3807 }
3808 else if (!strcmp(args[0], "messages")) {
3809 int cur_arg = 1;
3810 while (*args[cur_arg]) {
3811 struct spoe_placeholder *ph = NULL;
3812
3813 list_for_each_entry(ph, &curgrp->phs, list) {
3814 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003815 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3816 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003817 err_code |= ERR_ALERT | ERR_FATAL;
3818 goto out;
3819 }
3820 }
3821
3822 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003823 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003824 err_code |= ERR_ALERT | ERR_ABORT;
3825 goto out;
3826 }
3827 ph->id = strdup(args[cur_arg]);
3828 LIST_ADDQ(&curgrp->phs, &ph->list);
3829 cur_arg++;
3830 }
3831 }
3832 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003833 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3834 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003835 err_code |= ERR_ALERT | ERR_FATAL;
3836 goto out;
3837 }
3838 out:
3839 return err_code;
3840}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003841
3842static int
3843cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3844{
3845 struct spoe_message *msg;
3846 struct spoe_arg *arg;
3847 const char *err;
3848 char *errmsg = NULL;
3849 int err_code = 0;
3850
3851 if ((cfg_scope == NULL && curengine != NULL) ||
3852 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003853 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003854 goto out;
3855
3856 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3857 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003858 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3859 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003860 err_code |= ERR_ALERT | ERR_ABORT;
3861 goto out;
3862 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003863 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3864 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003865 goto out;
3866 }
3867
3868 err = invalid_char(args[1]);
3869 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003870 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3871 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003872 err_code |= ERR_ALERT | ERR_ABORT;
3873 goto out;
3874 }
3875
3876 list_for_each_entry(msg, &curmsgs, list) {
3877 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003878 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3879 " name as another one declared at %s:%d.\n",
3880 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003881 err_code |= ERR_ALERT | ERR_FATAL;
3882 goto out;
3883 }
3884 }
3885
3886 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003887 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003888 err_code |= ERR_ALERT | ERR_ABORT;
3889 goto out;
3890 }
3891
3892 curmsg->id = strdup(args[1]);
3893 curmsg->id_len = strlen(curmsg->id);
3894 curmsg->event = SPOE_EV_NONE;
3895 curmsg->conf.file = strdup(file);
3896 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003897 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003898 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003899 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003900 LIST_INIT(&curmsg->by_evt);
3901 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003902 LIST_ADDQ(&curmsgs, &curmsg->list);
3903 }
3904 else if (!strcmp(args[0], "args")) {
3905 int cur_arg = 1;
3906
3907 curproxy->conf.args.ctx = ARGC_SPOE;
3908 curproxy->conf.args.file = file;
3909 curproxy->conf.args.line = linenum;
3910 while (*args[cur_arg]) {
3911 char *delim = strchr(args[cur_arg], '=');
3912 int idx = 0;
3913
3914 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003915 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003916 err_code |= ERR_ALERT | ERR_ABORT;
3917 goto out;
3918 }
3919
3920 if (!delim) {
3921 arg->name = NULL;
3922 arg->name_len = 0;
3923 delim = args[cur_arg];
3924 }
3925 else {
3926 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3927 arg->name_len = delim - args[cur_arg];
3928 delim++;
3929 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003930 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3931 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003932 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003933 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003934 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003935 err_code |= ERR_ALERT | ERR_FATAL;
3936 free(arg->name);
3937 free(arg);
3938 goto out;
3939 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003940 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003941 LIST_ADDQ(&curmsg->args, &arg->list);
3942 cur_arg++;
3943 }
3944 curproxy->conf.args.file = NULL;
3945 curproxy->conf.args.line = 0;
3946 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003947 else if (!strcmp(args[0], "acl")) {
3948 err = invalid_char(args[1]);
3949 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003950 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3951 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003952 err_code |= ERR_ALERT | ERR_FATAL;
3953 goto out;
3954 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003955 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01003956 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003957 "logical disjunction within a condition.\n",
3958 file, linenum, args[1]);
3959 err_code |= ERR_ALERT | ERR_FATAL;
3960 goto out;
3961 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003962 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003963 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3964 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003965 err_code |= ERR_ALERT | ERR_FATAL;
3966 goto out;
3967 }
3968 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003969 else if (!strcmp(args[0], "event")) {
3970 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003971 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003972 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003973 goto out;
3974 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003975 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3976 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003977
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003978 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3979 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3980 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3981 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3982
3983 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3984 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3985 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3986 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3987 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3988 curmsg->event = SPOE_EV_ON_TCP_RSP;
3989
3990 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3991 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3992 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3993 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3994 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3995 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3996 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08003997 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003998 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003999 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004000 goto out;
4001 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004002
4003 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4004 struct acl_cond *cond;
4005
4006 cond = build_acl_cond(file, linenum, &curmsg->acls,
4007 curproxy, (const char **)args+2,
4008 &errmsg);
4009 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004010 ha_alert("parsing [%s:%d] : error detected while "
4011 "parsing an 'event %s' condition : %s.\n",
4012 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004013 err_code |= ERR_ALERT | ERR_FATAL;
4014 goto out;
4015 }
4016 curmsg->cond = cond;
4017 }
4018 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004019 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4020 "or 'unless' followed by a condition but found '%s'.\n",
4021 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004022 err_code |= ERR_ALERT | ERR_FATAL;
4023 goto out;
4024 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004025 }
4026 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004027 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4028 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004029 err_code |= ERR_ALERT | ERR_FATAL;
4030 goto out;
4031 }
4032 out:
4033 free(errmsg);
4034 return err_code;
4035}
4036
4037/* Return -1 on error, else 0 */
4038static int
4039parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4040 struct flt_conf *fconf, char **err, void *private)
4041{
4042 struct list backup_sections;
4043 struct spoe_config *conf;
4044 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004045 struct spoe_group *grp, *grpback;
4046 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004047 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004048 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004049 char *file = NULL, *engine = NULL;
4050 int ret, pos = *cur_arg + 1;
4051
Christopher Faulet84c844e2018-03-23 14:37:14 +01004052 LIST_INIT(&curmsgs);
4053 LIST_INIT(&curgrps);
4054 LIST_INIT(&curmphs);
4055 LIST_INIT(&curgphs);
4056 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004057 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004058 curpxopts = 0;
4059 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004060
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004061 conf = calloc(1, sizeof(*conf));
4062 if (conf == NULL) {
4063 memprintf(err, "%s: out of memory", args[*cur_arg]);
4064 goto error;
4065 }
4066 conf->proxy = px;
4067
4068 while (*args[pos]) {
4069 if (!strcmp(args[pos], "config")) {
4070 if (!*args[pos+1]) {
4071 memprintf(err, "'%s' : '%s' option without value",
4072 args[*cur_arg], args[pos]);
4073 goto error;
4074 }
4075 file = args[pos+1];
4076 pos += 2;
4077 }
4078 else if (!strcmp(args[pos], "engine")) {
4079 if (!*args[pos+1]) {
4080 memprintf(err, "'%s' : '%s' option without value",
4081 args[*cur_arg], args[pos]);
4082 goto error;
4083 }
4084 engine = args[pos+1];
4085 pos += 2;
4086 }
4087 else {
4088 memprintf(err, "unknown keyword '%s'", args[pos]);
4089 goto error;
4090 }
4091 }
4092 if (file == NULL) {
4093 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4094 goto error;
4095 }
4096
4097 /* backup sections and register SPOE sections */
4098 LIST_INIT(&backup_sections);
4099 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004100 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4101 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004102 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004103
4104 /* Parse SPOE filter configuration file */
4105 curengine = engine;
4106 curproxy = px;
4107 curagent = NULL;
4108 curmsg = NULL;
4109 ret = readcfgfile(file);
4110 curproxy = NULL;
4111
4112 /* unregister SPOE sections and restore previous sections */
4113 cfg_unregister_sections();
4114 cfg_restore_sections(&backup_sections);
4115
4116 if (ret == -1) {
4117 memprintf(err, "Could not open configuration file %s : %s",
4118 file, strerror(errno));
4119 goto error;
4120 }
4121 if (ret & (ERR_ABORT|ERR_FATAL)) {
4122 memprintf(err, "Error(s) found in configuration file %s", file);
4123 goto error;
4124 }
4125
4126 /* Check SPOE agent */
4127 if (curagent == NULL) {
4128 memprintf(err, "No SPOE agent found in file %s", file);
4129 goto error;
4130 }
4131 if (curagent->b.name == NULL) {
4132 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4133 curagent->id, curagent->conf.file, curagent->conf.line);
4134 goto error;
4135 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004136 if (curagent->timeout.hello == TICK_ETERNITY ||
4137 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004138 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004139 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4140 " | While not properly invalid, you will certainly encounter various problems\n"
4141 " | with such a configuration. To fix this, please ensure that all following\n"
4142 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4143 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004144 }
4145 if (curagent->var_pfx == NULL) {
4146 char *tmp = curagent->id;
4147
4148 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004149 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004150 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4151 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4152 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4153 goto error;
4154 }
4155 tmp++;
4156 }
4157 curagent->var_pfx = strdup(curagent->id);
4158 }
4159
Christopher Fauletb7426d12018-03-21 14:12:17 +01004160 if (curagent->var_on_error) {
4161 struct arg arg;
4162
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004163 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004164 curagent->var_pfx, curagent->var_on_error);
4165
4166 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004167 arg.data.str.area = trash.area;
4168 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004169 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004170 if (!vars_check_arg(&arg, err)) {
4171 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4172 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4173 goto error;
4174 }
4175 }
4176
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004177 if (curagent->var_t_process) {
4178 struct arg arg;
4179
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004180 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004181 curagent->var_pfx, curagent->var_t_process);
4182
4183 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004184 arg.data.str.area = trash.area;
4185 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004186 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004187 if (!vars_check_arg(&arg, err)) {
4188 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4189 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4190 goto error;
4191 }
4192 }
4193
4194 if (curagent->var_t_total) {
4195 struct arg arg;
4196
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004197 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004198 curagent->var_pfx, curagent->var_t_total);
4199
4200 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004201 arg.data.str.area = trash.area;
4202 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004203 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004204 if (!vars_check_arg(&arg, err)) {
4205 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4206 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4207 goto error;
4208 }
4209 }
4210
Christopher Faulet11610f32017-09-21 10:23:10 +02004211 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004212 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4213 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004214 goto finish;
4215 }
4216
Christopher Faulet11610f32017-09-21 10:23:10 +02004217 /* Replace placeholders by the corresponding messages for the SPOE
4218 * agent */
4219 list_for_each_entry(ph, &curmphs, list) {
4220 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004221 struct spoe_arg *arg;
4222 unsigned int where;
4223
Christopher Faulet11610f32017-09-21 10:23:10 +02004224 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004225 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4226 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4227 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4228 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4229 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4230 }
4231 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4232 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4233 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004234 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4235 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004236 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004237 }
4238 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004239 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4240 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004241 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004242 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004243
4244 where = 0;
4245 switch (msg->event) {
4246 case SPOE_EV_ON_CLIENT_SESS:
4247 where |= SMP_VAL_FE_CON_ACC;
4248 break;
4249
4250 case SPOE_EV_ON_TCP_REQ_FE:
4251 where |= SMP_VAL_FE_REQ_CNT;
4252 break;
4253
4254 case SPOE_EV_ON_HTTP_REQ_FE:
4255 where |= SMP_VAL_FE_HRQ_HDR;
4256 break;
4257
4258 case SPOE_EV_ON_TCP_REQ_BE:
4259 if (px->cap & PR_CAP_FE)
4260 where |= SMP_VAL_FE_REQ_CNT;
4261 if (px->cap & PR_CAP_BE)
4262 where |= SMP_VAL_BE_REQ_CNT;
4263 break;
4264
4265 case SPOE_EV_ON_HTTP_REQ_BE:
4266 if (px->cap & PR_CAP_FE)
4267 where |= SMP_VAL_FE_HRQ_HDR;
4268 if (px->cap & PR_CAP_BE)
4269 where |= SMP_VAL_BE_HRQ_HDR;
4270 break;
4271
4272 case SPOE_EV_ON_SERVER_SESS:
4273 where |= SMP_VAL_BE_SRV_CON;
4274 break;
4275
4276 case SPOE_EV_ON_TCP_RSP:
4277 if (px->cap & PR_CAP_FE)
4278 where |= SMP_VAL_FE_RES_CNT;
4279 if (px->cap & PR_CAP_BE)
4280 where |= SMP_VAL_BE_RES_CNT;
4281 break;
4282
4283 case SPOE_EV_ON_HTTP_RSP:
4284 if (px->cap & PR_CAP_FE)
4285 where |= SMP_VAL_FE_HRS_HDR;
4286 if (px->cap & PR_CAP_BE)
4287 where |= SMP_VAL_BE_HRS_HDR;
4288 break;
4289
4290 default:
4291 break;
4292 }
4293
4294 list_for_each_entry(arg, &msg->args, list) {
4295 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004296 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004297 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004298 "none of which is available here ('%s')",
4299 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004300 sample_ckp_names(arg->expr->fetch->use),
4301 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004302 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004303 }
4304 }
4305
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004306 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004307 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4308 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004309 }
4310 }
4311 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004312 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004313 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004314 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004315 continue;
4316 }
4317
Christopher Faulet11610f32017-09-21 10:23:10 +02004318 /* Replace placeholders by the corresponding groups for the SPOE
4319 * agent */
4320 list_for_each_entry(ph, &curgphs, list) {
4321 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4322 if (!strcmp(grp->id, ph->id)) {
4323 grp->agent = curagent;
4324 LIST_DEL(&grp->list);
4325 LIST_ADDQ(&curagent->groups, &grp->list);
4326 goto next_aph;
4327 }
4328 }
4329 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4330 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4331 goto error;
4332 next_aph:
4333 continue;
4334 }
4335
4336 /* Replace placeholders by the corresponding message for each SPOE
4337 * group of the SPOE agent */
4338 list_for_each_entry(grp, &curagent->groups, list) {
4339 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4340 list_for_each_entry(msg, &curmsgs, list) {
4341 if (!strcmp(msg->id, ph->id)) {
4342 if (msg->group != NULL) {
4343 memprintf(err, "SPOE message '%s' already belongs to "
4344 "the SPOE group '%s' declare at %s:%d",
4345 msg->id, msg->group->id,
4346 msg->group->conf.file,
4347 msg->group->conf.line);
4348 goto error;
4349 }
4350
4351 /* Scope for arguments are not checked for now. We will check
4352 * them only if a rule use the corresponding SPOE group. */
4353 msg->agent = curagent;
4354 msg->group = grp;
4355 LIST_DEL(&ph->list);
4356 LIST_ADDQ(&grp->messages, &msg->by_grp);
4357 goto next_mph_grp;
4358 }
4359 }
4360 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4361 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4362 goto error;
4363 next_mph_grp:
4364 continue;
4365 }
4366 }
4367
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004368 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004369 /* move curmsgs to the agent message list */
4370 curmsgs.n->p = &curagent->messages;
4371 curmsgs.p->n = &curagent->messages;
4372 curagent->messages = curmsgs;
4373 LIST_INIT(&curmsgs);
4374
Christopher Faulet7ee86672017-09-19 11:08:28 +02004375 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004376 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004377
4378 /* Start agent's proxy initialization here. It will be finished during
4379 * the filter init. */
4380 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4381 init_new_proxy(&conf->agent_fe);
4382 conf->agent_fe.id = conf->agent->id;
4383 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004384 conf->agent_fe.options |= curpxopts;
4385 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004386
4387 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4388 LIST_DEL(&logsrv->list);
4389 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4390 }
4391
Christopher Faulet11610f32017-09-21 10:23:10 +02004392 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4393 LIST_DEL(&ph->list);
4394 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004395 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004396 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4397 LIST_DEL(&ph->list);
4398 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004399 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004400 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4401 struct arg arg;
4402
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004403 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004404 curagent->var_pfx, vph->name);
4405
4406 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004407 arg.data.str.area = trash.area;
4408 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004409 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004410 if (!vars_check_arg(&arg, err)) {
4411 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4412 curagent->id, curagent->var_pfx, vph->name, *err);
4413 goto error;
4414 }
4415
4416 LIST_DEL(&vph->list);
4417 free(vph->name);
4418 free(vph);
4419 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004420 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4421 LIST_DEL(&grp->list);
4422 spoe_release_group(grp);
4423 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004424 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004425 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004426 fconf->ops = &spoe_ops;
4427 fconf->conf = conf;
4428 return 0;
4429
4430 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004431 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004432 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4433 LIST_DEL(&ph->list);
4434 spoe_release_placeholder(ph);
4435 }
4436 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4437 LIST_DEL(&ph->list);
4438 spoe_release_placeholder(ph);
4439 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004440 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4441 LIST_DEL(&vph->list);
4442 free(vph->name);
4443 free(vph);
4444 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004445 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4446 LIST_DEL(&grp->list);
4447 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004448 }
4449 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4450 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004451 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004452 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004453 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4454 LIST_DEL(&logsrv->list);
4455 free(logsrv);
4456 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004457 free(conf);
4458 return -1;
4459}
4460
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004461/* Send message of a SPOE group. This is the action_ptr callback of a rule
4462 * associated to a "send-spoe-group" action.
4463 *
Christopher Faulet13403762019-12-13 09:01:57 +01004464 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4465 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004466static enum act_return
4467spoe_send_group(struct act_rule *rule, struct proxy *px,
4468 struct session *sess, struct stream *s, int flags)
4469{
4470 struct filter *filter;
4471 struct spoe_agent *agent = NULL;
4472 struct spoe_group *group = NULL;
4473 struct spoe_context *ctx = NULL;
4474 int ret, dir;
4475
4476 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4477 if (filter->config == rule->arg.act.p[0]) {
4478 agent = rule->arg.act.p[2];
4479 group = rule->arg.act.p[3];
4480 ctx = filter->ctx;
4481 break;
4482 }
4483 }
4484 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004485 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004486 if (ctx->state == SPOE_CTX_ST_NONE)
4487 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004488
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004489 switch (rule->from) {
4490 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4491 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4492 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4493 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4494 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4495 default:
4496 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4497 " - internal error while execute spoe-send-group\n",
4498 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4499 __FUNCTION__, s);
4500 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4501 agent->id);
4502 return ACT_RET_CONT;
4503 }
4504
4505 ret = spoe_process_group(s, ctx, group, dir);
4506 if (ret == 1)
4507 return ACT_RET_CONT;
4508 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004509 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004510 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4511 " - failed to process group '%s': interrupted by caller\n",
4512 (int)now.tv_sec, (int)now.tv_usec,
4513 agent->id, __FUNCTION__, s, group->id);
4514 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004515 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004516 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004517 return ACT_RET_CONT;
4518 }
4519 return ACT_RET_YIELD;
4520 }
4521 else
Christopher Faulet13403762019-12-13 09:01:57 +01004522 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004523}
4524
4525/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4526 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4527 * action should be:
4528 *
4529 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4530 *
4531 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4532 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4533 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4534 * group.
4535 *
4536 * The function returns 1 in success case, otherwise, it returns 0 and err is
4537 * filled.
4538 */
4539static int
4540check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4541{
4542 struct flt_conf *fconf;
4543 struct spoe_config *conf;
4544 struct spoe_agent *agent = NULL;
4545 struct spoe_group *group;
4546 struct spoe_message *msg;
4547 char *engine_id = rule->arg.act.p[0];
4548 char *group_id = rule->arg.act.p[1];
4549 unsigned int where = 0;
4550
4551 switch (rule->from) {
4552 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4553 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4554 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4555 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4556 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4557 default:
4558 memprintf(err,
4559 "internal error, unexpected rule->from=%d, please report this bug!",
4560 rule->from);
4561 goto error;
4562 }
4563
4564 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4565 * <px> */
4566 list_for_each_entry(fconf, &px->filter_configs, list) {
4567 conf = fconf->conf;
4568
4569 /* This is not an SPOE filter */
4570 if (fconf->id != spoe_filter_id)
4571 continue;
4572
4573 /* This is the good engine */
4574 if (!strcmp(conf->id, engine_id)) {
4575 agent = conf->agent;
4576 break;
4577 }
4578 }
4579 if (agent == NULL) {
4580 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4581 engine_id, group_id);
4582 goto error;
4583 }
4584
4585 /* Try to find the right group */
4586 list_for_each_entry(group, &agent->groups, list) {
4587 /* This is the good group */
4588 if (!strcmp(group->id, group_id))
4589 break;
4590 }
4591 if (&group->list == &agent->groups) {
4592 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4593 group_id, engine_id);
4594 goto error;
4595 }
4596
4597 /* Ok, we found the group, we need to check messages and their
4598 * arguments */
4599 list_for_each_entry(msg, &group->messages, by_grp) {
4600 struct spoe_arg *arg;
4601
4602 list_for_each_entry(arg, &msg->args, list) {
4603 if (!(arg->expr->fetch->val & where)) {
4604 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4605 "some args extract information from '%s',"
4606 "none of which is available here ('%s')",
4607 msg->id, group->id, msg->conf.file, msg->conf.line,
4608 sample_ckp_names(arg->expr->fetch->use),
4609 sample_ckp_names(where));
4610 goto error;
4611 }
4612 }
4613 }
4614
4615 free(engine_id);
4616 free(group_id);
4617 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4618 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4619 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4620 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4621 return 1;
4622
4623 error:
4624 free(engine_id);
4625 free(group_id);
4626 return 0;
4627}
4628
4629/* Parse 'send-spoe-group' action following the format:
4630 *
4631 * ... send-spoe-group <engine-id> <group-id>
4632 *
4633 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4634 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4635 * ids are saved and used later, when the rule will be checked.
4636 */
4637static enum act_parse_ret
4638parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4639 struct act_rule *rule, char **err)
4640{
4641 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4642 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4643 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4644 return ACT_RET_PRS_ERR;
4645 }
4646 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4647 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4648
4649 (*orig_arg) += 2;
4650
4651 rule->action = ACT_CUSTOM;
4652 rule->action_ptr = spoe_send_group;
4653 rule->check_ptr = check_send_spoe_group;
4654 return ACT_RET_PRS_OK;
4655}
4656
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004657
4658/* Declare the filter parser for "spoe" keyword */
4659static struct flt_kw_list flt_kws = { "SPOE", { }, {
4660 { "spoe", parse_spoe_flt, NULL },
4661 { NULL, NULL, NULL },
4662 }
4663};
4664
Willy Tarreau0108d902018-11-25 19:14:37 +01004665INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4666
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004667/* Delcate the action parser for "spoe-action" keyword */
4668static struct action_kw_list tcp_req_action_kws = { { }, {
4669 { "send-spoe-group", parse_send_spoe_group },
4670 { /* END */ },
4671 }
4672};
Willy Tarreau0108d902018-11-25 19:14:37 +01004673
4674INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4675
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004676static struct action_kw_list tcp_res_action_kws = { { }, {
4677 { "send-spoe-group", parse_send_spoe_group },
4678 { /* END */ },
4679 }
4680};
Willy Tarreau0108d902018-11-25 19:14:37 +01004681
4682INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4683
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004684static struct action_kw_list http_req_action_kws = { { }, {
4685 { "send-spoe-group", parse_send_spoe_group },
4686 { /* END */ },
4687 }
4688};
Willy Tarreau0108d902018-11-25 19:14:37 +01004689
4690INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4691
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004692static struct action_kw_list http_res_action_kws = { { }, {
4693 { "send-spoe-group", parse_send_spoe_group },
4694 { /* END */ },
4695 }
4696};
4697
Willy Tarreau0108d902018-11-25 19:14:37 +01004698INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);