blob: 7d36a8e0ce90cae211d8abc4eb61776c6cc807a6 [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>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020018#include <common/cfgparse.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020019#include <haproxy/frontend.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020020#include <haproxy/http_rules.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020021#include <haproxy/sample.h>
Willy Tarreau3727a8a2020-06-04 17:37:26 +020022#include <haproxy/signal.h>
Willy Tarreau3f567e42020-05-28 15:29:19 +020023#include <haproxy/thread.h>
Willy Tarreaud0ef4392020-06-02 09:38:52 +020024#include <haproxy/pool.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020025#include <haproxy/session.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020026#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020027#include <haproxy/tcp_rules.h>
Willy Tarreau92b4f132020-06-01 11:05:15 +020028#include <haproxy/time.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020029#include <haproxy/vars.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020030
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020031#include <haproxy/arg-t.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020032#include <haproxy/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010033#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020034
Willy Tarreauaa74c4e2020-06-04 10:19:23 +020035#include <haproxy/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020036#include <proto/backend.h>
37#include <proto/filters.h>
Willy Tarreau66347942020-06-01 12:18:08 +020038#include <haproxy/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020039#include <proto/log.h>
Christopher Fauletfc9cfe42019-07-16 14:54:53 +020040#include <proto/http_ana.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020041#include <proto/proxy.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010042#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020043#include <proto/stream.h>
44#include <proto/stream_interface.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020045
46#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
47#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010048#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020049#else
50#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010051#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020052#endif
53
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010054/* Reserved 4 bytes to the frame size. So a frame and its size can be written
55 * together in a buffer */
56#define MAX_FRAME_SIZE global.tune.bufsize - 4
57
58/* The minimum size for a frame */
59#define MIN_FRAME_SIZE 256
60
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010061/* Reserved for the metadata and the frame type.
62 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010063#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020064
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010065/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010066#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
67
Christopher Faulet3b386a32017-02-23 10:17:15 +010068/* SPOE filter id. Used to identify SPOE filters */
69const char *spoe_filter_id = "SPOE filter";
70
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020071/* Set if the handle on SIGUSR1 is registered */
72static int sighandler_registered = 0;
73
74/* proxy used during the parsing */
75struct proxy *curproxy = NULL;
76
77/* The name of the SPOE engine, used during the parsing */
78char *curengine = NULL;
79
80/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020081/* SPOE agent/group/message used during the parsing */
82struct spoe_agent *curagent = NULL;
83struct spoe_group *curgrp = NULL;
84struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020085
86/* list of SPOE messages and placeholders used during the parsing */
87struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020088struct list curgrps;
89struct list curmphs;
90struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010091struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020092
Christopher Faulet7250b8f2018-03-26 17:19:01 +020093/* list of log servers used during the parsing */
94struct list curlogsrvs;
95
Christopher Faulet0e0f0852018-03-26 17:20:36 +020096/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
97int curpxopts;
98int curpxopts2;
99
Christopher Faulet42bfa462017-01-04 14:14:19 +0100100/* Pools used to allocate SPOE structs */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100101DECLARE_STATIC_POOL(pool_head_spoe_ctx, "spoe_ctx", sizeof(struct spoe_context));
102DECLARE_STATIC_POOL(pool_head_spoe_appctx, "spoe_appctx", sizeof(struct spoe_appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200103
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200104struct flt_ops spoe_ops;
105
Christopher Faulet8ef75252017-02-20 22:56:03 +0100106static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200107static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
108static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200109
110/********************************************************************
111 * helper functions/globals
112 ********************************************************************/
113static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200114spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200115{
Christopher Faulet11610f32017-09-21 10:23:10 +0200116 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200117 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200118 free(ph->id);
119 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200120}
121
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200122static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100123spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200124{
Christopher Faulet57583e42017-09-04 15:41:09 +0200125 struct spoe_arg *arg, *argback;
126 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200127
128 if (!msg)
129 return;
130 free(msg->id);
131 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200132 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200133 release_sample_expr(arg->expr);
134 free(arg->name);
135 LIST_DEL(&arg->list);
136 free(arg);
137 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200138 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
139 LIST_DEL(&acl->list);
140 prune_acl(acl);
141 free(acl);
142 }
143 if (msg->cond) {
144 prune_acl_cond(msg->cond);
145 free(msg->cond);
146 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200147 free(msg);
148}
149
150static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200151spoe_release_group(struct spoe_group *grp)
152{
153 if (!grp)
154 return;
155 free(grp->id);
156 free(grp->conf.file);
157 free(grp);
158}
159
160static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100161spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200162{
Christopher Faulet11610f32017-09-21 10:23:10 +0200163 struct spoe_message *msg, *msgback;
164 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200165 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200166
167 if (!agent)
168 return;
169 free(agent->id);
170 free(agent->conf.file);
171 free(agent->var_pfx);
Christopher Faulet985532d2016-11-16 15:36:19 +0100172 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100173 free(agent->var_t_process);
174 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200175 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
176 LIST_DEL(&msg->list);
177 spoe_release_message(msg);
178 }
179 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
180 LIST_DEL(&grp->list);
181 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200182 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100183 if (agent->rt) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200184 for (i = 0; i < global.nbthread; ++i) {
185 free(agent->rt[i].engine_id);
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100186 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200187 }
Willy Tarreau3ddcf762019-02-07 14:22:52 +0100188 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200189 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200190 free(agent);
191}
192
193static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100194 [SPOE_FRM_ERR_NONE] = "normal",
195 [SPOE_FRM_ERR_IO] = "I/O error",
196 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
197 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
198 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
199 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
200 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
201 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
202 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
203 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
204 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
205 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100206 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100207 [SPOE_FRM_ERR_RES] = "resource allocation error",
208 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200209};
210
211static const char *spoe_event_str[SPOE_EV_EVENTS] = {
212 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
213 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
214 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
215 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
216 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
217
218 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
219 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
220 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
221};
222
223
224#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
225
226static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100227 [SPOE_CTX_ST_NONE] = "NONE",
228 [SPOE_CTX_ST_READY] = "READY",
229 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
230 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
231 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
232 [SPOE_CTX_ST_DONE] = "DONE",
233 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200234};
235
236static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100237 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
238 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
239 [SPOE_APPCTX_ST_IDLE] = "IDLE",
240 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
241 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
242 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
243 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
244 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
245 [SPOE_APPCTX_ST_EXIT] = "EXIT",
246 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200247};
248
249#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100250
Christopher Faulet8ef75252017-02-20 22:56:03 +0100251/* Used to generates a unique id for an engine. On success, it returns a
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500252 * allocated string. So it is the caller's responsibility to release it. If the
Christopher Faulet8ef75252017-02-20 22:56:03 +0100253 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100254static char *
255generate_pseudo_uuid()
256{
Willy Tarreauee3bcdd2020-03-08 17:48:17 +0100257 ha_generate_uuid(&trash);
Kevin Zhu079f8082020-03-13 10:39:51 +0800258 return my_strndup(trash.area, trash.data);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100259}
260
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100261
262static inline void
263spoe_update_stat_time(struct timeval *tv, long *t)
264{
265 if (*t == -1)
266 *t = tv_ms_elapsed(tv, &now);
267 else
268 *t += tv_ms_elapsed(tv, &now);
269 tv_zero(tv);
270}
271
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200272/********************************************************************
273 * Functions that encode/decode SPOE frames
274 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200275/* Helper to get static string length, excluding the terminating null byte */
276#define SLEN(str) (sizeof(str)-1)
277
278/* Predefined key used in HELLO/DISCONNECT frames */
279#define SUPPORTED_VERSIONS_KEY "supported-versions"
280#define VERSION_KEY "version"
281#define MAX_FRAME_SIZE_KEY "max-frame-size"
282#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100283#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100284#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200285#define STATUS_CODE_KEY "status-code"
286#define MSG_KEY "message"
287
288struct spoe_version {
289 char *str;
290 int min;
291 int max;
292};
293
294/* All supported versions */
295static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200296 /* 1.0 is now unsupported because of a bug about frame's flags*/
297 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200298 {NULL, 0, 0}
299};
300
301/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200302#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200303
Christopher Faulet8ef75252017-02-20 22:56:03 +0100304/* Convert a string to a SPOE version value. The string must follow the format
305 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
306 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100308spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200309{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100310 const char *p, *end;
311 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200312
Christopher Faulet8ef75252017-02-20 22:56:03 +0100313 p = str;
314 end = str+len;
315 maj = min = 0;
316 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200317
Christopher Faulet8ef75252017-02-20 22:56:03 +0100318 /* skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100319 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100320 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200321
Christopher Faulet8ef75252017-02-20 22:56:03 +0100322 /* parse Major number, until the '.' */
323 while (*p != '.') {
324 if (p >= end || *p < '0' || *p > '9')
325 goto out;
326 maj *= 10;
327 maj += (*p - '0');
328 p++;
329 }
330
331 /* check Major version */
332 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200333 goto out;
334
Christopher Faulet8ef75252017-02-20 22:56:03 +0100335 p++; /* skip the '.' */
336 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
337 goto out;
338
339 /* Parse Minor number */
340 while (p < end) {
341 if (*p < '0' || *p > '9')
342 break;
343 min *= 10;
344 min += (*p - '0');
345 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200346 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100347
348 /* check Minor number */
349 if (min > 999)
350 goto out;
351
352 /* skip trailing spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100353 while (p < end && isspace((unsigned char)*p))
Christopher Faulet8ef75252017-02-20 22:56:03 +0100354 p++;
355 if (p != end)
356 goto out;
357
358 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200359 out:
360 return vsn;
361}
362
Christopher Faulet8ef75252017-02-20 22:56:03 +0100363/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
Joseph Herlantf7f60312018-11-15 13:46:49 -0800364 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
Christopher Faulet8ef75252017-02-20 22:56:03 +0100365 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200366static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100367spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200368{
Willy Tarreau83061a82018-07-13 11:56:34 +0200369 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100370 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100371 char *p, *end;
372 unsigned int flags = SPOE_FRM_FL_FIN;
373 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200374
Christopher Faulet8ef75252017-02-20 22:56:03 +0100375 p = frame;
376 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200377
Christopher Faulet8ef75252017-02-20 22:56:03 +0100378 /* Set Frame type */
379 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200380
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100381 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200382 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100383 memcpy(p, (char *)&flags, 4);
384 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200385
386 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100387 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388
389 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
390 * and "capabilities" */
391
392 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100393 sz = SLEN(SUPPORTED_VERSIONS_KEY);
394 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
395 goto too_big;
396
397 *p++ = SPOE_DATA_T_STR;
398 sz = SLEN(SUPPORTED_VERSIONS_VAL);
399 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
400 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200401
402 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100403 sz = SLEN(MAX_FRAME_SIZE_KEY);
404 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
405 goto too_big;
406
407 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200408 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100409 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200410
411 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100412 sz = SLEN(CAPABILITIES_KEY);
413 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
414 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200415
Christopher Faulet8ef75252017-02-20 22:56:03 +0100416 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100417 chk = get_trash_chunk();
418 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200419 memcpy(chk->area, "pipelining", 10);
420 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100421 }
422 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200423 if (chk->data) chk->area[chk->data++] = ',';
424 memcpy(chk->area+chk->data, "async", 5);
425 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100426 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100427 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200428 if (chk->data) chk->area[chk->data++] = ',';
429 memcpy(chk->area+chk->data, "fragmentation", 13);
Miroslav Zagorac6b3690b2019-01-13 16:55:01 +0100430 chk->data += 13;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100431 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200432 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100433 goto too_big;
434
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +0500435 /* (optional) "engine-id" K/V item, if present */
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200436 if (agent != NULL && agent->rt[tid].engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100437 sz = SLEN(ENGINE_ID_KEY);
438 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
439 goto too_big;
440
441 *p++ = SPOE_DATA_T_STR;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +0200442 sz = strlen(agent->rt[tid].engine_id);
443 if (spoe_encode_buffer(agent->rt[tid].engine_id, sz, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100444 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100445 }
446
Christopher Faulet8ef75252017-02-20 22:56:03 +0100447 return (p - frame);
448
449 too_big:
450 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
451 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200452}
453
Christopher Faulet8ef75252017-02-20 22:56:03 +0100454/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
455 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
456 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200457static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100458spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200459{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100460 const char *reason;
461 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100462 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100463 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200464
Christopher Faulet8ef75252017-02-20 22:56:03 +0100465 p = frame;
466 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200467
Christopher Faulet8ef75252017-02-20 22:56:03 +0100468 /* Set Frame type */
469 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200470
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100471 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200472 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100473 memcpy(p, (char *)&flags, 4);
474 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200475
476 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100477 *p++ = 0; *p++ = 0;
478
479 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
480 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200481
482 /* There are 2 mandatory items: "status-code" and "message" */
483
484 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100485 sz = SLEN(STATUS_CODE_KEY);
486 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
487 goto too_big;
488
489 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200490 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100491 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200492
493 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100494 sz = SLEN(MSG_KEY);
495 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
496 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200497
Christopher Faulet8ef75252017-02-20 22:56:03 +0100498 /*Get the message corresponding to the status code */
499 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
500
501 *p++ = SPOE_DATA_T_STR;
502 sz = strlen(reason);
503 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
504 goto too_big;
505
506 return (p - frame);
507
508 too_big:
509 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
510 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200511}
512
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
514 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
515 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200516static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100517spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100518 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200519{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100520 char *p, *end;
521 unsigned int stream_id, frame_id;
522 unsigned int flags = SPOE_FRM_FL_FIN;
523 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200524
Christopher Faulet8ef75252017-02-20 22:56:03 +0100525 p = frame;
526 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200527
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100528 stream_id = ctx->stream_id;
529 frame_id = ctx->frame_id;
530
531 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
532 /* The fragmentation is not supported by the applet */
533 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
534 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
535 return -1;
536 }
537 flags = ctx->frag_ctx.flags;
538 }
539
540 /* Set Frame type */
541 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
542
543 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200544 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100545 memcpy(p, (char *)&flags, 4);
546 p += 4;
547
548 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200549 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100550 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200551 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100552 goto too_big;
553
554 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200555 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100556 if (p + sz >= end)
557 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200558 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100559 p += sz;
560
561 return (p - frame);
562
563 too_big:
564 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
565 return 0;
566}
567
568/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
569 * returns the number of encoded bytes in the frame on success, 0 if an encoding
570 * error occurred and -1 if a fatal error occurred. */
571static int
572spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
573 char *frame, size_t size)
574{
575 char *p, *end;
576 unsigned int stream_id, frame_id;
577 unsigned int flags;
578 size_t sz;
579
580 p = frame;
581 end = frame+size;
582
Christopher Faulet8ef75252017-02-20 22:56:03 +0100583 /* <ctx> is null when the stream has aborted the processing of a
584 * fragmented frame. In this case, we must notify the corresponding
585 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100586 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100587 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100588 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
589 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
590 }
591 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100592 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100593 stream_id = ctx->stream_id;
594 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100595 }
596
Christopher Faulet8ef75252017-02-20 22:56:03 +0100597 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100598 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100599
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100600 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200601 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100602 memcpy(p, (char *)&flags, 4);
603 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200604
605 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200606 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100607 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200608 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100609 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200610
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100611 if (ctx == NULL)
612 goto end;
613
Christopher Faulet8ef75252017-02-20 22:56:03 +0100614 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200615 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100616 if (p + sz >= end)
617 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200618 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100620
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100621 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100622 return (p - frame);
623
624 too_big:
625 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
626 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200627}
628
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629/* Decode and process the HELLO frame sent by an agent. It returns the number of
630 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
631 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100633spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200634{
Christopher Faulet305c6072017-02-23 16:17:53 +0100635 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
636 char *p, *end;
637 int vsn, max_frame_size;
638 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100639
640 p = frame;
641 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200642
643 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
645 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200646 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100647 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200648
Christopher Faulet8ef75252017-02-20 22:56:03 +0100649 if (size < 7 /* TYPE + METADATA */) {
650 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
651 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200652 }
653
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100654 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100655 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200656 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100657 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200658
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100659 /* Fragmentation is not supported for HELLO frame */
660 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100661 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100662 return -1;
663 }
664
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200665 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100666 if (*p != 0 || *(p+1) != 0) {
667 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
668 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200669 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100670 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200671
672 /* There are 3 mandatory items: "version", "max-frame-size" and
673 * "capabilities" */
674
675 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100676 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 while (p < end) {
678 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200679 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100680 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200681
682 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683 ret = spoe_decode_buffer(&p, end, &str, &sz);
684 if (ret == -1 || !sz) {
685 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
686 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200689 /* Check "version" K/V item */
690 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100691 int i, type = *p++;
692
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100694 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
695 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
696 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200697 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100698 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
699 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
700 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200701 }
702
Christopher Faulet8ef75252017-02-20 22:56:03 +0100703 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200704 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100705 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200706 return -1;
707 }
708 for (i = 0; supported_versions[i].str != NULL; ++i) {
709 if (vsn >= supported_versions[i].min &&
710 vsn <= supported_versions[i].max)
711 break;
712 }
713 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100714 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200715 return -1;
716 }
717 }
718 /* Check "max-frame-size" K/V item */
719 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200721
722 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200723 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
724 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
725 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
726 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
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 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200730 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100731 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
732 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200733 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100734 if (sz < MIN_FRAME_SIZE ||
735 sz > SPOE_APPCTX(appctx)->max_frame_size) {
736 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200737 return -1;
738 }
739 max_frame_size = sz;
740 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100741 /* Check "capabilities" K/V item */
742 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100743 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100744
745 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100746 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
747 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
748 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100749 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100750 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
751 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
752 return 0;
753 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100754
Christopher Faulet8ef75252017-02-20 22:56:03 +0100755 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100756 char *delim;
757
758 /* Skip leading spaces */
Willy Tarreau90807112020-02-25 08:16:33 +0100759 for (; isspace((unsigned char)*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100760
Christopher Faulet8ef75252017-02-20 22:56:03 +0100761 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
762 str += 10; sz -= 10;
Willy Tarreau90807112020-02-25 08:16:33 +0100763 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100764 flags |= SPOE_APPCTX_FL_PIPELINING;
765 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100766 else if (sz >= 5 && !strncmp(str, "async", 5)) {
767 str += 5; sz -= 5;
Willy Tarreau90807112020-02-25 08:16:33 +0100768 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100769 flags |= SPOE_APPCTX_FL_ASYNC;
770 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100771 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
772 str += 13; sz -= 13;
Willy Tarreau90807112020-02-25 08:16:33 +0100773 if (!sz || isspace((unsigned char)*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100774 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
775 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 /* Get the next comma or break */
778 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100779 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100780 delim++;
781 sz -= (delim - str);
782 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100783 }
784 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200785 else {
786 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100787 if (spoe_skip_data(&p, end) == -1) {
788 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
789 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200790 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200791 }
792 }
793
794 /* Final checks */
795 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100796 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200797 return -1;
798 }
799 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100800 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200801 return -1;
802 }
Christopher Faulet11389012019-02-07 16:13:26 +0100803 if (!agent)
804 flags &= ~(SPOE_APPCTX_FL_PIPELINING|SPOE_APPCTX_FL_ASYNC);
805 else {
806 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
807 flags &= ~SPOE_APPCTX_FL_PIPELINING;
808 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
809 flags &= ~SPOE_APPCTX_FL_ASYNC;
810 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200811
Christopher Faulet42bfa462017-01-04 14:14:19 +0100812 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
813 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
814 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100815
816 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200817}
818
819/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
820 * bytes on success, 0 if the frame can be ignored and -1 if an error
821 * occurred. */
822static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100823spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200824{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100825 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100826 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100827
828 p = frame;
829 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200830
831 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100832 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
833 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200834 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100835 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200836
Christopher Faulet8ef75252017-02-20 22:56:03 +0100837 if (size < 7 /* TYPE + METADATA */) {
838 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
839 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200840 }
841
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100842 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200844 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100845 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200846
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100847 /* Fragmentation is not supported for DISCONNECT frame */
848 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100849 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100850 return -1;
851 }
852
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200853 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100854 if (*p != 0 || *(p+1) != 0) {
855 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
856 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200857 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100858 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200859
860 /* There are 2 mandatory items: "status-code" and "message" */
861
862 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100863 while (p < end) {
864 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200865 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100866 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200867
868 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100869 ret = spoe_decode_buffer(&p, end, &str, &sz);
870 if (ret == -1 || !sz) {
871 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
872 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200873 }
874
875 /* Check "status-code" K/V item */
876 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100877 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200878
879 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200880 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
881 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
882 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
883 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
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 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200887 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100888 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
889 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200890 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100891 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200892 }
893
894 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 else if (!memcmp(str, MSG_KEY, sz)) {
896 int type = *p++;
897
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100899 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
900 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
901 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200902 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100903 ret = spoe_decode_buffer(&p, end, &str, &sz);
904 if (ret == -1 || sz > 255) {
905 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
906 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200907 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100908#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
909 SPOE_APPCTX(appctx)->reason = str;
910 SPOE_APPCTX(appctx)->rlen = sz;
911#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200912 }
913 else {
914 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100915 if (spoe_skip_data(&p, end) == -1) {
916 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
917 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200918 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200919 }
920 }
921
Christopher Faulet8ef75252017-02-20 22:56:03 +0100922 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200923}
924
925
Christopher Fauleta1cda022016-12-21 08:58:06 +0100926/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200927 * success, 0 if the frame can be ignored and -1 if an error occurred. */
928static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100929spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100930 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200931{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100932 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100934 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100935 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100936 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100937
938 p = frame;
939 end = frame + size;
940 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941
942 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100943 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
944 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200945 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100946 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200947
Christopher Faulet8ef75252017-02-20 22:56:03 +0100948 if (size < 7 /* TYPE + METADATA */) {
949 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
950 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200951 }
952
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100953 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100954 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200955 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100956 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200957
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100958 /* Fragmentation is not supported for now */
959 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100960 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100961 return -1;
962 }
963
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200964 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200965 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100967 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100968 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200969 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100970 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200971 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100973
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100975 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200976 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100977 if ((*ctx)->stream_id == (unsigned int)stream_id &&
978 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100979 goto found;
980 }
981 }
982 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100983 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
984 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +0100985 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100986 goto found;
987 }
988 }
989
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100990 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
991 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
992 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
993
994 /* ABRT bit is set for an unfinished fragmented frame */
995 if (flags & SPOE_FRM_FL_ABRT) {
996 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100997 (*ctx)->state = SPOE_CTX_ST_ERROR;
998 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
999 /* Ignore the payload */
1000 goto end;
1001 }
1002 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1003 /* For now, we ignore the ack */
1004 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1005 return 0;
1006 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001007
Christopher Fauleta1cda022016-12-21 08:58:06 +01001008 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001009 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1010 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001011 " - stream-id=%u - frame-id=%u\n",
1012 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1013 __FUNCTION__, appctx,
1014 (unsigned int)stream_id, (unsigned int)frame_id);
1015
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001016 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001017 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1018 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001019 return 0;
1020
1021 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001022 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1023 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001024 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001025 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001026 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001027
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001028 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001029 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001030 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1031 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001032 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001033
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001034 /* Transfer the buffer ownership to the SPOE context */
1035 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001036 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001037
Christopher Faulet8ef75252017-02-20 22:56:03 +01001038 (*ctx)->state = SPOE_CTX_ST_DONE;
1039
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001040 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001041 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001042 " - ACK frame received"
1043 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001044 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001045 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1046 (*ctx)->frame_id, flags);
1047 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001048}
1049
Christopher Fauletba7bc162016-11-07 21:07:38 +01001050/* This function is used in cfgparse.c and declared in proto/checks.h. It
1051 * prepare the request to send to agents during a healthcheck. It returns 0 on
1052 * success and -1 if an error occurred. */
1053int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001054spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001055{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 struct appctx appctx;
1057 struct spoe_appctx spoe_appctx;
1058 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1059 size_t sz;
1060 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001061
Christopher Faulet42bfa462017-01-04 14:14:19 +01001062 memset(&appctx, 0, sizeof(appctx));
1063 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001064 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001065
1066 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001067 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068
Christopher Faulet8ef75252017-02-20 22:56:03 +01001069 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1070 end = frame + MAX_FRAME_SIZE;
1071
1072 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1073 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001074 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001075 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001076
Christopher Faulet8ef75252017-02-20 22:56:03 +01001077 /* Add "healthcheck" K/V item */
1078 sz = SLEN(HEALTHCHECK_KEY);
1079 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1080 return -1;
1081 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082
Christopher Faulet8ef75252017-02-20 22:56:03 +01001083 *len = frame - buf;
1084 sz = htonl(*len - 4);
1085 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001088 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001089 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001090 return 0;
1091}
1092
1093/* This function is used in checks.c and declared in proto/checks.h. It decode
1094 * the response received from an agent during a healthcheck. It returns 0 on
1095 * success and -1 if an error occurred. */
1096int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001097spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001098{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001099 struct appctx appctx;
1100 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001101
Christopher Faulet42bfa462017-01-04 14:14:19 +01001102 memset(&appctx, 0, sizeof(appctx));
1103 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104
Christopher Faulet42bfa462017-01-04 14:14:19 +01001105 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001106 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001107
Christopher Faulet8ef75252017-02-20 22:56:03 +01001108 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1109 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001110 goto error;
1111 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001112 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1113 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001114
1115 return 0;
1116
1117 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001118 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1119 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1120 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001121 return -1;
1122}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001123
Christopher Fauleta1cda022016-12-21 08:58:06 +01001124/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05001125 * the frame can be ignored, 1 to retry later, and the frame length on
Christopher Fauleta1cda022016-12-21 08:58:06 +01001126 * success. */
1127static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001128spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001129{
1130 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001131 int ret;
1132 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001133
Christopher Faulet8ef75252017-02-20 22:56:03 +01001134 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1135 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001136 netint = htonl(framesz);
1137 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001138 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001139 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001140 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreaudb398432018-11-15 11:08:52 +01001141 si_rx_room_blk(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001143 }
1144 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001145 return -1; /* error */
1146 }
1147 return framesz;
1148}
1149
1150/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1151 * when the frame can be ignored, 1 to retry later and the frame length on
1152 * success. */
1153static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001154spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001155{
1156 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001157 int ret;
1158 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001159
Willy Tarreau06d80a92017-10-19 14:32:15 +02001160 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001161 if (ret > 0) {
1162 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001163 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001164 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001165 return -1;
1166 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001167 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001168 }
1169 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001170 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001171 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001172 }
1173 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001174 return -1; /* error */
1175 }
1176 return framesz;
1177}
1178
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001179/********************************************************************
1180 * Functions that manage the SPOE applet
1181 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001182static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001183spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001184{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001185 si_want_get(appctx->owner);
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001186 si_rx_endp_more(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001187 appctx_wakeup(appctx);
1188 return 1;
1189}
1190
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001191/* Callback function that catches applet timeouts. If a timeout occurred, we set
1192 * <appctx->st1> flag and the SPOE applet is woken up. */
1193static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001194spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001195{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001196 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001197
1198 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1199 if (tick_is_expired(task->expire, now_ms)) {
1200 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001201 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001202 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001203 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001204 return task;
1205}
1206
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001207/* Callback function that releases a SPOE applet. This happens when the
1208 * connection with the agent is closed. */
1209static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001210spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001211{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001212 struct stream_interface *si = appctx->owner;
1213 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1214 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001215 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001216
1217 if (spoe_appctx == NULL)
1218 return;
1219
1220 appctx->ctx.spoe.ptr = NULL;
1221 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001222
1223 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1224 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1225 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001226
Christopher Faulet8ef75252017-02-20 22:56:03 +01001227 /* Remove applet from the list of running applets */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001228 _HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001229 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1231 LIST_DEL(&spoe_appctx->list);
1232 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001233 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001234 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001235
Christopher Faulet8ef75252017-02-20 22:56:03 +01001236 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001237 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001238 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1239 eb32_delete(&spoe_appctx->node);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001240 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001241 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001242
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001243 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001244 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1245 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001246
1247 si_shutw(si);
1248 si_shutr(si);
1249 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001250 }
1251
Christopher Faulet8ef75252017-02-20 22:56:03 +01001252 /* Destroy the task attached to this applet */
Willy Tarreauf6562792019-05-07 19:05:35 +02001253 task_destroy(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001254
Christopher Faulet8ef75252017-02-20 22:56:03 +01001255 /* Notify all waiting streams */
1256 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001257 LIST_DEL(&ctx->list);
1258 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001259 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001260 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001261 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001263 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001264 }
1265
Christopher Faulet8ef75252017-02-20 22:56:03 +01001266 /* If the applet was processing a fragmented frame, notify the
1267 * corresponding stream. */
1268 if (spoe_appctx->frag_ctx.ctx) {
1269 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001270 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001271 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001272 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001273 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1274 }
1275
Christopher Faulet24289f22017-09-25 14:48:02 +02001276 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001277 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001278
Christopher Faulet8ef75252017-02-20 22:56:03 +01001279 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001280 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001281 LIST_DEL(&ctx->list);
1282 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001283 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001284 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001285 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001286 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001287 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001288 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001289 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001290 LIST_DEL(&ctx->list);
1291 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001292 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001293 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001294 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001295 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001296 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1297 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001298
1299 end:
Christopher Faulet55ae8a62019-05-23 22:47:48 +02001300 /* Release allocated memory */
1301 spoe_release_buffer(&spoe_appctx->buffer,
1302 &spoe_appctx->buffer_wait);
1303 pool_free(pool_head_spoe_appctx, spoe_appctx);
1304
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001305 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001306 agent->rt[tid].frame_size = agent->max_frame_size;
1307 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1308 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001309}
1310
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001311static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001313{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001314 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001315 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001316 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001317 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001318
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001319 if (si_state_in(si->state, SI_SB_CER|SI_SB_DIS|SI_SB_CLO)) {
1320 /* closed */
1321 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1322 goto exit;
1323 }
1324
Willy Tarreau4f283fa2019-06-05 14:34:03 +02001325 if (!si_state_in(si->state, SI_SB_RDY|SI_SB_EST)) {
Willy Tarreau7ab22adb2019-06-05 14:53:22 +02001326 /* not connected yet */
Willy Tarreau8bb2ffb2018-11-14 17:54:13 +01001327 si_rx_endp_more(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001328 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1329 goto stop;
1330 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001331
Christopher Fauleta1cda022016-12-21 08:58:06 +01001332 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001333 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1334 " - Connection timed out\n",
1335 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1336 __FUNCTION__, appctx);
1337 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001338 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001340
Christopher Faulet42bfa462017-01-04 14:14:19 +01001341 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001342 SPOE_APPCTX(appctx)->task->expire =
1343 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001344
Christopher Faulet8ef75252017-02-20 22:56:03 +01001345 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1346 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001347 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001348 ret = spoe_prepare_hahello_frame(appctx, frame,
1349 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001350 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001351 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001352
1353 switch (ret) {
1354 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001355 case 0: /* ignore => an error, cannot be ignored */
1356 goto exit;
1357
1358 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001359 goto stop;
1360
Christopher Faulet8ef75252017-02-20 22:56:03 +01001361 default:
1362 /* HELLO frame successfully sent, now wait for the
1363 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001364 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1365 goto next;
1366 }
1367
1368 next:
1369 return 0;
1370 stop:
1371 return 1;
1372 exit:
1373 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1374 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001375}
1376
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001377static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001378spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001379{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001380 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001381 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001382 char *frame;
1383 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001384
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001385
Christopher Fauletb067b062017-01-04 16:39:11 +01001386 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001387 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001388 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001389 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001390
Christopher Fauleta1cda022016-12-21 08:58:06 +01001391 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001392 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1393 " - Connection timed out\n",
1394 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1395 __FUNCTION__, appctx);
1396 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001397 goto exit;
1398 }
1399
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001400 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001401 ret = spoe_recv_frame(appctx, frame,
1402 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001403 if (ret > 1) {
1404 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1405 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1406 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001407 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001408 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001409 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001410 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001411
Christopher Fauleta1cda022016-12-21 08:58:06 +01001412 switch (ret) {
1413 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001414 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1416 goto next;
1417
1418 case 1: /* retry later */
1419 goto stop;
1420
1421 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001422 /* HELLO handshake is finished, set the idle timeout and
1423 * add the applet in the list of running applets. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001424 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001425 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001426 SPOE_APPCTX(appctx)->node.key = 0;
1427 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001428
1429 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001430 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001431 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001432 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001433
Christopher Fauleta1cda022016-12-21 08:58:06 +01001434 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001435 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001436 if (trash.data)
1437 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001438
1439 SPOE_APPCTX(appctx)->task->expire =
1440 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001441 return 0;
1442 stop:
1443 return 1;
1444 exit:
1445 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1446 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001447}
1448
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001449
Christopher Fauleta1cda022016-12-21 08:58:06 +01001450static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001451spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001452{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001453 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1454 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001455 char *frame, *buf;
1456 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001457
Christopher Faulet8ef75252017-02-20 22:56:03 +01001458 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1459 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001460 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001461
1462 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1463 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1464 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1465 SPOE_APPCTX(appctx)->max_frame_size);
1466 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001467 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001468 *skip = 1;
1469 ret = 1;
1470 goto end;
1471 }
1472 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001473 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001474 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1475 SPOE_APPCTX(appctx)->max_frame_size);
1476
1477 }
1478
Christopher Faulet8ef75252017-02-20 22:56:03 +01001479 if (ret > 1)
1480 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001481
Christopher Faulet8ef75252017-02-20 22:56:03 +01001482 switch (ret) {
1483 case -1: /* error */
1484 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1485 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001486
Christopher Faulet8ef75252017-02-20 22:56:03 +01001487 case 0: /* ignore */
1488 if (ctx == NULL)
1489 goto abort_frag_frame;
1490
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001491 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001492 LIST_DEL(&ctx->list);
1493 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001494 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001495 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001496 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001497 ctx->state = SPOE_CTX_ST_ERROR;
1498 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1499 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001500 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001501 break;
1502
1503 case 1: /* retry */
1504 *skip = 1;
1505 break;
1506
1507 default:
1508 if (ctx == NULL)
1509 goto abort_frag_frame;
1510
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001511 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001512 LIST_DEL(&ctx->list);
1513 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001514 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001515 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001516 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001517 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1518 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1519 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001520 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001521 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001522 }
1523 goto end;
1524
1525 frag_frame_sent:
1526 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001527 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001528 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1529 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1530 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001531 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1532 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1533 goto end;
1534
1535 no_frag_frame_sent:
1536 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1537 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001538 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001539 }
1540 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1541 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1542 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1543 }
1544 else {
1545 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001546 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001547 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1548 }
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001549 _HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001550 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001551 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1552 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1553 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001554 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001555
Christopher Faulet8ef75252017-02-20 22:56:03 +01001556 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1557 goto end;
1558
1559 abort_frag_frame:
1560 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1561 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1562 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1563 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1564 goto end;
1565
1566 end:
1567 return ret;
1568}
1569
1570static int
1571spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1572{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001573 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001574 struct spoe_context *ctx = NULL;
1575 char *frame;
1576 int ret;
1577
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001578 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001579 ret = spoe_recv_frame(appctx, frame,
1580 SPOE_APPCTX(appctx)->max_frame_size);
1581 if (ret > 1) {
1582 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1583 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001584 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001585 goto end;
1586 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001587 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001588 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1589 }
1590 switch (ret) {
1591 case -1: /* error */
1592 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1593 break;
1594
1595 case 0: /* ignore */
1596 break;
1597
1598 case 1: /* retry */
1599 *skip = 1;
1600 break;
1601
1602 default:
1603 LIST_DEL(&ctx->list);
1604 LIST_INIT(&ctx->list);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001605 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001606 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1607 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001608 if (ctx->spoe_appctx) {
1609 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001610 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001611 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001612 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1613 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1614 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1615 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1616 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1617 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1618 }
1619 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1620 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001621 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1622 break;
1623 }
1624
1625 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001626 if (trash.data)
1627 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001628 end:
1629 return ret;
1630}
1631
1632static int
1633spoe_handle_processing_appctx(struct appctx *appctx)
1634{
1635 struct stream_interface *si = appctx->owner;
1636 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001637 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001638
1639 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1640 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1641 goto exit;
1642 }
1643
1644 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1645 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1646 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1647 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1648 goto next;
1649 }
1650
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001651 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001652 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001653 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001654 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1655 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001656 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001657
Christopher Faulet8f82b202018-01-24 16:23:03 +01001658 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1659 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001660
Christopher Faulet8f82b202018-01-24 16:23:03 +01001661 /* receiving_frame loop */
1662 while (!skip_receiving) {
1663 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1664 switch (ret) {
1665 case -1: /* error */
1666 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001667
Christopher Faulet8f82b202018-01-24 16:23:03 +01001668 case 0: /* ignore */
1669 active_r = 1;
1670 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001671
Christopher Faulet8f82b202018-01-24 16:23:03 +01001672 case 1: /* retry */
1673 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001674
Christopher Faulet8f82b202018-01-24 16:23:03 +01001675 default:
1676 active_r = 1;
1677 break;
1678 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001679 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001680
Christopher Faulet8f82b202018-01-24 16:23:03 +01001681 /* send_frame loop */
1682 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1683 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1684 switch (ret) {
1685 case -1: /* error */
1686 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001687
Christopher Faulet8f82b202018-01-24 16:23:03 +01001688 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001689 if (SPOE_APPCTX(appctx)->node.key)
1690 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001691 active_s++;
1692 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001693
Christopher Faulet8f82b202018-01-24 16:23:03 +01001694 case 1: /* retry */
1695 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001696
Christopher Faulet8f82b202018-01-24 16:23:03 +01001697 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001698 if (SPOE_APPCTX(appctx)->node.key)
1699 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001700 active_s++;
1701 break;
1702 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001703 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001704
Christopher Faulet8f82b202018-01-24 16:23:03 +01001705 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001706 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1707 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1708 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001709
Christopher Faulet8f82b202018-01-24 16:23:03 +01001710 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001711 _HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001712 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001713 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714 }
1715 return 1;
1716
Christopher Faulet8f82b202018-01-24 16:23:03 +01001717 next:
1718 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1719 return 0;
1720
Christopher Fauleta1cda022016-12-21 08:58:06 +01001721 exit:
1722 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1723 return 0;
1724}
1725
1726static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001727spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001728{
1729 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001730 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001731 char *frame, *buf;
1732 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001733
Christopher Fauleta1cda022016-12-21 08:58:06 +01001734 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1735 goto exit;
1736
1737 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1738 goto exit;
1739
Christopher Faulet8ef75252017-02-20 22:56:03 +01001740 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1741 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001742 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001743 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1744 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001745 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001746 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001747
1748 switch (ret) {
1749 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001750 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001751 goto exit;
1752
1753 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001754 goto stop;
1755
1756 default:
1757 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1758 " - disconnected by HAProxy (%d): %s\n",
1759 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001760 __FUNCTION__, appctx,
1761 SPOE_APPCTX(appctx)->status_code,
1762 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763
1764 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1765 goto next;
1766 }
1767
1768 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001769 SPOE_APPCTX(appctx)->task->expire =
1770 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001771 return 0;
1772 stop:
1773 return 1;
1774 exit:
1775 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1776 return 0;
1777}
1778
1779static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001780spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781{
1782 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001783 char *frame;
1784 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001785
Christopher Fauletb067b062017-01-04 16:39:11 +01001786 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001787 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001788 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001789 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001790
Christopher Fauletb067b062017-01-04 16:39:11 +01001791 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001792 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001793 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001794 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001795
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001796 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001797 ret = spoe_recv_frame(appctx, frame,
1798 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001800 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001802 }
1803
1804 switch (ret) {
1805 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1807 " - error on frame (%s)\n",
1808 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001809 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001810 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001811 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001812 goto exit;
1813
1814 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001815 goto next;
1816
1817 case 1: /* retry */
1818 goto stop;
1819
1820 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001821 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001822 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001823 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001824 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001825 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1826 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001827 goto exit;
1828 }
1829
1830 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001831 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001832 if (trash.data)
1833 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001834
Christopher Fauleta1cda022016-12-21 08:58:06 +01001835 return 0;
1836 stop:
1837 return 1;
1838 exit:
1839 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1840 return 0;
1841}
1842
1843/* I/O Handler processing messages exchanged with the agent */
1844static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001845spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001846{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001847 struct stream_interface *si = appctx->owner;
1848 struct spoe_agent *agent;
1849
1850 if (SPOE_APPCTX(appctx) == NULL)
1851 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001852
Christopher Faulet8ef75252017-02-20 22:56:03 +01001853 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1854 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001855
Christopher Fauleta1cda022016-12-21 08:58:06 +01001856 switchstate:
1857 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1858 " - appctx-state=%s\n",
1859 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1860 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1861
1862 switch (appctx->st0) {
1863 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001864 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001865 goto out;
1866 goto switchstate;
1867
1868 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001869 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870 goto out;
1871 goto switchstate;
1872
1873 case SPOE_APPCTX_ST_IDLE:
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001874 _HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001875 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001876 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001877 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001878 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001879 SPOE_APPCTX(appctx)->task->expire =
1880 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001881 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001882 goto switchstate;
1883 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001884 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1885 /* fall through */
1886
1887 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001888 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1889 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001890 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001891 goto out;
1892 goto switchstate;
1893
1894 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001895 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001896 goto out;
1897 goto switchstate;
1898
1899 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001900 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001901 goto out;
1902 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001903
1904 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001905 appctx->st0 = SPOE_APPCTX_ST_END;
1906 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1907
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001908 si_shutw(si);
1909 si_shutr(si);
1910 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001911 /* fall through */
1912
1913 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001914 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001915 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001916 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001917 if (stopping)
1918 spoe_wakeup_appctx(appctx);
1919
Christopher Faulet42bfa462017-01-04 14:14:19 +01001920 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1921 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001922}
1923
1924struct applet spoe_applet = {
1925 .obj_type = OBJ_TYPE_APPLET,
1926 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001927 .fct = spoe_handle_appctx,
1928 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001929};
1930
1931/* Create a SPOE applet. On success, the created applet is returned, else
1932 * NULL. */
1933static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001934spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001935{
1936 struct appctx *appctx;
1937 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001938 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001939
Emeric Brun1138fd02017-06-19 12:38:55 +02001940 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001941 goto out_error;
1942
Willy Tarreaubafbe012017-11-24 17:34:44 +01001943 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001944 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001945 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001946 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001947
Christopher Faulet42bfa462017-01-04 14:14:19 +01001948 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001949 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001950 goto out_free_spoe_appctx;
1951
1952 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001953 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001954 SPOE_APPCTX(appctx)->task->context = appctx;
1955 SPOE_APPCTX(appctx)->agent = conf->agent;
1956 SPOE_APPCTX(appctx)->version = 0;
1957 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1958 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001959 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001960 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001961 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001962
Willy Tarreau21046592020-02-26 10:39:36 +01001963 MT_LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001964 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001965 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001966
1967 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1968 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001969
Willy Tarreau5820a362016-12-22 15:59:02 +01001970 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001971 if (!sess)
1972 goto out_free_spoe;
1973
Willy Tarreau87787ac2017-08-28 16:22:54 +02001974 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001975 goto out_free_sess;
1976
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001977 stream_set_backend(strm, conf->agent->b.be);
1978
1979 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001980 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001981 appctx_wakeup(appctx);
1982
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001983 strm->do_log = NULL;
1984 strm->res.flags |= CF_READ_DONTWAIT;
1985
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001986 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02001987 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01001988 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Olivier Houchard9e7ae282019-03-08 18:50:42 +01001989 _HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02001990
Emeric Brunc60def82017-09-27 14:59:38 +02001991 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02001992 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001993 return appctx;
1994
1995 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001996 out_free_sess:
1997 session_free(sess);
1998 out_free_spoe:
Olivier Houchard3f795f72019-04-17 22:51:06 +02001999 task_destroy(SPOE_APPCTX(appctx)->task);
Christopher Faulet42bfa462017-01-04 14:14:19 +01002000 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002001 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002002 out_free_appctx:
2003 appctx_free(appctx);
2004 out_error:
2005 return NULL;
2006}
2007
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002008static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002009spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002010{
2011 struct spoe_config *conf = FLT_CONF(ctx->filter);
2012 struct spoe_agent *agent = conf->agent;
2013 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002014 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002015
Christopher Fauleta1cda022016-12-21 08:58:06 +01002016 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002017 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002018 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002019 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002020
2021 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002022 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002023 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2024 ctx->strm);
2025
Christopher Fauleta1cda022016-12-21 08:58:06 +01002026 /* Do not try to create a new applet if there is no server up for the
2027 * agent's backend. */
2028 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2029 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2030 " - cannot create SPOE appctx: no server up\n",
2031 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2032 __FUNCTION__, ctx->strm);
2033 goto end;
2034 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035
Christopher Fauleta1cda022016-12-21 08:58:06 +01002036 /* Do not try to create a new applet if we have reached the maximum of
2037 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002038 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002039 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002040 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2041 " - cannot create SPOE appctx: max CPS reached\n",
2042 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2043 __FUNCTION__, ctx->strm);
2044 goto end;
2045 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002046 }
2047
Christopher Faulet8ef75252017-02-20 22:56:03 +01002048 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002049 if (appctx == NULL) {
2050 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2051 " - failed to create SPOE appctx\n",
2052 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2053 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002054 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002055 "SPOE: [%s] failed to create SPOE applet\n",
2056 agent->id);
2057
Christopher Fauleta1cda022016-12-21 08:58:06 +01002058 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002059 }
2060
Christopher Fauleta1cda022016-12-21 08:58:06 +01002061 /* Increase the per-process number of cumulated connections */
2062 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002063 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002064
Christopher Fauleta1cda022016-12-21 08:58:06 +01002065 end:
2066 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002067 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002068 ctx->status_code = SPOE_CTX_ERR_RES;
2069 return -1;
2070 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002071
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002072 /* Add the SPOE context in the sending queue if the stream has no applet
2073 * already assigned and wakeup all idle applets. Otherwise, don't queue
2074 * it. */
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002075 _HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002076 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2077 ctx->stats.tv_queue = now;
Christopher Faulet3e86cec2019-04-10 14:02:12 +02002078 if (ctx->spoe_appctx)
2079 return 1;
2080 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002081
2082 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002083 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002084 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002085 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002086 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002087 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002088
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002089 /* Finally try to wakeup an IDLE applet. */
2090 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2091 struct eb32_node *node;
2092
2093 node = eb32_first(&agent->rt[tid].idle_applets);
2094 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2095 if (node && spoe_appctx) {
2096 eb32_delete(&spoe_appctx->node);
2097 spoe_appctx->node.key++;
2098 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2099 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002100 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002101 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002102 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002103}
2104
2105/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002106 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002107 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002108/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2109 * fragmented_content. If the next message can be processed, it returns 0. If
2110 * the message is too big, it returns -1.*/
2111static int
2112spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2113 struct spoe_message *msg, int dir,
2114 char **buf, char *end)
2115{
2116 struct sample *smp;
2117 struct spoe_arg *arg;
2118 int ret;
2119
2120 if (msg->cond) {
2121 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2122 ret = acl_pass(ret);
2123 if (msg->cond->pol == ACL_COND_UNLESS)
2124 ret = !ret;
2125
2126 /* the rule does not match */
2127 if (!ret)
2128 goto next;
2129 }
2130
2131 /* Resume encoding of a SPOE argument */
2132 if (ctx->frag_ctx.curarg != NULL) {
2133 arg = ctx->frag_ctx.curarg;
2134 goto encode_argument;
2135 }
2136
2137 if (ctx->frag_ctx.curoff != UINT_MAX)
2138 goto encode_msg_payload;
2139
2140 /* Check if there is enough space for the message name and the
2141 * number of arguments. It implies <msg->id_len> is encoded on 2
2142 * bytes, at most (< 2288). */
2143 if (*buf + 2 + msg->id_len + 1 > end)
2144 goto too_big;
2145
2146 /* Encode the message name */
2147 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2148 goto too_big;
2149
2150 /* Set the number of arguments for this message */
2151 **buf = msg->nargs;
2152 (*buf)++;
2153
2154 ctx->frag_ctx.curoff = 0;
2155 encode_msg_payload:
2156
2157 /* Loop on arguments */
2158 list_for_each_entry(arg, &msg->args, list) {
2159 ctx->frag_ctx.curarg = arg;
2160 ctx->frag_ctx.curoff = UINT_MAX;
Kevin Zhuf7f54282019-04-26 14:00:01 +08002161 ctx->frag_ctx.curlen = 0;
Christopher Faulet10e37672017-09-21 16:38:22 +02002162
2163 encode_argument:
2164 if (ctx->frag_ctx.curoff != UINT_MAX)
2165 goto encode_arg_value;
2166
Joseph Herlantf7f60312018-11-15 13:46:49 -08002167 /* Encode the argument name as a string. It can by NULL */
Christopher Faulet10e37672017-09-21 16:38:22 +02002168 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2169 goto too_big;
2170
2171 ctx->frag_ctx.curoff = 0;
2172 encode_arg_value:
2173
Joseph Herlantf7f60312018-11-15 13:46:49 -08002174 /* Fetch the argument value */
Christopher Faulet10e37672017-09-21 16:38:22 +02002175 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
Christopher Faulet3b1d0042019-05-06 09:53:10 +02002176 if (smp) {
2177 smp->ctx.a[0] = &ctx->frag_ctx.curlen;
2178 smp->ctx.a[1] = &ctx->frag_ctx.curoff;
2179 }
Christopher Faulet85db3212019-04-26 14:30:15 +02002180 ret = spoe_encode_data(smp, buf, end);
Christopher Faulet10e37672017-09-21 16:38:22 +02002181 if (ret == -1 || ctx->frag_ctx.curoff)
2182 goto too_big;
2183 }
2184
2185 next:
2186 return 0;
2187
2188 too_big:
2189 return -1;
2190}
2191
Christopher Fauletc718b822017-09-21 16:50:56 +02002192/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2193 * handle fragmented content. On success it returns 1. If an error occurred, -1
2194 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2195 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002196static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002197spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002198 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002199{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002200 struct spoe_config *conf = FLT_CONF(ctx->filter);
2201 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002202 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002203 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002204
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002205 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002206 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002207
Christopher Fauletc718b822017-09-21 16:50:56 +02002208 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2209 /* Resume encoding of a SPOE message */
2210 if (ctx->frag_ctx.curmsg != NULL) {
2211 msg = ctx->frag_ctx.curmsg;
2212 goto encode_evt_message;
2213 }
2214
2215 list_for_each_entry(msg, messages, by_evt) {
2216 ctx->frag_ctx.curmsg = msg;
2217 ctx->frag_ctx.curarg = NULL;
2218 ctx->frag_ctx.curoff = UINT_MAX;
2219
2220 encode_evt_message:
2221 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2222 goto too_big;
2223 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002224 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002225 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2226 /* Resume encoding of a SPOE message */
2227 if (ctx->frag_ctx.curmsg != NULL) {
2228 msg = ctx->frag_ctx.curmsg;
2229 goto encode_grp_message;
2230 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002231
Christopher Fauletc718b822017-09-21 16:50:56 +02002232 list_for_each_entry(msg, messages, by_grp) {
2233 ctx->frag_ctx.curmsg = msg;
2234 ctx->frag_ctx.curarg = NULL;
2235 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002236
Christopher Fauletc718b822017-09-21 16:50:56 +02002237 encode_grp_message:
2238 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2239 goto too_big;
2240 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002241 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002242 else
2243 goto skip;
2244
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002245
Christopher Faulet57583e42017-09-04 15:41:09 +02002246 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002247 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002248 goto skip;
2249
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002250 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002251 " - encode %s messages - spoe_appctx=%p"
2252 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002253 (int)now.tv_sec, (int)now.tv_usec,
2254 agent->id, __FUNCTION__, s,
2255 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002256 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002257 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002258
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002259 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002260 ctx->frag_ctx.curmsg = NULL;
2261 ctx->frag_ctx.curarg = NULL;
2262 ctx->frag_ctx.curoff = 0;
2263 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002264
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002265 return 1;
2266
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002267 too_big:
Christopher Fauleta715ea82019-04-10 14:21:51 +02002268 /* Return an error if fragmentation is unsupported or if nothing has
2269 * been encoded because its too big and not splittable. */
2270 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION) || p == b_head(&ctx->buffer)) {
Christopher Fauletcecd8522017-02-24 22:11:21 +01002271 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2272 return -1;
2273 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002274
2275 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002276 " - encode fragmented messages - spoe_appctx=%p"
2277 " - curmsg=%p - curarg=%p - curoff=%u"
2278 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002279 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002280 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002281 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002282 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), 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->flags |= SPOE_CTX_FL_FRAGMENTED;
2286 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2287 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002288
2289 skip:
2290 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2291 " - skip the frame because nothing has been encoded\n",
2292 (int)now.tv_sec, (int)now.tv_usec,
2293 agent->id, __FUNCTION__, s);
2294 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002295}
2296
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002297
2298/***************************************************************************
2299 * Functions that handle SPOE actions
2300 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002301/* Helper function to set a variable */
2302static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002303spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002304 struct sample *smp)
2305{
2306 struct spoe_config *conf = FLT_CONF(ctx->filter);
2307 struct spoe_agent *agent = conf->agent;
2308 char varname[64];
2309
2310 memset(varname, 0, sizeof(varname));
2311 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2312 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002313 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2314 vars_set_by_name(varname, len, smp);
2315 else
2316 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002317}
2318
2319/* Helper function to unset a variable */
2320static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002321spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002322 struct sample *smp)
2323{
2324 struct spoe_config *conf = FLT_CONF(ctx->filter);
2325 struct spoe_agent *agent = conf->agent;
2326 char varname[64];
2327
2328 memset(varname, 0, sizeof(varname));
2329 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2330 scope, agent->var_pfx, len, name);
2331 vars_unset_by_name_ifexist(varname, len, smp);
2332}
2333
2334
Christopher Faulet8ef75252017-02-20 22:56:03 +01002335static inline int
2336spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2337 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002338{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002339 char *str, *scope, *p = *buf;
2340 struct sample smp;
2341 uint64_t sz;
2342 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002343
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344 if (p + 2 >= end)
2345 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002346
Christopher Faulet8ef75252017-02-20 22:56:03 +01002347 /* SET-VAR requires 3 arguments */
2348 if (*p++ != 3)
2349 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002350
Christopher Faulet8ef75252017-02-20 22:56:03 +01002351 switch (*p++) {
2352 case SPOE_SCOPE_PROC: scope = "proc"; break;
2353 case SPOE_SCOPE_SESS: scope = "sess"; break;
2354 case SPOE_SCOPE_TXN : scope = "txn"; break;
2355 case SPOE_SCOPE_REQ : scope = "req"; break;
2356 case SPOE_SCOPE_RES : scope = "res"; break;
2357 default: goto skip;
2358 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2361 goto skip;
2362 memset(&smp, 0, sizeof(smp));
2363 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002364
Christopher Faulet8ef75252017-02-20 22:56:03 +01002365 if (spoe_decode_data(&p, end, &smp) == -1)
2366 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002367
Christopher Faulet8ef75252017-02-20 22:56:03 +01002368 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2369 " - set-var '%s.%s.%.*s'\n",
2370 (int)now.tv_sec, (int)now.tv_usec,
2371 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2372 __FUNCTION__, s, scope,
2373 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2374 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002375
Christopher Faulet8ef75252017-02-20 22:56:03 +01002376 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002377
Christopher Faulet8ef75252017-02-20 22:56:03 +01002378 ret = (p - *buf);
2379 *buf = p;
2380 return ret;
2381 skip:
2382 return 0;
2383}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002384
Christopher Faulet8ef75252017-02-20 22:56:03 +01002385static inline int
2386spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2387 char **buf, char *end, int dir)
2388{
2389 char *str, *scope, *p = *buf;
2390 struct sample smp;
2391 uint64_t sz;
2392 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002393
Christopher Faulet8ef75252017-02-20 22:56:03 +01002394 if (p + 2 >= end)
2395 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002396
Christopher Faulet8ef75252017-02-20 22:56:03 +01002397 /* UNSET-VAR requires 2 arguments */
2398 if (*p++ != 2)
2399 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002400
Christopher Faulet8ef75252017-02-20 22:56:03 +01002401 switch (*p++) {
2402 case SPOE_SCOPE_PROC: scope = "proc"; break;
2403 case SPOE_SCOPE_SESS: scope = "sess"; break;
2404 case SPOE_SCOPE_TXN : scope = "txn"; break;
2405 case SPOE_SCOPE_REQ : scope = "req"; break;
2406 case SPOE_SCOPE_RES : scope = "res"; break;
2407 default: goto skip;
2408 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002409
Christopher Faulet8ef75252017-02-20 22:56:03 +01002410 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2411 goto skip;
2412 memset(&smp, 0, sizeof(smp));
2413 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002414
Christopher Faulet8ef75252017-02-20 22:56:03 +01002415 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2416 " - unset-var '%s.%s.%.*s'\n",
2417 (int)now.tv_sec, (int)now.tv_usec,
2418 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2419 __FUNCTION__, s, scope,
2420 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2421 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002422
Christopher Faulet8ef75252017-02-20 22:56:03 +01002423 spoe_unset_var(ctx, scope, str, sz, &smp);
2424
2425 ret = (p - *buf);
2426 *buf = p;
2427 return ret;
2428 skip:
2429 return 0;
2430}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002431
Christopher Faulet8ef75252017-02-20 22:56:03 +01002432/* Process SPOE actions for a specific event. It returns 1 on success. If an
2433 * error occurred, 0 is returned. */
2434static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002435spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002436{
2437 char *p, *end;
2438 int ret;
2439
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002440 p = b_head(&ctx->buffer);
2441 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002442
2443 while (p < end) {
2444 enum spoe_action_type type;
2445
2446 type = *p++;
2447 switch (type) {
2448 case SPOE_ACT_T_SET_VAR:
2449 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2450 if (!ret)
2451 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002452 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002453
Christopher Faulet8ef75252017-02-20 22:56:03 +01002454 case SPOE_ACT_T_UNSET_VAR:
2455 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2456 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002457 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002458 break;
2459
2460 default:
2461 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002462 }
2463 }
2464
2465 return 1;
2466 skip:
2467 return 0;
2468}
2469
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002470/***************************************************************************
2471 * Functions that process SPOE events
2472 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002473static void
2474spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2475 struct spoe_context *ctx, int dir)
2476{
2477 if (!tv_iszero(&ctx->stats.tv_start)) {
2478 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2479 ctx->stats.t_total += ctx->stats.t_process;
2480 tv_zero(&ctx->stats.tv_request);
2481 tv_zero(&ctx->stats.tv_queue);
2482 tv_zero(&ctx->stats.tv_wait);
2483 tv_zero(&ctx->stats.tv_response);
2484 }
2485
2486 if (agent->var_t_process) {
2487 struct sample smp;
2488
2489 memset(&smp, 0, sizeof(smp));
2490 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2491 smp.data.u.sint = ctx->stats.t_process;
2492 smp.data.type = SMP_T_SINT;
2493
2494 spoe_set_var(ctx, "txn", agent->var_t_process,
2495 strlen(agent->var_t_process), &smp);
2496 }
2497
2498 if (agent->var_t_total) {
2499 struct sample smp;
2500
2501 memset(&smp, 0, sizeof(smp));
2502 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2503 smp.data.u.sint = ctx->stats.t_total;
2504 smp.data.type = SMP_T_SINT;
2505
2506 spoe_set_var(ctx, "txn", agent->var_t_total,
2507 strlen(agent->var_t_total), &smp);
2508 }
2509}
2510
2511static void
2512spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2513 struct spoe_context *ctx, int dir)
2514{
2515 if (agent->eps_max > 0)
2516 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2517
2518 if (agent->var_on_error) {
2519 struct sample smp;
2520
2521 memset(&smp, 0, sizeof(smp));
2522 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2523 smp.data.u.sint = ctx->status_code;
2524 smp.data.type = SMP_T_BOOL;
2525
2526 spoe_set_var(ctx, "txn", agent->var_on_error,
2527 strlen(agent->var_on_error), &smp);
2528 }
2529
2530 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2531 ? SPOE_CTX_ST_READY
2532 : SPOE_CTX_ST_NONE);
2533}
2534
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002535static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002536spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002537{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002538 /* If a process is already started for this SPOE context, retry
2539 * later. */
2540 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002541 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002542
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002543 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002544 ctx->stats.tv_start = now;
2545 ctx->stats.tv_request = now;
2546 ctx->stats.t_request = -1;
2547 ctx->stats.t_queue = -1;
2548 ctx->stats.t_waiting = -1;
2549 ctx->stats.t_response = -1;
2550 ctx->stats.t_process = -1;
2551
2552 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002553
Christopher Fauleta1cda022016-12-21 08:58:06 +01002554 /* Set the right flag to prevent request and response processing
2555 * in same time. */
2556 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2557 ? SPOE_CTX_FL_REQ_PROCESS
2558 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002559 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002560}
2561
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002562static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002563spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002564{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002565 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002566
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002567 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2568 return;
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002569 _HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002570 if (sa) {
2571 if (sa->frag_ctx.ctx == ctx) {
2572 sa->frag_ctx.ctx = NULL;
2573 spoe_wakeup_appctx(sa->owner);
2574 }
2575 else
2576 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002577 }
2578
Christopher Fauleta1cda022016-12-21 08:58:06 +01002579 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002580 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002581 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002582
2583 /* Reset processing timer */
2584 ctx->process_exp = TICK_ETERNITY;
2585
Christopher Faulet8ef75252017-02-20 22:56:03 +01002586 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002587
Christopher Fauletfce747b2018-01-24 15:59:32 +01002588 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002589 ctx->frag_ctx.curmsg = NULL;
2590 ctx->frag_ctx.curarg = NULL;
2591 ctx->frag_ctx.curoff = 0;
2592 ctx->frag_ctx.flags = 0;
2593
Christopher Fauleta1cda022016-12-21 08:58:06 +01002594 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002595 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002596 _HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002597 else
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002598 _HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002599
Christopher Fauleta1cda022016-12-21 08:58:06 +01002600 LIST_DEL(&ctx->list);
2601 LIST_INIT(&ctx->list);
2602 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002603}
2604
Christopher Faulet58d03682017-09-21 16:57:24 +02002605/* Process a list of SPOE messages. First, this functions will process messages
2606 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2607 * to process corresponding actions. During all the processing, it returns 0
2608 * and it returns 1 when the processing is finished. If an error occurred, -1
2609 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002610static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002611spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2612 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002613{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002614 struct spoe_config *conf = FLT_CONF(ctx->filter);
2615 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002616 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002617
2618 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002619 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002620
2621 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2622 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002623 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002624 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002625 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002626 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002627 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002628 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002629
2630 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002631 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002632 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002633 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002634 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002635 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002636 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002637 goto skip;
2638 }
2639 }
2640
Christopher Fauletf7a30922016-11-10 15:04:51 +01002641 if (!tick_isset(ctx->process_exp)) {
2642 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2643 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2644 ctx->process_exp);
2645 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002646 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002647 if (!ret)
2648 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002649
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002650 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002651 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002652 }
2653
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002654 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Faulet63263e52019-04-10 14:47:18 +02002655 if (tv_iszero(&ctx->stats.tv_request))
2656 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002657 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002658 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002659 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002660 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002661 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002662 if (!ret)
2663 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002664 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002665 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002666 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2667 }
2668
2669 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002670 if (ctx->spoe_appctx)
2671 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002672 ret = 0;
2673 goto out;
2674 }
2675
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002676 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2677 ret = 0;
2678 goto out;
2679 }
2680
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002681 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002682 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002683 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002684 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002685 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002686 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002687 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002688 }
2689
2690 out:
2691 return ret;
2692
Christopher Fauleta1cda022016-12-21 08:58:06 +01002693 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002694 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002695 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002696 spoe_stop_processing(agent, ctx);
2697 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002698
Christopher Fauleta1cda022016-12-21 08:58:06 +01002699 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002700 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002701 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002702 if (ctx->status_code) {
Olivier Houchard9e7ae282019-03-08 18:50:42 +01002703 _HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002704 spoe_handle_processing_error(s, agent, ctx, dir);
2705 ret = 1;
2706 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002707 return ret;
2708}
2709
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002710/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2711 * See spoe_process_message for details. */
2712static int
2713spoe_process_group(struct stream *s, struct spoe_context *ctx,
2714 struct spoe_group *group, int dir)
2715{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002716 struct spoe_config *conf = FLT_CONF(ctx->filter);
2717 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002718 int ret;
2719
2720 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2721 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002722 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002723 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2724 group->id);
2725
2726 if (LIST_ISEMPTY(&group->messages))
2727 return 1;
2728
2729 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002730 if (ret && ctx->stats.t_process != -1) {
2731 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002732 " - <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 +01002733 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002734 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002735 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002736 ctx->stats.t_response, ctx->stats.t_process,
2737 agent->counters.idles, agent->counters.applets,
2738 agent->counters.nb_sending, agent->counters.nb_waiting,
2739 agent->counters.nb_errors, agent->counters.nb_processed,
2740 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2741 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2742 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2743 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2744 agent->id, group->id, s->uniq_id, ctx->status_code,
2745 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2746 ctx->stats.t_response, ctx->stats.t_process,
2747 agent->counters.idles, agent->counters.applets,
2748 agent->counters.nb_sending, agent->counters.nb_waiting,
2749 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002750 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002751 return ret;
2752}
2753
Christopher Faulet58d03682017-09-21 16:57:24 +02002754/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2755 * See spoe_process_message for details. */
2756static int
2757spoe_process_event(struct stream *s, struct spoe_context *ctx,
2758 enum spoe_event ev)
2759{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002760 struct spoe_config *conf = FLT_CONF(ctx->filter);
2761 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002762 int dir, ret;
2763
2764 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002765 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002766 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002767 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2768 spoe_event_str[ev]);
2769
2770 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2771
2772 if (LIST_ISEMPTY(&(ctx->events[ev])))
2773 return 1;
2774
2775 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002776 if (ret && ctx->stats.t_process != -1) {
2777 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002778 " - <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 +01002779 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2780 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2781 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002782 ctx->stats.t_response, ctx->stats.t_process,
2783 agent->counters.idles, agent->counters.applets,
2784 agent->counters.nb_sending, agent->counters.nb_waiting,
2785 agent->counters.nb_errors, agent->counters.nb_processed,
2786 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2787 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2788 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2789 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2790 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2791 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2792 ctx->stats.t_response, ctx->stats.t_process,
2793 agent->counters.idles, agent->counters.applets,
2794 agent->counters.nb_sending, agent->counters.nb_waiting,
2795 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002796 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002797 return ret;
2798}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002799
2800/***************************************************************************
2801 * Functions that create/destroy SPOE contexts
2802 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002803static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002804spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002805{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002806 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002807 return 1;
2808
Willy Tarreau21046592020-02-26 10:39:36 +01002809 if (MT_LIST_ADDED(&buffer_wait->list))
2810 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002811
Christopher Faulet4596fb72017-01-11 14:05:19 +01002812 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002813 return 1;
2814
Willy Tarreau21046592020-02-26 10:39:36 +01002815 MT_LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002816 return 0;
2817}
2818
2819static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002820spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002821{
Willy Tarreau21046592020-02-26 10:39:36 +01002822 if (MT_LIST_ADDED(&buffer_wait->list))
2823 MT_LIST_DEL(&buffer_wait->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002824
2825 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002826 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002827 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002828 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002829 }
2830}
2831
Christopher Faulet4596fb72017-01-11 14:05:19 +01002832static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002833spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002834{
2835 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2836 return 1;
2837}
2838
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002839static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002840spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002841{
2842 struct spoe_config *conf = FLT_CONF(filter);
2843 struct spoe_context *ctx;
2844
Willy Tarreaubafbe012017-11-24 17:34:44 +01002845 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002846 if (ctx == NULL) {
2847 return NULL;
2848 }
2849 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002850 ctx->filter = filter;
2851 ctx->state = SPOE_CTX_ST_NONE;
2852 ctx->status_code = SPOE_CTX_ERR_NONE;
2853 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002854 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002855 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002856 ctx->buffer = BUF_NULL;
Willy Tarreau21046592020-02-26 10:39:36 +01002857 MT_LIST_INIT(&ctx->buffer_wait.list);
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002858 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002859 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002860 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002861
Christopher Fauletf7a30922016-11-10 15:04:51 +01002862 ctx->stream_id = 0;
2863 ctx->frame_id = 1;
2864 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002865
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002866 tv_zero(&ctx->stats.tv_start);
2867 tv_zero(&ctx->stats.tv_request);
2868 tv_zero(&ctx->stats.tv_queue);
2869 tv_zero(&ctx->stats.tv_wait);
2870 tv_zero(&ctx->stats.tv_response);
2871 ctx->stats.t_request = -1;
2872 ctx->stats.t_queue = -1;
2873 ctx->stats.t_waiting = -1;
2874 ctx->stats.t_response = -1;
2875 ctx->stats.t_process = -1;
2876 ctx->stats.t_total = 0;
2877
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002878 ctx->strm = s;
2879 ctx->state = SPOE_CTX_ST_READY;
2880 filter->ctx = ctx;
2881
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002882 return ctx;
2883}
2884
2885static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002886spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002887{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002888 struct spoe_config *conf = FLT_CONF(filter);
2889 struct spoe_context *ctx = filter->ctx;
2890
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002891 if (!ctx)
2892 return;
2893
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002894 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002895 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002896 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002897}
2898
2899static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002900spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002901{
2902 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002903 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002904
2905 tv_zero(&ctx->stats.tv_start);
2906 tv_zero(&ctx->stats.tv_request);
2907 tv_zero(&ctx->stats.tv_queue);
2908 tv_zero(&ctx->stats.tv_wait);
2909 tv_zero(&ctx->stats.tv_response);
2910 ctx->stats.t_request = -1;
2911 ctx->stats.t_queue = -1;
2912 ctx->stats.t_waiting = -1;
2913 ctx->stats.t_response = -1;
2914 ctx->stats.t_process = -1;
2915 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002916}
2917
2918
2919/***************************************************************************
2920 * Hooks that manage the filter lifecycle (init/check/deinit)
2921 **************************************************************************/
2922/* Signal handler: Do a soft stop, wakeup SPOE applet */
2923static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002924spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002925{
2926 struct proxy *p;
2927
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002928 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002929 while (p) {
2930 struct flt_conf *fconf;
2931
2932 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002933 struct spoe_config *conf;
2934 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002935 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002936 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002937
Christopher Faulet3b386a32017-02-23 10:17:15 +01002938 if (fconf->id != spoe_filter_id)
2939 continue;
2940
2941 conf = fconf->conf;
2942 agent = conf->agent;
2943
Christopher Faulet24289f22017-09-25 14:48:02 +02002944 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002945 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002946 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2947 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002948 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002949 }
2950 }
2951 p = p->next;
2952 }
2953}
2954
2955
2956/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2957static int
2958spoe_init(struct proxy *px, struct flt_conf *fconf)
2959{
2960 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002961
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002962 /* conf->agent_fe was already initialized during the config
2963 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002964 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002965 conf->agent_fe.cap = PR_CAP_FE;
2966 conf->agent_fe.mode = PR_MODE_TCP;
2967 conf->agent_fe.maxconn = 0;
2968 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2969 conf->agent_fe.conn_retries = CONN_RETRIES;
2970 conf->agent_fe.accept = frontend_accept;
2971 conf->agent_fe.srv = NULL;
2972 conf->agent_fe.timeout.client = TICK_ETERNITY;
2973 conf->agent_fe.default_target = &spoe_applet.obj_type;
2974 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2975
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002976 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002977 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002978 sighandler_registered = 1;
2979 }
2980
Christopher Faulet00292352019-01-09 15:05:10 +01002981 fconf->flags |= FLT_CFG_FL_HTX;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002982 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002983}
2984
Ilya Shipitsin6fb0f212020-04-02 15:25:26 +05002985/* Free resources allocated by the SPOE filter. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002986static void
2987spoe_deinit(struct proxy *px, struct flt_conf *fconf)
2988{
2989 struct spoe_config *conf = fconf->conf;
2990
2991 if (conf) {
2992 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002993
Christopher Faulet8ef75252017-02-20 22:56:03 +01002994 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02002995 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002996 free(conf);
2997 }
2998 fconf->conf = NULL;
2999}
3000
3001/* Check configuration of a SPOE filter for a specified proxy.
3002 * Return 1 on error, else 0. */
3003static int
3004spoe_check(struct proxy *px, struct flt_conf *fconf)
3005{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003006 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003007 struct spoe_config *conf = fconf->conf;
3008 struct proxy *target;
Willy Tarreaub0769b22019-02-07 13:40:33 +01003009 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003010
Christopher Faulet7ee86672017-09-19 11:08:28 +02003011 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3012 * are uniq */
3013 list_for_each_entry(f, &px->filter_configs, list) {
3014 struct spoe_config *c = f->conf;
3015
3016 /* This is not an SPOE filter */
3017 if (f->id != spoe_filter_id)
3018 continue;
3019 /* This is the current SPOE filter */
3020 if (f == fconf)
3021 continue;
3022
3023 /* Check engine Id. It should be uniq */
3024 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003025 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3026 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003027 return 1;
3028 }
3029 }
3030
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003031 target = proxy_be_by_name(conf->agent->b.name);
3032 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003033 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3034 " declared at %s:%d.\n",
3035 px->id, conf->agent->b.name, conf->agent->id,
3036 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003037 return 1;
3038 }
3039 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003040 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3041 " at %s:%d does not support HTTP mode.\n",
3042 px->id, target->id, conf->agent->id,
3043 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003044 return 1;
3045 }
3046
Willy Tarreau2bdcfde2019-02-05 13:37:19 +01003047 if (px->bind_proc & ~target->bind_proc) {
3048 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3049 " at %s:%d does not cover all of its processes.\n",
3050 px->id, target->id, conf->agent->id,
3051 conf->agent->conf.file, conf->agent->conf.line);
3052 return 1;
3053 }
3054
Christopher Fauletfe261552019-03-18 13:57:42 +01003055 if ((conf->agent->rt = calloc(global.nbthread, sizeof(*conf->agent->rt))) == NULL) {
Willy Tarreaub0769b22019-02-07 13:40:33 +01003056 ha_alert("Proxy %s : out of memory initializing SPOE agent '%s' declared at %s:%d.\n",
3057 px->id, conf->agent->id, conf->agent->conf.file, conf->agent->conf.line);
3058 return 1;
3059 }
3060 for (i = 0; i < global.nbthread; ++i) {
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003061 conf->agent->rt[i].engine_id = NULL;
Christopher Fauletfe261552019-03-18 13:57:42 +01003062 conf->agent->rt[i].frame_size = conf->agent->max_frame_size;
3063 conf->agent->rt[i].processing = 0;
3064 LIST_INIT(&conf->agent->rt[i].applets);
3065 LIST_INIT(&conf->agent->rt[i].sending_queue);
3066 LIST_INIT(&conf->agent->rt[i].waiting_queue);
3067 HA_SPIN_INIT(&conf->agent->rt[i].lock);
Willy Tarreaub0769b22019-02-07 13:40:33 +01003068 }
3069
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003070 free(conf->agent->b.name);
3071 conf->agent->b.name = NULL;
3072 conf->agent->b.be = target;
3073 return 0;
3074}
3075
Kevin Zhud87b1a52019-09-17 15:05:45 +02003076/* Initializes the SPOE filter for a proxy for a specific thread.
3077 * Returns a negative value if an error occurs. */
3078static int
3079spoe_init_per_thread(struct proxy *p, struct flt_conf *fconf)
3080{
3081 struct spoe_config *conf = fconf->conf;
3082 struct spoe_agent *agent = conf->agent;
3083
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003084 agent->rt[tid].engine_id = generate_pseudo_uuid();
3085 if (agent->rt[tid].engine_id == NULL)
3086 return -1;
Kevin Zhud87b1a52019-09-17 15:05:45 +02003087 return 0;
3088}
3089
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003090/**************************************************************************
3091 * Hooks attached to a stream
3092 *************************************************************************/
3093/* Called when a filter instance is created and attach to a stream. It creates
3094 * the context that will be used to process this stream. */
3095static int
3096spoe_start(struct stream *s, struct filter *filter)
3097{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003098 struct spoe_config *conf = FLT_CONF(filter);
3099 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003100 struct spoe_context *ctx;
3101
3102 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003103 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003104 __FUNCTION__, s);
3105
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003106 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003107 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3108 " - failed to create SPOE context\n",
3109 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003110 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003111 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003112 "SPOE: [%s] failed to create SPOE context\n",
3113 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003114 return 0;
3115 }
3116
Christopher Faulet11610f32017-09-21 10:23:10 +02003117 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003118 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3119
Christopher Faulet11610f32017-09-21 10:23:10 +02003120 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003121 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3122
Christopher Faulet11610f32017-09-21 10:23:10 +02003123 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003124 filter->pre_analyzers |= AN_RES_INSPECT;
3125
Christopher Faulet11610f32017-09-21 10:23:10 +02003126 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003127 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3128
Christopher Faulet11610f32017-09-21 10:23:10 +02003129 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003130 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3131
Christopher Faulet11610f32017-09-21 10:23:10 +02003132 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003133 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3134
3135 return 1;
3136}
3137
3138/* Called when a filter instance is detached from a stream. It release the
3139 * attached SPOE context. */
3140static void
3141spoe_stop(struct stream *s, struct filter *filter)
3142{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003143 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3144 (int)now.tv_sec, (int)now.tv_usec,
3145 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3146 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003147 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003148}
3149
Christopher Fauletf7a30922016-11-10 15:04:51 +01003150
3151/*
3152 * Called when the stream is woken up because of expired timer.
3153 */
3154static void
3155spoe_check_timeouts(struct stream *s, struct filter *filter)
3156{
3157 struct spoe_context *ctx = filter->ctx;
3158
Christopher Fauletac580602018-03-20 16:09:20 +01003159 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003160 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003161}
3162
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003163/* Called when we are ready to filter data on a channel */
3164static int
3165spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3166{
3167 struct spoe_context *ctx = filter->ctx;
3168 int ret = 1;
3169
3170 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3171 " - ctx-flags=0x%08x\n",
3172 (int)now.tv_sec, (int)now.tv_usec,
3173 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3174 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3175
Christopher Fauletb067b062017-01-04 16:39:11 +01003176 if (ctx->state == SPOE_CTX_ST_NONE)
3177 goto out;
3178
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003179 if (!(chn->flags & CF_ISRESP)) {
3180 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3181 chn->analysers |= AN_REQ_INSPECT_FE;
3182 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3183 chn->analysers |= AN_REQ_INSPECT_BE;
3184
3185 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3186 goto out;
3187
3188 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003189 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003190 if (!ret)
3191 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003192 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3193 }
3194 else {
3195 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3196 chn->analysers |= AN_RES_INSPECT;
3197
3198 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3199 goto out;
3200
Christopher Faulet8ef75252017-02-20 22:56:03 +01003201 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003202 if (!ret) {
3203 channel_dont_read(chn);
3204 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003205 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003206 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003207 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003208 }
3209
3210 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003211 return ret;
3212}
3213
3214/* Called before a processing happens on a given channel */
3215static int
3216spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3217 struct channel *chn, unsigned an_bit)
3218{
3219 struct spoe_context *ctx = filter->ctx;
3220 int ret = 1;
3221
3222 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3223 " - ctx-flags=0x%08x - ana=0x%08x\n",
3224 (int)now.tv_sec, (int)now.tv_usec,
3225 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3226 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3227 ctx->flags, an_bit);
3228
Christopher Fauletb067b062017-01-04 16:39:11 +01003229 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230 goto out;
3231
3232 switch (an_bit) {
3233 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003234 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003235 break;
3236 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003237 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003238 break;
3239 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003240 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003241 break;
3242 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003243 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003244 break;
3245 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003246 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003247 break;
3248 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003249 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003250 break;
3251 }
3252
3253 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003254 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003255 channel_dont_read(chn);
3256 channel_dont_close(chn);
3257 }
3258 return ret;
3259}
3260
3261/* Called when the filtering on the channel ends. */
3262static int
3263spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3264{
3265 struct spoe_context *ctx = filter->ctx;
3266
3267 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3268 " - ctx-flags=0x%08x\n",
3269 (int)now.tv_sec, (int)now.tv_usec,
3270 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3271 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3272
3273 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003274 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003275 }
3276
3277 return 1;
3278}
3279
3280/********************************************************************
3281 * Functions that manage the filter initialization
3282 ********************************************************************/
3283struct flt_ops spoe_ops = {
3284 /* Manage SPOE filter, called for each filter declaration */
3285 .init = spoe_init,
3286 .deinit = spoe_deinit,
3287 .check = spoe_check,
Kevin Zhud87b1a52019-09-17 15:05:45 +02003288 .init_per_thread = spoe_init_per_thread,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003289
3290 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003291 .attach = spoe_start,
3292 .detach = spoe_stop,
3293 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003294
3295 /* Handle channels activity */
3296 .channel_start_analyze = spoe_start_analyze,
3297 .channel_pre_analyze = spoe_chn_pre_analyze,
3298 .channel_end_analyze = spoe_end_analyze,
3299};
3300
3301
3302static int
3303cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3304{
3305 const char *err;
3306 int i, err_code = 0;
3307
3308 if ((cfg_scope == NULL && curengine != NULL) ||
3309 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003310 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003311 goto out;
3312
3313 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3314 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003315 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3316 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003317 err_code |= ERR_ALERT | ERR_ABORT;
3318 goto out;
3319 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003320 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3321 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003322 goto out;
3323 }
3324
3325 err = invalid_char(args[1]);
3326 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003327 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3328 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003329 err_code |= ERR_ALERT | ERR_ABORT;
3330 goto out;
3331 }
3332
3333 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003334 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3335 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003336 err_code |= ERR_ALERT | ERR_ABORT;
3337 goto out;
3338 }
3339 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003340 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003341 err_code |= ERR_ALERT | ERR_ABORT;
3342 goto out;
3343 }
3344
3345 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003346
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003347 curagent->conf.file = strdup(file);
3348 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003349
3350 curagent->timeout.hello = TICK_ETERNITY;
3351 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003352 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003353
Christopher Fauleta1cda022016-12-21 08:58:06 +01003354 curagent->var_pfx = NULL;
3355 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003356 curagent->var_t_process = NULL;
3357 curagent->var_t_total = NULL;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003358 curagent->flags = (SPOE_FL_ASYNC | SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003359 curagent->cps_max = 0;
3360 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003361 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003362 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003363
3364 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003365 LIST_INIT(&curagent->events[i]);
3366 LIST_INIT(&curagent->groups);
3367 LIST_INIT(&curagent->messages);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003368 }
3369 else if (!strcmp(args[0], "use-backend")) {
3370 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003371 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3372 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003373 err_code |= ERR_ALERT | ERR_FATAL;
3374 goto out;
3375 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003376 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003377 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003378 free(curagent->b.name);
3379 curagent->b.name = strdup(args[1]);
3380 }
3381 else if (!strcmp(args[0], "messages")) {
3382 int cur_arg = 1;
3383 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003384 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003385
Christopher Faulet11610f32017-09-21 10:23:10 +02003386 list_for_each_entry(ph, &curmphs, list) {
3387 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003388 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3389 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003390 err_code |= ERR_ALERT | ERR_FATAL;
3391 goto out;
3392 }
3393 }
3394
Christopher Faulet11610f32017-09-21 10:23:10 +02003395 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003396 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003397 err_code |= ERR_ALERT | ERR_ABORT;
3398 goto out;
3399 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003400 ph->id = strdup(args[cur_arg]);
3401 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003402 cur_arg++;
3403 }
3404 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003405 else if (!strcmp(args[0], "groups")) {
3406 int cur_arg = 1;
3407 while (*args[cur_arg]) {
3408 struct spoe_placeholder *ph = NULL;
3409
3410 list_for_each_entry(ph, &curgphs, list) {
3411 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003412 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3413 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003414 err_code |= ERR_ALERT | ERR_FATAL;
3415 goto out;
3416 }
3417 }
3418
3419 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003420 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003421 err_code |= ERR_ALERT | ERR_ABORT;
3422 goto out;
3423 }
3424 ph->id = strdup(args[cur_arg]);
3425 LIST_ADDQ(&curgphs, &ph->list);
3426 cur_arg++;
3427 }
3428 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003429 else if (!strcmp(args[0], "timeout")) {
3430 unsigned int *tv = NULL;
3431 const char *res;
3432 unsigned timeout;
3433
3434 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003435 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3436 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003437 err_code |= ERR_ALERT | ERR_FATAL;
3438 goto out;
3439 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003440 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3441 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003442 if (!strcmp(args[1], "hello"))
3443 tv = &curagent->timeout.hello;
3444 else if (!strcmp(args[1], "idle"))
3445 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003446 else if (!strcmp(args[1], "processing"))
3447 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003448 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003449 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3450 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003451 err_code |= ERR_ALERT | ERR_FATAL;
3452 goto out;
3453 }
3454 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003455 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3456 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003457 err_code |= ERR_ALERT | ERR_FATAL;
3458 goto out;
3459 }
3460 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02003461 if (res == PARSE_TIME_OVER) {
3462 ha_alert("parsing [%s:%d]: timer overflow in argument <%s> to <%s %s>, maximum value is 2147483647 ms (~24.8 days).\n",
3463 file, linenum, args[2], args[0], args[1]);
3464 err_code |= ERR_ALERT | ERR_FATAL;
3465 goto out;
3466 }
3467 else if (res == PARSE_TIME_UNDER) {
3468 ha_alert("parsing [%s:%d]: timer underflow in argument <%s> to <%s %s>, minimum non-null value is 1 ms.\n",
3469 file, linenum, args[2], args[0], args[1]);
3470 err_code |= ERR_ALERT | ERR_FATAL;
3471 goto out;
3472 }
3473 else if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003474 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3475 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003476 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003477 goto out;
3478 }
3479 *tv = MS_TO_TICKS(timeout);
3480 }
3481 else if (!strcmp(args[0], "option")) {
3482 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003483 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3484 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003485 err_code |= ERR_ALERT | ERR_FATAL;
3486 goto out;
3487 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003488
Christopher Faulet305c6072017-02-23 16:17:53 +01003489 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003490 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003491 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003492 if (kwm == 1)
3493 curagent->flags &= ~SPOE_FL_PIPELINING;
3494 else
3495 curagent->flags |= SPOE_FL_PIPELINING;
3496 goto out;
3497 }
3498 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003499 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003500 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003501 if (kwm == 1)
3502 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Fauletb1bb1af2019-09-17 11:55:52 +02003503 else
3504 curagent->flags |= SPOE_FL_ASYNC;
Christopher Faulet305c6072017-02-23 16:17:53 +01003505 goto out;
3506 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003507 else if (!strcmp(args[1], "send-frag-payload")) {
3508 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3509 goto out;
3510 if (kwm == 1)
3511 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3512 else
3513 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3514 goto out;
3515 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003516 else if (!strcmp(args[1], "dontlog-normal")) {
3517 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3518 goto out;
3519 if (kwm == 1)
3520 curpxopts2 &= ~PR_O2_NOLOGNORM;
3521 else
3522 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003523 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003524 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003525
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003526 /* Following options does not support negation */
3527 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003528 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3529 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003530 err_code |= ERR_ALERT | ERR_FATAL;
3531 goto out;
3532 }
3533
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003534 if (!strcmp(args[1], "var-prefix")) {
3535 char *tmp;
3536
3537 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003538 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3539 file, linenum, args[0],
3540 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003541 err_code |= ERR_ALERT | ERR_FATAL;
3542 goto out;
3543 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003544 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3545 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003546 tmp = args[2];
3547 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003548 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003549 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003550 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003551 err_code |= ERR_ALERT | ERR_FATAL;
3552 goto out;
3553 }
3554 tmp++;
3555 }
3556 curagent->var_pfx = strdup(args[2]);
3557 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003558 else if (!strcmp(args[1], "force-set-var")) {
3559 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3560 goto out;
3561 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3562 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003563 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003564 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003565 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003566 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3567 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003568 else if (!strcmp(args[1], "set-on-error")) {
3569 char *tmp;
3570
3571 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003572 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3573 file, linenum, args[0],
3574 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003575 err_code |= ERR_ALERT | ERR_FATAL;
3576 goto out;
3577 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003578 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3579 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003580 tmp = args[2];
3581 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003582 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003583 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003584 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003585 err_code |= ERR_ALERT | ERR_FATAL;
3586 goto out;
3587 }
3588 tmp++;
3589 }
3590 curagent->var_on_error = strdup(args[2]);
3591 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003592 else if (!strcmp(args[1], "set-process-time")) {
3593 char *tmp;
3594
3595 if (!*args[2]) {
3596 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3597 file, linenum, args[0],
3598 args[1]);
3599 err_code |= ERR_ALERT | ERR_FATAL;
3600 goto out;
3601 }
3602 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3603 goto out;
3604 tmp = args[2];
3605 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003606 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003607 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003608 file, linenum, args[0], args[1]);
3609 err_code |= ERR_ALERT | ERR_FATAL;
3610 goto out;
3611 }
3612 tmp++;
3613 }
3614 curagent->var_t_process = strdup(args[2]);
3615 }
3616 else if (!strcmp(args[1], "set-total-time")) {
3617 char *tmp;
3618
3619 if (!*args[2]) {
3620 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3621 file, linenum, args[0],
3622 args[1]);
3623 err_code |= ERR_ALERT | ERR_FATAL;
3624 goto out;
3625 }
3626 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3627 goto out;
3628 tmp = args[2];
3629 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01003630 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003631 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003632 file, linenum, args[0], args[1]);
3633 err_code |= ERR_ALERT | ERR_FATAL;
3634 goto out;
3635 }
3636 tmp++;
3637 }
3638 curagent->var_t_total = strdup(args[2]);
3639 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003640 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003641 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3642 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003643 err_code |= ERR_ALERT | ERR_FATAL;
3644 goto out;
3645 }
Christopher Faulet48026722016-11-16 15:01:12 +01003646 }
3647 else if (!strcmp(args[0], "maxconnrate")) {
3648 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003649 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3650 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003651 err_code |= ERR_ALERT | ERR_FATAL;
3652 goto out;
3653 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003654 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003655 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003656 curagent->cps_max = atol(args[1]);
3657 }
3658 else if (!strcmp(args[0], "maxerrrate")) {
3659 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003660 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3661 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003662 err_code |= ERR_ALERT | ERR_FATAL;
3663 goto out;
3664 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003665 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003666 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003667 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003668 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003669 else if (!strcmp(args[0], "max-frame-size")) {
3670 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003671 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3672 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003673 err_code |= ERR_ALERT | ERR_FATAL;
3674 goto out;
3675 }
3676 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3677 goto out;
3678 curagent->max_frame_size = atol(args[1]);
3679 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3680 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003681 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3682 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003683 err_code |= ERR_ALERT | ERR_FATAL;
3684 goto out;
3685 }
3686 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003687 else if (!strcmp(args[0], "max-waiting-frames")) {
3688 if (!*args[1]) {
3689 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3690 file, linenum, args[0]);
3691 err_code |= ERR_ALERT | ERR_FATAL;
3692 goto out;
3693 }
3694 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3695 goto out;
3696 curagent->max_fpa = atol(args[1]);
3697 if (curagent->max_fpa < 1) {
3698 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3699 file, linenum, args[0]);
3700 err_code |= ERR_ALERT | ERR_FATAL;
3701 goto out;
3702 }
3703 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003704 else if (!strcmp(args[0], "register-var-names")) {
3705 int cur_arg;
3706
3707 if (!*args[1]) {
3708 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3709 file, linenum, args[0]);
3710 err_code |= ERR_ALERT | ERR_FATAL;
3711 goto out;
3712 }
3713 cur_arg = 1;
3714 while (*args[cur_arg]) {
3715 struct spoe_var_placeholder *vph;
3716
3717 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3718 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3719 err_code |= ERR_ALERT | ERR_ABORT;
3720 goto out;
3721 }
3722 if ((vph->name = strdup(args[cur_arg])) == NULL) {
Tim Duesterhusb2986132019-06-23 22:10:13 +02003723 free(vph);
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003724 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3725 err_code |= ERR_ALERT | ERR_ABORT;
3726 goto out;
3727 }
3728 LIST_ADDQ(&curvars, &vph->list);
3729 cur_arg++;
3730 }
3731 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003732 else if (!strcmp(args[0], "log")) {
3733 char *errmsg = NULL;
3734
3735 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3736 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3737 err_code |= ERR_ALERT | ERR_FATAL;
3738 goto out;
3739 }
3740 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003741 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003742 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3743 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003744 err_code |= ERR_ALERT | ERR_FATAL;
3745 goto out;
3746 }
3747 out:
3748 return err_code;
3749}
Christopher Faulet11610f32017-09-21 10:23:10 +02003750static int
3751cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3752{
3753 struct spoe_group *grp;
3754 const char *err;
3755 int err_code = 0;
3756
3757 if ((cfg_scope == NULL && curengine != NULL) ||
3758 (cfg_scope != NULL && curengine == NULL) ||
3759 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3760 goto out;
3761
3762 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3763 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003764 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3765 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003766 err_code |= ERR_ALERT | ERR_ABORT;
3767 goto out;
3768 }
3769 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3770 err_code |= ERR_ABORT;
3771 goto out;
3772 }
3773
3774 err = invalid_char(args[1]);
3775 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003776 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3777 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003778 err_code |= ERR_ALERT | ERR_ABORT;
3779 goto out;
3780 }
3781
3782 list_for_each_entry(grp, &curgrps, list) {
3783 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003784 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3785 " name as another one declared at %s:%d.\n",
3786 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003787 err_code |= ERR_ALERT | ERR_FATAL;
3788 goto out;
3789 }
3790 }
3791
3792 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003793 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003794 err_code |= ERR_ALERT | ERR_ABORT;
3795 goto out;
3796 }
3797
3798 curgrp->id = strdup(args[1]);
3799 curgrp->conf.file = strdup(file);
3800 curgrp->conf.line = linenum;
3801 LIST_INIT(&curgrp->phs);
3802 LIST_INIT(&curgrp->messages);
3803 LIST_ADDQ(&curgrps, &curgrp->list);
3804 }
3805 else if (!strcmp(args[0], "messages")) {
3806 int cur_arg = 1;
3807 while (*args[cur_arg]) {
3808 struct spoe_placeholder *ph = NULL;
3809
3810 list_for_each_entry(ph, &curgrp->phs, list) {
3811 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003812 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3813 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003814 err_code |= ERR_ALERT | ERR_FATAL;
3815 goto out;
3816 }
3817 }
3818
3819 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003820 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003821 err_code |= ERR_ALERT | ERR_ABORT;
3822 goto out;
3823 }
3824 ph->id = strdup(args[cur_arg]);
3825 LIST_ADDQ(&curgrp->phs, &ph->list);
3826 cur_arg++;
3827 }
3828 }
3829 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003830 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3831 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003832 err_code |= ERR_ALERT | ERR_FATAL;
3833 goto out;
3834 }
3835 out:
3836 return err_code;
3837}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003838
3839static int
3840cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3841{
3842 struct spoe_message *msg;
3843 struct spoe_arg *arg;
3844 const char *err;
3845 char *errmsg = NULL;
3846 int err_code = 0;
3847
3848 if ((cfg_scope == NULL && curengine != NULL) ||
3849 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003850 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003851 goto out;
3852
3853 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3854 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003855 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3856 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003857 err_code |= ERR_ALERT | ERR_ABORT;
3858 goto out;
3859 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003860 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3861 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003862 goto out;
3863 }
3864
3865 err = invalid_char(args[1]);
3866 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003867 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3868 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003869 err_code |= ERR_ALERT | ERR_ABORT;
3870 goto out;
3871 }
3872
3873 list_for_each_entry(msg, &curmsgs, list) {
3874 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003875 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3876 " name as another one declared at %s:%d.\n",
3877 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003878 err_code |= ERR_ALERT | ERR_FATAL;
3879 goto out;
3880 }
3881 }
3882
3883 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003884 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003885 err_code |= ERR_ALERT | ERR_ABORT;
3886 goto out;
3887 }
3888
3889 curmsg->id = strdup(args[1]);
3890 curmsg->id_len = strlen(curmsg->id);
3891 curmsg->event = SPOE_EV_NONE;
3892 curmsg->conf.file = strdup(file);
3893 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003894 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003895 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003896 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003897 LIST_INIT(&curmsg->by_evt);
3898 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003899 LIST_ADDQ(&curmsgs, &curmsg->list);
3900 }
3901 else if (!strcmp(args[0], "args")) {
3902 int cur_arg = 1;
3903
3904 curproxy->conf.args.ctx = ARGC_SPOE;
3905 curproxy->conf.args.file = file;
3906 curproxy->conf.args.line = linenum;
3907 while (*args[cur_arg]) {
3908 char *delim = strchr(args[cur_arg], '=');
3909 int idx = 0;
3910
3911 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003912 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003913 err_code |= ERR_ALERT | ERR_ABORT;
3914 goto out;
3915 }
3916
3917 if (!delim) {
3918 arg->name = NULL;
3919 arg->name_len = 0;
3920 delim = args[cur_arg];
3921 }
3922 else {
3923 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3924 arg->name_len = delim - args[cur_arg];
3925 delim++;
3926 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003927 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3928 &idx, file, linenum, &errmsg,
Willy Tarreaue3b57bf2020-02-14 16:50:14 +01003929 &curproxy->conf.args, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003930 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003931 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003932 err_code |= ERR_ALERT | ERR_FATAL;
3933 free(arg->name);
3934 free(arg);
3935 goto out;
3936 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003937 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003938 LIST_ADDQ(&curmsg->args, &arg->list);
3939 cur_arg++;
3940 }
3941 curproxy->conf.args.file = NULL;
3942 curproxy->conf.args.line = 0;
3943 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003944 else if (!strcmp(args[0], "acl")) {
3945 err = invalid_char(args[1]);
3946 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003947 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3948 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003949 err_code |= ERR_ALERT | ERR_FATAL;
3950 goto out;
3951 }
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003952 if (strcasecmp(args[1], "or") == 0) {
Tim Duesterhusf1bc24c2020-02-06 22:04:03 +01003953 ha_alert("parsing [%s:%d] : acl name '%s' will never match. 'or' is used to express a "
Tim Duesterhus0cf811a2020-02-05 21:00:50 +01003954 "logical disjunction within a condition.\n",
3955 file, linenum, args[1]);
3956 err_code |= ERR_ALERT | ERR_FATAL;
3957 goto out;
3958 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003959 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003960 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3961 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003962 err_code |= ERR_ALERT | ERR_FATAL;
3963 goto out;
3964 }
3965 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003966 else if (!strcmp(args[0], "event")) {
3967 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003968 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003969 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003970 goto out;
3971 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003972 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3973 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003974
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003975 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3976 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3977 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3978 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3979
3980 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3981 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3982 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3983 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3984 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3985 curmsg->event = SPOE_EV_ON_TCP_RSP;
3986
3987 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3988 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3989 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3990 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3991 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3992 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3993 else {
Joseph Herlantf1da69d2018-11-15 13:49:02 -08003994 ha_alert("parsing [%s:%d] : unknown event '%s'.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003995 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003996 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003997 goto out;
3998 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003999
4000 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
4001 struct acl_cond *cond;
4002
4003 cond = build_acl_cond(file, linenum, &curmsg->acls,
4004 curproxy, (const char **)args+2,
4005 &errmsg);
4006 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004007 ha_alert("parsing [%s:%d] : error detected while "
4008 "parsing an 'event %s' condition : %s.\n",
4009 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02004010 err_code |= ERR_ALERT | ERR_FATAL;
4011 goto out;
4012 }
4013 curmsg->cond = cond;
4014 }
4015 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004016 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4017 "or 'unless' followed by a condition but found '%s'.\n",
4018 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004019 err_code |= ERR_ALERT | ERR_FATAL;
4020 goto out;
4021 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004022 }
4023 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004024 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4025 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004026 err_code |= ERR_ALERT | ERR_FATAL;
4027 goto out;
4028 }
4029 out:
4030 free(errmsg);
4031 return err_code;
4032}
4033
4034/* Return -1 on error, else 0 */
4035static int
4036parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4037 struct flt_conf *fconf, char **err, void *private)
4038{
4039 struct list backup_sections;
4040 struct spoe_config *conf;
4041 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004042 struct spoe_group *grp, *grpback;
4043 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004044 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004045 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004046 char *file = NULL, *engine = NULL;
4047 int ret, pos = *cur_arg + 1;
4048
Christopher Faulet84c844e2018-03-23 14:37:14 +01004049 LIST_INIT(&curmsgs);
4050 LIST_INIT(&curgrps);
4051 LIST_INIT(&curmphs);
4052 LIST_INIT(&curgphs);
4053 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004054 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004055 curpxopts = 0;
4056 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004057
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004058 conf = calloc(1, sizeof(*conf));
4059 if (conf == NULL) {
4060 memprintf(err, "%s: out of memory", args[*cur_arg]);
4061 goto error;
4062 }
4063 conf->proxy = px;
4064
4065 while (*args[pos]) {
4066 if (!strcmp(args[pos], "config")) {
4067 if (!*args[pos+1]) {
4068 memprintf(err, "'%s' : '%s' option without value",
4069 args[*cur_arg], args[pos]);
4070 goto error;
4071 }
4072 file = args[pos+1];
4073 pos += 2;
4074 }
4075 else if (!strcmp(args[pos], "engine")) {
4076 if (!*args[pos+1]) {
4077 memprintf(err, "'%s' : '%s' option without value",
4078 args[*cur_arg], args[pos]);
4079 goto error;
4080 }
4081 engine = args[pos+1];
4082 pos += 2;
4083 }
4084 else {
4085 memprintf(err, "unknown keyword '%s'", args[pos]);
4086 goto error;
4087 }
4088 }
4089 if (file == NULL) {
4090 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4091 goto error;
4092 }
4093
4094 /* backup sections and register SPOE sections */
4095 LIST_INIT(&backup_sections);
4096 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004097 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4098 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004099 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004100
4101 /* Parse SPOE filter configuration file */
4102 curengine = engine;
4103 curproxy = px;
4104 curagent = NULL;
4105 curmsg = NULL;
4106 ret = readcfgfile(file);
4107 curproxy = NULL;
4108
4109 /* unregister SPOE sections and restore previous sections */
4110 cfg_unregister_sections();
4111 cfg_restore_sections(&backup_sections);
4112
4113 if (ret == -1) {
4114 memprintf(err, "Could not open configuration file %s : %s",
4115 file, strerror(errno));
4116 goto error;
4117 }
4118 if (ret & (ERR_ABORT|ERR_FATAL)) {
4119 memprintf(err, "Error(s) found in configuration file %s", file);
4120 goto error;
4121 }
4122
4123 /* Check SPOE agent */
4124 if (curagent == NULL) {
4125 memprintf(err, "No SPOE agent found in file %s", file);
4126 goto error;
4127 }
4128 if (curagent->b.name == NULL) {
4129 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4130 curagent->id, curagent->conf.file, curagent->conf.line);
4131 goto error;
4132 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004133 if (curagent->timeout.hello == TICK_ETERNITY ||
4134 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004135 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004136 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4137 " | While not properly invalid, you will certainly encounter various problems\n"
4138 " | with such a configuration. To fix this, please ensure that all following\n"
4139 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4140 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004141 }
4142 if (curagent->var_pfx == NULL) {
4143 char *tmp = curagent->id;
4144
4145 while (*tmp) {
Willy Tarreau90807112020-02-25 08:16:33 +01004146 if (!isalnum((unsigned char)*tmp) && *tmp != '_' && *tmp != '.') {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004147 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4148 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4149 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4150 goto error;
4151 }
4152 tmp++;
4153 }
4154 curagent->var_pfx = strdup(curagent->id);
4155 }
4156
Christopher Fauletb7426d12018-03-21 14:12:17 +01004157 if (curagent->var_on_error) {
4158 struct arg arg;
4159
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004160 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004161 curagent->var_pfx, curagent->var_on_error);
4162
4163 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004164 arg.data.str.area = trash.area;
4165 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004166 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Fauletb7426d12018-03-21 14:12:17 +01004167 if (!vars_check_arg(&arg, err)) {
4168 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4169 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4170 goto error;
4171 }
4172 }
4173
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004174 if (curagent->var_t_process) {
4175 struct arg arg;
4176
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004177 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004178 curagent->var_pfx, curagent->var_t_process);
4179
4180 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004181 arg.data.str.area = trash.area;
4182 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004183 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004184 if (!vars_check_arg(&arg, err)) {
4185 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4186 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4187 goto error;
4188 }
4189 }
4190
4191 if (curagent->var_t_total) {
4192 struct arg arg;
4193
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004194 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004195 curagent->var_pfx, curagent->var_t_total);
4196
4197 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004198 arg.data.str.area = trash.area;
4199 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004200 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004201 if (!vars_check_arg(&arg, err)) {
4202 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4203 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4204 goto error;
4205 }
4206 }
4207
Christopher Faulet11610f32017-09-21 10:23:10 +02004208 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004209 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4210 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004211 goto finish;
4212 }
4213
Christopher Faulet11610f32017-09-21 10:23:10 +02004214 /* Replace placeholders by the corresponding messages for the SPOE
4215 * agent */
4216 list_for_each_entry(ph, &curmphs, list) {
4217 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004218 struct spoe_arg *arg;
4219 unsigned int where;
4220
Christopher Faulet11610f32017-09-21 10:23:10 +02004221 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004222 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4223 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4224 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4225 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4226 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4227 }
4228 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4229 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4230 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004231 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4232 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004233 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004234 }
4235 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004236 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4237 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004238 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004239 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004240
4241 where = 0;
4242 switch (msg->event) {
4243 case SPOE_EV_ON_CLIENT_SESS:
4244 where |= SMP_VAL_FE_CON_ACC;
4245 break;
4246
4247 case SPOE_EV_ON_TCP_REQ_FE:
4248 where |= SMP_VAL_FE_REQ_CNT;
4249 break;
4250
4251 case SPOE_EV_ON_HTTP_REQ_FE:
4252 where |= SMP_VAL_FE_HRQ_HDR;
4253 break;
4254
4255 case SPOE_EV_ON_TCP_REQ_BE:
4256 if (px->cap & PR_CAP_FE)
4257 where |= SMP_VAL_FE_REQ_CNT;
4258 if (px->cap & PR_CAP_BE)
4259 where |= SMP_VAL_BE_REQ_CNT;
4260 break;
4261
4262 case SPOE_EV_ON_HTTP_REQ_BE:
4263 if (px->cap & PR_CAP_FE)
4264 where |= SMP_VAL_FE_HRQ_HDR;
4265 if (px->cap & PR_CAP_BE)
4266 where |= SMP_VAL_BE_HRQ_HDR;
4267 break;
4268
4269 case SPOE_EV_ON_SERVER_SESS:
4270 where |= SMP_VAL_BE_SRV_CON;
4271 break;
4272
4273 case SPOE_EV_ON_TCP_RSP:
4274 if (px->cap & PR_CAP_FE)
4275 where |= SMP_VAL_FE_RES_CNT;
4276 if (px->cap & PR_CAP_BE)
4277 where |= SMP_VAL_BE_RES_CNT;
4278 break;
4279
4280 case SPOE_EV_ON_HTTP_RSP:
4281 if (px->cap & PR_CAP_FE)
4282 where |= SMP_VAL_FE_HRS_HDR;
4283 if (px->cap & PR_CAP_BE)
4284 where |= SMP_VAL_BE_HRS_HDR;
4285 break;
4286
4287 default:
4288 break;
4289 }
4290
4291 list_for_each_entry(arg, &msg->args, list) {
4292 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004293 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004294 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004295 "none of which is available here ('%s')",
4296 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004297 sample_ckp_names(arg->expr->fetch->use),
4298 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004299 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004300 }
4301 }
4302
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004303 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004304 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4305 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004306 }
4307 }
4308 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004309 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004310 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004311 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004312 continue;
4313 }
4314
Christopher Faulet11610f32017-09-21 10:23:10 +02004315 /* Replace placeholders by the corresponding groups for the SPOE
4316 * agent */
4317 list_for_each_entry(ph, &curgphs, list) {
4318 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4319 if (!strcmp(grp->id, ph->id)) {
4320 grp->agent = curagent;
4321 LIST_DEL(&grp->list);
4322 LIST_ADDQ(&curagent->groups, &grp->list);
4323 goto next_aph;
4324 }
4325 }
4326 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4327 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4328 goto error;
4329 next_aph:
4330 continue;
4331 }
4332
4333 /* Replace placeholders by the corresponding message for each SPOE
4334 * group of the SPOE agent */
4335 list_for_each_entry(grp, &curagent->groups, list) {
4336 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4337 list_for_each_entry(msg, &curmsgs, list) {
4338 if (!strcmp(msg->id, ph->id)) {
4339 if (msg->group != NULL) {
4340 memprintf(err, "SPOE message '%s' already belongs to "
4341 "the SPOE group '%s' declare at %s:%d",
4342 msg->id, msg->group->id,
4343 msg->group->conf.file,
4344 msg->group->conf.line);
4345 goto error;
4346 }
4347
4348 /* Scope for arguments are not checked for now. We will check
4349 * them only if a rule use the corresponding SPOE group. */
4350 msg->agent = curagent;
4351 msg->group = grp;
4352 LIST_DEL(&ph->list);
4353 LIST_ADDQ(&grp->messages, &msg->by_grp);
4354 goto next_mph_grp;
4355 }
4356 }
4357 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4358 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4359 goto error;
4360 next_mph_grp:
4361 continue;
4362 }
4363 }
4364
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004365 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004366 /* move curmsgs to the agent message list */
4367 curmsgs.n->p = &curagent->messages;
4368 curmsgs.p->n = &curagent->messages;
4369 curagent->messages = curmsgs;
4370 LIST_INIT(&curmsgs);
4371
Christopher Faulet7ee86672017-09-19 11:08:28 +02004372 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004373 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004374
4375 /* Start agent's proxy initialization here. It will be finished during
4376 * the filter init. */
4377 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4378 init_new_proxy(&conf->agent_fe);
4379 conf->agent_fe.id = conf->agent->id;
4380 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004381 conf->agent_fe.options |= curpxopts;
4382 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004383
4384 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4385 LIST_DEL(&logsrv->list);
4386 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4387 }
4388
Christopher Faulet11610f32017-09-21 10:23:10 +02004389 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4390 LIST_DEL(&ph->list);
4391 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004392 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004393 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4394 LIST_DEL(&ph->list);
4395 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004396 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004397 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4398 struct arg arg;
4399
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004400 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004401 curagent->var_pfx, vph->name);
4402
4403 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004404 arg.data.str.area = trash.area;
4405 arg.data.str.data = trash.data;
Christopher Fauletbf9bcb02019-05-13 10:39:36 +02004406 arg.data.str.size = 0; /* Set it to 0 to not release it in vars_check_args() */
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004407 if (!vars_check_arg(&arg, err)) {
4408 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4409 curagent->id, curagent->var_pfx, vph->name, *err);
4410 goto error;
4411 }
4412
4413 LIST_DEL(&vph->list);
4414 free(vph->name);
4415 free(vph);
4416 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004417 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4418 LIST_DEL(&grp->list);
4419 spoe_release_group(grp);
4420 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004421 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004422 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004423 fconf->ops = &spoe_ops;
4424 fconf->conf = conf;
4425 return 0;
4426
4427 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004428 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004429 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4430 LIST_DEL(&ph->list);
4431 spoe_release_placeholder(ph);
4432 }
4433 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4434 LIST_DEL(&ph->list);
4435 spoe_release_placeholder(ph);
4436 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004437 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4438 LIST_DEL(&vph->list);
4439 free(vph->name);
4440 free(vph);
4441 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004442 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4443 LIST_DEL(&grp->list);
4444 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004445 }
4446 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4447 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004448 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004449 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004450 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4451 LIST_DEL(&logsrv->list);
4452 free(logsrv);
4453 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004454 free(conf);
4455 return -1;
4456}
4457
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004458/* Send message of a SPOE group. This is the action_ptr callback of a rule
4459 * associated to a "send-spoe-group" action.
4460 *
Christopher Faulet13403762019-12-13 09:01:57 +01004461 * It returns ACT_RET_CONT if processing is finished (with error or not), it returns
4462 * ACT_RET_YIELD if the action is in progress. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004463static enum act_return
4464spoe_send_group(struct act_rule *rule, struct proxy *px,
4465 struct session *sess, struct stream *s, int flags)
4466{
4467 struct filter *filter;
4468 struct spoe_agent *agent = NULL;
4469 struct spoe_group *group = NULL;
4470 struct spoe_context *ctx = NULL;
4471 int ret, dir;
4472
4473 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4474 if (filter->config == rule->arg.act.p[0]) {
4475 agent = rule->arg.act.p[2];
4476 group = rule->arg.act.p[3];
4477 ctx = filter->ctx;
4478 break;
4479 }
4480 }
4481 if (agent == NULL || group == NULL || ctx == NULL)
Christopher Faulet13403762019-12-13 09:01:57 +01004482 return ACT_RET_CONT;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004483 if (ctx->state == SPOE_CTX_ST_NONE)
4484 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004485
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004486 switch (rule->from) {
4487 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4488 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4489 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4490 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4491 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4492 default:
4493 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4494 " - internal error while execute spoe-send-group\n",
4495 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4496 __FUNCTION__, s);
4497 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4498 agent->id);
4499 return ACT_RET_CONT;
4500 }
4501
4502 ret = spoe_process_group(s, ctx, group, dir);
4503 if (ret == 1)
4504 return ACT_RET_CONT;
4505 else if (ret == 0) {
Christopher Faulet105ba6c2019-12-18 14:41:51 +01004506 if (flags & ACT_OPT_FINAL) {
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004507 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4508 " - failed to process group '%s': interrupted by caller\n",
4509 (int)now.tv_sec, (int)now.tv_usec,
4510 agent->id, __FUNCTION__, s, group->id);
4511 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004512 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004513 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004514 return ACT_RET_CONT;
4515 }
4516 return ACT_RET_YIELD;
4517 }
4518 else
Christopher Faulet13403762019-12-13 09:01:57 +01004519 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004520}
4521
4522/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4523 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4524 * action should be:
4525 *
4526 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4527 *
4528 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4529 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4530 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4531 * group.
4532 *
4533 * The function returns 1 in success case, otherwise, it returns 0 and err is
4534 * filled.
4535 */
4536static int
4537check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4538{
4539 struct flt_conf *fconf;
4540 struct spoe_config *conf;
4541 struct spoe_agent *agent = NULL;
4542 struct spoe_group *group;
4543 struct spoe_message *msg;
4544 char *engine_id = rule->arg.act.p[0];
4545 char *group_id = rule->arg.act.p[1];
4546 unsigned int where = 0;
4547
4548 switch (rule->from) {
4549 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4550 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4551 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4552 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4553 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4554 default:
4555 memprintf(err,
4556 "internal error, unexpected rule->from=%d, please report this bug!",
4557 rule->from);
4558 goto error;
4559 }
4560
4561 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4562 * <px> */
4563 list_for_each_entry(fconf, &px->filter_configs, list) {
4564 conf = fconf->conf;
4565
4566 /* This is not an SPOE filter */
4567 if (fconf->id != spoe_filter_id)
4568 continue;
4569
4570 /* This is the good engine */
4571 if (!strcmp(conf->id, engine_id)) {
4572 agent = conf->agent;
4573 break;
4574 }
4575 }
4576 if (agent == NULL) {
4577 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4578 engine_id, group_id);
4579 goto error;
4580 }
4581
4582 /* Try to find the right group */
4583 list_for_each_entry(group, &agent->groups, list) {
4584 /* This is the good group */
4585 if (!strcmp(group->id, group_id))
4586 break;
4587 }
4588 if (&group->list == &agent->groups) {
4589 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4590 group_id, engine_id);
4591 goto error;
4592 }
4593
4594 /* Ok, we found the group, we need to check messages and their
4595 * arguments */
4596 list_for_each_entry(msg, &group->messages, by_grp) {
4597 struct spoe_arg *arg;
4598
4599 list_for_each_entry(arg, &msg->args, list) {
4600 if (!(arg->expr->fetch->val & where)) {
4601 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4602 "some args extract information from '%s',"
4603 "none of which is available here ('%s')",
4604 msg->id, group->id, msg->conf.file, msg->conf.line,
4605 sample_ckp_names(arg->expr->fetch->use),
4606 sample_ckp_names(where));
4607 goto error;
4608 }
4609 }
4610 }
4611
4612 free(engine_id);
4613 free(group_id);
4614 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4615 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4616 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4617 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4618 return 1;
4619
4620 error:
4621 free(engine_id);
4622 free(group_id);
4623 return 0;
4624}
4625
4626/* Parse 'send-spoe-group' action following the format:
4627 *
4628 * ... send-spoe-group <engine-id> <group-id>
4629 *
4630 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4631 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4632 * ids are saved and used later, when the rule will be checked.
4633 */
4634static enum act_parse_ret
4635parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4636 struct act_rule *rule, char **err)
4637{
4638 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4639 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4640 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4641 return ACT_RET_PRS_ERR;
4642 }
4643 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4644 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4645
4646 (*orig_arg) += 2;
4647
4648 rule->action = ACT_CUSTOM;
4649 rule->action_ptr = spoe_send_group;
4650 rule->check_ptr = check_send_spoe_group;
4651 return ACT_RET_PRS_OK;
4652}
4653
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004654
4655/* Declare the filter parser for "spoe" keyword */
4656static struct flt_kw_list flt_kws = { "SPOE", { }, {
4657 { "spoe", parse_spoe_flt, NULL },
4658 { NULL, NULL, NULL },
4659 }
4660};
4661
Willy Tarreau0108d902018-11-25 19:14:37 +01004662INITCALL1(STG_REGISTER, flt_register_keywords, &flt_kws);
4663
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004664/* Delcate the action parser for "spoe-action" keyword */
4665static struct action_kw_list tcp_req_action_kws = { { }, {
4666 { "send-spoe-group", parse_send_spoe_group },
4667 { /* END */ },
4668 }
4669};
Willy Tarreau0108d902018-11-25 19:14:37 +01004670
4671INITCALL1(STG_REGISTER, tcp_req_cont_keywords_register, &tcp_req_action_kws);
4672
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004673static struct action_kw_list tcp_res_action_kws = { { }, {
4674 { "send-spoe-group", parse_send_spoe_group },
4675 { /* END */ },
4676 }
4677};
Willy Tarreau0108d902018-11-25 19:14:37 +01004678
4679INITCALL1(STG_REGISTER, tcp_res_cont_keywords_register, &tcp_res_action_kws);
4680
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004681static struct action_kw_list http_req_action_kws = { { }, {
4682 { "send-spoe-group", parse_send_spoe_group },
4683 { /* END */ },
4684 }
4685};
Willy Tarreau0108d902018-11-25 19:14:37 +01004686
4687INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_action_kws);
4688
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004689static struct action_kw_list http_res_action_kws = { { }, {
4690 { "send-spoe-group", parse_send_spoe_group },
4691 { /* END */ },
4692 }
4693};
4694
Willy Tarreau0108d902018-11-25 19:14:37 +01004695INITCALL1(STG_REGISTER, http_res_keywords_register, &http_res_action_kws);