blob: 2accd8b21a6964d8a1d110eaea9499d9acc74cb9 [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Willy Tarreaudcc048a2020-06-04 19:11:43 +020015#include <haproxy/acl.h>
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreau4c7e4b72020-05-27 12:58:42 +020017#include <haproxy/api.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020018#include <haproxy/arg.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020019#include <haproxy/cfgparse.h>
Willy Tarreauc7babd82020-06-04 21:29:29 +020020#include <haproxy/filters.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020021#include <haproxy/freq_ctr.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020023#include <haproxy/global.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020024#include <haproxy/http_rules.h>
Willy Tarreauaeed4a82020-06-04 22:01:04 +020025#include <haproxy/log.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020026#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020027#include <haproxy/proxy.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/sample.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020029#include <haproxy/session.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020030#include <haproxy/signal.h>
Christopher Faulet0dccf892021-02-19 10:56:41 +010031#include <haproxy/sink.h>
Willy Tarreau6c58ab02020-06-04 22:35:49 +020032#include <haproxy/spoe.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020033#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020034#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020035#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020036#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020037#include <haproxy/thread.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020038#include <haproxy/time.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020039#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020040
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041
42#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
43#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010044#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045#else
46#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010047#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020048#endif
49
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010050/* Reserved 4 bytes to the frame size. So a frame and its size can be written
51 * together in a buffer */
52#define MAX_FRAME_SIZE global.tune.bufsize - 4
53
54/* The minimum size for a frame */
55#define MIN_FRAME_SIZE 256
56
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010057/* Reserved for the metadata and the frame type.
58 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010059#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020060
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010061/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010062#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
63
Christopher Faulet3b386a32017-02-23 10:17:15 +010064/* SPOE filter id. Used to identify SPOE filters */
65const char *spoe_filter_id = "SPOE filter";
66
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020067/* Set if the handle on SIGUSR1 is registered */
68static int sighandler_registered = 0;
69
70/* proxy used during the parsing */
71struct proxy *curproxy = NULL;
72
73/* The name of the SPOE engine, used during the parsing */
74char *curengine = NULL;
75
76/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020077/* SPOE agent/group/message used during the parsing */
78struct spoe_agent *curagent = NULL;
79struct spoe_group *curgrp = NULL;
80struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020081
82/* list of SPOE messages and placeholders used during the parsing */
83struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020084struct list curgrps;
85struct list curmphs;
86struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010087struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020088
Christopher Faulet7250b8f2018-03-26 17:19:01 +020089/* list of log servers used during the parsing */
90struct list curlogsrvs;
91
Christopher Faulet0e0f0852018-03-26 17:20:36 +020092/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
93int curpxopts;
94int curpxopts2;
95
Christopher Faulet42bfa462017-01-04 14:14:19 +010096/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +010097DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
98DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020099
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200100struct flt_ops spoe_ops;
101
Christopher Faulet8ef75252017-02-20 22:56:03 +0100102static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200103static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
104static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105
106/********************************************************************
107 * helper functions/globals
108 ********************************************************************/
109static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200110spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200111{
Christopher Faulet11610f32017-09-21 10:23:10 +0200112 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200113 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200114 free(ph->id);
115 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200116}
117
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200118static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100119spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120{
Christopher Faulet57583e42017-09-04 15:41:09 +0200121 struct spoe_arg *arg, *argback;
122 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200123
124 if (!msg)
125 return;
126 free(msg->id);
127 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200128 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200129 release_sample_expr(arg->expr);
130 free(arg->name);
131 LIST_DEL(&arg->list);
132 free(arg);
133 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200134 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
135 LIST_DEL(&acl->list);
136 prune_acl(acl);
137 free(acl);
138 }
139 if (msg->cond) {
140 prune_acl_cond(msg->cond);
141 free(msg->cond);
142 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200143 free(msg);
144}
145
146static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200147spoe_release_group(struct spoe_group *grp)
148{
149 if (!grp)
150 return;
151 free(grp->id);
152 free(grp->conf.file);
153 free(grp);
154}
155
156static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100157spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200158{
Christopher Faulet11610f32017-09-21 10:23:10 +0200159 struct spoe_message *msg, *msgback;
160 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200161 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200162
163 if (!agent)
164 return;
165 free(agent->id);
166 free(agent->conf.file);
167 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100168 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100169 free(agent->var_t_process);
170 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200171 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
172 LIST_DEL(&msg->list);
173 spoe_release_message(msg);
174 }
175 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
176 LIST_DEL(&grp->list);
177 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200178 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100179 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200180 for (i = 0; i < global.nbthread; ++i) {
181 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100182 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200183 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100184 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200185 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200186 free(agent);
187}
188
189static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100190 [SPOE_FRM_ERR_NONE] = "normal",
191 [SPOE_FRM_ERR_IO] = "I/O error",
192 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
193 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
194 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
195 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
196 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
197 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
198 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
199 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
200 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
201 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100202 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100203 [SPOE_FRM_ERR_RES] = "resource allocation error",
204 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200205};
206
207static const char *spoe_event_str[SPOE_EV_EVENTS] = {
208 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
209 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
210 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
211 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
212 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
213
214 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
215 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
216 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
217};
218
219
220#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
221
222static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100223 [SPOE_CTX_ST_NONE] = "NONE",
224 [SPOE_CTX_ST_READY] = "READY",
225 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
226 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
227 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
228 [SPOE_CTX_ST_DONE] = "DONE",
229 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200230};
231
232static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100233 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
234 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
235 [SPOE_APPCTX_ST_IDLE] = "IDLE",
236 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
237 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
238 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
239 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
240 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
241 [SPOE_APPCTX_ST_EXIT] = "EXIT",
242 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200243};
244
245#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100246
Christopher Faulet8ef75252017-02-20 22:56:03 +0100247/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500248 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100249 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100250static char *
251generate_pseudo_uuid()
252{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100253 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800254 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100255}
256
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100257
258static inline void
259spoe_update_stat_time(struct timeval *tv, long *t)
260{
261 if (*t == -1)
262 *t = tv_ms_elapsed(tv, &now);
263 else
264 *t += tv_ms_elapsed(tv, &now);
265 tv_zero(tv);
266}
267
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200268/********************************************************************
269 * Functions that encode/decode SPOE frames
270 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200271/* Helper to get static string length, excluding the terminating null byte */
272#define SLEN(str) (sizeof(str)-1)
273
274/* Predefined key used in HELLO/DISCONNECT frames */
275#define SUPPORTED_VERSIONS_KEY "supported-versions"
276#define VERSION_KEY "version"
277#define MAX_FRAME_SIZE_KEY "max-frame-size"
278#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100279#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100280#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200281#define STATUS_CODE_KEY "status-code"
282#define MSG_KEY "message"
283
284struct spoe_version {
285 char *str;
286 int min;
287 int max;
288};
289
290/* All supported versions */
291static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200292 /* 1.0 is now unsupported because of a bug about frame's flags*/
293 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200294 {NULL, 0, 0}
295};
296
297/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200298#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200299
Christopher Faulet8ef75252017-02-20 22:56:03 +0100300/* Convert a string to a SPOE version value. The string must follow the format
301 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
302 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200303static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100304spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200305{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100306 const char *p, *end;
307 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200308
Christopher Faulet8ef75252017-02-20 22:56:03 +0100309 p = str;
310 end = str+len;
311 maj = min = 0;
312 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200313
Christopher Faulet8ef75252017-02-20 22:56:03 +0100314 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100315 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100316 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200317
Christopher Faulet8ef75252017-02-20 22:56:03 +0100318 /* parse Major number, until the '.' */
319 while (*p != '.') {
320 if (p >= end || *p < '0' || *p > '9')
321 goto out;
322 maj *= 10;
323 maj += (*p - '0');
324 p++;
325 }
326
327 /* check Major version */
328 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329 goto out;
330
Christopher Faulet8ef75252017-02-20 22:56:03 +0100331 p++; /* skip the '.' */
332 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
333 goto out;
334
335 /* Parse Minor number */
336 while (p < end) {
337 if (*p < '0' || *p > '9')
338 break;
339 min *= 10;
340 min += (*p - '0');
341 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200342 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100343
344 /* check Minor number */
345 if (min > 999)
346 goto out;
347
348 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100349 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100350 p++;
351 if (p != end)
352 goto out;
353
354 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200355 out:
356 return vsn;
357}
358
Christopher Faulet8ef75252017-02-20 22:56:03 +0100359/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800360 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100361 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200362static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200364{
Willy Tarreau83061a82018-07-13 11:56:34 +0200365 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100366 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367 char *p, *end;
368 unsigned int flags = SPOE_FRM_FL_FIN;
369 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200370
Christopher Faulet8ef75252017-02-20 22:56:03 +0100371 p = frame;
372 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200373
Christopher Faulet8ef75252017-02-20 22:56:03 +0100374 /* Set Frame type */
375 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200376
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100377 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200378 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100379 memcpy(p, (char *)&flags, 4);
380 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200381
382 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200384
385 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
386 * and "capabilities" */
387
388 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100389 sz = SLEN(SUPPORTED_VERSIONS_KEY);
390 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
391 goto too_big;
392
393 *p++ = SPOE_DATA_T_STR;
394 sz = SLEN(SUPPORTED_VERSIONS_VAL);
395 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
396 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200397
398 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100399 sz = SLEN(MAX_FRAME_SIZE_KEY);
400 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
401 goto too_big;
402
403 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200404 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100405 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200406
407 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100408 sz = SLEN(CAPABILITIES_KEY);
409 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
410 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200411
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100413 chk = get_trash_chunk();
414 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200415 memcpy(chk->area, "pipelining", 10);
416 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100417 }
418 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 if (chk->data) chk->area[chk->data++] = ',';
420 memcpy(chk->area+chk->data, "async", 5);
421 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100422 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100423 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200424 if (chk->data) chk->area[chk->data++] = ',';
425 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100426 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100427 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200428 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100429 goto too_big;
430
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500431 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200432 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100433 sz = SLEN(ENGINE_ID_KEY);
434 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
435 goto too_big;
436
437 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200438 sz = strlen(agent->rt[tid].engine_id);
439 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100440 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100441 }
442
Christopher Faulet8ef75252017-02-20 22:56:03 +0100443 return (p - frame);
444
445 too_big:
446 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
447 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200448}
449
Christopher Faulet8ef75252017-02-20 22:56:03 +0100450/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
451 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
452 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200453static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100454spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200455{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100456 const char *reason;
457 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100458 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200460
Christopher Faulet8ef75252017-02-20 22:56:03 +0100461 p = frame;
462 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200463
Christopher Faulet8ef75252017-02-20 22:56:03 +0100464 /* Set Frame type */
465 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200466
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100467 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200468 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 memcpy(p, (char *)&flags, 4);
470 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200471
472 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 *p++ = 0; *p++ = 0;
474
475 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
476 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200477
478 /* There are 2 mandatory items: "status-code" and "message" */
479
480 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100481 sz = SLEN(STATUS_CODE_KEY);
482 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
483 goto too_big;
484
485 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200486 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200488
489 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 sz = SLEN(MSG_KEY);
491 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
492 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200493
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 /*Get the message corresponding to the status code */
495 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
496
497 *p++ = SPOE_DATA_T_STR;
498 sz = strlen(reason);
499 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
500 goto too_big;
501
502 return (p - frame);
503
504 too_big:
505 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
506 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200507}
508
Christopher Faulet8ef75252017-02-20 22:56:03 +0100509/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
510 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
511 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200512static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100514 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200515{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 char *p, *end;
517 unsigned int stream_id, frame_id;
518 unsigned int flags = SPOE_FRM_FL_FIN;
519 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200520
Christopher Faulet8ef75252017-02-20 22:56:03 +0100521 p = frame;
522 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200523
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100524 stream_id = ctx->stream_id;
525 frame_id = ctx->frame_id;
526
527 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
528 /* The fragmentation is not supported by the applet */
529 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
530 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
531 return -1;
532 }
533 flags = ctx->frag_ctx.flags;
534 }
535
536 /* Set Frame type */
537 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
538
539 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200540 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100541 memcpy(p, (char *)&flags, 4);
542 p += 4;
543
544 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200545 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100546 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200547 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100548 goto too_big;
549
550 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200551 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100552 if (p + sz >= end)
553 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200554 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100555 p += sz;
556
557 return (p - frame);
558
559 too_big:
560 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
561 return 0;
562}
563
564/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
565 * returns the number of encoded bytes in the frame on success, 0 if an encoding
566 * error occurred and -1 if a fatal error occurred. */
567static int
568spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
569 char *frame, size_t size)
570{
571 char *p, *end;
572 unsigned int stream_id, frame_id;
573 unsigned int flags;
574 size_t sz;
575
576 p = frame;
577 end = frame+size;
578
Christopher Faulet8ef75252017-02-20 22:56:03 +0100579 /* <ctx> is null when the stream has aborted the processing of a
580 * fragmented frame. In this case, we must notify the corresponding
581 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100582 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100583 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100584 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
585 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
586 }
587 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100588 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100589 stream_id = ctx->stream_id;
590 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100591 }
592
Christopher Faulet8ef75252017-02-20 22:56:03 +0100593 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100594 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100595
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100596 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200597 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100598 memcpy(p, (char *)&flags, 4);
599 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200600
601 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200602 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100603 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200604 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200606
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100607 if (ctx == NULL)
608 goto end;
609
Christopher Faulet8ef75252017-02-20 22:56:03 +0100610 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200611 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100612 if (p + sz >= end)
613 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200614 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100615 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100616
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100617 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100618 return (p - frame);
619
620 too_big:
621 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
622 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200623}
624
Christopher Faulet8ef75252017-02-20 22:56:03 +0100625/* Decode and process the HELLO frame sent by an agent. It returns the number of
626 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
627 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200628static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200630{
Christopher Faulet305c6072017-02-23 16:17:53 +0100631 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
632 char *p, *end;
633 int vsn, max_frame_size;
634 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100635
636 p = frame;
637 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200638
639 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100640 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
641 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200642 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100643 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200644
Christopher Faulet8ef75252017-02-20 22:56:03 +0100645 if (size < 7 /* TYPE + METADATA */) {
646 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
647 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648 }
649
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100650 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200652 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100653 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100655 /* Fragmentation is not supported for HELLO frame */
656 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100657 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100658 return -1;
659 }
660
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200661 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100662 if (*p != 0 || *(p+1) != 0) {
663 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
664 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200665 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100666 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200667
668 /* There are 3 mandatory items: "version", "max-frame-size" and
669 * "capabilities" */
670
671 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100672 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100673 while (p < end) {
674 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200675 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100676 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200677
678 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100679 ret = spoe_decode_buffer(&p, end, &str, &sz);
680 if (ret == -1 || !sz) {
681 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
682 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200683 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100684
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200685 /* Check "version" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200686 if (sz >= strlen(VERSION_KEY) && !memcmp(str, VERSION_KEY, strlen(VERSION_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100687 int i, type = *p++;
688
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100690 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
691 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
692 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
695 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
696 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 }
698
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200700 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100701 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200702 return -1;
703 }
704 for (i = 0; supported_versions[i].str != NULL; ++i) {
705 if (vsn >= supported_versions[i].min &&
706 vsn <= supported_versions[i].max)
707 break;
708 }
709 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100710 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200711 return -1;
712 }
713 }
714 /* Check "max-frame-size" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200715 else if (sz >= strlen(MAX_FRAME_SIZE_KEY) && !memcmp(str, MAX_FRAME_SIZE_KEY, strlen(MAX_FRAME_SIZE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200717
718 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
720 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
721 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
722 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100723 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
724 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200725 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200726 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
728 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200729 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100730 if (sz < MIN_FRAME_SIZE ||
731 sz > SPOE_APPCTX(appctx)->max_frame_size) {
732 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200733 return -1;
734 }
735 max_frame_size = sz;
736 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100737 /* Check "capabilities" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200738 else if (sz >= strlen(CAPABILITIES_KEY) && !memcmp(str, CAPABILITIES_KEY, strlen(CAPABILITIES_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100739 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100740
741 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
743 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
744 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100745 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
748 return 0;
749 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100750
Christopher Faulet8ef75252017-02-20 22:56:03 +0100751 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100752 char *delim;
753
754 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100755 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100756
Christopher Faulet8ef75252017-02-20 22:56:03 +0100757 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
758 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100759 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100760 flags |= SPOE_APPCTX_FL_PIPELINING;
761 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100762 else if (sz >= 5 && !strncmp(str, "async", 5)) {
763 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100764 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100765 flags |= SPOE_APPCTX_FL_ASYNC;
766 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100767 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
768 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100769 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100770 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
771 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100772
Christopher Faulet8ef75252017-02-20 22:56:03 +0100773 /* Get the next comma or break */
774 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100775 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100776 delim++;
777 sz -= (delim - str);
778 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779 }
780 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200781 else {
782 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 if (spoe_skip_data(&p, end) == -1) {
784 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
785 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200786 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200787 }
788 }
789
790 /* Final checks */
791 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100792 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200793 return -1;
794 }
795 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100796 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797 return -1;
798 }
Christopher Faulet11389012019-02-07 16:13:26 +0100799 if (!agent)
800 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
801 else {
802 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
803 flags &= ~SPOE_APPCTX_FL_PIPELINING;
804 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
805 flags &= ~SPOE_APPCTX_FL_ASYNC;
806 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200807
Christopher Faulet42bfa462017-01-04 14:14:19 +0100808 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
809 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
810 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100811
812 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813}
814
815/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
816 * bytes on success, 0 if the frame can be ignored and -1 if an error
817 * occurred. */
818static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100819spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200820{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100821 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100822 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823
824 p = frame;
825 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200826
827 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100828 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
829 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100831 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200832
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833 if (size < 7 /* TYPE + METADATA */) {
834 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
835 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836 }
837
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100838 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100839 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200840 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100843 /* Fragmentation is not supported for DISCONNECT frame */
844 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100845 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100846 return -1;
847 }
848
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200849 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 if (*p != 0 || *(p+1) != 0) {
851 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
852 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200855
856 /* There are 2 mandatory items: "status-code" and "message" */
857
858 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100859 while (p < end) {
860 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200861 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100862 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200863
864 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100865 ret = spoe_decode_buffer(&p, end, &str, &sz);
866 if (ret == -1 || !sz) {
867 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
868 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200869 }
870
871 /* Check "status-code" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200872 if (sz >= strlen(STATUS_CODE_KEY) && !memcmp(str, STATUS_CODE_KEY, strlen(STATUS_CODE_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100873 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200874
875 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200876 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
877 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
878 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
879 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100880 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
881 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200882 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200883 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100884 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
885 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200886 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200888 }
889
890 /* Check "message" K/V item */
Willy Tarreauda21ed12020-06-16 17:58:14 +0200891 else if (sz >= strlen(MSG_KEY) && !memcmp(str, MSG_KEY, strlen(MSG_KEY))) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100892 int type = *p++;
893
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200894 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
896 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
897 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100899 ret = spoe_decode_buffer(&p, end, &str, &sz);
900 if (ret == -1 || sz > 255) {
901 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
902 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200903 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100904#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
905 SPOE_APPCTX(appctx)->reason = str;
906 SPOE_APPCTX(appctx)->rlen = sz;
907#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
909 else {
910 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100911 if (spoe_skip_data(&p, end) == -1) {
912 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
913 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200914 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200915 }
916 }
917
Christopher Faulet8ef75252017-02-20 22:56:03 +0100918 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919}
920
921
Christopher Fauleta1cda022016-12-21 08:58:06 +0100922/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923 * success, 0 if the frame can be ignored and -1 if an error occurred. */
924static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100925spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100926 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100928 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100929 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100930 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100931 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100932 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933
934 p = frame;
935 end = frame + size;
936 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937
938 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100939 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
940 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100942 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200943
Christopher Faulet8ef75252017-02-20 22:56:03 +0100944 if (size < 7 /* TYPE + METADATA */) {
945 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
946 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947 }
948
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100949 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100950 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200951 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100952 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200953
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 /* Fragmentation is not supported for now */
955 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100957 return -1;
958 }
959
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200960 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200961 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100963 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200965 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200967 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100969
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100971 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200972 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100973 if ((*ctx)->stream_id == (unsigned int)stream_id &&
974 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100975 goto found;
976 }
977 }
978 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100979 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
980 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100981 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100982 goto found;
983 }
984 }
985
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100986 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
987 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
988 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
989
990 /* ABRT bit is set for an unfinished fragmented frame */
991 if (flags & SPOE_FRM_FL_ABRT) {
992 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100993 (*ctx)->state = SPOE_CTX_ST_ERROR;
994 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
995 /* Ignore the payload */
996 goto end;
997 }
998 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
999 /* For now, we ignore the ack */
1000 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1001 return 0;
1002 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001003
Christopher Fauleta1cda022016-12-21 08:58:06 +01001004 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001005 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1006 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001007 " - stream-id=%u - frame-id=%u\n",
1008 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1009 __FUNCTION__, appctx,
1010 (unsigned int)stream_id, (unsigned int)frame_id);
1011
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001012 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Fauletd9eb3c32020-11-10 14:31:39 +01001013 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK) {
1014 /* Report an error if we are waiting the ack for another frame,
1015 * but not if there is no longer frame waiting for a ack
1016 * (timeout)
1017 */
1018 if (!LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue) ||
1019 SPOE_APPCTX(appctx)->frag_ctx.ctx)
1020 return -1;
1021 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1022 SPOE_APPCTX(appctx)->cur_fpa = 0;
1023 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001024 return 0;
1025
1026 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001027 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1028 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001029 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001030 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001031 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001032
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001033 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001034 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001035 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1036 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001037 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001038
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001039 /* Transfer the buffer ownership to the SPOE context */
1040 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001041 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001042
Christopher Faulet8ef75252017-02-20 22:56:03 +01001043 (*ctx)->state = SPOE_CTX_ST_DONE;
1044
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001045 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001047 " - ACK frame received"
1048 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001049 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1051 (*ctx)->frame_id, flags);
1052 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001053}
1054
Christopher Fauletba7bc162016-11-07 21:07:38 +01001055/* This function is used in cfgparse.c and declared in proto/checks.h. It
1056 * prepare the request to send to agents during a healthcheck. It returns 0 on
1057 * success and -1 if an error occurred. */
1058int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001059spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001060{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001061 struct appctx appctx;
1062 struct spoe_appctx spoe_appctx;
1063 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1064 size_t sz;
1065 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001066
Christopher Faulet42bfa462017-01-04 14:14:19 +01001067 memset(&appctx, 0, sizeof(appctx));
1068 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001069 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001070
1071 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001072 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1075 end = frame + MAX_FRAME_SIZE;
1076
1077 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1078 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001079 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001080 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001081
Christopher Faulet8ef75252017-02-20 22:56:03 +01001082 /* Add "healthcheck" K/V item */
1083 sz = SLEN(HEALTHCHECK_KEY);
1084 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1085 return -1;
1086 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001087
Christopher Faulet8ef75252017-02-20 22:56:03 +01001088 *len = frame - buf;
1089 sz = htonl(*len - 4);
1090 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001091
Christopher Faulet8ef75252017-02-20 22:56:03 +01001092 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001093 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001094 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001095 return 0;
1096}
1097
1098/* This function is used in checks.c and declared in proto/checks.h. It decode
1099 * the response received from an agent during a healthcheck. It returns 0 on
1100 * success and -1 if an error occurred. */
1101int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001102spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001103{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001104 struct appctx appctx;
1105 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106
Christopher Faulet42bfa462017-01-04 14:14:19 +01001107 memset(&appctx, 0, sizeof(appctx));
1108 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001109
Christopher Faulet42bfa462017-01-04 14:14:19 +01001110 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001111 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001112
Christopher Faulet8ef75252017-02-20 22:56:03 +01001113 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1114 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001115 goto error;
1116 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001117 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1118 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001119
1120 return 0;
1121
1122 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001123 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1124 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1125 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001126 return -1;
1127}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001128
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001130 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001131 * success. */
1132static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001133spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001134{
1135 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136 int ret;
1137 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001138
Christopher Faulet8ef75252017-02-20 22:56:03 +01001139 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1140 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001141 netint = htonl(framesz);
1142 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001143 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001144 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001145 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001146 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001148 }
1149 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001150 return -1; /* error */
1151 }
1152 return framesz;
1153}
1154
1155/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1156 * when the frame can be ignored, 1 to retry later and the frame length on
1157 * success. */
1158static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001159spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001160{
1161 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001162 int ret;
1163 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001164
Willy Tarreau06d80a92017-10-19 14:32:15 +02001165 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001166 if (ret > 0) {
1167 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001168 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001169 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001170 return -1;
1171 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001172 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173 }
1174 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001176 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001177 }
1178 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 return -1; /* error */
1180 }
1181 return framesz;
1182}
1183
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001184/********************************************************************
1185 * Functions that manage the SPOE applet
1186 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001187static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001188spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001189{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001190 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001191 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001192 appctx_wakeup(appctx);
1193 return 1;
1194}
1195
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001196/* Callback function that catches applet timeouts. If a timeout occurred, we set
1197 * <appctx->st1> flag and the SPOE applet is woken up. */
1198static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001199spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001200{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001201 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202
1203 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1204 if (tick_is_expired(task->expire, now_ms)) {
1205 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001206 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001208 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209 return task;
1210}
1211
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001212/* Callback function that releases a SPOE applet. This happens when the
1213 * connection with the agent is closed. */
1214static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001215spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001216{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001217 struct stream_interface *si = appctx->owner;
1218 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1219 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001220 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221
1222 if (spoe_appctx == NULL)
1223 return;
1224
1225 appctx->ctx.spoe.ptr = NULL;
1226 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001227
1228 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1229 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1230 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001231
Christopher Faulet8ef75252017-02-20 22:56:03 +01001232 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001233 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001234 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001235 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1236 LIST_DEL(&spoe_appctx->list);
1237 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001238 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001239 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001240
Christopher Faulet8ef75252017-02-20 22:56:03 +01001241 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001242 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001243 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1244 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001245 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001246 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001247
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001248 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001249 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1250 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001251
1252 si_shutw(si);
1253 si_shutr(si);
1254 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001255 }
1256
Christopher Faulet8ef75252017-02-20 22:56:03 +01001257 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001258 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001259
Christopher Faulet8ef75252017-02-20 22:56:03 +01001260 /* Notify all waiting streams */
1261 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001262 LIST_DEL(&ctx->list);
1263 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001264 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001265 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletabe894d2020-11-10 18:45:34 +01001266 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001267 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001268 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001269 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001270 }
1271
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 /* If the applet was processing a fragmented frame, notify the
1273 * corresponding stream. */
1274 if (spoe_appctx->frag_ctx.ctx) {
1275 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001276 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001277 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001278 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001279 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1280 }
1281
Christopher Fauletabe894d2020-11-10 18:45:34 +01001282 if (!LIST_ISEMPTY(&agent->rt[tid].applets)) {
1283 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
1284 if (ctx->spoe_appctx == spoe_appctx)
1285 ctx->spoe_appctx = NULL;
1286 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001287 goto end;
Christopher Fauletabe894d2020-11-10 18:45:34 +01001288 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001289
Christopher Faulet8ef75252017-02-20 22:56:03 +01001290 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001291 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001292 LIST_DEL(&ctx->list);
1293 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001294 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001295 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletabe894d2020-11-10 18:45:34 +01001296 ctx->spoe_appctx = NULL;
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);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001300 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001301 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001302 LIST_DEL(&ctx->list);
1303 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001304 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001305 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauletabe894d2020-11-10 18:45:34 +01001306 ctx->spoe_appctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001308 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001309 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1310 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001311
1312 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001313 /* Release allocated memory */
1314 spoe_release_buffer(&spoe_appctx->buffer,
1315 &spoe_appctx->buffer_wait);
1316 pool_free(pool_head_spoe_appctx, spoe_appctx);
1317
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001318 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001319 agent->rt[tid].frame_size = agent->max_frame_size;
1320 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1321 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001322}
1323
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001324static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001325spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001326{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001327 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001328 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001329 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001330 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001331
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001332 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1333 /* closed */
1334 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1335 goto exit;
1336 }
1337
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001338 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001339 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001340 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001341 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1342 goto stop;
1343 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001344
Christopher Fauleta1cda022016-12-21 08:58:06 +01001345 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001346 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1347 " - Connection timed out\n",
1348 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1349 __FUNCTION__, appctx);
1350 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001351 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001352 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001353
Christopher Faulet42bfa462017-01-04 14:14:19 +01001354 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001355 SPOE_APPCTX(appctx)->task->expire =
1356 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001357
Christopher Faulet8ef75252017-02-20 22:56:03 +01001358 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1359 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001360 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 ret = spoe_prepare_hahello_frame(appctx, frame,
1362 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001363 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001364 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001365
1366 switch (ret) {
1367 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 case 0: /* ignore => an error, cannot be ignored */
1369 goto exit;
1370
1371 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001372 goto stop;
1373
Christopher Faulet8ef75252017-02-20 22:56:03 +01001374 default:
1375 /* HELLO frame successfully sent, now wait for the
1376 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001377 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1378 goto next;
1379 }
1380
1381 next:
1382 return 0;
1383 stop:
1384 return 1;
1385 exit:
1386 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1387 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001388}
1389
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001390static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001391spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001392{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001393 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001394 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001395 char *frame;
1396 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001397
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001398
Christopher Fauletb067b062017-01-04 16:39:11 +01001399 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001400 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001401 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001402 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403
Christopher Fauleta1cda022016-12-21 08:58:06 +01001404 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001405 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1406 " - Connection timed out\n",
1407 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1408 __FUNCTION__, appctx);
1409 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001410 goto exit;
1411 }
1412
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001413 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 ret = spoe_recv_frame(appctx, frame,
1415 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001416 if (ret > 1) {
1417 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1418 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1419 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001420 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001421 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001423 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001424
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 switch (ret) {
1426 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001427 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001428 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1429 goto next;
1430
1431 case 1: /* retry later */
1432 goto stop;
1433
1434 default:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001435 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001436 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001437 SPOE_APPCTX(appctx)->node.key = 0;
1438 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001439
1440 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001441 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001442 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001443 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001444
Christopher Fauleta1cda022016-12-21 08:58:06 +01001445 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001446 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001447 if (trash.data)
1448 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001449
1450 SPOE_APPCTX(appctx)->task->expire =
1451 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001452 return 0;
1453 stop:
1454 return 1;
1455 exit:
1456 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1457 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001458}
1459
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001460
Christopher Fauleta1cda022016-12-21 08:58:06 +01001461static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001462spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001463{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001464 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1465 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001466 char *frame, *buf;
1467 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001468
Christopher Faulet8ef75252017-02-20 22:56:03 +01001469 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1470 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001471 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001472
1473 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1474 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1475 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1476 SPOE_APPCTX(appctx)->max_frame_size);
1477 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001478 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001479 *skip = 1;
1480 ret = 1;
1481 goto end;
1482 }
1483 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001484 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001485 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1486 SPOE_APPCTX(appctx)->max_frame_size);
1487
1488 }
1489
Christopher Faulet8ef75252017-02-20 22:56:03 +01001490 if (ret > 1)
1491 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001492
Christopher Faulet8ef75252017-02-20 22:56:03 +01001493 switch (ret) {
1494 case -1: /* error */
1495 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1496 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001497
Christopher Faulet8ef75252017-02-20 22:56:03 +01001498 case 0: /* ignore */
1499 if (ctx == NULL)
1500 goto abort_frag_frame;
1501
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001502 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001503 LIST_DEL(&ctx->list);
1504 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001505 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001506 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001507 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001508 ctx->state = SPOE_CTX_ST_ERROR;
1509 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1510 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001511 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001512 break;
1513
1514 case 1: /* retry */
1515 *skip = 1;
1516 break;
1517
1518 default:
1519 if (ctx == NULL)
1520 goto abort_frag_frame;
1521
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001522 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001523 LIST_DEL(&ctx->list);
1524 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001525 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001526 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001527 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001528 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1529 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1530 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001531 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001532 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001533 }
1534 goto end;
1535
1536 frag_frame_sent:
1537 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001538 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001539 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1540 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1541 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001542 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1543 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1544 goto end;
1545
1546 no_frag_frame_sent:
1547 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1548 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001549 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001550 }
1551 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1552 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1553 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1554 }
1555 else {
1556 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001557 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001558 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1559 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001560 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001561 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001562 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1563 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1564 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001565 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001566
Christopher Faulet8ef75252017-02-20 22:56:03 +01001567 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1568 goto end;
1569
1570 abort_frag_frame:
1571 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1572 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1573 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1574 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1575 goto end;
1576
1577 end:
1578 return ret;
1579}
1580
1581static int
1582spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1583{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001584 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001585 struct spoe_context *ctx = NULL;
1586 char *frame;
1587 int ret;
1588
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001589 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001590 ret = spoe_recv_frame(appctx, frame,
1591 SPOE_APPCTX(appctx)->max_frame_size);
1592 if (ret > 1) {
1593 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1594 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001595 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001596 goto end;
1597 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001598 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001599 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1600 }
1601 switch (ret) {
1602 case -1: /* error */
1603 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1604 break;
1605
1606 case 0: /* ignore */
1607 break;
1608
1609 case 1: /* retry */
1610 *skip = 1;
1611 break;
1612
1613 default:
1614 LIST_DEL(&ctx->list);
1615 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001616 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001617 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1618 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001619 if (ctx->spoe_appctx) {
1620 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001621 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001622 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001623 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1624 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1625 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1626 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1627 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1628 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1629 }
1630 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1631 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001632 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1633 break;
1634 }
1635
1636 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001637 if (trash.data)
1638 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001639 end:
1640 return ret;
1641}
1642
1643static int
1644spoe_handle_processing_appctx(struct appctx *appctx)
1645{
1646 struct stream_interface *si = appctx->owner;
1647 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001648 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001649
1650 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1651 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1652 goto exit;
1653 }
1654
1655 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1656 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1657 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1658 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1659 goto next;
1660 }
1661
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001662 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001663 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001664 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001665 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1666 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001667 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001668
Christopher Faulet8f82b202018-01-24 16:23:03 +01001669 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1670 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001671
Christopher Faulet8f82b202018-01-24 16:23:03 +01001672 /* receiving_frame loop */
1673 while (!skip_receiving) {
1674 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1675 switch (ret) {
1676 case -1: /* error */
1677 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001678
Christopher Faulet8f82b202018-01-24 16:23:03 +01001679 case 0: /* ignore */
1680 active_r = 1;
1681 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001682
Christopher Faulet8f82b202018-01-24 16:23:03 +01001683 case 1: /* retry */
1684 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001685
Christopher Faulet8f82b202018-01-24 16:23:03 +01001686 default:
1687 active_r = 1;
1688 break;
1689 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001690 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001691
Christopher Faulet8f82b202018-01-24 16:23:03 +01001692 /* send_frame loop */
1693 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1694 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1695 switch (ret) {
1696 case -1: /* error */
1697 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001698
Christopher Faulet8f82b202018-01-24 16:23:03 +01001699 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001700 if (SPOE_APPCTX(appctx)->node.key)
1701 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001702 active_s++;
1703 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 case 1: /* retry */
1706 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001707
Christopher Faulet8f82b202018-01-24 16:23:03 +01001708 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001709 if (SPOE_APPCTX(appctx)->node.key)
1710 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001711 active_s++;
1712 break;
1713 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001715
Christopher Faulet8f82b202018-01-24 16:23:03 +01001716 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001717 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1718 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1719 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001720
Christopher Faulet8f82b202018-01-24 16:23:03 +01001721 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Faulet1b28e0a2021-03-01 15:01:14 +01001722 struct server *srv = objt_server(si_strm(si)->target);
1723
1724 /* With several threads, close the applet if there are pending
1725 * connections or if the server is full. Otherwise, add the
1726 * applet in the idle list.
1727 */
1728 if (global.nbthread > 1 &&
1729 (agent->b.be->nbpend ||
1730 (srv && (srv->nbpend || (srv->maxconn && srv->served >=srv_dynamic_maxconn(srv)))))) {
1731 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1732 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1733 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1734 goto next;
1735 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001736 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001737 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001738 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739 }
1740 return 1;
1741
Christopher Faulet8f82b202018-01-24 16:23:03 +01001742 next:
1743 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1744 return 0;
1745
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746 exit:
1747 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1748 return 0;
1749}
1750
1751static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001752spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001753{
1754 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001755 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001756 char *frame, *buf;
1757 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001758
Christopher Fauleta1cda022016-12-21 08:58:06 +01001759 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1760 goto exit;
1761
1762 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1763 goto exit;
1764
Christopher Faulet8ef75252017-02-20 22:56:03 +01001765 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1766 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001767 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1769 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001770 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001771 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772
1773 switch (ret) {
1774 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001775 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001776 goto exit;
1777
1778 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001779 goto stop;
1780
1781 default:
1782 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1783 " - disconnected by HAProxy (%d): %s\n",
1784 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001785 __FUNCTION__, appctx,
1786 SPOE_APPCTX(appctx)->status_code,
1787 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788
1789 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1790 goto next;
1791 }
1792
1793 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001794 SPOE_APPCTX(appctx)->task->expire =
1795 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001796 return 0;
1797 stop:
1798 return 1;
1799 exit:
1800 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1801 return 0;
1802}
1803
1804static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806{
1807 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001808 char *frame;
1809 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001810
Christopher Fauletb067b062017-01-04 16:39:11 +01001811 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001812 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001814 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815
Christopher Fauletb067b062017-01-04 16:39:11 +01001816 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001817 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001818 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001819 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001821 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001822 ret = spoe_recv_frame(appctx, frame,
1823 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001825 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001826 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001827 }
1828
1829 switch (ret) {
1830 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001831 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1832 " - error on frame (%s)\n",
1833 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001834 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001836 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001837 goto exit;
1838
1839 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001840 goto next;
1841
1842 case 1: /* retry */
1843 goto stop;
1844
1845 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001848 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001849 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001850 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1851 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001852 goto exit;
1853 }
1854
1855 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001856 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001857 if (trash.data)
1858 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001859
Christopher Fauleta1cda022016-12-21 08:58:06 +01001860 return 0;
1861 stop:
1862 return 1;
1863 exit:
1864 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1865 return 0;
1866}
1867
1868/* I/O Handler processing messages exchanged with the agent */
1869static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001870spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001871{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001872 struct stream_interface *si = appctx->owner;
1873 struct spoe_agent *agent;
1874
1875 if (SPOE_APPCTX(appctx) == NULL)
1876 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001877
Christopher Faulet8ef75252017-02-20 22:56:03 +01001878 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1879 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001880
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 switchstate:
1882 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1883 " - appctx-state=%s\n",
1884 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1885 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1886
1887 switch (appctx->st0) {
1888 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001889 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001890 goto out;
1891 goto switchstate;
1892
1893 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001894 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001895 goto out;
1896 goto switchstate;
1897
1898 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001899 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001900 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001901 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001902 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001903 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001904 SPOE_APPCTX(appctx)->task->expire =
1905 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001906 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001907 goto switchstate;
1908 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001909 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1910 /* fall through */
1911
1912 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001913 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1914 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001915 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001916 goto out;
1917 goto switchstate;
1918
1919 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001920 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001921 goto out;
1922 goto switchstate;
1923
1924 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001925 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001926 goto out;
1927 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001928
1929 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001930 appctx->st0 = SPOE_APPCTX_ST_END;
1931 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1932
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933 si_shutw(si);
1934 si_shutr(si);
1935 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001936 /* fall through */
1937
1938 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001939 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001940 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001941 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001942 if (stopping)
1943 spoe_wakeup_appctx(appctx);
1944
Christopher Faulet42bfa462017-01-04 14:14:19 +01001945 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1946 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001947}
1948
1949struct applet spoe_applet = {
1950 .obj_type = OBJ_TYPE_APPLET,
1951 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001952 .fct = spoe_handle_appctx,
1953 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001954};
1955
1956/* Create a SPOE applet. On success, the created applet is returned, else
1957 * NULL. */
1958static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001959spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001960{
1961 struct appctx *appctx;
1962 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001963 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001964
Emeric Brun1138fd02017-06-19 12:38:55 +02001965 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001966 goto out_error;
1967
Willy Tarreaubafbe012017-11-24 17:34:44 +01001968 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001969 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001970 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001971 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001972
Christopher Faulet42bfa462017-01-04 14:14:19 +01001973 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001974 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001975 goto out_free_spoe_appctx;
1976
1977 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001978 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001979 SPOE_APPCTX(appctx)->task->context = appctx;
1980 SPOE_APPCTX(appctx)->agent = conf->agent;
1981 SPOE_APPCTX(appctx)->version = 0;
1982 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1983 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001984 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001985 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001986 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001987
Willy Tarreau954827a2021-02-20 11:49:49 +01001988 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001989 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001990 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001991
1992 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1993 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001994
Willy Tarreau5820a362016-12-22 15:59:02 +01001995 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 if (!sess)
1997 goto out_free_spoe;
1998
Willy Tarreau87787ac2017-08-28 16:22:54 +02001999 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002000 goto out_free_sess;
2001
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002 stream_set_backend(strm, conf->agent->b.be);
2003
2004 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002005 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002006 appctx_wakeup(appctx);
2007
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002008 strm->do_log = NULL;
2009 strm->res.flags |= CF_READ_DONTWAIT;
2010
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002011 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002012 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002013 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002014 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002015
Emeric Brunc60def82017-09-27 14:59:38 +02002016 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002017 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002018 return appctx;
2019
2020 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002021 out_free_sess:
2022 session_free(sess);
2023 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02002024 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002025 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002026 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002027 out_free_appctx:
2028 appctx_free(appctx);
2029 out_error:
2030 return NULL;
2031}
2032
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002033static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002034spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035{
2036 struct spoe_config *conf = FLT_CONF(ctx->filter);
2037 struct spoe_agent *agent = conf->agent;
2038 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002039 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040
Christopher Fauleta1cda022016-12-21 08:58:06 +01002041 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002042 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Fauleteccfa612020-06-22 15:32:14 +02002043 (agent->rt[tid].processing == 1 || agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec)))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002044 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002045
2046 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002047 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002048 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2049 ctx->strm);
2050
Christopher Fauleta1cda022016-12-21 08:58:06 +01002051 /* Do not try to create a new applet if there is no server up for the
2052 * agent's backend. */
2053 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2054 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2055 " - cannot create SPOE appctx: no server up\n",
2056 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2057 __FUNCTION__, ctx->strm);
2058 goto end;
2059 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002060
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 /* Do not try to create a new applet if we have reached the maximum of
2062 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002063 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002064 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2066 " - cannot create SPOE appctx: max CPS reached\n",
2067 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2068 __FUNCTION__, ctx->strm);
2069 goto end;
2070 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002071 }
2072
Christopher Faulet8ef75252017-02-20 22:56:03 +01002073 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002074 if (appctx == NULL) {
2075 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2076 " - failed to create SPOE appctx\n",
2077 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2078 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002079 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002080 "SPOE: [%s] failed to create SPOE applet\n",
2081 agent->id);
2082
Christopher Fauleta1cda022016-12-21 08:58:06 +01002083 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084 }
2085
Christopher Fauleta1cda022016-12-21 08:58:06 +01002086 /* Increase the per-process number of cumulated connections */
2087 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002088 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002089
Christopher Fauleta1cda022016-12-21 08:58:06 +01002090 end:
2091 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002092 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002093 ctx->status_code = SPOE_CTX_ERR_RES;
2094 return -1;
2095 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002096
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002097 /* Add the SPOE context in the sending queue if the stream has no applet
2098 * already assigned and wakeup all idle applets. Otherwise, don't queue
2099 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002100 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002101 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2102 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002103 if (ctx->spoe_appctx)
2104 return 1;
2105 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002106
2107 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002108 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002109 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002110 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002111 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002112 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002113
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002114 /* Finally try to wakeup an IDLE applet. */
2115 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2116 struct eb32_node *node;
2117
2118 node = eb32_first(&agent->rt[tid].idle_applets);
2119 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2120 if (node && spoe_appctx) {
2121 eb32_delete(&spoe_appctx->node);
2122 spoe_appctx->node.key++;
2123 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2124 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002125 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002126 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002127 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002128}
2129
2130/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002131 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002132 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002133/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2134 * fragmented_content. If the next message can be processed, it returns 0. If
2135 * the message is too big, it returns -1.*/
2136static int
2137spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2138 struct spoe_message *msg, int dir,
2139 char **buf, char *end)
2140{
2141 struct sample *smp;
2142 struct spoe_arg *arg;
2143 int ret;
2144
2145 if (msg->cond) {
2146 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2147 ret = acl_pass(ret);
2148 if (msg->cond->pol == ACL_COND_UNLESS)
2149 ret = !ret;
2150
2151 /* the rule does not match */
2152 if (!ret)
2153 goto next;
2154 }
2155
2156 /* Resume encoding of a SPOE argument */
2157 if (ctx->frag_ctx.curarg != NULL) {
2158 arg = ctx->frag_ctx.curarg;
2159 goto encode_argument;
2160 }
2161
2162 if (ctx->frag_ctx.curoff != UINT_MAX)
2163 goto encode_msg_payload;
2164
2165 /* Check if there is enough space for the message name and the
2166 * number of arguments. It implies <msg->id_len> is encoded on 2
2167 * bytes, at most (< 2288). */
2168 if (*buf + 2 + msg->id_len + 1 > end)
2169 goto too_big;
2170
2171 /* Encode the message name */
2172 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2173 goto too_big;
2174
2175 /* Set the number of arguments for this message */
2176 **buf = msg->nargs;
2177 (*buf)++;
2178
2179 ctx->frag_ctx.curoff = 0;
2180 encode_msg_payload:
2181
2182 /* Loop on arguments */
2183 list_for_each_entry(arg, &msg->args, list) {
2184 ctx->frag_ctx.curarg = arg;
2185 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002186 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002187
2188 encode_argument:
2189 if (ctx->frag_ctx.curoff != UINT_MAX)
2190 goto encode_arg_value;
2191
Joseph Herlantf7f60312018-11-15 13:46:49 -08002192 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002193 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2194 goto too_big;
2195
2196 ctx->frag_ctx.curoff = 0;
2197 encode_arg_value:
2198
Joseph Herlantf7f60312018-11-15 13:46:49 -08002199 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002200 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002201 if (smp) {
2202 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2203 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2204 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002205 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002206 if (ret == -1 || ctx->frag_ctx.curoff)
2207 goto too_big;
2208 }
2209
2210 next:
2211 return 0;
2212
2213 too_big:
2214 return -1;
2215}
2216
Christopher Fauletc718b822017-09-21 16:50:56 +02002217/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2218 * handle fragmented content. On success it returns 1. If an error occurred, -1
2219 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2220 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002221static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002222spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002223 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002224{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002225 struct spoe_config *conf = FLT_CONF(ctx->filter);
2226 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002227 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002228 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002229
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002230 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002231 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002232
Christopher Fauletc718b822017-09-21 16:50:56 +02002233 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2234 /* Resume encoding of a SPOE message */
2235 if (ctx->frag_ctx.curmsg != NULL) {
2236 msg = ctx->frag_ctx.curmsg;
2237 goto encode_evt_message;
2238 }
2239
2240 list_for_each_entry(msg, messages, by_evt) {
2241 ctx->frag_ctx.curmsg = msg;
2242 ctx->frag_ctx.curarg = NULL;
2243 ctx->frag_ctx.curoff = UINT_MAX;
2244
2245 encode_evt_message:
2246 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2247 goto too_big;
2248 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002249 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002250 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2251 /* Resume encoding of a SPOE message */
2252 if (ctx->frag_ctx.curmsg != NULL) {
2253 msg = ctx->frag_ctx.curmsg;
2254 goto encode_grp_message;
2255 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002256
Christopher Fauletc718b822017-09-21 16:50:56 +02002257 list_for_each_entry(msg, messages, by_grp) {
2258 ctx->frag_ctx.curmsg = msg;
2259 ctx->frag_ctx.curarg = NULL;
2260 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002261
Christopher Fauletc718b822017-09-21 16:50:56 +02002262 encode_grp_message:
2263 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2264 goto too_big;
2265 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002266 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002267 else
2268 goto skip;
2269
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002270
Christopher Faulet57583e42017-09-04 15:41:09 +02002271 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002272 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002273 goto skip;
2274
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002275 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002276 " - encode %s messages - spoe_appctx=%p"
2277 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002278 (int)now.tv_sec, (int)now.tv_usec,
2279 agent->id, __FUNCTION__, s,
2280 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002281 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002282 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002283
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002284 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002285 ctx->frag_ctx.curmsg = NULL;
2286 ctx->frag_ctx.curarg = NULL;
2287 ctx->frag_ctx.curoff = 0;
2288 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002289
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002290 return 1;
2291
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002293 /* Return an error if fragmentation is unsupported or if nothing has
2294 * been encoded because its too big and not splittable. */
2295 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002296 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2297 return -1;
2298 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002299
2300 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002301 " - encode fragmented messages - spoe_appctx=%p"
2302 " - curmsg=%p - curarg=%p - curoff=%u"
2303 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002304 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002305 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002306 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002307 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002308
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002309 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002310 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2311 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2312 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002313
2314 skip:
2315 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2316 " - skip the frame because nothing has been encoded\n",
2317 (int)now.tv_sec, (int)now.tv_usec,
2318 agent->id, __FUNCTION__, s);
2319 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002320}
2321
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002322
2323/***************************************************************************
2324 * Functions that handle SPOE actions
2325 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326/* Helper function to set a variable */
2327static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002328spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002329 struct sample *smp)
2330{
2331 struct spoe_config *conf = FLT_CONF(ctx->filter);
2332 struct spoe_agent *agent = conf->agent;
2333 char varname[64];
2334
2335 memset(varname, 0, sizeof(varname));
2336 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2337 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002338 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2339 vars_set_by_name(varname, len, smp);
2340 else
2341 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002342}
2343
2344/* Helper function to unset a variable */
2345static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002346spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347 struct sample *smp)
2348{
2349 struct spoe_config *conf = FLT_CONF(ctx->filter);
2350 struct spoe_agent *agent = conf->agent;
2351 char varname[64];
2352
2353 memset(varname, 0, sizeof(varname));
2354 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2355 scope, agent->var_pfx, len, name);
2356 vars_unset_by_name_ifexist(varname, len, smp);
2357}
2358
2359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360static inline int
2361spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2362 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002363{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002364 char *str, *scope, *p = *buf;
2365 struct sample smp;
2366 uint64_t sz;
2367 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368
Christopher Faulet8ef75252017-02-20 22:56:03 +01002369 if (p + 2 >= end)
2370 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002371
Christopher Faulet8ef75252017-02-20 22:56:03 +01002372 /* SET-VAR requires 3 arguments */
2373 if (*p++ != 3)
2374 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 switch (*p++) {
2377 case SPOE_SCOPE_PROC: scope = "proc"; break;
2378 case SPOE_SCOPE_SESS: scope = "sess"; break;
2379 case SPOE_SCOPE_TXN : scope = "txn"; break;
2380 case SPOE_SCOPE_REQ : scope = "req"; break;
2381 case SPOE_SCOPE_RES : scope = "res"; break;
2382 default: goto skip;
2383 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002384
Christopher Faulet8ef75252017-02-20 22:56:03 +01002385 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2386 goto skip;
2387 memset(&smp, 0, sizeof(smp));
2388 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002389
Christopher Faulet8ef75252017-02-20 22:56:03 +01002390 if (spoe_decode_data(&p, end, &smp) == -1)
2391 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002392
Christopher Faulet8ef75252017-02-20 22:56:03 +01002393 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2394 " - set-var '%s.%s.%.*s'\n",
2395 (int)now.tv_sec, (int)now.tv_usec,
2396 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2397 __FUNCTION__, s, scope,
2398 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2399 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400
Christopher Faulet2469eba2020-10-15 16:08:30 +02002401 if (smp.data.type == SMP_T_ANY)
2402 spoe_unset_var(ctx, scope, str, sz, &smp);
2403 else
2404 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406 ret = (p - *buf);
2407 *buf = p;
2408 return ret;
2409 skip:
2410 return 0;
2411}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002412
Christopher Faulet8ef75252017-02-20 22:56:03 +01002413static inline int
2414spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2415 char **buf, char *end, int dir)
2416{
2417 char *str, *scope, *p = *buf;
2418 struct sample smp;
2419 uint64_t sz;
2420 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002421
Christopher Faulet8ef75252017-02-20 22:56:03 +01002422 if (p + 2 >= end)
2423 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002424
Christopher Faulet8ef75252017-02-20 22:56:03 +01002425 /* UNSET-VAR requires 2 arguments */
2426 if (*p++ != 2)
2427 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002428
Christopher Faulet8ef75252017-02-20 22:56:03 +01002429 switch (*p++) {
2430 case SPOE_SCOPE_PROC: scope = "proc"; break;
2431 case SPOE_SCOPE_SESS: scope = "sess"; break;
2432 case SPOE_SCOPE_TXN : scope = "txn"; break;
2433 case SPOE_SCOPE_REQ : scope = "req"; break;
2434 case SPOE_SCOPE_RES : scope = "res"; break;
2435 default: goto skip;
2436 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002437
Christopher Faulet8ef75252017-02-20 22:56:03 +01002438 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2439 goto skip;
2440 memset(&smp, 0, sizeof(smp));
2441 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002442
Christopher Faulet8ef75252017-02-20 22:56:03 +01002443 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2444 " - unset-var '%s.%s.%.*s'\n",
2445 (int)now.tv_sec, (int)now.tv_usec,
2446 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2447 __FUNCTION__, s, scope,
2448 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2449 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002450
Christopher Faulet8ef75252017-02-20 22:56:03 +01002451 spoe_unset_var(ctx, scope, str, sz, &smp);
2452
2453 ret = (p - *buf);
2454 *buf = p;
2455 return ret;
2456 skip:
2457 return 0;
2458}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002459
Christopher Faulet8ef75252017-02-20 22:56:03 +01002460/* Process SPOE actions for a specific event. It returns 1 on success. If an
2461 * error occurred, 0 is returned. */
2462static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002463spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002464{
2465 char *p, *end;
2466 int ret;
2467
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002468 p = b_head(&ctx->buffer);
2469 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002470
2471 while (p < end) {
2472 enum spoe_action_type type;
2473
2474 type = *p++;
2475 switch (type) {
2476 case SPOE_ACT_T_SET_VAR:
2477 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2478 if (!ret)
2479 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002480 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002481
Christopher Faulet8ef75252017-02-20 22:56:03 +01002482 case SPOE_ACT_T_UNSET_VAR:
2483 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2484 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002485 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002486 break;
2487
2488 default:
2489 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002490 }
2491 }
2492
2493 return 1;
2494 skip:
2495 return 0;
2496}
2497
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002498/***************************************************************************
2499 * Functions that process SPOE events
2500 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002501static void
2502spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2503 struct spoe_context *ctx, int dir)
2504{
2505 if (!tv_iszero(&ctx->stats.tv_start)) {
2506 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2507 ctx->stats.t_total += ctx->stats.t_process;
2508 tv_zero(&ctx->stats.tv_request);
2509 tv_zero(&ctx->stats.tv_queue);
2510 tv_zero(&ctx->stats.tv_wait);
2511 tv_zero(&ctx->stats.tv_response);
2512 }
2513
2514 if (agent->var_t_process) {
2515 struct sample smp;
2516
2517 memset(&smp, 0, sizeof(smp));
2518 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2519 smp.data.u.sint = ctx->stats.t_process;
2520 smp.data.type = SMP_T_SINT;
2521
2522 spoe_set_var(ctx, "txn", agent->var_t_process,
2523 strlen(agent->var_t_process), &smp);
2524 }
2525
2526 if (agent->var_t_total) {
2527 struct sample smp;
2528
2529 memset(&smp, 0, sizeof(smp));
2530 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2531 smp.data.u.sint = ctx->stats.t_total;
2532 smp.data.type = SMP_T_SINT;
2533
2534 spoe_set_var(ctx, "txn", agent->var_t_total,
2535 strlen(agent->var_t_total), &smp);
2536 }
2537}
2538
2539static void
2540spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2541 struct spoe_context *ctx, int dir)
2542{
2543 if (agent->eps_max > 0)
2544 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2545
2546 if (agent->var_on_error) {
2547 struct sample smp;
2548
2549 memset(&smp, 0, sizeof(smp));
2550 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2551 smp.data.u.sint = ctx->status_code;
2552 smp.data.type = SMP_T_BOOL;
2553
2554 spoe_set_var(ctx, "txn", agent->var_on_error,
2555 strlen(agent->var_on_error), &smp);
2556 }
2557
2558 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2559 ? SPOE_CTX_ST_READY
2560 : SPOE_CTX_ST_NONE);
2561}
2562
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002563static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002564spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002565{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002566 /* If a process is already started for this SPOE context, retry
2567 * later. */
2568 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002569 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002570
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002571 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002572 ctx->stats.tv_start = now;
2573 ctx->stats.tv_request = now;
2574 ctx->stats.t_request = -1;
2575 ctx->stats.t_queue = -1;
2576 ctx->stats.t_waiting = -1;
2577 ctx->stats.t_response = -1;
2578 ctx->stats.t_process = -1;
2579
2580 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002581
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582 /* Set the right flag to prevent request and response processing
2583 * in same time. */
2584 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2585 ? SPOE_CTX_FL_REQ_PROCESS
2586 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002588}
2589
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002590static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002591spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002592{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002593 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002594
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002595 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2596 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002597 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002598 if (sa) {
2599 if (sa->frag_ctx.ctx == ctx) {
2600 sa->frag_ctx.ctx = NULL;
2601 spoe_wakeup_appctx(sa->owner);
2602 }
2603 else
2604 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002605 }
2606
Christopher Fauleta1cda022016-12-21 08:58:06 +01002607 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002608 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002609 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002610
2611 /* Reset processing timer */
2612 ctx->process_exp = TICK_ETERNITY;
2613
Christopher Faulet8ef75252017-02-20 22:56:03 +01002614 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002615
Christopher Fauletfce747b2018-01-24 15:59:32 +01002616 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002617 ctx->frag_ctx.curmsg = NULL;
2618 ctx->frag_ctx.curarg = NULL;
2619 ctx->frag_ctx.curoff = 0;
2620 ctx->frag_ctx.flags = 0;
2621
Christopher Fauleta1cda022016-12-21 08:58:06 +01002622 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002623 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002624 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002625 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002626 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002627
Christopher Fauleta1cda022016-12-21 08:58:06 +01002628 LIST_DEL(&ctx->list);
2629 LIST_INIT(&ctx->list);
2630 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002631}
2632
Christopher Faulet58d03682017-09-21 16:57:24 +02002633/* Process a list of SPOE messages. First, this functions will process messages
2634 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2635 * to process corresponding actions. During all the processing, it returns 0
2636 * and it returns 1 when the processing is finished. If an error occurred, -1
2637 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002638static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002639spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2640 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002641{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002642 struct spoe_config *conf = FLT_CONF(ctx->filter);
2643 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002644 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002645
2646 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002647 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002648
2649 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2650 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002651 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002652 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002653 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002654 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002655 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002656 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002657
2658 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002659 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002660 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002661 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002662 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002663 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002664 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002665 goto skip;
2666 }
2667 }
2668
Christopher Fauletf7a30922016-11-10 15:04:51 +01002669 if (!tick_isset(ctx->process_exp)) {
2670 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2671 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2672 ctx->process_exp);
2673 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002674 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002675 if (!ret)
2676 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002677
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002678 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002679 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002680 }
2681
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002682 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002683 if (tv_iszero(&ctx->stats.tv_request))
2684 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002685 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002686 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002687 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002688 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002689 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002690 if (!ret)
2691 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002692 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002693 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002694 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2695 }
2696
2697 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002698 if (ctx->spoe_appctx)
2699 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002700 ret = 0;
2701 goto out;
2702 }
2703
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002704 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2705 ret = 0;
2706 goto out;
2707 }
2708
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002709 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002710 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002711 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002712 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002713 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002714 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002715 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002716 }
2717
2718 out:
2719 return ret;
2720
Christopher Fauleta1cda022016-12-21 08:58:06 +01002721 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002722 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002723 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002724 spoe_stop_processing(agent, ctx);
2725 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002726
Christopher Fauleta1cda022016-12-21 08:58:06 +01002727 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002728 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002729 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002730 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002731 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002732 spoe_handle_processing_error(s, agent, ctx, dir);
2733 ret = 1;
2734 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002735 return ret;
2736}
2737
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002738/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2739 * See spoe_process_message for details. */
2740static int
2741spoe_process_group(struct stream *s, struct spoe_context *ctx,
2742 struct spoe_group *group, int dir)
2743{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002744 struct spoe_config *conf = FLT_CONF(ctx->filter);
2745 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002746 int ret;
2747
2748 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2749 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002750 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002751 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2752 group->id);
2753
2754 if (LIST_ISEMPTY(&group->messages))
2755 return 1;
2756
2757 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002758 if (ret && ctx->stats.t_process != -1) {
2759 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002760 " - <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002761 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002762 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002763 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002764 ctx->stats.t_response, ctx->stats.t_process,
2765 agent->counters.idles, agent->counters.applets,
2766 agent->counters.nb_sending, agent->counters.nb_waiting,
2767 agent->counters.nb_errors, agent->counters.nb_processed,
2768 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2769 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2770 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2771 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2772 agent->id, group->id, s->uniq_id, ctx->status_code,
2773 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2774 ctx->stats.t_response, ctx->stats.t_process,
2775 agent->counters.idles, agent->counters.applets,
2776 agent->counters.nb_sending, agent->counters.nb_waiting,
2777 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002778 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002779 return ret;
2780}
2781
Christopher Faulet58d03682017-09-21 16:57:24 +02002782/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2783 * See spoe_process_message for details. */
2784static int
2785spoe_process_event(struct stream *s, struct spoe_context *ctx,
2786 enum spoe_event ev)
2787{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002788 struct spoe_config *conf = FLT_CONF(ctx->filter);
2789 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002790 int dir, ret;
2791
2792 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002793 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002794 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002795 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2796 spoe_event_str[ev]);
2797
2798 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2799
2800 if (LIST_ISEMPTY(&(ctx->events[ev])))
2801 return 1;
2802
2803 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002804 if (ret && ctx->stats.t_process != -1) {
2805 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002806 " - <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu %u/%u\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002807 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2808 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2809 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002810 ctx->stats.t_response, ctx->stats.t_process,
2811 agent->counters.idles, agent->counters.applets,
2812 agent->counters.nb_sending, agent->counters.nb_waiting,
2813 agent->counters.nb_errors, agent->counters.nb_processed,
2814 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2815 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2816 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2817 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2818 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2819 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2820 ctx->stats.t_response, ctx->stats.t_process,
2821 agent->counters.idles, agent->counters.applets,
2822 agent->counters.nb_sending, agent->counters.nb_waiting,
2823 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002824 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002825 return ret;
2826}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002827
2828/***************************************************************************
2829 * Functions that create/destroy SPOE contexts
2830 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002831static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002832spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002833{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002834 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002835 return 1;
2836
Willy Tarreau954827a2021-02-20 11:49:49 +01002837 if (LIST_ADDED(&buffer_wait->list))
2838 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002839
Christopher Faulet4596fb72017-01-11 14:05:19 +01002840 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002841 return 1;
2842
Willy Tarreau954827a2021-02-20 11:49:49 +01002843 LIST_ADDQ(&ti->buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002844 return 0;
2845}
2846
2847static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002848spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002849{
Willy Tarreau954827a2021-02-20 11:49:49 +01002850 if (LIST_ADDED(&buffer_wait->list))
2851 LIST_DEL_INIT(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002852
2853 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002854 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002855 b_free(buf);
Willy Tarreau132b3a42021-02-20 12:02:46 +01002856 offer_buffers(buffer_wait->target, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002857 }
2858}
2859
Christopher Faulet4596fb72017-01-11 14:05:19 +01002860static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002861spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002862{
2863 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2864 return 1;
2865}
2866
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002867static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002868spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002869{
2870 struct spoe_config *conf = FLT_CONF(filter);
2871 struct spoe_context *ctx;
2872
Willy Tarreaubafbe012017-11-24 17:34:44 +01002873 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002874 if (ctx == NULL) {
2875 return NULL;
2876 }
2877 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002878 ctx->filter = filter;
2879 ctx->state = SPOE_CTX_ST_NONE;
2880 ctx->status_code = SPOE_CTX_ERR_NONE;
2881 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002882 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002883 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002884 ctx->buffer = BUF_NULL;
Willy Tarreau954827a2021-02-20 11:49:49 +01002885 LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002886 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002887 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002888 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002889
Christopher Fauletf7a30922016-11-10 15:04:51 +01002890 ctx->stream_id = 0;
2891 ctx->frame_id = 1;
2892 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002893
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002894 tv_zero(&ctx->stats.tv_start);
2895 tv_zero(&ctx->stats.tv_request);
2896 tv_zero(&ctx->stats.tv_queue);
2897 tv_zero(&ctx->stats.tv_wait);
2898 tv_zero(&ctx->stats.tv_response);
2899 ctx->stats.t_request = -1;
2900 ctx->stats.t_queue = -1;
2901 ctx->stats.t_waiting = -1;
2902 ctx->stats.t_response = -1;
2903 ctx->stats.t_process = -1;
2904 ctx->stats.t_total = 0;
2905
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002906 ctx->strm = s;
2907 ctx->state = SPOE_CTX_ST_READY;
2908 filter->ctx = ctx;
2909
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002910 return ctx;
2911}
2912
2913static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002914spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002915{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002916 struct spoe_config *conf = FLT_CONF(filter);
2917 struct spoe_context *ctx = filter->ctx;
2918
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002919 if (!ctx)
2920 return;
2921
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002922 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002923 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002924 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002925}
2926
2927static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002928spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002929{
2930 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002931 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002932
2933 tv_zero(&ctx->stats.tv_start);
2934 tv_zero(&ctx->stats.tv_request);
2935 tv_zero(&ctx->stats.tv_queue);
2936 tv_zero(&ctx->stats.tv_wait);
2937 tv_zero(&ctx->stats.tv_response);
2938 ctx->stats.t_request = -1;
2939 ctx->stats.t_queue = -1;
2940 ctx->stats.t_waiting = -1;
2941 ctx->stats.t_response = -1;
2942 ctx->stats.t_process = -1;
2943 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002944}
2945
2946
2947/***************************************************************************
2948 * Hooks that manage the filter lifecycle (init/check/deinit)
2949 **************************************************************************/
2950/* Signal handler: Do a soft stop, wakeup SPOE applet */
2951static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002952spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002953{
2954 struct proxy *p;
2955
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002956 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002957 while (p) {
2958 struct flt_conf *fconf;
2959
2960 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002961 struct spoe_config *conf;
2962 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002963 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002964 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002965
Christopher Faulet3b386a32017-02-23 10:17:15 +01002966 if (fconf->id != spoe_filter_id)
2967 continue;
2968
2969 conf = fconf->conf;
2970 agent = conf->agent;
2971
Christopher Faulet24289f22017-09-25 14:48:02 +02002972 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002973 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002974 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2975 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002976 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002977 }
2978 }
2979 p = p->next;
2980 }
2981}
2982
2983
2984/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2985static int
2986spoe_init(struct proxy *px, struct flt_conf *fconf)
2987{
2988 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002989
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002990 /* conf->agent_fe was already initialized during the config
2991 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002992 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993 conf->agent_fe.cap = PR_CAP_FE;
2994 conf->agent_fe.mode = PR_MODE_TCP;
2995 conf->agent_fe.maxconn = 0;
2996 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2997 conf->agent_fe.conn_retries = CONN_RETRIES;
2998 conf->agent_fe.accept = frontend_accept;
2999 conf->agent_fe.srv = NULL;
3000 conf->agent_fe.timeout.client = TICK_ETERNITY;
3001 conf->agent_fe.default_target = &spoe_applet.obj_type;
3002 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
3003
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003004 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003005 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003006 sighandler_registered = 1;
3007 }
3008
Christopher Faulet00292352019-01-09 15:05:10 +01003009 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003011}
3012
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05003013/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014static void
3015spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3016{
3017 struct spoe_config *conf = fconf->conf;
3018
3019 if (conf) {
3020 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003021
Christopher Faulet8ef75252017-02-20 22:56:03 +01003022 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003023 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003024 free(conf);
3025 }
3026 fconf->conf = NULL;
3027}
3028
3029/* Check configuration of a SPOE filter for a specified proxy.
3030 * Return 1 on error, else 0. */
3031static int
3032spoe_check(struct proxy *px, struct flt_conf *fconf)
3033{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003034 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003035 struct spoe_config *conf = fconf->conf;
3036 struct proxy *target;
Christopher Faulet0dccf892021-02-19 10:56:41 +01003037 struct logsrv *logsrv;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003038 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003039
Christopher Faulet7ee86672017-09-19 11:08:28 +02003040 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3041 * are uniq */
3042 list_for_each_entry(f, &px->filter_configs, list) {
3043 struct spoe_config *c = f->conf;
3044
3045 /* This is not an SPOE filter */
3046 if (f->id != spoe_filter_id)
3047 continue;
3048 /* This is the current SPOE filter */
3049 if (f == fconf)
3050 continue;
3051
3052 /* Check engine Id. It should be uniq */
3053 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003054 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3055 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003056 return 1;
3057 }
3058 }
3059
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003060 target = proxy_be_by_name(conf->agent->b.name);
3061 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003062 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3063 " declared at %s:%d.\n",
3064 px->id, conf->agent->b.name, conf->agent->id,
3065 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003066 return 1;
3067 }
3068 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003069 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3070 " at %s:%d does not support HTTP mode.\n",
3071 px->id, target->id, conf->agent->id,
3072 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003073 return 1;
3074 }
3075
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003076 if (px->bind_proc & ~target->bind_proc) {
3077 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3078 " at %s:%d does not cover all of its processes.\n",
3079 px->id, target->id, conf->agent->id,
3080 conf->agent->conf.file, conf->agent->conf.line);
3081 return 1;
3082 }
3083
Christopher Fauletfe261552019-03-18 13:57:42 +01003084 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003085 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3086 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3087 return 1;
3088 }
3089 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003090 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003091 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3092 conf->agent->rt[i].processing = 0;
3093 LIST_INIT(&conf->agent->rt[i].applets);
3094 LIST_INIT(&conf->agent->rt[i].sending_queue);
3095 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3096 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003097 }
3098
Christopher Faulet0dccf892021-02-19 10:56:41 +01003099 list_for_each_entry(logsrv, &conf->agent_fe.logsrvs, list) {
3100 if (logsrv->type == LOG_TARGET_BUFFER) {
3101 struct sink *sink = sink_find(logsrv->ring_name);
3102
3103 if (!sink || sink->type != SINK_TYPE_BUFFER) {
3104 ha_alert("Proxy %s : log server used by SPOE agent '%s' declared"
3105 " at %s:%d uses unkown ring named '%s'.\n",
3106 px->id, conf->agent->id, conf->agent->conf.file,
3107 conf->agent->conf.line, logsrv->ring_name);
3108 return 1;
3109 }
3110 logsrv->sink = sink;
3111 }
3112 }
3113
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003114 free(conf->agent->b.name);
3115 conf->agent->b.name = NULL;
3116 conf->agent->b.be = target;
3117 return 0;
3118}
3119
Kevin Zhud87b1a52019-09-17 15:05:45 +02003120/* Initializes the SPOE filter for a proxy for a specific thread.
3121 * Returns a negative value if an error occurs. */
3122static int
3123spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3124{
3125 struct spoe_config *conf = fconf->conf;
3126 struct spoe_agent *agent = conf->agent;
3127
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003128 agent->rt[tid].engine_id = generate_pseudo_uuid();
3129 if (agent->rt[tid].engine_id == NULL)
3130 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003131 return 0;
3132}
3133
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003134/**************************************************************************
3135 * Hooks attached to a stream
3136 *************************************************************************/
3137/* Called when a filter instance is created and attach to a stream. It creates
3138 * the context that will be used to process this stream. */
3139static int
3140spoe_start(struct stream *s, struct filter *filter)
3141{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003142 struct spoe_config *conf = FLT_CONF(filter);
3143 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003144 struct spoe_context *ctx;
3145
3146 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003147 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003148 __FUNCTION__, s);
3149
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003150 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003151 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3152 " - failed to create SPOE context\n",
3153 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003154 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003155 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003156 "SPOE: [%s] failed to create SPOE context\n",
3157 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003158 return 0;
3159 }
3160
Christopher Faulet11610f32017-09-21 10:23:10 +02003161 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003162 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3163
Christopher Faulet11610f32017-09-21 10:23:10 +02003164 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003165 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3166
Christopher Faulet11610f32017-09-21 10:23:10 +02003167 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003168 filter->pre_analyzers |= AN_RES_INSPECT;
3169
Christopher Faulet11610f32017-09-21 10:23:10 +02003170 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003171 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3172
Christopher Faulet11610f32017-09-21 10:23:10 +02003173 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003174 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3175
Christopher Faulet11610f32017-09-21 10:23:10 +02003176 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003177 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3178
3179 return 1;
3180}
3181
3182/* Called when a filter instance is detached from a stream. It release the
3183 * attached SPOE context. */
3184static void
3185spoe_stop(struct stream *s, struct filter *filter)
3186{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003187 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3188 (int)now.tv_sec, (int)now.tv_usec,
3189 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3190 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003191 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003192}
3193
Christopher Fauletf7a30922016-11-10 15:04:51 +01003194
3195/*
3196 * Called when the stream is woken up because of expired timer.
3197 */
3198static void
3199spoe_check_timeouts(struct stream *s, struct filter *filter)
3200{
3201 struct spoe_context *ctx = filter->ctx;
3202
Christopher Fauletac580602018-03-20 16:09:20 +01003203 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003204 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003205}
3206
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003207/* Called when we are ready to filter data on a channel */
3208static int
3209spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3210{
3211 struct spoe_context *ctx = filter->ctx;
3212 int ret = 1;
3213
3214 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3215 " - ctx-flags=0x%08x\n",
3216 (int)now.tv_sec, (int)now.tv_usec,
3217 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3218 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3219
Christopher Fauletb067b062017-01-04 16:39:11 +01003220 if (ctx->state == SPOE_CTX_ST_NONE)
3221 goto out;
3222
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003223 if (!(chn->flags & CF_ISRESP)) {
3224 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3225 chn->analysers |= AN_REQ_INSPECT_FE;
3226 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3227 chn->analysers |= AN_REQ_INSPECT_BE;
3228
3229 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3230 goto out;
3231
3232 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003233 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003234 if (!ret)
3235 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003236 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3237 }
3238 else {
Miroslav Zagorac88403262020-06-19 22:17:17 +02003239 if (filter->pre_analyzers & AN_RES_INSPECT)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003240 chn->analysers |= AN_RES_INSPECT;
3241
3242 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3243 goto out;
3244
Christopher Faulet8ef75252017-02-20 22:56:03 +01003245 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003246 if (!ret) {
3247 channel_dont_read(chn);
3248 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003249 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003250 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003251 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003252 }
3253
3254 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003255 return ret;
3256}
3257
3258/* Called before a processing happens on a given channel */
3259static int
3260spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3261 struct channel *chn, unsigned an_bit)
3262{
3263 struct spoe_context *ctx = filter->ctx;
3264 int ret = 1;
3265
3266 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3267 " - ctx-flags=0x%08x - ana=0x%08x\n",
3268 (int)now.tv_sec, (int)now.tv_usec,
3269 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3270 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3271 ctx->flags, an_bit);
3272
Christopher Fauletb067b062017-01-04 16:39:11 +01003273 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003274 goto out;
3275
3276 switch (an_bit) {
3277 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003278 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003279 break;
3280 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003281 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003282 break;
3283 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003284 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003285 break;
3286 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003287 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003288 break;
3289 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003290 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003291 break;
3292 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003293 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003294 break;
3295 }
3296
3297 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003298 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003299 channel_dont_read(chn);
3300 channel_dont_close(chn);
3301 }
3302 return ret;
3303}
3304
3305/* Called when the filtering on the channel ends. */
3306static int
3307spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3308{
3309 struct spoe_context *ctx = filter->ctx;
3310
3311 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3312 " - ctx-flags=0x%08x\n",
3313 (int)now.tv_sec, (int)now.tv_usec,
3314 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3315 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3316
3317 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003318 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003319 }
3320
3321 return 1;
3322}
3323
3324/********************************************************************
3325 * Functions that manage the filter initialization
3326 ********************************************************************/
3327struct flt_ops spoe_ops = {
3328 /* Manage SPOE filter, called for each filter declaration */
3329 .init = spoe_init,
3330 .deinit = spoe_deinit,
3331 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003332 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003333
3334 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003335 .attach = spoe_start,
3336 .detach = spoe_stop,
3337 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003338
3339 /* Handle channels activity */
3340 .channel_start_analyze = spoe_start_analyze,
3341 .channel_pre_analyze = spoe_chn_pre_analyze,
3342 .channel_end_analyze = spoe_end_analyze,
3343};
3344
3345
3346static int
3347cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3348{
3349 const char *err;
3350 int i, err_code = 0;
3351
3352 if ((cfg_scope == NULL && curengine != NULL) ||
3353 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003354 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003355 goto out;
3356
3357 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3358 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003359 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3360 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003361 err_code |= ERR_ALERT | ERR_ABORT;
3362 goto out;
3363 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003364 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3365 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003366 goto out;
3367 }
3368
3369 err = invalid_char(args[1]);
3370 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003371 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3372 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003373 err_code |= ERR_ALERT | ERR_ABORT;
3374 goto out;
3375 }
3376
3377 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003378 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3379 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003380 err_code |= ERR_ALERT | ERR_ABORT;
3381 goto out;
3382 }
3383 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003384 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003385 err_code |= ERR_ALERT | ERR_ABORT;
3386 goto out;
3387 }
3388
3389 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003390
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003391 curagent->conf.file = strdup(file);
3392 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003393
3394 curagent->timeout.hello = TICK_ETERNITY;
3395 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003396 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003397
Christopher Fauleta1cda022016-12-21 08:58:06 +01003398 curagent->var_pfx = NULL;
3399 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003400 curagent->var_t_process = NULL;
3401 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003402 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003403 curagent->cps_max = 0;
3404 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003405 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003406 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003407
3408 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003409 LIST_INIT(&curagent->events[i]);
3410 LIST_INIT(&curagent->groups);
3411 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003412 }
3413 else if (!strcmp(args[0], "use-backend")) {
3414 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003415 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3416 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003417 err_code |= ERR_ALERT | ERR_FATAL;
3418 goto out;
3419 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003420 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003421 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003422 free(curagent->b.name);
3423 curagent->b.name = strdup(args[1]);
3424 }
3425 else if (!strcmp(args[0], "messages")) {
3426 int cur_arg = 1;
3427 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003428 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003429
Christopher Faulet11610f32017-09-21 10:23:10 +02003430 list_for_each_entry(ph, &curmphs, list) {
3431 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003432 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3433 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003434 err_code |= ERR_ALERT | ERR_FATAL;
3435 goto out;
3436 }
3437 }
3438
Christopher Faulet11610f32017-09-21 10:23:10 +02003439 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003440 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003441 err_code |= ERR_ALERT | ERR_ABORT;
3442 goto out;
3443 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003444 ph->id = strdup(args[cur_arg]);
3445 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003446 cur_arg++;
3447 }
3448 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003449 else if (!strcmp(args[0], "groups")) {
3450 int cur_arg = 1;
3451 while (*args[cur_arg]) {
3452 struct spoe_placeholder *ph = NULL;
3453
3454 list_for_each_entry(ph, &curgphs, list) {
3455 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003456 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3457 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003458 err_code |= ERR_ALERT | ERR_FATAL;
3459 goto out;
3460 }
3461 }
3462
3463 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003464 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003465 err_code |= ERR_ALERT | ERR_ABORT;
3466 goto out;
3467 }
3468 ph->id = strdup(args[cur_arg]);
3469 LIST_ADDQ(&curgphs, &ph->list);
3470 cur_arg++;
3471 }
3472 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003473 else if (!strcmp(args[0], "timeout")) {
3474 unsigned int *tv = NULL;
3475 const char *res;
3476 unsigned timeout;
3477
3478 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003479 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3480 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003481 err_code |= ERR_ALERT | ERR_FATAL;
3482 goto out;
3483 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003484 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3485 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003486 if (!strcmp(args[1], "hello"))
3487 tv = &curagent->timeout.hello;
3488 else if (!strcmp(args[1], "idle"))
3489 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003490 else if (!strcmp(args[1], "processing"))
3491 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003492 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003493 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3494 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003495 err_code |= ERR_ALERT | ERR_FATAL;
3496 goto out;
3497 }
3498 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003499 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3500 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003501 err_code |= ERR_ALERT | ERR_FATAL;
3502 goto out;
3503 }
3504 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003505 if (res == PARSE_TIME_OVER) {
3506 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3507 file, linenum, args[2], args[0], args[1]);
3508 err_code |= ERR_ALERT | ERR_FATAL;
3509 goto out;
3510 }
3511 else if (res == PARSE_TIME_UNDER) {
3512 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3513 file, linenum, args[2], args[0], args[1]);
3514 err_code |= ERR_ALERT | ERR_FATAL;
3515 goto out;
3516 }
3517 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003518 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3519 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003520 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003521 goto out;
3522 }
3523 *tv = MS_TO_TICKS(timeout);
3524 }
3525 else if (!strcmp(args[0], "option")) {
3526 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003527 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3528 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003529 err_code |= ERR_ALERT | ERR_FATAL;
3530 goto out;
3531 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003532
Christopher Faulet305c6072017-02-23 16:17:53 +01003533 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003534 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003535 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003536 if (kwm == 1)
3537 curagent->flags &= ~SPOE_FL_PIPELINING;
3538 else
3539 curagent->flags |= SPOE_FL_PIPELINING;
3540 goto out;
3541 }
3542 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003543 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003544 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003545 if (kwm == 1)
3546 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003547 else
3548 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003549 goto out;
3550 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003551 else if (!strcmp(args[1], "send-frag-payload")) {
3552 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3553 goto out;
3554 if (kwm == 1)
3555 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3556 else
3557 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3558 goto out;
3559 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003560 else if (!strcmp(args[1], "dontlog-normal")) {
3561 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3562 goto out;
3563 if (kwm == 1)
3564 curpxopts2 &= ~PR_O2_NOLOGNORM;
3565 else
3566 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003567 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003568 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003569
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003570 /* Following options does not support negation */
3571 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003572 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3573 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003574 err_code |= ERR_ALERT | ERR_FATAL;
3575 goto out;
3576 }
3577
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003578 if (!strcmp(args[1], "var-prefix")) {
3579 char *tmp;
3580
3581 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003582 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3583 file, linenum, args[0],
3584 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003585 err_code |= ERR_ALERT | ERR_FATAL;
3586 goto out;
3587 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003588 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3589 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003590 tmp = args[2];
3591 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003592 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003593 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003594 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003595 err_code |= ERR_ALERT | ERR_FATAL;
3596 goto out;
3597 }
3598 tmp++;
3599 }
3600 curagent->var_pfx = strdup(args[2]);
3601 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003602 else if (!strcmp(args[1], "force-set-var")) {
3603 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3604 goto out;
3605 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3606 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003607 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003608 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003609 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003610 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3611 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003612 else if (!strcmp(args[1], "set-on-error")) {
3613 char *tmp;
3614
3615 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003616 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3617 file, linenum, args[0],
3618 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003619 err_code |= ERR_ALERT | ERR_FATAL;
3620 goto out;
3621 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003622 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3623 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003624 tmp = args[2];
3625 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003626 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003627 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003628 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003629 err_code |= ERR_ALERT | ERR_FATAL;
3630 goto out;
3631 }
3632 tmp++;
3633 }
3634 curagent->var_on_error = strdup(args[2]);
3635 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003636 else if (!strcmp(args[1], "set-process-time")) {
3637 char *tmp;
3638
3639 if (!*args[2]) {
3640 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3641 file, linenum, args[0],
3642 args[1]);
3643 err_code |= ERR_ALERT | ERR_FATAL;
3644 goto out;
3645 }
3646 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3647 goto out;
3648 tmp = args[2];
3649 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003650 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003651 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003652 file, linenum, args[0], args[1]);
3653 err_code |= ERR_ALERT | ERR_FATAL;
3654 goto out;
3655 }
3656 tmp++;
3657 }
3658 curagent->var_t_process = strdup(args[2]);
3659 }
3660 else if (!strcmp(args[1], "set-total-time")) {
3661 char *tmp;
3662
3663 if (!*args[2]) {
3664 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3665 file, linenum, args[0],
3666 args[1]);
3667 err_code |= ERR_ALERT | ERR_FATAL;
3668 goto out;
3669 }
3670 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3671 goto out;
3672 tmp = args[2];
3673 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003674 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003675 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003676 file, linenum, args[0], args[1]);
3677 err_code |= ERR_ALERT | ERR_FATAL;
3678 goto out;
3679 }
3680 tmp++;
3681 }
3682 curagent->var_t_total = strdup(args[2]);
3683 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003684 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003685 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3686 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003687 err_code |= ERR_ALERT | ERR_FATAL;
3688 goto out;
3689 }
Christopher Faulet48026722016-11-16 15:01:12 +01003690 }
3691 else if (!strcmp(args[0], "maxconnrate")) {
3692 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003693 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3694 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003695 err_code |= ERR_ALERT | ERR_FATAL;
3696 goto out;
3697 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003698 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003699 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003700 curagent->cps_max = atol(args[1]);
3701 }
3702 else if (!strcmp(args[0], "maxerrrate")) {
3703 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003704 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3705 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003706 err_code |= ERR_ALERT | ERR_FATAL;
3707 goto out;
3708 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003709 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003710 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003711 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003712 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003713 else if (!strcmp(args[0], "max-frame-size")) {
3714 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003715 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3716 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003717 err_code |= ERR_ALERT | ERR_FATAL;
3718 goto out;
3719 }
3720 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3721 goto out;
3722 curagent->max_frame_size = atol(args[1]);
3723 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3724 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003725 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3726 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003727 err_code |= ERR_ALERT | ERR_FATAL;
3728 goto out;
3729 }
3730 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003731 else if (!strcmp(args[0], "max-waiting-frames")) {
3732 if (!*args[1]) {
3733 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3734 file, linenum, args[0]);
3735 err_code |= ERR_ALERT | ERR_FATAL;
3736 goto out;
3737 }
3738 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3739 goto out;
3740 curagent->max_fpa = atol(args[1]);
3741 if (curagent->max_fpa < 1) {
3742 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3743 file, linenum, args[0]);
3744 err_code |= ERR_ALERT | ERR_FATAL;
3745 goto out;
3746 }
3747 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003748 else if (!strcmp(args[0], "register-var-names")) {
3749 int cur_arg;
3750
3751 if (!*args[1]) {
3752 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3753 file, linenum, args[0]);
3754 err_code |= ERR_ALERT | ERR_FATAL;
3755 goto out;
3756 }
3757 cur_arg = 1;
3758 while (*args[cur_arg]) {
3759 struct spoe_var_placeholder *vph;
3760
3761 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3762 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3763 err_code |= ERR_ALERT | ERR_ABORT;
3764 goto out;
3765 }
3766 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003767 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003768 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3769 err_code |= ERR_ALERT | ERR_ABORT;
3770 goto out;
3771 }
3772 LIST_ADDQ(&curvars, &vph->list);
3773 cur_arg++;
3774 }
3775 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003776 else if (!strcmp(args[0], "log")) {
3777 char *errmsg = NULL;
3778
3779 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3780 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3781 err_code |= ERR_ALERT | ERR_FATAL;
3782 goto out;
3783 }
3784 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003785 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003786 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3787 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003788 err_code |= ERR_ALERT | ERR_FATAL;
3789 goto out;
3790 }
3791 out:
3792 return err_code;
3793}
Christopher Faulet11610f32017-09-21 10:23:10 +02003794static int
3795cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3796{
3797 struct spoe_group *grp;
3798 const char *err;
3799 int err_code = 0;
3800
3801 if ((cfg_scope == NULL && curengine != NULL) ||
3802 (cfg_scope != NULL && curengine == NULL) ||
3803 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3804 goto out;
3805
3806 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3807 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003808 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3809 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003810 err_code |= ERR_ALERT | ERR_ABORT;
3811 goto out;
3812 }
3813 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3814 err_code |= ERR_ABORT;
3815 goto out;
3816 }
3817
3818 err = invalid_char(args[1]);
3819 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003820 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3821 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003822 err_code |= ERR_ALERT | ERR_ABORT;
3823 goto out;
3824 }
3825
3826 list_for_each_entry(grp, &curgrps, list) {
3827 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003828 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3829 " name as another one declared at %s:%d.\n",
3830 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003831 err_code |= ERR_ALERT | ERR_FATAL;
3832 goto out;
3833 }
3834 }
3835
3836 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003837 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003838 err_code |= ERR_ALERT | ERR_ABORT;
3839 goto out;
3840 }
3841
3842 curgrp->id = strdup(args[1]);
3843 curgrp->conf.file = strdup(file);
3844 curgrp->conf.line = linenum;
3845 LIST_INIT(&curgrp->phs);
3846 LIST_INIT(&curgrp->messages);
3847 LIST_ADDQ(&curgrps, &curgrp->list);
3848 }
3849 else if (!strcmp(args[0], "messages")) {
3850 int cur_arg = 1;
3851 while (*args[cur_arg]) {
3852 struct spoe_placeholder *ph = NULL;
3853
3854 list_for_each_entry(ph, &curgrp->phs, list) {
3855 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003856 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3857 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003858 err_code |= ERR_ALERT | ERR_FATAL;
3859 goto out;
3860 }
3861 }
3862
3863 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003864 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003865 err_code |= ERR_ALERT | ERR_ABORT;
3866 goto out;
3867 }
3868 ph->id = strdup(args[cur_arg]);
3869 LIST_ADDQ(&curgrp->phs, &ph->list);
3870 cur_arg++;
3871 }
3872 }
3873 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003874 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3875 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003876 err_code |= ERR_ALERT | ERR_FATAL;
3877 goto out;
3878 }
3879 out:
3880 return err_code;
3881}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003882
3883static int
3884cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3885{
3886 struct spoe_message *msg;
3887 struct spoe_arg *arg;
3888 const char *err;
3889 char *errmsg = NULL;
3890 int err_code = 0;
3891
3892 if ((cfg_scope == NULL && curengine != NULL) ||
3893 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003894 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003895 goto out;
3896
3897 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3898 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003899 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3900 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003901 err_code |= ERR_ALERT | ERR_ABORT;
3902 goto out;
3903 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003904 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3905 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003906 goto out;
3907 }
3908
3909 err = invalid_char(args[1]);
3910 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003911 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3912 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003913 err_code |= ERR_ALERT | ERR_ABORT;
3914 goto out;
3915 }
3916
3917 list_for_each_entry(msg, &curmsgs, list) {
3918 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003919 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3920 " name as another one declared at %s:%d.\n",
3921 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003922 err_code |= ERR_ALERT | ERR_FATAL;
3923 goto out;
3924 }
3925 }
3926
3927 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003928 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003929 err_code |= ERR_ALERT | ERR_ABORT;
3930 goto out;
3931 }
3932
3933 curmsg->id = strdup(args[1]);
3934 curmsg->id_len = strlen(curmsg->id);
3935 curmsg->event = SPOE_EV_NONE;
3936 curmsg->conf.file = strdup(file);
3937 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003938 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003939 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003940 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003941 LIST_INIT(&curmsg->by_evt);
3942 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003943 LIST_ADDQ(&curmsgs, &curmsg->list);
3944 }
3945 else if (!strcmp(args[0], "args")) {
3946 int cur_arg = 1;
3947
3948 curproxy->conf.args.ctx = ARGC_SPOE;
3949 curproxy->conf.args.file = file;
3950 curproxy->conf.args.line = linenum;
3951 while (*args[cur_arg]) {
3952 char *delim = strchr(args[cur_arg], '=');
3953 int idx = 0;
3954
3955 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003956 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003957 err_code |= ERR_ALERT | ERR_ABORT;
3958 goto out;
3959 }
3960
3961 if (!delim) {
3962 arg->name = NULL;
3963 arg->name_len = 0;
3964 delim = args[cur_arg];
3965 }
3966 else {
3967 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3968 arg->name_len = delim - args[cur_arg];
3969 delim++;
3970 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003971 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3972 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003973 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003974 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003975 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003976 err_code |= ERR_ALERT | ERR_FATAL;
3977 free(arg->name);
3978 free(arg);
3979 goto out;
3980 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003981 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003982 LIST_ADDQ(&curmsg->args, &arg->list);
3983 cur_arg++;
3984 }
3985 curproxy->conf.args.file = NULL;
3986 curproxy->conf.args.line = 0;
3987 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003988 else if (!strcmp(args[0], "acl")) {
3989 err = invalid_char(args[1]);
3990 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003991 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3992 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003993 err_code |= ERR_ALERT | ERR_FATAL;
3994 goto out;
3995 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003996 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01003997 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003998 "logical disjunction within a condition.\n",
3999 file, linenum, args[1]);
4000 err_code |= ERR_ALERT | ERR_FATAL;
4001 goto out;
4002 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004003 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004004 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
4005 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004006 err_code |= ERR_ALERT | ERR_FATAL;
4007 goto out;
4008 }
4009 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004010 else if (!strcmp(args[0], "event")) {
4011 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004012 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004013 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004014 goto out;
4015 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004016 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
4017 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01004018
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004019 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
4020 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
4021 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
4022 curmsg->event = SPOE_EV_ON_SERVER_SESS;
4023
4024 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
4025 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
4026 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
4027 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
4028 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
4029 curmsg->event = SPOE_EV_ON_TCP_RSP;
4030
4031 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
4032 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
4033 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
4034 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
4035 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
4036 curmsg->event = SPOE_EV_ON_HTTP_RSP;
4037 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08004038 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01004039 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01004040 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004041 goto out;
4042 }
Christopher Faulet57583e42017-09-04 15:41:09 +02004043
4044 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4045 struct acl_cond *cond;
4046
4047 cond = build_acl_cond(file, linenum, &curmsg->acls,
4048 curproxy, (const char **)args+2,
4049 &errmsg);
4050 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004051 ha_alert("parsing [%s:%d] : error detected while "
4052 "parsing an 'event %s' condition : %s.\n",
4053 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004054 err_code |= ERR_ALERT | ERR_FATAL;
4055 goto out;
4056 }
4057 curmsg->cond = cond;
4058 }
4059 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004060 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4061 "or 'unless' followed by a condition but found '%s'.\n",
4062 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004063 err_code |= ERR_ALERT | ERR_FATAL;
4064 goto out;
4065 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004066 }
4067 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004068 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4069 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004070 err_code |= ERR_ALERT | ERR_FATAL;
4071 goto out;
4072 }
4073 out:
4074 free(errmsg);
4075 return err_code;
4076}
4077
4078/* Return -1 on error, else 0 */
4079static int
4080parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4081 struct flt_conf *fconf, char **err, void *private)
4082{
4083 struct list backup_sections;
4084 struct spoe_config *conf;
4085 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004086 struct spoe_group *grp, *grpback;
4087 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004088 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004089 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004090 char *file = NULL, *engine = NULL;
4091 int ret, pos = *cur_arg + 1;
4092
Christopher Faulet84c844e2018-03-23 14:37:14 +01004093 LIST_INIT(&curmsgs);
4094 LIST_INIT(&curgrps);
4095 LIST_INIT(&curmphs);
4096 LIST_INIT(&curgphs);
4097 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004098 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004099 curpxopts = 0;
4100 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004101
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004102 conf = calloc(1, sizeof(*conf));
4103 if (conf == NULL) {
4104 memprintf(err, "%s: out of memory", args[*cur_arg]);
4105 goto error;
4106 }
4107 conf->proxy = px;
4108
4109 while (*args[pos]) {
4110 if (!strcmp(args[pos], "config")) {
4111 if (!*args[pos+1]) {
4112 memprintf(err, "'%s' : '%s' option without value",
4113 args[*cur_arg], args[pos]);
4114 goto error;
4115 }
4116 file = args[pos+1];
4117 pos += 2;
4118 }
4119 else if (!strcmp(args[pos], "engine")) {
4120 if (!*args[pos+1]) {
4121 memprintf(err, "'%s' : '%s' option without value",
4122 args[*cur_arg], args[pos]);
4123 goto error;
4124 }
4125 engine = args[pos+1];
4126 pos += 2;
4127 }
4128 else {
4129 memprintf(err, "unknown keyword '%s'", args[pos]);
4130 goto error;
4131 }
4132 }
4133 if (file == NULL) {
4134 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4135 goto error;
4136 }
4137
4138 /* backup sections and register SPOE sections */
4139 LIST_INIT(&backup_sections);
4140 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004141 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4142 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004143 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004144
4145 /* Parse SPOE filter configuration file */
4146 curengine = engine;
4147 curproxy = px;
4148 curagent = NULL;
4149 curmsg = NULL;
4150 ret = readcfgfile(file);
4151 curproxy = NULL;
4152
4153 /* unregister SPOE sections and restore previous sections */
4154 cfg_unregister_sections();
4155 cfg_restore_sections(&backup_sections);
4156
4157 if (ret == -1) {
4158 memprintf(err, "Could not open configuration file %s : %s",
4159 file, strerror(errno));
4160 goto error;
4161 }
4162 if (ret & (ERR_ABORT|ERR_FATAL)) {
4163 memprintf(err, "Error(s) found in configuration file %s", file);
4164 goto error;
4165 }
4166
4167 /* Check SPOE agent */
4168 if (curagent == NULL) {
4169 memprintf(err, "No SPOE agent found in file %s", file);
4170 goto error;
4171 }
4172 if (curagent->b.name == NULL) {
4173 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4174 curagent->id, curagent->conf.file, curagent->conf.line);
4175 goto error;
4176 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004177 if (curagent->timeout.hello == TICK_ETERNITY ||
4178 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004179 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004180 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4181 " | While not properly invalid, you will certainly encounter various problems\n"
4182 " | with such a configuration. To fix this, please ensure that all following\n"
4183 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4184 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004185 }
4186 if (curagent->var_pfx == NULL) {
4187 char *tmp = curagent->id;
4188
4189 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004190 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004191 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4192 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4193 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4194 goto error;
4195 }
4196 tmp++;
4197 }
4198 curagent->var_pfx = strdup(curagent->id);
4199 }
4200
Christopher Fauletb7426d12018-03-21 14:12:17 +01004201 if (curagent->var_on_error) {
4202 struct arg arg;
4203
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004204 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004205 curagent->var_pfx, curagent->var_on_error);
4206
4207 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004208 arg.data.str.area = trash.area;
4209 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004210 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004211 if (!vars_check_arg(&arg, err)) {
4212 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4213 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4214 goto error;
4215 }
4216 }
4217
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004218 if (curagent->var_t_process) {
4219 struct arg arg;
4220
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004221 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004222 curagent->var_pfx, curagent->var_t_process);
4223
4224 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004225 arg.data.str.area = trash.area;
4226 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004227 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004228 if (!vars_check_arg(&arg, err)) {
4229 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4230 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4231 goto error;
4232 }
4233 }
4234
4235 if (curagent->var_t_total) {
4236 struct arg arg;
4237
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004238 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004239 curagent->var_pfx, curagent->var_t_total);
4240
4241 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004242 arg.data.str.area = trash.area;
4243 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004244 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004245 if (!vars_check_arg(&arg, err)) {
4246 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4247 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4248 goto error;
4249 }
4250 }
4251
Christopher Faulet11610f32017-09-21 10:23:10 +02004252 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004253 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4254 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004255 goto finish;
4256 }
4257
Christopher Faulet11610f32017-09-21 10:23:10 +02004258 /* Replace placeholders by the corresponding messages for the SPOE
4259 * agent */
4260 list_for_each_entry(ph, &curmphs, list) {
4261 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004262 struct spoe_arg *arg;
4263 unsigned int where;
4264
Christopher Faulet11610f32017-09-21 10:23:10 +02004265 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004266 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4267 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4268 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4269 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4270 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4271 }
4272 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4273 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4274 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004275 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4276 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004277 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004278 }
4279 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004280 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4281 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004282 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004283 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004284
4285 where = 0;
4286 switch (msg->event) {
4287 case SPOE_EV_ON_CLIENT_SESS:
4288 where |= SMP_VAL_FE_CON_ACC;
4289 break;
4290
4291 case SPOE_EV_ON_TCP_REQ_FE:
4292 where |= SMP_VAL_FE_REQ_CNT;
4293 break;
4294
4295 case SPOE_EV_ON_HTTP_REQ_FE:
4296 where |= SMP_VAL_FE_HRQ_HDR;
4297 break;
4298
4299 case SPOE_EV_ON_TCP_REQ_BE:
4300 if (px->cap & PR_CAP_FE)
4301 where |= SMP_VAL_FE_REQ_CNT;
4302 if (px->cap & PR_CAP_BE)
4303 where |= SMP_VAL_BE_REQ_CNT;
4304 break;
4305
4306 case SPOE_EV_ON_HTTP_REQ_BE:
4307 if (px->cap & PR_CAP_FE)
4308 where |= SMP_VAL_FE_HRQ_HDR;
4309 if (px->cap & PR_CAP_BE)
4310 where |= SMP_VAL_BE_HRQ_HDR;
4311 break;
4312
4313 case SPOE_EV_ON_SERVER_SESS:
4314 where |= SMP_VAL_BE_SRV_CON;
4315 break;
4316
4317 case SPOE_EV_ON_TCP_RSP:
4318 if (px->cap & PR_CAP_FE)
4319 where |= SMP_VAL_FE_RES_CNT;
4320 if (px->cap & PR_CAP_BE)
4321 where |= SMP_VAL_BE_RES_CNT;
4322 break;
4323
4324 case SPOE_EV_ON_HTTP_RSP:
4325 if (px->cap & PR_CAP_FE)
4326 where |= SMP_VAL_FE_HRS_HDR;
4327 if (px->cap & PR_CAP_BE)
4328 where |= SMP_VAL_BE_HRS_HDR;
4329 break;
4330
4331 default:
4332 break;
4333 }
4334
4335 list_for_each_entry(arg, &msg->args, list) {
4336 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004337 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004338 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004339 "none of which is available here ('%s')",
4340 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004341 sample_ckp_names(arg->expr->fetch->use),
4342 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004343 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004344 }
4345 }
4346
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004347 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004348 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4349 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004350 }
4351 }
4352 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004353 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004354 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004355 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004356 continue;
4357 }
4358
Christopher Faulet11610f32017-09-21 10:23:10 +02004359 /* Replace placeholders by the corresponding groups for the SPOE
4360 * agent */
4361 list_for_each_entry(ph, &curgphs, list) {
4362 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4363 if (!strcmp(grp->id, ph->id)) {
4364 grp->agent = curagent;
4365 LIST_DEL(&grp->list);
4366 LIST_ADDQ(&curagent->groups, &grp->list);
4367 goto next_aph;
4368 }
4369 }
4370 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4371 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4372 goto error;
4373 next_aph:
4374 continue;
4375 }
4376
4377 /* Replace placeholders by the corresponding message for each SPOE
4378 * group of the SPOE agent */
4379 list_for_each_entry(grp, &curagent->groups, list) {
4380 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4381 list_for_each_entry(msg, &curmsgs, list) {
4382 if (!strcmp(msg->id, ph->id)) {
4383 if (msg->group != NULL) {
4384 memprintf(err, "SPOE message '%s' already belongs to "
4385 "the SPOE group '%s' declare at %s:%d",
4386 msg->id, msg->group->id,
4387 msg->group->conf.file,
4388 msg->group->conf.line);
4389 goto error;
4390 }
4391
4392 /* Scope for arguments are not checked for now. We will check
4393 * them only if a rule use the corresponding SPOE group. */
4394 msg->agent = curagent;
4395 msg->group = grp;
4396 LIST_DEL(&ph->list);
4397 LIST_ADDQ(&grp->messages, &msg->by_grp);
4398 goto next_mph_grp;
4399 }
4400 }
4401 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4402 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4403 goto error;
4404 next_mph_grp:
4405 continue;
4406 }
4407 }
4408
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004409 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004410 /* move curmsgs to the agent message list */
4411 curmsgs.n->p = &curagent->messages;
4412 curmsgs.p->n = &curagent->messages;
4413 curagent->messages = curmsgs;
4414 LIST_INIT(&curmsgs);
4415
Christopher Faulet7ee86672017-09-19 11:08:28 +02004416 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004417 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004418
4419 /* Start agent's proxy initialization here. It will be finished during
4420 * the filter init. */
4421 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4422 init_new_proxy(&conf->agent_fe);
4423 conf->agent_fe.id = conf->agent->id;
4424 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004425 conf->agent_fe.options |= curpxopts;
4426 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004427
4428 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4429 LIST_DEL(&logsrv->list);
4430 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4431 }
4432
Christopher Faulet11610f32017-09-21 10:23:10 +02004433 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4434 LIST_DEL(&ph->list);
4435 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004436 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004437 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4438 LIST_DEL(&ph->list);
4439 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004440 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004441 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4442 struct arg arg;
4443
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004444 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004445 curagent->var_pfx, vph->name);
4446
4447 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004448 arg.data.str.area = trash.area;
4449 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004450 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004451 if (!vars_check_arg(&arg, err)) {
4452 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4453 curagent->id, curagent->var_pfx, vph->name, *err);
4454 goto error;
4455 }
4456
4457 LIST_DEL(&vph->list);
4458 free(vph->name);
4459 free(vph);
4460 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004461 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4462 LIST_DEL(&grp->list);
4463 spoe_release_group(grp);
4464 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004465 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004466 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004467 fconf->ops = &spoe_ops;
4468 fconf->conf = conf;
4469 return 0;
4470
4471 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004472 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004473 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4474 LIST_DEL(&ph->list);
4475 spoe_release_placeholder(ph);
4476 }
4477 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4478 LIST_DEL(&ph->list);
4479 spoe_release_placeholder(ph);
4480 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004481 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4482 LIST_DEL(&vph->list);
4483 free(vph->name);
4484 free(vph);
4485 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004486 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4487 LIST_DEL(&grp->list);
4488 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004489 }
4490 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4491 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004492 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004493 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004494 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4495 LIST_DEL(&logsrv->list);
4496 free(logsrv);
4497 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004498 free(conf);
4499 return -1;
4500}
4501
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004502/* Send message of a SPOE group. This is the action_ptr callback of a rule
4503 * associated to a "send-spoe-group" action.
4504 *
Christopher Faulet13403762019-12-13 09:01:57 +01004505 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4506 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004507static enum act_return
4508spoe_send_group(struct act_rule *rule, struct proxy *px,
4509 struct session *sess, struct stream *s, int flags)
4510{
4511 struct filter *filter;
4512 struct spoe_agent *agent = NULL;
4513 struct spoe_group *group = NULL;
4514 struct spoe_context *ctx = NULL;
4515 int ret, dir;
4516
4517 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4518 if (filter->config == rule->arg.act.p[0]) {
4519 agent = rule->arg.act.p[2];
4520 group = rule->arg.act.p[3];
4521 ctx = filter->ctx;
4522 break;
4523 }
4524 }
4525 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004526 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004527 if (ctx->state == SPOE_CTX_ST_NONE)
4528 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004529
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004530 switch (rule->from) {
4531 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4532 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4533 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4534 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4535 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4536 default:
4537 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4538 " - internal error while execute spoe-send-group\n",
4539 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4540 __FUNCTION__, s);
4541 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4542 agent->id);
4543 return ACT_RET_CONT;
4544 }
4545
4546 ret = spoe_process_group(s, ctx, group, dir);
4547 if (ret == 1)
4548 return ACT_RET_CONT;
4549 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004550 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004551 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4552 " - failed to process group '%s': interrupted by caller\n",
4553 (int)now.tv_sec, (int)now.tv_usec,
4554 agent->id, __FUNCTION__, s, group->id);
4555 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004556 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004557 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004558 return ACT_RET_CONT;
4559 }
4560 return ACT_RET_YIELD;
4561 }
4562 else
Christopher Faulet13403762019-12-13 09:01:57 +01004563 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004564}
4565
4566/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4567 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4568 * action should be:
4569 *
4570 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4571 *
4572 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4573 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4574 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4575 * group.
4576 *
4577 * The function returns 1 in success case, otherwise, it returns 0 and err is
4578 * filled.
4579 */
4580static int
4581check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4582{
4583 struct flt_conf *fconf;
4584 struct spoe_config *conf;
4585 struct spoe_agent *agent = NULL;
4586 struct spoe_group *group;
4587 struct spoe_message *msg;
4588 char *engine_id = rule->arg.act.p[0];
4589 char *group_id = rule->arg.act.p[1];
4590 unsigned int where = 0;
4591
4592 switch (rule->from) {
4593 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4594 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4595 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4596 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4597 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4598 default:
4599 memprintf(err,
4600 "internal error, unexpected rule->from=%d, please report this bug!",
4601 rule->from);
4602 goto error;
4603 }
4604
4605 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4606 * <px> */
4607 list_for_each_entry(fconf, &px->filter_configs, list) {
4608 conf = fconf->conf;
4609
4610 /* This is not an SPOE filter */
4611 if (fconf->id != spoe_filter_id)
4612 continue;
4613
4614 /* This is the good engine */
4615 if (!strcmp(conf->id, engine_id)) {
4616 agent = conf->agent;
4617 break;
4618 }
4619 }
4620 if (agent == NULL) {
4621 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4622 engine_id, group_id);
4623 goto error;
4624 }
4625
4626 /* Try to find the right group */
4627 list_for_each_entry(group, &agent->groups, list) {
4628 /* This is the good group */
4629 if (!strcmp(group->id, group_id))
4630 break;
4631 }
4632 if (&group->list == &agent->groups) {
4633 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4634 group_id, engine_id);
4635 goto error;
4636 }
4637
4638 /* Ok, we found the group, we need to check messages and their
4639 * arguments */
4640 list_for_each_entry(msg, &group->messages, by_grp) {
4641 struct spoe_arg *arg;
4642
4643 list_for_each_entry(arg, &msg->args, list) {
4644 if (!(arg->expr->fetch->val & where)) {
4645 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4646 "some args extract information from '%s',"
4647 "none of which is available here ('%s')",
4648 msg->id, group->id, msg->conf.file, msg->conf.line,
4649 sample_ckp_names(arg->expr->fetch->use),
4650 sample_ckp_names(where));
4651 goto error;
4652 }
4653 }
4654 }
4655
4656 free(engine_id);
4657 free(group_id);
4658 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4659 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4660 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4661 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4662 return 1;
4663
4664 error:
4665 free(engine_id);
4666 free(group_id);
4667 return 0;
4668}
4669
4670/* Parse 'send-spoe-group' action following the format:
4671 *
4672 * ... send-spoe-group <engine-id> <group-id>
4673 *
4674 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4675 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4676 * ids are saved and used later, when the rule will be checked.
4677 */
4678static enum act_parse_ret
4679parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4680 struct act_rule *rule, char **err)
4681{
4682 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4683 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4684 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4685 return ACT_RET_PRS_ERR;
4686 }
4687 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4688 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4689
4690 (*orig_arg) += 2;
4691
4692 rule->action = ACT_CUSTOM;
4693 rule->action_ptr = spoe_send_group;
4694 rule->check_ptr = check_send_spoe_group;
4695 return ACT_RET_PRS_OK;
4696}
4697
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004698
4699/* Declare the filter parser for "spoe" keyword */
4700static struct flt_kw_list flt_kws = { "SPOE", { }, {
4701 { "spoe", parse_spoe_flt, NULL },
4702 { NULL, NULL, NULL },
4703 }
4704};
4705
Willy Tarreau0108d902018-11-25 19:14:37 +01004706INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4707
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004708/* Delcate the action parser for "spoe-action" keyword */
4709static struct action_kw_list tcp_req_action_kws = { { }, {
4710 { "send-spoe-group", parse_send_spoe_group },
4711 { /* END */ },
4712 }
4713};
Willy Tarreau0108d902018-11-25 19:14:37 +01004714
4715INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4716
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004717static struct action_kw_list tcp_res_action_kws = { { }, {
4718 { "send-spoe-group", parse_send_spoe_group },
4719 { /* END */ },
4720 }
4721};
Willy Tarreau0108d902018-11-25 19:14:37 +01004722
4723INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4724
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004725static struct action_kw_list http_req_action_kws = { { }, {
4726 { "send-spoe-group", parse_send_spoe_group },
4727 { /* END */ },
4728 }
4729};
Willy Tarreau0108d902018-11-25 19:14:37 +01004730
4731INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4732
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004733static struct action_kw_list http_res_action_kws = { { }, {
4734 { "send-spoe-group", parse_send_spoe_group },
4735 { /* END */ },
4736 }
4737};
4738
Willy Tarreau0108d902018-11-25 19:14:37 +01004739INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);