blob: cd0b88a94284664284f4444449fa19f1a555a1aa [file] [log] [blame]
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001/*
2 * Stream processing offload engine management.
3 *
4 * Copyright 2016 HAProxy Technologies, Christopher Faulet <cfaulet@haproxy.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12#include <ctype.h>
13#include <errno.h>
14
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020015#include <common/cfgparse.h>
16#include <common/compat.h>
17#include <common/config.h>
18#include <common/debug.h>
19#include <common/memory.h>
20#include <common/time.h>
Emeric Bruna1dd2432017-06-21 15:42:52 +020021#include <common/hathreads.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020022
23#include <types/arg.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020024#include <types/global.h>
Christopher Faulet1f40b912017-02-17 09:32:19 +010025#include <types/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020026
Christopher Faulet57583e42017-09-04 15:41:09 +020027#include <proto/acl.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020028#include <proto/action.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020029#include <proto/arg.h>
30#include <proto/backend.h>
31#include <proto/filters.h>
Christopher Faulet48026722016-11-16 15:01:12 +010032#include <proto/freq_ctr.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020033#include <proto/frontend.h>
Willy Tarreau61c112a2018-10-02 16:43:32 +020034#include <proto/http_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020035#include <proto/log.h>
36#include <proto/proto_http.h>
37#include <proto/proxy.h>
38#include <proto/sample.h>
39#include <proto/session.h>
40#include <proto/signal.h>
Christopher Faulet4ff3e572017-02-24 14:31:11 +010041#include <proto/spoe.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020042#include <proto/stream.h>
43#include <proto/stream_interface.h>
44#include <proto/task.h>
Christopher Faulet76c09ef2017-09-21 11:03:52 +020045#include <proto/tcp_rules.h>
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020046#include <proto/vars.h>
47
48#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
49#define SPOE_PRINTF(x...) fprintf(x)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010050#define SPOE_DEBUG_STMT(statement) statement
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020051#else
52#define SPOE_PRINTF(x...)
Christopher Fauletb077cdc2018-01-24 16:37:57 +010053#define SPOE_DEBUG_STMT(statement)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020054#endif
55
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010056/* Reserved 4 bytes to the frame size. So a frame and its size can be written
57 * together in a buffer */
58#define MAX_FRAME_SIZE global.tune.bufsize - 4
59
60/* The minimum size for a frame */
61#define MIN_FRAME_SIZE 256
62
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010063/* Reserved for the metadata and the frame type.
64 * So <MAX_FRAME_SIZE> - <FRAME_HDR_SIZE> is the maximum payload size */
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +010065#define FRAME_HDR_SIZE 32
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020066
Christopher Fauletf51f5fa2017-01-19 10:01:12 +010067/* Helper to get SPOE ctx inside an appctx */
Christopher Faulet42bfa462017-01-04 14:14:19 +010068#define SPOE_APPCTX(appctx) ((struct spoe_appctx *)((appctx)->ctx.spoe.ptr))
69
Christopher Faulet3b386a32017-02-23 10:17:15 +010070/* SPOE filter id. Used to identify SPOE filters */
71const char *spoe_filter_id = "SPOE filter";
72
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020073/* Set if the handle on SIGUSR1 is registered */
74static int sighandler_registered = 0;
75
76/* proxy used during the parsing */
77struct proxy *curproxy = NULL;
78
79/* The name of the SPOE engine, used during the parsing */
80char *curengine = NULL;
81
82/* SPOE agent used during the parsing */
Christopher Faulet11610f32017-09-21 10:23:10 +020083/* SPOE agent/group/message used during the parsing */
84struct spoe_agent *curagent = NULL;
85struct spoe_group *curgrp = NULL;
86struct spoe_message *curmsg = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020087
88/* list of SPOE messages and placeholders used during the parsing */
89struct list curmsgs;
Christopher Faulet11610f32017-09-21 10:23:10 +020090struct list curgrps;
91struct list curmphs;
92struct list curgphs;
Christopher Faulet336d3ef2017-12-22 10:00:55 +010093struct list curvars;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +020094
Christopher Faulet7250b8f2018-03-26 17:19:01 +020095/* list of log servers used during the parsing */
96struct list curlogsrvs;
97
Christopher Faulet0e0f0852018-03-26 17:20:36 +020098/* agent's proxy flags (PR_O_* and PR_O2_*) used during parsing */
99int curpxopts;
100int curpxopts2;
101
Christopher Faulet42bfa462017-01-04 14:14:19 +0100102/* Pools used to allocate SPOE structs */
Willy Tarreaubafbe012017-11-24 17:34:44 +0100103static struct pool_head *pool_head_spoe_ctx = NULL;
104static struct pool_head *pool_head_spoe_appctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200105
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200106struct flt_ops spoe_ops;
107
Christopher Faulet8ef75252017-02-20 22:56:03 +0100108static int spoe_queue_context(struct spoe_context *ctx);
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200109static int spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
110static void spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200111
112/********************************************************************
113 * helper functions/globals
114 ********************************************************************/
115static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200116spoe_release_placeholder(struct spoe_placeholder *ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200117{
Christopher Faulet11610f32017-09-21 10:23:10 +0200118 if (!ph)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200119 return;
Christopher Faulet11610f32017-09-21 10:23:10 +0200120 free(ph->id);
121 free(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200122}
123
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200124static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100125spoe_release_message(struct spoe_message *msg)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200126{
Christopher Faulet57583e42017-09-04 15:41:09 +0200127 struct spoe_arg *arg, *argback;
128 struct acl *acl, *aclback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200129
130 if (!msg)
131 return;
132 free(msg->id);
133 free(msg->conf.file);
Christopher Faulet57583e42017-09-04 15:41:09 +0200134 list_for_each_entry_safe(arg, argback, &msg->args, list) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200135 release_sample_expr(arg->expr);
136 free(arg->name);
137 LIST_DEL(&arg->list);
138 free(arg);
139 }
Christopher Faulet57583e42017-09-04 15:41:09 +0200140 list_for_each_entry_safe(acl, aclback, &msg->acls, list) {
141 LIST_DEL(&acl->list);
142 prune_acl(acl);
143 free(acl);
144 }
145 if (msg->cond) {
146 prune_acl_cond(msg->cond);
147 free(msg->cond);
148 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200149 free(msg);
150}
151
152static void
Christopher Faulet11610f32017-09-21 10:23:10 +0200153spoe_release_group(struct spoe_group *grp)
154{
155 if (!grp)
156 return;
157 free(grp->id);
158 free(grp->conf.file);
159 free(grp);
160}
161
162static void
Christopher Faulet8ef75252017-02-20 22:56:03 +0100163spoe_release_agent(struct spoe_agent *agent)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200164{
Christopher Faulet11610f32017-09-21 10:23:10 +0200165 struct spoe_message *msg, *msgback;
166 struct spoe_group *grp, *grpback;
Christopher Faulet24289f22017-09-25 14:48:02 +0200167 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200168
169 if (!agent)
170 return;
171 free(agent->id);
172 free(agent->conf.file);
173 free(agent->var_pfx);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100174 free(agent->engine_id);
Christopher Faulet985532d2016-11-16 15:36:19 +0100175 free(agent->var_on_error);
Christopher Faulet36bda1c2018-03-22 09:08:20 +0100176 free(agent->var_t_process);
177 free(agent->var_t_total);
Christopher Faulet11610f32017-09-21 10:23:10 +0200178 list_for_each_entry_safe(msg, msgback, &agent->messages, list) {
179 LIST_DEL(&msg->list);
180 spoe_release_message(msg);
181 }
182 list_for_each_entry_safe(grp, grpback, &agent->groups, list) {
183 LIST_DEL(&grp->list);
184 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200185 }
Christopher Faulet24289f22017-09-25 14:48:02 +0200186 for (i = 0; i < global.nbthread; ++i)
Christopher Faulet2a944ee2017-11-07 10:42:54 +0100187 HA_SPIN_DESTROY(&agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +0200188 free(agent->rt);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200189 free(agent);
190}
191
192static const char *spoe_frm_err_reasons[SPOE_FRM_ERRS] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100193 [SPOE_FRM_ERR_NONE] = "normal",
194 [SPOE_FRM_ERR_IO] = "I/O error",
195 [SPOE_FRM_ERR_TOUT] = "a timeout occurred",
196 [SPOE_FRM_ERR_TOO_BIG] = "frame is too big",
197 [SPOE_FRM_ERR_INVALID] = "invalid frame received",
198 [SPOE_FRM_ERR_NO_VSN] = "version value not found",
199 [SPOE_FRM_ERR_NO_FRAME_SIZE] = "max-frame-size value not found",
200 [SPOE_FRM_ERR_NO_CAP] = "capabilities value not found",
201 [SPOE_FRM_ERR_BAD_VSN] = "unsupported version",
202 [SPOE_FRM_ERR_BAD_FRAME_SIZE] = "max-frame-size too big or too small",
203 [SPOE_FRM_ERR_FRAG_NOT_SUPPORTED] = "fragmentation not supported",
204 [SPOE_FRM_ERR_INTERLACED_FRAMES] = "invalid interlaced frames",
Christopher Faulet8eda93f2017-02-09 09:44:33 +0100205 [SPOE_FRM_ERR_FRAMEID_NOTFOUND] = "frame-id not found",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100206 [SPOE_FRM_ERR_RES] = "resource allocation error",
207 [SPOE_FRM_ERR_UNKNOWN] = "an unknown error occurred",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200208};
209
210static const char *spoe_event_str[SPOE_EV_EVENTS] = {
211 [SPOE_EV_ON_CLIENT_SESS] = "on-client-session",
212 [SPOE_EV_ON_TCP_REQ_FE] = "on-frontend-tcp-request",
213 [SPOE_EV_ON_TCP_REQ_BE] = "on-backend-tcp-request",
214 [SPOE_EV_ON_HTTP_REQ_FE] = "on-frontend-http-request",
215 [SPOE_EV_ON_HTTP_REQ_BE] = "on-backend-http-request",
216
217 [SPOE_EV_ON_SERVER_SESS] = "on-server-session",
218 [SPOE_EV_ON_TCP_RSP] = "on-tcp-response",
219 [SPOE_EV_ON_HTTP_RSP] = "on-http-response",
220};
221
222
223#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
224
225static const char *spoe_ctx_state_str[SPOE_CTX_ST_ERROR+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100226 [SPOE_CTX_ST_NONE] = "NONE",
227 [SPOE_CTX_ST_READY] = "READY",
228 [SPOE_CTX_ST_ENCODING_MSGS] = "ENCODING_MSGS",
229 [SPOE_CTX_ST_SENDING_MSGS] = "SENDING_MSGS",
230 [SPOE_CTX_ST_WAITING_ACK] = "WAITING_ACK",
231 [SPOE_CTX_ST_DONE] = "DONE",
232 [SPOE_CTX_ST_ERROR] = "ERROR",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200233};
234
235static const char *spoe_appctx_state_str[SPOE_APPCTX_ST_END+1] = {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100236 [SPOE_APPCTX_ST_CONNECT] = "CONNECT",
237 [SPOE_APPCTX_ST_CONNECTING] = "CONNECTING",
238 [SPOE_APPCTX_ST_IDLE] = "IDLE",
239 [SPOE_APPCTX_ST_PROCESSING] = "PROCESSING",
240 [SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY] = "SENDING_FRAG_NOTIFY",
241 [SPOE_APPCTX_ST_WAITING_SYNC_ACK] = "WAITING_SYNC_ACK",
242 [SPOE_APPCTX_ST_DISCONNECT] = "DISCONNECT",
243 [SPOE_APPCTX_ST_DISCONNECTING] = "DISCONNECTING",
244 [SPOE_APPCTX_ST_EXIT] = "EXIT",
245 [SPOE_APPCTX_ST_END] = "END",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200246};
247
248#endif
Christopher Fauleta1cda022016-12-21 08:58:06 +0100249
Christopher Faulet8ef75252017-02-20 22:56:03 +0100250/* Used to generates a unique id for an engine. On success, it returns a
251 * allocated string. So it is the caller's reponsibility to release it. If the
252 * allocation failed, it returns NULL. */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100253static char *
254generate_pseudo_uuid()
255{
256 static int init = 0;
257
258 const char uuid_fmt[] = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx";
259 const char uuid_chr[] = "0123456789ABCDEF-";
260 char *uuid;
261 int i;
262
263 if ((uuid = calloc(1, sizeof(uuid_fmt))) == NULL)
264 return NULL;
265
266 if (!init) {
267 srand(now_ms);
268 init = 1;
269 }
270
271 for (i = 0; i < sizeof(uuid_fmt)-1; i++) {
272 int r = rand () % 16;
273
274 switch (uuid_fmt[i]) {
275 case 'x' : uuid[i] = uuid_chr[r]; break;
276 case 'y' : uuid[i] = uuid_chr[(r & 0x03) | 0x08]; break;
277 default : uuid[i] = uuid_fmt[i]; break;
278 }
279 }
280 return uuid;
281}
282
Christopher Fauletb2dd1e02018-03-22 09:07:41 +0100283
284static inline void
285spoe_update_stat_time(struct timeval *tv, long *t)
286{
287 if (*t == -1)
288 *t = tv_ms_elapsed(tv, &now);
289 else
290 *t += tv_ms_elapsed(tv, &now);
291 tv_zero(tv);
292}
293
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200294/********************************************************************
295 * Functions that encode/decode SPOE frames
296 ********************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200297/* Helper to get static string length, excluding the terminating null byte */
298#define SLEN(str) (sizeof(str)-1)
299
300/* Predefined key used in HELLO/DISCONNECT frames */
301#define SUPPORTED_VERSIONS_KEY "supported-versions"
302#define VERSION_KEY "version"
303#define MAX_FRAME_SIZE_KEY "max-frame-size"
304#define CAPABILITIES_KEY "capabilities"
Christopher Fauleta1cda022016-12-21 08:58:06 +0100305#define ENGINE_ID_KEY "engine-id"
Christopher Fauletba7bc162016-11-07 21:07:38 +0100306#define HEALTHCHECK_KEY "healthcheck"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200307#define STATUS_CODE_KEY "status-code"
308#define MSG_KEY "message"
309
310struct spoe_version {
311 char *str;
312 int min;
313 int max;
314};
315
316/* All supported versions */
317static struct spoe_version supported_versions[] = {
Christopher Faulet63816502018-05-31 14:56:42 +0200318 /* 1.0 is now unsupported because of a bug about frame's flags*/
319 {"2.0", 2000, 2000},
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200320 {NULL, 0, 0}
321};
322
323/* Comma-separated list of supported versions */
Christopher Faulet63816502018-05-31 14:56:42 +0200324#define SUPPORTED_VERSIONS_VAL "2.0"
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200325
Christopher Faulet8ef75252017-02-20 22:56:03 +0100326/* Convert a string to a SPOE version value. The string must follow the format
327 * "MAJOR.MINOR". It will be concerted into the integer (1000 * MAJOR + MINOR).
328 * If an error occurred, -1 is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200329static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100330spoe_str_to_vsn(const char *str, size_t len)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200331{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100332 const char *p, *end;
333 int maj, min, vsn;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200334
Christopher Faulet8ef75252017-02-20 22:56:03 +0100335 p = str;
336 end = str+len;
337 maj = min = 0;
338 vsn = -1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200339
Christopher Faulet8ef75252017-02-20 22:56:03 +0100340 /* skip leading spaces */
341 while (p < end && isspace(*p))
342 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200343
Christopher Faulet8ef75252017-02-20 22:56:03 +0100344 /* parse Major number, until the '.' */
345 while (*p != '.') {
346 if (p >= end || *p < '0' || *p > '9')
347 goto out;
348 maj *= 10;
349 maj += (*p - '0');
350 p++;
351 }
352
353 /* check Major version */
354 if (!maj)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200355 goto out;
356
Christopher Faulet8ef75252017-02-20 22:56:03 +0100357 p++; /* skip the '.' */
358 if (p >= end || *p < '0' || *p > '9') /* Minor number is missing */
359 goto out;
360
361 /* Parse Minor number */
362 while (p < end) {
363 if (*p < '0' || *p > '9')
364 break;
365 min *= 10;
366 min += (*p - '0');
367 p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200368 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100369
370 /* check Minor number */
371 if (min > 999)
372 goto out;
373
374 /* skip trailing spaces */
375 while (p < end && isspace(*p))
376 p++;
377 if (p != end)
378 goto out;
379
380 vsn = maj * 1000 + min;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200381 out:
382 return vsn;
383}
384
Christopher Faulet8ef75252017-02-20 22:56:03 +0100385/* Encode the HELLO frame sent by HAProxy to an agent. It returns the number of
386 * encoded bytes in the frame on success, 0 if an encoding error occured and -1
387 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200388static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100389spoe_prepare_hahello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200390{
Willy Tarreau83061a82018-07-13 11:56:34 +0200391 struct buffer *chk;
Christopher Faulet42bfa462017-01-04 14:14:19 +0100392 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100393 char *p, *end;
394 unsigned int flags = SPOE_FRM_FL_FIN;
395 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200396
Christopher Faulet8ef75252017-02-20 22:56:03 +0100397 p = frame;
398 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200399
Christopher Faulet8ef75252017-02-20 22:56:03 +0100400 /* Set Frame type */
401 *p++ = SPOE_FRM_T_HAPROXY_HELLO;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200402
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100403 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200404 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100405 memcpy(p, (char *)&flags, 4);
406 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200407
408 /* No stream-id and frame-id for HELLO frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100409 *p++ = 0; *p++ = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200410
411 /* There are 3 mandatory items: "supported-versions", "max-frame-size"
412 * and "capabilities" */
413
414 /* "supported-versions" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100415 sz = SLEN(SUPPORTED_VERSIONS_KEY);
416 if (spoe_encode_buffer(SUPPORTED_VERSIONS_KEY, sz, &p, end) == -1)
417 goto too_big;
418
419 *p++ = SPOE_DATA_T_STR;
420 sz = SLEN(SUPPORTED_VERSIONS_VAL);
421 if (spoe_encode_buffer(SUPPORTED_VERSIONS_VAL, sz, &p, end) == -1)
422 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200423
424 /* "max-fram-size" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100425 sz = SLEN(MAX_FRAME_SIZE_KEY);
426 if (spoe_encode_buffer(MAX_FRAME_SIZE_KEY, sz, &p, end) == -1)
427 goto too_big;
428
429 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200430 if (encode_varint(SPOE_APPCTX(appctx)->max_frame_size, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100431 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200432
433 /* "capabilities" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100434 sz = SLEN(CAPABILITIES_KEY);
435 if (spoe_encode_buffer(CAPABILITIES_KEY, sz, &p, end) == -1)
436 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200437
Christopher Faulet8ef75252017-02-20 22:56:03 +0100438 *p++ = SPOE_DATA_T_STR;
Christopher Faulet305c6072017-02-23 16:17:53 +0100439 chk = get_trash_chunk();
440 if (agent != NULL && (agent->flags & SPOE_FL_PIPELINING)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200441 memcpy(chk->area, "pipelining", 10);
442 chk->data += 10;
Christopher Faulet305c6072017-02-23 16:17:53 +0100443 }
444 if (agent != NULL && (agent->flags & SPOE_FL_ASYNC)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200445 if (chk->data) chk->area[chk->data++] = ',';
446 memcpy(chk->area+chk->data, "async", 5);
447 chk->data += 5;
Christopher Faulet305c6072017-02-23 16:17:53 +0100448 }
Christopher Fauletcecd8522017-02-24 22:11:21 +0100449 if (agent != NULL && (agent->flags & SPOE_FL_RCV_FRAGMENTATION)) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200450 if (chk->data) chk->area[chk->data++] = ',';
451 memcpy(chk->area+chk->data, "fragmentation", 13);
452 chk->data += 5;
Christopher Fauletcecd8522017-02-24 22:11:21 +0100453 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200454 if (spoe_encode_buffer(chk->area, chk->data, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100455 goto too_big;
456
457 /* (optionnal) "engine-id" K/V item, if present */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100458 if (agent != NULL && agent->engine_id != NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100459 sz = SLEN(ENGINE_ID_KEY);
460 if (spoe_encode_buffer(ENGINE_ID_KEY, sz, &p, end) == -1)
461 goto too_big;
462
463 *p++ = SPOE_DATA_T_STR;
464 sz = strlen(agent->engine_id);
465 if (spoe_encode_buffer(agent->engine_id, sz, &p, end) == -1)
466 goto too_big;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100467 }
468
Christopher Faulet8ef75252017-02-20 22:56:03 +0100469 return (p - frame);
470
471 too_big:
472 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
473 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200474}
475
Christopher Faulet8ef75252017-02-20 22:56:03 +0100476/* Encode DISCONNECT frame sent by HAProxy to an agent. It returns the number of
477 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
478 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200479static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100480spoe_prepare_hadiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200481{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100482 const char *reason;
483 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100484 unsigned int flags = SPOE_FRM_FL_FIN;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100485 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200486
Christopher Faulet8ef75252017-02-20 22:56:03 +0100487 p = frame;
488 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200489
Christopher Faulet8ef75252017-02-20 22:56:03 +0100490 /* Set Frame type */
491 *p++ = SPOE_FRM_T_HAPROXY_DISCON;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200492
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100493 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200494 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100495 memcpy(p, (char *)&flags, 4);
496 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200497
498 /* No stream-id and frame-id for DISCONNECT frames */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100499 *p++ = 0; *p++ = 0;
500
501 if (SPOE_APPCTX(appctx)->status_code >= SPOE_FRM_ERRS)
502 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200503
504 /* There are 2 mandatory items: "status-code" and "message" */
505
506 /* "status-code" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100507 sz = SLEN(STATUS_CODE_KEY);
508 if (spoe_encode_buffer(STATUS_CODE_KEY, sz, &p, end) == -1)
509 goto too_big;
510
511 *p++ = SPOE_DATA_T_UINT32;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200512 if (encode_varint(SPOE_APPCTX(appctx)->status_code, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100513 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200514
515 /* "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100516 sz = SLEN(MSG_KEY);
517 if (spoe_encode_buffer(MSG_KEY, sz, &p, end) == -1)
518 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200519
Christopher Faulet8ef75252017-02-20 22:56:03 +0100520 /*Get the message corresponding to the status code */
521 reason = spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code];
522
523 *p++ = SPOE_DATA_T_STR;
524 sz = strlen(reason);
525 if (spoe_encode_buffer(reason, sz, &p, end) == -1)
526 goto too_big;
527
528 return (p - frame);
529
530 too_big:
531 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
532 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200533}
534
Christopher Faulet8ef75252017-02-20 22:56:03 +0100535/* Encode the NOTIFY frame sent by HAProxy to an agent. It returns the number of
536 * encoded bytes in the frame on success, 0 if an encoding error occurred and -1
537 * if a fatal error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200538static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100539spoe_prepare_hanotify_frame(struct appctx *appctx, struct spoe_context *ctx,
Christopher Fauleta1cda022016-12-21 08:58:06 +0100540 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200541{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100542 char *p, *end;
543 unsigned int stream_id, frame_id;
544 unsigned int flags = SPOE_FRM_FL_FIN;
545 size_t sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200546
Christopher Faulet8ef75252017-02-20 22:56:03 +0100547 p = frame;
548 end = frame+size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200549
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100550 stream_id = ctx->stream_id;
551 frame_id = ctx->frame_id;
552
553 if (ctx->flags & SPOE_CTX_FL_FRAGMENTED) {
554 /* The fragmentation is not supported by the applet */
555 if (!(SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_FRAGMENTATION)) {
556 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
557 return -1;
558 }
559 flags = ctx->frag_ctx.flags;
560 }
561
562 /* Set Frame type */
563 *p++ = SPOE_FRM_T_HAPROXY_NOTIFY;
564
565 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200566 flags = htonl(flags);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100567 memcpy(p, (char *)&flags, 4);
568 p += 4;
569
570 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200571 if (encode_varint(stream_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100572 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200573 if (encode_varint(frame_id, &p, end) == -1)
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100574 goto too_big;
575
576 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200577 sz = b_data(&ctx->buffer);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100578 if (p + sz >= end)
579 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200580 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100581 p += sz;
582
583 return (p - frame);
584
585 too_big:
586 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
587 return 0;
588}
589
590/* Encode next part of a fragmented frame sent by HAProxy to an agent. It
591 * returns the number of encoded bytes in the frame on success, 0 if an encoding
592 * error occurred and -1 if a fatal error occurred. */
593static int
594spoe_prepare_hafrag_frame(struct appctx *appctx, struct spoe_context *ctx,
595 char *frame, size_t size)
596{
597 char *p, *end;
598 unsigned int stream_id, frame_id;
599 unsigned int flags;
600 size_t sz;
601
602 p = frame;
603 end = frame+size;
604
Christopher Faulet8ef75252017-02-20 22:56:03 +0100605 /* <ctx> is null when the stream has aborted the processing of a
606 * fragmented frame. In this case, we must notify the corresponding
607 * agent using ids stored in <frag_ctx>. */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100608 if (ctx == NULL) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100609 flags = (SPOE_FRM_FL_FIN|SPOE_FRM_FL_ABRT);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100610 stream_id = SPOE_APPCTX(appctx)->frag_ctx.cursid;
611 frame_id = SPOE_APPCTX(appctx)->frag_ctx.curfid;
612 }
613 else {
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100614 flags = ctx->frag_ctx.flags;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100615 stream_id = ctx->stream_id;
616 frame_id = ctx->frame_id;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100617 }
618
Christopher Faulet8ef75252017-02-20 22:56:03 +0100619 /* Set Frame type */
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100620 *p++ = SPOE_FRM_T_UNSET;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100621
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100622 /* Set flags */
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200623 flags = htonl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100624 memcpy(p, (char *)&flags, 4);
625 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200626
627 /* Set stream-id and frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200628 if (encode_varint(stream_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100629 goto too_big;
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200630 if (encode_varint(frame_id, &p, end) == -1)
Christopher Faulet8ef75252017-02-20 22:56:03 +0100631 goto too_big;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200632
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100633 if (ctx == NULL)
634 goto end;
635
Christopher Faulet8ef75252017-02-20 22:56:03 +0100636 /* Copy encoded messages, if possible */
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200637 sz = b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100638 if (p + sz >= end)
639 goto too_big;
Willy Tarreauc9fa0482018-07-10 17:43:27 +0200640 memcpy(p, b_head(&ctx->buffer), sz);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100641 p += sz;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100642
Christopher Fauletf032c3e2017-02-17 15:18:35 +0100643 end:
Christopher Faulet8ef75252017-02-20 22:56:03 +0100644 return (p - frame);
645
646 too_big:
647 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
648 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200649}
650
Christopher Faulet8ef75252017-02-20 22:56:03 +0100651/* Decode and process the HELLO frame sent by an agent. It returns the number of
652 * read bytes on success, 0 if a decoding error occurred, and -1 if a fatal
653 * error occurred. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200654static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100655spoe_handle_agenthello_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200656{
Christopher Faulet305c6072017-02-23 16:17:53 +0100657 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
658 char *p, *end;
659 int vsn, max_frame_size;
660 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100661
662 p = frame;
663 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200664
665 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100666 if (*p++ != SPOE_FRM_T_AGENT_HELLO) {
667 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200668 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100669 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200670
Christopher Faulet8ef75252017-02-20 22:56:03 +0100671 if (size < 7 /* TYPE + METADATA */) {
672 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
673 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200674 }
675
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100676 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100677 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200678 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100679 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200680
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100681 /* Fragmentation is not supported for HELLO frame */
682 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100683 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100684 return -1;
685 }
686
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200687 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100688 if (*p != 0 || *(p+1) != 0) {
689 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
690 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200691 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100692 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200693
694 /* There are 3 mandatory items: "version", "max-frame-size" and
695 * "capabilities" */
696
697 /* Loop on K/V items */
Christopher Fauleta1cda022016-12-21 08:58:06 +0100698 vsn = max_frame_size = flags = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100699 while (p < end) {
700 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200701 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100702 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200703
704 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100705 ret = spoe_decode_buffer(&p, end, &str, &sz);
706 if (ret == -1 || !sz) {
707 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
708 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200709 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100710
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200711 /* Check "version" K/V item */
712 if (!memcmp(str, VERSION_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100713 int i, type = *p++;
714
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200715 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100716 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
717 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
718 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200719 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100720 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
721 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
722 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200723 }
724
Christopher Faulet8ef75252017-02-20 22:56:03 +0100725 vsn = spoe_str_to_vsn(str, sz);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200726 if (vsn == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100727 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200728 return -1;
729 }
730 for (i = 0; supported_versions[i].str != NULL; ++i) {
731 if (vsn >= supported_versions[i].min &&
732 vsn <= supported_versions[i].max)
733 break;
734 }
735 if (supported_versions[i].str == NULL) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100736 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200737 return -1;
738 }
739 }
740 /* Check "max-frame-size" K/V item */
741 else if (!memcmp(str, MAX_FRAME_SIZE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100742 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200743
744 /* The value must be integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200745 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
746 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
747 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
748 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100749 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
750 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200751 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200752 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100753 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
754 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200755 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100756 if (sz < MIN_FRAME_SIZE ||
757 sz > SPOE_APPCTX(appctx)->max_frame_size) {
758 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_BAD_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200759 return -1;
760 }
761 max_frame_size = sz;
762 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100763 /* Check "capabilities" K/V item */
764 else if (!memcmp(str, CAPABILITIES_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100765 int type = *p++;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100766
767 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100768 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
769 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
770 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100771 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100772 if (spoe_decode_buffer(&p, end, &str, &sz) == -1) {
773 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
774 return 0;
775 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100776
Christopher Faulet8ef75252017-02-20 22:56:03 +0100777 while (sz) {
Christopher Fauleta1cda022016-12-21 08:58:06 +0100778 char *delim;
779
780 /* Skip leading spaces */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100781 for (; isspace(*str) && sz; str++, sz--);
Christopher Fauleta1cda022016-12-21 08:58:06 +0100782
Christopher Faulet8ef75252017-02-20 22:56:03 +0100783 if (sz >= 10 && !strncmp(str, "pipelining", 10)) {
784 str += 10; sz -= 10;
785 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100786 flags |= SPOE_APPCTX_FL_PIPELINING;
787 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100788 else if (sz >= 5 && !strncmp(str, "async", 5)) {
789 str += 5; sz -= 5;
790 if (!sz || isspace(*str) || *str == ',')
Christopher Fauleta1cda022016-12-21 08:58:06 +0100791 flags |= SPOE_APPCTX_FL_ASYNC;
792 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100793 else if (sz >= 13 && !strncmp(str, "fragmentation", 13)) {
794 str += 13; sz -= 13;
795 if (!sz || isspace(*str) || *str == ',')
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100796 flags |= SPOE_APPCTX_FL_FRAGMENTATION;
797 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100798
Christopher Faulet8ef75252017-02-20 22:56:03 +0100799 /* Get the next comma or break */
800 if (!sz || (delim = memchr(str, ',', sz)) == NULL)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100801 break;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100802 delim++;
803 sz -= (delim - str);
804 str = delim;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100805 }
806 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200807 else {
808 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100809 if (spoe_skip_data(&p, end) == -1) {
810 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
811 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200812 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200813 }
814 }
815
816 /* Final checks */
817 if (!vsn) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100818 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_VSN;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200819 return -1;
820 }
821 if (!max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100822 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NO_FRAME_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200823 return -1;
824 }
Christopher Faulet305c6072017-02-23 16:17:53 +0100825 if ((flags & SPOE_APPCTX_FL_PIPELINING) && !(agent->flags & SPOE_FL_PIPELINING))
826 flags &= ~SPOE_APPCTX_FL_PIPELINING;
827 if ((flags & SPOE_APPCTX_FL_ASYNC) && !(agent->flags & SPOE_FL_ASYNC))
828 flags &= ~SPOE_APPCTX_FL_ASYNC;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200829
Christopher Faulet42bfa462017-01-04 14:14:19 +0100830 SPOE_APPCTX(appctx)->version = (unsigned int)vsn;
831 SPOE_APPCTX(appctx)->max_frame_size = (unsigned int)max_frame_size;
832 SPOE_APPCTX(appctx)->flags |= flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100833
834 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200835}
836
837/* Decode DISCONNECT frame sent by an agent. It returns the number of by read
838 * bytes on success, 0 if the frame can be ignored and -1 if an error
839 * occurred. */
840static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100841spoe_handle_agentdiscon_frame(struct appctx *appctx, char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200842{
Christopher Faulet8ef75252017-02-20 22:56:03 +0100843 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100844 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100845
846 p = frame;
847 end = frame + size;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200848
849 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100850 if (*p++ != SPOE_FRM_T_AGENT_DISCON) {
851 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200852 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100853 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200854
Christopher Faulet8ef75252017-02-20 22:56:03 +0100855 if (size < 7 /* TYPE + METADATA */) {
856 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
857 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200858 }
859
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100860 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100861 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200862 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100863 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200864
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100865 /* Fragmentation is not supported for DISCONNECT frame */
866 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100867 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100868 return -1;
869 }
870
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200871 /* stream-id and frame-id must be cleared */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100872 if (*p != 0 || *(p+1) != 0) {
873 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
874 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200875 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100876 p += 2;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200877
878 /* There are 2 mandatory items: "status-code" and "message" */
879
880 /* Loop on K/V items */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100881 while (p < end) {
882 char *str;
Frédéric Lécaille6ca71a92017-08-22 10:33:14 +0200883 uint64_t sz;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100884 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200885
886 /* Decode the item key */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100887 ret = spoe_decode_buffer(&p, end, &str, &sz);
888 if (ret == -1 || !sz) {
889 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
890 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200891 }
892
893 /* Check "status-code" K/V item */
894 if (!memcmp(str, STATUS_CODE_KEY, sz)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100895 int type = *p++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200896
897 /* The value must be an integer */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200898 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT32 &&
899 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_INT64 &&
900 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT32 &&
901 (type & SPOE_DATA_T_MASK) != SPOE_DATA_T_UINT64) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100902 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
903 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200904 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200905 if (decode_varint(&p, end, &sz) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100906 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
907 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200908 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100909 SPOE_APPCTX(appctx)->status_code = sz;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200910 }
911
912 /* Check "message" K/V item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100913 else if (!memcmp(str, MSG_KEY, sz)) {
914 int type = *p++;
915
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200916 /* The value must be a string */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100917 if ((type & SPOE_DATA_T_MASK) != SPOE_DATA_T_STR) {
918 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
919 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200920 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100921 ret = spoe_decode_buffer(&p, end, &str, &sz);
922 if (ret == -1 || sz > 255) {
923 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
924 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200925 }
Christopher Faulet8ef75252017-02-20 22:56:03 +0100926#if defined(DEBUG_SPOE) || defined(DEBUG_FULL)
927 SPOE_APPCTX(appctx)->reason = str;
928 SPOE_APPCTX(appctx)->rlen = sz;
929#endif
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200930 }
931 else {
932 /* Silently ignore unknown item */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100933 if (spoe_skip_data(&p, end) == -1) {
934 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
935 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200936 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200937 }
938 }
939
Christopher Faulet8ef75252017-02-20 22:56:03 +0100940 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200941}
942
943
Christopher Fauleta1cda022016-12-21 08:58:06 +0100944/* Decode ACK frame sent by an agent. It returns the number of read bytes on
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200945 * success, 0 if the frame can be ignored and -1 if an error occurred. */
946static int
Christopher Faulet8ef75252017-02-20 22:56:03 +0100947spoe_handle_agentack_frame(struct appctx *appctx, struct spoe_context **ctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100948 char *frame, size_t size)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200949{
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100950 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100951 char *p, *end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100952 uint64_t stream_id, frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100953 int len;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100954 unsigned int flags;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100955
956 p = frame;
957 end = frame + size;
958 *ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200959
960 /* Check frame type */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100961 if (*p++ != SPOE_FRM_T_AGENT_ACK) {
962 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200963 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100964 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200965
Christopher Faulet8ef75252017-02-20 22:56:03 +0100966 if (size < 7 /* TYPE + METADATA */) {
967 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
968 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200969 }
970
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100971 /* Retrieve flags */
Christopher Faulet8ef75252017-02-20 22:56:03 +0100972 memcpy((char *)&flags, p, 4);
Thierry FOURNIERc4dcaff2018-05-18 12:25:39 +0200973 flags = ntohl(flags);
Christopher Faulet8ef75252017-02-20 22:56:03 +0100974 p += 4;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200975
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100976 /* Fragmentation is not supported for now */
977 if (!(flags & SPOE_FRM_FL_FIN)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100978 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAG_NOT_SUPPORTED;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100979 return -1;
980 }
981
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200982 /* Get the stream-id and the frame-id */
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200983 if (decode_varint(&p, end, &stream_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100984 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauleta1cda022016-12-21 08:58:06 +0100985 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100986 }
Thierry FOURNIER6ab2bae2017-04-19 11:49:44 +0200987 if (decode_varint(&p, end, &frame_id) == -1) {
Christopher Faulet8ef75252017-02-20 22:56:03 +0100988 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +0200989 return 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +0100990 }
Christopher Fauleta1cda022016-12-21 08:58:06 +0100991
Christopher Faulet8ef75252017-02-20 22:56:03 +0100992 /* Try to find the corresponding SPOE context */
Christopher Faulet42bfa462017-01-04 14:14:19 +0100993 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
Christopher Faulet24289f22017-09-25 14:48:02 +0200994 list_for_each_entry((*ctx), &agent->rt[tid].waiting_queue, list) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +0100995 if ((*ctx)->stream_id == (unsigned int)stream_id &&
996 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +0100997 goto found;
998 }
999 }
1000 else {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001001 list_for_each_entry((*ctx), &SPOE_APPCTX(appctx)->waiting_queue, list) {
1002 if ((*ctx)->stream_id == (unsigned int)stream_id &&
Christopher Faulet8ef75252017-02-20 22:56:03 +01001003 (*ctx)->frame_id == (unsigned int)frame_id)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001004 goto found;
1005 }
1006 }
1007
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001008 if (SPOE_APPCTX(appctx)->frag_ctx.ctx &&
1009 SPOE_APPCTX(appctx)->frag_ctx.cursid == (unsigned int)stream_id &&
1010 SPOE_APPCTX(appctx)->frag_ctx.curfid == (unsigned int)frame_id) {
1011
1012 /* ABRT bit is set for an unfinished fragmented frame */
1013 if (flags & SPOE_FRM_FL_ABRT) {
1014 *ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001015 (*ctx)->state = SPOE_CTX_ST_ERROR;
1016 (*ctx)->status_code = SPOE_CTX_ERR_FRAG_FRAME_ABRT;
1017 /* Ignore the payload */
1018 goto end;
1019 }
1020 /* TODO: Handle more flags for fragmented frames: RESUME, FINISH... */
1021 /* For now, we ignore the ack */
1022 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_INVALID;
1023 return 0;
1024 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001025
Christopher Fauleta1cda022016-12-21 08:58:06 +01001026 /* No Stream found, ignore the frame */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001027 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1028 " - Ignore ACK frame"
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001029 " - stream-id=%u - frame-id=%u\n",
1030 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1031 __FUNCTION__, appctx,
1032 (unsigned int)stream_id, (unsigned int)frame_id);
1033
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001034 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_FRAMEID_NOTFOUND;
Christopher Faulet3a47e5e2018-05-25 10:42:37 +02001035 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1036 return -1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001037 return 0;
1038
1039 found:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001040 if (!spoe_acquire_buffer(&SPOE_APPCTX(appctx)->buffer,
1041 &SPOE_APPCTX(appctx)->buffer_wait)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001042 *ctx = NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001043 return 1; /* Retry later */
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001044 }
Christopher Faulet4596fb72017-01-11 14:05:19 +01001045
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001046 /* Copy encoded actions */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001047 len = (end - p);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001048 memcpy(b_head(&SPOE_APPCTX(appctx)->buffer), p, len);
1049 b_set_data(&SPOE_APPCTX(appctx)->buffer, len);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001050 p += len;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001051
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001052 /* Transfer the buffer ownership to the SPOE context */
1053 (*ctx)->buffer = SPOE_APPCTX(appctx)->buffer;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001054 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001055
Christopher Faulet8ef75252017-02-20 22:56:03 +01001056 (*ctx)->state = SPOE_CTX_ST_DONE;
1057
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001058 end:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001059 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001060 " - ACK frame received"
1061 " - ctx=%p - stream-id=%u - frame-id=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001062 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001063 __FUNCTION__, appctx, *ctx, (*ctx)->stream_id,
1064 (*ctx)->frame_id, flags);
1065 return (p - frame);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001066}
1067
Christopher Fauletba7bc162016-11-07 21:07:38 +01001068/* This function is used in cfgparse.c and declared in proto/checks.h. It
1069 * prepare the request to send to agents during a healthcheck. It returns 0 on
1070 * success and -1 if an error occurred. */
1071int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001072spoe_prepare_healthcheck_request(char **req, int *len)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001073{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001074 struct appctx appctx;
1075 struct spoe_appctx spoe_appctx;
1076 char *frame, *end, buf[MAX_FRAME_SIZE+4];
1077 size_t sz;
1078 int ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001079
Christopher Faulet42bfa462017-01-04 14:14:19 +01001080 memset(&appctx, 0, sizeof(appctx));
1081 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001082 memset(buf, 0, sizeof(buf));
Christopher Faulet42bfa462017-01-04 14:14:19 +01001083
1084 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001085 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001086
Christopher Faulet8ef75252017-02-20 22:56:03 +01001087 frame = buf+4; /* Reserved the 4 first bytes for the frame size */
1088 end = frame + MAX_FRAME_SIZE;
1089
1090 ret = spoe_prepare_hahello_frame(&appctx, frame, MAX_FRAME_SIZE);
1091 if (ret <= 0)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001092 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001093 frame += ret;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001094
Christopher Faulet8ef75252017-02-20 22:56:03 +01001095 /* Add "healthcheck" K/V item */
1096 sz = SLEN(HEALTHCHECK_KEY);
1097 if (spoe_encode_buffer(HEALTHCHECK_KEY, sz, &frame, end) == -1)
1098 return -1;
1099 *frame++ = (SPOE_DATA_T_BOOL | SPOE_DATA_FL_TRUE);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001100
Christopher Faulet8ef75252017-02-20 22:56:03 +01001101 *len = frame - buf;
1102 sz = htonl(*len - 4);
1103 memcpy(buf, (char *)&sz, 4);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001104
Christopher Faulet8ef75252017-02-20 22:56:03 +01001105 if ((*req = malloc(*len)) == NULL)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001106 return -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001107 memcpy(*req, buf, *len);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001108 return 0;
1109}
1110
1111/* This function is used in checks.c and declared in proto/checks.h. It decode
1112 * the response received from an agent during a healthcheck. It returns 0 on
1113 * success and -1 if an error occurred. */
1114int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001115spoe_handle_healthcheck_response(char *frame, size_t size, char *err, int errlen)
Christopher Fauletba7bc162016-11-07 21:07:38 +01001116{
Christopher Faulet42bfa462017-01-04 14:14:19 +01001117 struct appctx appctx;
1118 struct spoe_appctx spoe_appctx;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001119
Christopher Faulet42bfa462017-01-04 14:14:19 +01001120 memset(&appctx, 0, sizeof(appctx));
1121 memset(&spoe_appctx, 0, sizeof(spoe_appctx));
Christopher Fauletba7bc162016-11-07 21:07:38 +01001122
Christopher Faulet42bfa462017-01-04 14:14:19 +01001123 appctx.ctx.spoe.ptr = &spoe_appctx;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001124 SPOE_APPCTX(&appctx)->max_frame_size = MAX_FRAME_SIZE;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001125
Christopher Faulet8ef75252017-02-20 22:56:03 +01001126 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1127 spoe_handle_agentdiscon_frame(&appctx, frame, size);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001128 goto error;
1129 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001130 if (spoe_handle_agenthello_frame(&appctx, frame, size) <= 0)
1131 goto error;
Christopher Fauletba7bc162016-11-07 21:07:38 +01001132
1133 return 0;
1134
1135 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001136 if (SPOE_APPCTX(&appctx)->status_code >= SPOE_FRM_ERRS)
1137 SPOE_APPCTX(&appctx)->status_code = SPOE_FRM_ERR_UNKNOWN;
1138 strncpy(err, spoe_frm_err_reasons[SPOE_APPCTX(&appctx)->status_code], errlen);
Christopher Fauletba7bc162016-11-07 21:07:38 +01001139 return -1;
1140}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001141
Christopher Fauleta1cda022016-12-21 08:58:06 +01001142/* Send a SPOE frame to an agent. It returns -1 when an error occurred, 0 when
1143 * the frame can be ignored, 1 to retry later, and the frame legnth on
1144 * success. */
1145static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001146spoe_send_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001147{
1148 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001149 int ret;
1150 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001151
Christopher Faulet8ef75252017-02-20 22:56:03 +01001152 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1153 * length. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001154 netint = htonl(framesz);
1155 memcpy(buf, (char *)&netint, 4);
Willy Tarreau06d80a92017-10-19 14:32:15 +02001156 ret = ci_putblk(si_ic(si), buf, framesz+4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001157 if (ret <= 0) {
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001158 if ((ret == -3 && b_is_null(&si_ic(si)->buf)) || ret == -1) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001159 si_cant_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001160 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001161 }
1162 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001163 return -1; /* error */
1164 }
1165 return framesz;
1166}
1167
1168/* Receive a SPOE frame from an agent. It return -1 when an error occurred, 0
1169 * when the frame can be ignored, 1 to retry later and the frame length on
1170 * success. */
1171static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001172spoe_recv_frame(struct appctx *appctx, char *buf, size_t framesz)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001173{
1174 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001175 int ret;
1176 uint32_t netint;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001177
Willy Tarreau06d80a92017-10-19 14:32:15 +02001178 ret = co_getblk(si_oc(si), (char *)&netint, 4, 0);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001179 if (ret > 0) {
1180 framesz = ntohl(netint);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001181 if (framesz > SPOE_APPCTX(appctx)->max_frame_size) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001182 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOO_BIG;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001183 return -1;
1184 }
Willy Tarreau06d80a92017-10-19 14:32:15 +02001185 ret = co_getblk(si_oc(si), buf, framesz, 4);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001186 }
1187 if (ret <= 0) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001188 if (ret == 0) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001189 return 1; /* retry */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001190 }
1191 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001192 return -1; /* error */
1193 }
1194 return framesz;
1195}
1196
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001197/********************************************************************
1198 * Functions that manage the SPOE applet
1199 ********************************************************************/
Christopher Faulet4596fb72017-01-11 14:05:19 +01001200static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001201spoe_wakeup_appctx(struct appctx *appctx)
Christopher Faulet4596fb72017-01-11 14:05:19 +01001202{
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001203 si_want_get(appctx->owner);
1204 si_want_put(appctx->owner);
Christopher Faulet4596fb72017-01-11 14:05:19 +01001205 appctx_wakeup(appctx);
1206 return 1;
1207}
1208
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001209/* Callback function that catches applet timeouts. If a timeout occurred, we set
1210 * <appctx->st1> flag and the SPOE applet is woken up. */
1211static struct task *
Olivier Houchard9f6af332018-05-25 14:04:04 +02001212spoe_process_appctx(struct task * task, void *context, unsigned short state)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001213{
Olivier Houchard9f6af332018-05-25 14:04:04 +02001214 struct appctx *appctx = context;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001215
1216 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1217 if (tick_is_expired(task->expire, now_ms)) {
1218 task->expire = TICK_ETERNITY;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001219 appctx->st1 = SPOE_APPCTX_ERR_TOUT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001220 }
Christopher Faulet8ef75252017-02-20 22:56:03 +01001221 spoe_wakeup_appctx(appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001222 return task;
1223}
1224
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001225/* Callback function that releases a SPOE applet. This happens when the
1226 * connection with the agent is closed. */
1227static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001228spoe_release_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001229{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001230 struct stream_interface *si = appctx->owner;
1231 struct spoe_appctx *spoe_appctx = SPOE_APPCTX(appctx);
1232 struct spoe_agent *agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001233 struct spoe_context *ctx, *back;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001234
1235 if (spoe_appctx == NULL)
1236 return;
1237
1238 appctx->ctx.spoe.ptr = NULL;
1239 agent = spoe_appctx->agent;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001240
1241 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p\n",
1242 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1243 __FUNCTION__, appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001244
Christopher Faulet8ef75252017-02-20 22:56:03 +01001245 /* Remove applet from the list of running applets */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001246 HA_ATOMIC_SUB(&agent->counters.applets, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001247 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001248 if (!LIST_ISEMPTY(&spoe_appctx->list)) {
1249 LIST_DEL(&spoe_appctx->list);
1250 LIST_INIT(&spoe_appctx->list);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001251 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001252 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[tid].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001253
Christopher Faulet8ef75252017-02-20 22:56:03 +01001254 /* Shutdown the server connection, if needed */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001255 if (appctx->st0 != SPOE_APPCTX_ST_END) {
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001256 if (appctx->st0 == SPOE_APPCTX_ST_IDLE) {
1257 eb32_delete(&spoe_appctx->node);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001258 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001259 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001260
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001261 appctx->st0 = SPOE_APPCTX_ST_END;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001262 if (spoe_appctx->status_code == SPOE_FRM_ERR_NONE)
1263 spoe_appctx->status_code = SPOE_FRM_ERR_IO;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001264
1265 si_shutw(si);
1266 si_shutr(si);
1267 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001268 }
1269
Christopher Faulet8ef75252017-02-20 22:56:03 +01001270 /* Destroy the task attached to this applet */
1271 if (spoe_appctx->task) {
1272 task_delete(spoe_appctx->task);
1273 task_free(spoe_appctx->task);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001274 }
1275
Christopher Faulet8ef75252017-02-20 22:56:03 +01001276 /* Notify all waiting streams */
1277 list_for_each_entry_safe(ctx, back, &spoe_appctx->waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001278 LIST_DEL(&ctx->list);
1279 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001280 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001281 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001282 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001283 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001284 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001285 }
1286
Christopher Faulet8ef75252017-02-20 22:56:03 +01001287 /* If the applet was processing a fragmented frame, notify the
1288 * corresponding stream. */
1289 if (spoe_appctx->frag_ctx.ctx) {
1290 ctx = spoe_appctx->frag_ctx.ctx;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001291 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001292 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001293 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001294 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1295 }
1296
Christopher Faulet8ef75252017-02-20 22:56:03 +01001297 /* Release allocated memory */
1298 spoe_release_buffer(&spoe_appctx->buffer,
1299 &spoe_appctx->buffer_wait);
Willy Tarreaubafbe012017-11-24 17:34:44 +01001300 pool_free(pool_head_spoe_appctx, spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001301
Christopher Faulet24289f22017-09-25 14:48:02 +02001302 if (!LIST_ISEMPTY(&agent->rt[tid].applets))
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001303 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001304
Christopher Faulet8ef75252017-02-20 22:56:03 +01001305 /* If this was the last running applet, notify all waiting streams */
Christopher Faulet24289f22017-09-25 14:48:02 +02001306 list_for_each_entry_safe(ctx, back, &agent->rt[tid].sending_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001307 LIST_DEL(&ctx->list);
1308 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001309 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001310 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001311 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001312 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001313 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001314 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001315 list_for_each_entry_safe(ctx, back, &agent->rt[tid].waiting_queue, list) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01001316 LIST_DEL(&ctx->list);
1317 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001318 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001319 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001320 ctx->state = SPOE_CTX_ST_ERROR;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001321 ctx->status_code = (spoe_appctx->status_code + 0x100);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001322 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1323 }
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001324
1325 end:
1326 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001327 agent->rt[tid].frame_size = agent->max_frame_size;
1328 list_for_each_entry(spoe_appctx, &agent->rt[tid].applets, list)
1329 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, spoe_appctx->max_frame_size);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001330}
1331
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001332static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001333spoe_handle_connect_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001334{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001335 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001336 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001337 char *frame, *buf;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001338 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001339
Christopher Fauleta1cda022016-12-21 08:58:06 +01001340 if (si->state <= SI_ST_CON) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001341 si_want_put(si);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001342 task_wakeup(si_strm(si)->task, TASK_WOKEN_MSG);
1343 goto stop;
1344 }
Christopher Fauletb067b062017-01-04 16:39:11 +01001345 if (si->state != SI_ST_EST) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001346 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001347 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001348 }
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001349
Christopher Fauleta1cda022016-12-21 08:58:06 +01001350 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001351 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1352 " - Connection timed out\n",
1353 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1354 __FUNCTION__, appctx);
1355 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001356 goto exit;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001357 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001358
Christopher Faulet42bfa462017-01-04 14:14:19 +01001359 if (SPOE_APPCTX(appctx)->task->expire == TICK_ETERNITY)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001360 SPOE_APPCTX(appctx)->task->expire =
1361 tick_add_ifset(now_ms, agent->timeout.hello);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001362
Christopher Faulet8ef75252017-02-20 22:56:03 +01001363 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1364 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001365 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001366 ret = spoe_prepare_hahello_frame(appctx, frame,
1367 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001368 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001369 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001370
1371 switch (ret) {
1372 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001373 case 0: /* ignore => an error, cannot be ignored */
1374 goto exit;
1375
1376 case 1: /* retry later */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001377 goto stop;
1378
Christopher Faulet8ef75252017-02-20 22:56:03 +01001379 default:
1380 /* HELLO frame successfully sent, now wait for the
1381 * reply. */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001382 appctx->st0 = SPOE_APPCTX_ST_CONNECTING;
1383 goto next;
1384 }
1385
1386 next:
1387 return 0;
1388 stop:
1389 return 1;
1390 exit:
1391 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1392 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001393}
1394
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001395static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001396spoe_handle_connecting_appctx(struct appctx *appctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001397{
Christopher Fauleta1cda022016-12-21 08:58:06 +01001398 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001399 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001400 char *frame;
1401 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001402
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001403
Christopher Fauletb067b062017-01-04 16:39:11 +01001404 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001405 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001406 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001407 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001408
Christopher Fauleta1cda022016-12-21 08:58:06 +01001409 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001410 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1411 " - Connection timed out\n",
1412 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1413 __FUNCTION__, appctx);
1414 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001415 goto exit;
1416 }
1417
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001418 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001419 ret = spoe_recv_frame(appctx, frame,
1420 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001421 if (ret > 1) {
1422 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1423 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1424 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001425 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001426 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001427 ret = spoe_handle_agenthello_frame(appctx, frame, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001428 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001429
Christopher Fauleta1cda022016-12-21 08:58:06 +01001430 switch (ret) {
1431 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001432 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001433 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1434 goto next;
1435
1436 case 1: /* retry later */
1437 goto stop;
1438
1439 default:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001440 /* HELLO handshake is finished, set the idle timeout and
1441 * add the applet in the list of running applets. */
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001442 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001443 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001444 SPOE_APPCTX(appctx)->node.key = 0;
1445 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01001446
1447 /* Update runtinme agent info */
Christopher Faulet24289f22017-09-25 14:48:02 +02001448 HA_ATOMIC_UPDATE_MIN(&agent->rt[tid].frame_size, SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001449 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001450 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001451
Christopher Fauleta1cda022016-12-21 08:58:06 +01001452 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001453 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001454 if (trash.data)
1455 co_skip(si_oc(si), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001456
1457 SPOE_APPCTX(appctx)->task->expire =
1458 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001459 return 0;
1460 stop:
1461 return 1;
1462 exit:
1463 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1464 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001465}
1466
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001467
Christopher Fauleta1cda022016-12-21 08:58:06 +01001468static int
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001469spoe_handle_sending_frame_appctx(struct appctx *appctx, int *skip)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001470{
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001471 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
1472 struct spoe_context *ctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001473 char *frame, *buf;
1474 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001475
Christopher Faulet8ef75252017-02-20 22:56:03 +01001476 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1477 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001478 buf = trash.area; frame = buf+4;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001479
1480 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY) {
1481 ctx = SPOE_APPCTX(appctx)->frag_ctx.ctx;
1482 ret = spoe_prepare_hafrag_frame(appctx, ctx, frame,
1483 SPOE_APPCTX(appctx)->max_frame_size);
1484 }
Christopher Faulet24289f22017-09-25 14:48:02 +02001485 else if (LIST_ISEMPTY(&agent->rt[tid].sending_queue)) {
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001486 *skip = 1;
1487 ret = 1;
1488 goto end;
1489 }
1490 else {
Christopher Faulet24289f22017-09-25 14:48:02 +02001491 ctx = LIST_NEXT(&agent->rt[tid].sending_queue, typeof(ctx), list);
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001492 ret = spoe_prepare_hanotify_frame(appctx, ctx, frame,
1493 SPOE_APPCTX(appctx)->max_frame_size);
1494
1495 }
1496
Christopher Faulet8ef75252017-02-20 22:56:03 +01001497 if (ret > 1)
1498 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001499
Christopher Faulet8ef75252017-02-20 22:56:03 +01001500 switch (ret) {
1501 case -1: /* error */
1502 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1503 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001504
Christopher Faulet8ef75252017-02-20 22:56:03 +01001505 case 0: /* ignore */
1506 if (ctx == NULL)
1507 goto abort_frag_frame;
1508
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001509 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001510 LIST_DEL(&ctx->list);
1511 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001512 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001513 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001514 ctx->spoe_appctx = NULL;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001515 ctx->state = SPOE_CTX_ST_ERROR;
1516 ctx->status_code = (SPOE_APPCTX(appctx)->status_code + 0x100);
1517 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001518 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001519 break;
1520
1521 case 1: /* retry */
1522 *skip = 1;
1523 break;
1524
1525 default:
1526 if (ctx == NULL)
1527 goto abort_frag_frame;
1528
Christopher Fauletf032c3e2017-02-17 15:18:35 +01001529 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001530 LIST_DEL(&ctx->list);
1531 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001532 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001533 spoe_update_stat_time(&ctx->stats.tv_queue, &ctx->stats.t_queue);
Christopher Fauletfce747b2018-01-24 15:59:32 +01001534 ctx->spoe_appctx = SPOE_APPCTX(appctx);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001535 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) ||
1536 (ctx->frag_ctx.flags & SPOE_FRM_FL_FIN))
1537 goto no_frag_frame_sent;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001538 else
Christopher Faulet8ef75252017-02-20 22:56:03 +01001539 goto frag_frame_sent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001540 }
1541 goto end;
1542
1543 frag_frame_sent:
1544 appctx->st0 = SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001545 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001546 SPOE_APPCTX(appctx)->frag_ctx.ctx = ctx;
1547 SPOE_APPCTX(appctx)->frag_ctx.cursid = ctx->stream_id;
1548 SPOE_APPCTX(appctx)->frag_ctx.curfid = ctx->frame_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001549 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
1550 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1551 goto end;
1552
1553 no_frag_frame_sent:
1554 if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_ASYNC) {
1555 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet24289f22017-09-25 14:48:02 +02001556 LIST_ADDQ(&agent->rt[tid].waiting_queue, &ctx->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001557 }
1558 else if (SPOE_APPCTX(appctx)->flags & SPOE_APPCTX_FL_PIPELINING) {
1559 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1560 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1561 }
1562 else {
1563 appctx->st0 = SPOE_APPCTX_ST_WAITING_SYNC_ACK;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001564 *skip = 1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001565 LIST_ADDQ(&SPOE_APPCTX(appctx)->waiting_queue, &ctx->list);
1566 }
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001567 HA_ATOMIC_ADD(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001568 ctx->stats.tv_wait = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001569 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1570 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1571 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001572 SPOE_APPCTX(appctx)->cur_fpa++;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001573
Christopher Faulet8ef75252017-02-20 22:56:03 +01001574 ctx->state = SPOE_CTX_ST_WAITING_ACK;
1575 goto end;
1576
1577 abort_frag_frame:
1578 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1579 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1580 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1581 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1582 goto end;
1583
1584 end:
1585 return ret;
1586}
1587
1588static int
1589spoe_handle_receiving_frame_appctx(struct appctx *appctx, int *skip)
1590{
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001591 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001592 struct spoe_context *ctx = NULL;
1593 char *frame;
1594 int ret;
1595
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001596 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001597 ret = spoe_recv_frame(appctx, frame,
1598 SPOE_APPCTX(appctx)->max_frame_size);
1599 if (ret > 1) {
1600 if (*frame == SPOE_FRM_T_AGENT_DISCON) {
1601 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001602 ret = -1;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001603 goto end;
1604 }
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001605 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001606 ret = spoe_handle_agentack_frame(appctx, &ctx, frame, ret);
1607 }
1608 switch (ret) {
1609 case -1: /* error */
1610 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1611 break;
1612
1613 case 0: /* ignore */
1614 break;
1615
1616 case 1: /* retry */
1617 *skip = 1;
1618 break;
1619
1620 default:
1621 LIST_DEL(&ctx->list);
1622 LIST_INIT(&ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001623 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01001624 spoe_update_stat_time(&ctx->stats.tv_wait, &ctx->stats.t_waiting);
1625 ctx->stats.tv_response = now;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001626 if (ctx->spoe_appctx) {
1627 ctx->spoe_appctx->cur_fpa--;
Christopher Fauletfce747b2018-01-24 15:59:32 +01001628 ctx->spoe_appctx = NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001629 }
Christopher Faulet8eda93f2017-02-09 09:44:33 +01001630 if (appctx->st0 == SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY &&
1631 ctx == SPOE_APPCTX(appctx)->frag_ctx.ctx) {
1632 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1633 SPOE_APPCTX(appctx)->frag_ctx.ctx = NULL;
1634 SPOE_APPCTX(appctx)->frag_ctx.cursid = 0;
1635 SPOE_APPCTX(appctx)->frag_ctx.curfid = 0;
1636 }
1637 else if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1638 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001639 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
1640 break;
1641 }
1642
1643 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001644 if (trash.data)
1645 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001646 end:
1647 return ret;
1648}
1649
1650static int
1651spoe_handle_processing_appctx(struct appctx *appctx)
1652{
1653 struct stream_interface *si = appctx->owner;
1654 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001655 int ret, skip_sending = 0, skip_receiving = 0, active_s = 0, active_r = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001656
1657 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
1658 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
1659 goto exit;
1660 }
1661
1662 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
1663 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
1664 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
1665 appctx->st1 = SPOE_APPCTX_ERR_NONE;
1666 goto next;
1667 }
1668
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001669 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001670 " - process: fpa=%u/%u - appctx-state=%s - weight=%u - flags=0x%08x\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001671 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8f82b202018-01-24 16:23:03 +01001672 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->cur_fpa,
1673 agent->max_fpa, spoe_appctx_state_str[appctx->st0],
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001674 SPOE_APPCTX(appctx)->node.key, SPOE_APPCTX(appctx)->flags);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001675
Christopher Faulet8f82b202018-01-24 16:23:03 +01001676 if (appctx->st0 == SPOE_APPCTX_ST_WAITING_SYNC_ACK)
1677 skip_sending = 1;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001678
Christopher Faulet8f82b202018-01-24 16:23:03 +01001679 /* receiving_frame loop */
1680 while (!skip_receiving) {
1681 ret = spoe_handle_receiving_frame_appctx(appctx, &skip_receiving);
1682 switch (ret) {
1683 case -1: /* error */
1684 goto next;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001685
Christopher Faulet8f82b202018-01-24 16:23:03 +01001686 case 0: /* ignore */
1687 active_r = 1;
1688 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001689
Christopher Faulet8f82b202018-01-24 16:23:03 +01001690 case 1: /* retry */
1691 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001692
Christopher Faulet8f82b202018-01-24 16:23:03 +01001693 default:
1694 active_r = 1;
1695 break;
1696 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001697 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001698
Christopher Faulet8f82b202018-01-24 16:23:03 +01001699 /* send_frame loop */
1700 while (!skip_sending && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
1701 ret = spoe_handle_sending_frame_appctx(appctx, &skip_sending);
1702 switch (ret) {
1703 case -1: /* error */
1704 goto next;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001705
Christopher Faulet8f82b202018-01-24 16:23:03 +01001706 case 0: /* ignore */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001707 if (SPOE_APPCTX(appctx)->node.key)
1708 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001709 active_s++;
1710 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001711
Christopher Faulet8f82b202018-01-24 16:23:03 +01001712 case 1: /* retry */
1713 break;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001714
Christopher Faulet8f82b202018-01-24 16:23:03 +01001715 default:
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001716 if (SPOE_APPCTX(appctx)->node.key)
1717 SPOE_APPCTX(appctx)->node.key--;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001718 active_s++;
1719 break;
1720 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001721 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001722
Christopher Faulet8f82b202018-01-24 16:23:03 +01001723 if (active_s || active_r) {
Christopher Faulet8f82b202018-01-24 16:23:03 +01001724 update_freq_ctr(&agent->rt[tid].processing_per_sec, active_s);
1725 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1726 }
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001727
Christopher Faulet8f82b202018-01-24 16:23:03 +01001728 if (appctx->st0 == SPOE_APPCTX_ST_PROCESSING && SPOE_APPCTX(appctx)->cur_fpa < agent->max_fpa) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001729 HA_ATOMIC_ADD(&agent->counters.idles, 1);
Christopher Faulet8f82b202018-01-24 16:23:03 +01001730 appctx->st0 = SPOE_APPCTX_ST_IDLE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01001731 eb32_insert(&agent->rt[tid].idle_applets, &SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001732 }
1733 return 1;
1734
Christopher Faulet8f82b202018-01-24 16:23:03 +01001735 next:
1736 SPOE_APPCTX(appctx)->task->expire = tick_add_ifset(now_ms, agent->timeout.idle);
1737 return 0;
1738
Christopher Fauleta1cda022016-12-21 08:58:06 +01001739 exit:
1740 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1741 return 0;
1742}
1743
1744static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001745spoe_handle_disconnect_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001746{
1747 struct stream_interface *si = appctx->owner;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001748 struct spoe_agent *agent = SPOE_APPCTX(appctx)->agent;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001749 char *frame, *buf;
1750 int ret;
Christopher Fauletb067b062017-01-04 16:39:11 +01001751
Christopher Fauleta1cda022016-12-21 08:58:06 +01001752 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO)
1753 goto exit;
1754
1755 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT)
1756 goto exit;
1757
Christopher Faulet8ef75252017-02-20 22:56:03 +01001758 /* 4 bytes are reserved at the beginning of <buf> to store the frame
1759 * length. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001760 buf = trash.area; frame = buf+4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001761 ret = spoe_prepare_hadiscon_frame(appctx, frame,
1762 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001763 if (ret > 1)
Christopher Faulet8ef75252017-02-20 22:56:03 +01001764 ret = spoe_send_frame(appctx, buf, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001765
1766 switch (ret) {
1767 case -1: /* error */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001768 case 0: /* ignore => an error, cannot be ignored */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001769 goto exit;
1770
1771 case 1: /* retry */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001772 goto stop;
1773
1774 default:
1775 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1776 " - disconnected by HAProxy (%d): %s\n",
1777 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001778 __FUNCTION__, appctx,
1779 SPOE_APPCTX(appctx)->status_code,
1780 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001781
1782 appctx->st0 = SPOE_APPCTX_ST_DISCONNECTING;
1783 goto next;
1784 }
1785
1786 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001787 SPOE_APPCTX(appctx)->task->expire =
1788 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001789 return 0;
1790 stop:
1791 return 1;
1792 exit:
1793 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1794 return 0;
1795}
1796
1797static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01001798spoe_handle_disconnecting_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001799{
1800 struct stream_interface *si = appctx->owner;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001801 char *frame;
1802 int ret;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001803
Christopher Fauletb067b062017-01-04 16:39:11 +01001804 if (si->state == SI_ST_CLO || si_opposite(si)->state == SI_ST_CLO) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001805 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_IO;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001806 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001807 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001808
Christopher Fauletb067b062017-01-04 16:39:11 +01001809 if (appctx->st1 == SPOE_APPCTX_ERR_TOUT) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001810 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_TOUT;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001811 goto exit;
Christopher Fauletb067b062017-01-04 16:39:11 +01001812 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001813
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001814 frame = trash.area; trash.data = 0;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001815 ret = spoe_recv_frame(appctx, frame,
1816 SPOE_APPCTX(appctx)->max_frame_size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001817 if (ret > 1) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001818 trash.data = ret + 4;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001819 ret = spoe_handle_agentdiscon_frame(appctx, frame, ret);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001820 }
1821
1822 switch (ret) {
1823 case -1: /* error */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001824 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1825 " - error on frame (%s)\n",
1826 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001827 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Fauleta1cda022016-12-21 08:58:06 +01001828 __FUNCTION__, appctx,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001829 spoe_frm_err_reasons[SPOE_APPCTX(appctx)->status_code]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001830 goto exit;
1831
1832 case 0: /* ignore */
Christopher Fauleta1cda022016-12-21 08:58:06 +01001833 goto next;
1834
1835 case 1: /* retry */
1836 goto stop;
1837
1838 default:
Christopher Fauleta1cda022016-12-21 08:58:06 +01001839 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01001840 " - disconnected by peer (%d): %.*s\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01001841 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet42bfa462017-01-04 14:14:19 +01001842 ((struct spoe_agent *)SPOE_APPCTX(appctx)->agent)->id,
Christopher Faulet8ef75252017-02-20 22:56:03 +01001843 __FUNCTION__, appctx, SPOE_APPCTX(appctx)->status_code,
1844 SPOE_APPCTX(appctx)->rlen, SPOE_APPCTX(appctx)->reason);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001845 goto exit;
1846 }
1847
1848 next:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001849 /* Do not forget to remove processed frame from the output buffer */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001850 if (trash.data)
1851 co_skip(si_oc(appctx->owner), trash.data);
Christopher Faulet8ef75252017-02-20 22:56:03 +01001852
Christopher Fauleta1cda022016-12-21 08:58:06 +01001853 return 0;
1854 stop:
1855 return 1;
1856 exit:
1857 appctx->st0 = SPOE_APPCTX_ST_EXIT;
1858 return 0;
1859}
1860
1861/* I/O Handler processing messages exchanged with the agent */
1862static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01001863spoe_handle_appctx(struct appctx *appctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01001864{
Christopher Faulet8ef75252017-02-20 22:56:03 +01001865 struct stream_interface *si = appctx->owner;
1866 struct spoe_agent *agent;
1867
1868 if (SPOE_APPCTX(appctx) == NULL)
1869 return;
Christopher Fauleta1cda022016-12-21 08:58:06 +01001870
Christopher Faulet8ef75252017-02-20 22:56:03 +01001871 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
1872 agent = SPOE_APPCTX(appctx)->agent;
Christopher Fauletb067b062017-01-04 16:39:11 +01001873
Christopher Fauleta1cda022016-12-21 08:58:06 +01001874 switchstate:
1875 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: appctx=%p"
1876 " - appctx-state=%s\n",
1877 (int)now.tv_sec, (int)now.tv_usec, agent->id,
1878 __FUNCTION__, appctx, spoe_appctx_state_str[appctx->st0]);
1879
1880 switch (appctx->st0) {
1881 case SPOE_APPCTX_ST_CONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001882 if (spoe_handle_connect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001883 goto out;
1884 goto switchstate;
1885
1886 case SPOE_APPCTX_ST_CONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001887 if (spoe_handle_connecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001888 goto out;
1889 goto switchstate;
1890
1891 case SPOE_APPCTX_ST_IDLE:
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02001892 HA_ATOMIC_SUB(&agent->counters.idles, 1);
Christopher Faulet7d9f1ba2018-02-28 13:33:26 +01001893 eb32_delete(&SPOE_APPCTX(appctx)->node);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001894 if (stopping &&
Christopher Faulet24289f22017-09-25 14:48:02 +02001895 LIST_ISEMPTY(&agent->rt[tid].sending_queue) &&
Christopher Faulet42bfa462017-01-04 14:14:19 +01001896 LIST_ISEMPTY(&SPOE_APPCTX(appctx)->waiting_queue)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01001897 SPOE_APPCTX(appctx)->task->expire =
1898 tick_add_ifset(now_ms, agent->timeout.idle);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001899 appctx->st0 = SPOE_APPCTX_ST_DISCONNECT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001900 goto switchstate;
1901 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001902 appctx->st0 = SPOE_APPCTX_ST_PROCESSING;
1903 /* fall through */
1904
1905 case SPOE_APPCTX_ST_PROCESSING:
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01001906 case SPOE_APPCTX_ST_SENDING_FRAG_NOTIFY:
1907 case SPOE_APPCTX_ST_WAITING_SYNC_ACK:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001908 if (spoe_handle_processing_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001909 goto out;
1910 goto switchstate;
1911
1912 case SPOE_APPCTX_ST_DISCONNECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001913 if (spoe_handle_disconnect_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001914 goto out;
1915 goto switchstate;
1916
1917 case SPOE_APPCTX_ST_DISCONNECTING:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001918 if (spoe_handle_disconnecting_appctx(appctx))
Christopher Fauleta1cda022016-12-21 08:58:06 +01001919 goto out;
1920 goto switchstate;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001921
1922 case SPOE_APPCTX_ST_EXIT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01001923 appctx->st0 = SPOE_APPCTX_ST_END;
1924 SPOE_APPCTX(appctx)->task->expire = TICK_ETERNITY;
1925
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001926 si_shutw(si);
1927 si_shutr(si);
1928 si_ic(si)->flags |= CF_READ_NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001929 /* fall through */
1930
1931 case SPOE_APPCTX_ST_END:
Christopher Fauleta73e59b2016-12-09 17:30:18 +01001932 return;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001933 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01001934 out:
Christopher Faulet24289f22017-09-25 14:48:02 +02001935 if (stopping)
1936 spoe_wakeup_appctx(appctx);
1937
Christopher Faulet42bfa462017-01-04 14:14:19 +01001938 if (SPOE_APPCTX(appctx)->task->expire != TICK_ETERNITY)
1939 task_queue(SPOE_APPCTX(appctx)->task);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001940 si_oc(si)->flags |= CF_READ_DONTWAIT;
1941 task_wakeup(si_strm(si)->task, TASK_WOKEN_IO);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001942}
1943
1944struct applet spoe_applet = {
1945 .obj_type = OBJ_TYPE_APPLET,
1946 .name = "<SPOE>", /* used for logging */
Christopher Faulet8ef75252017-02-20 22:56:03 +01001947 .fct = spoe_handle_appctx,
1948 .release = spoe_release_appctx,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001949};
1950
1951/* Create a SPOE applet. On success, the created applet is returned, else
1952 * NULL. */
1953static struct appctx *
Christopher Faulet8ef75252017-02-20 22:56:03 +01001954spoe_create_appctx(struct spoe_config *conf)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001955{
1956 struct appctx *appctx;
1957 struct session *sess;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001958 struct stream *strm;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001959
Emeric Brun1138fd02017-06-19 12:38:55 +02001960 if ((appctx = appctx_new(&spoe_applet, tid_bit)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001961 goto out_error;
1962
Willy Tarreaubafbe012017-11-24 17:34:44 +01001963 appctx->ctx.spoe.ptr = pool_alloc_dirty(pool_head_spoe_appctx);
Christopher Faulet42bfa462017-01-04 14:14:19 +01001964 if (SPOE_APPCTX(appctx) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001965 goto out_free_appctx;
Willy Tarreaubafbe012017-11-24 17:34:44 +01001966 memset(appctx->ctx.spoe.ptr, 0, pool_head_spoe_appctx->size);
Christopher Fauleta1cda022016-12-21 08:58:06 +01001967
Christopher Faulet42bfa462017-01-04 14:14:19 +01001968 appctx->st0 = SPOE_APPCTX_ST_CONNECT;
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01001969 if ((SPOE_APPCTX(appctx)->task = task_new(tid_bit)) == NULL)
Christopher Faulet42bfa462017-01-04 14:14:19 +01001970 goto out_free_spoe_appctx;
1971
1972 SPOE_APPCTX(appctx)->owner = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001973 SPOE_APPCTX(appctx)->task->process = spoe_process_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001974 SPOE_APPCTX(appctx)->task->context = appctx;
1975 SPOE_APPCTX(appctx)->agent = conf->agent;
1976 SPOE_APPCTX(appctx)->version = 0;
1977 SPOE_APPCTX(appctx)->max_frame_size = conf->agent->max_frame_size;
1978 SPOE_APPCTX(appctx)->flags = 0;
Christopher Fauletb067b062017-01-04 16:39:11 +01001979 SPOE_APPCTX(appctx)->status_code = SPOE_FRM_ERR_NONE;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02001980 SPOE_APPCTX(appctx)->buffer = BUF_NULL;
Christopher Faulet8f82b202018-01-24 16:23:03 +01001981 SPOE_APPCTX(appctx)->cur_fpa = 0;
Christopher Faulet4596fb72017-01-11 14:05:19 +01001982
1983 LIST_INIT(&SPOE_APPCTX(appctx)->buffer_wait.list);
1984 SPOE_APPCTX(appctx)->buffer_wait.target = appctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01001985 SPOE_APPCTX(appctx)->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01001986
1987 LIST_INIT(&SPOE_APPCTX(appctx)->list);
1988 LIST_INIT(&SPOE_APPCTX(appctx)->waiting_queue);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001989
Willy Tarreau5820a362016-12-22 15:59:02 +01001990 sess = session_new(&conf->agent_fe, NULL, &appctx->obj_type);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001991 if (!sess)
1992 goto out_free_spoe;
1993
Willy Tarreau87787ac2017-08-28 16:22:54 +02001994 if ((strm = stream_new(sess, &appctx->obj_type)) == NULL)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001995 goto out_free_sess;
1996
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02001997 stream_set_backend(strm, conf->agent->b.be);
1998
1999 /* applet is waiting for data */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002000 si_cant_get(&strm->si[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002001 appctx_wakeup(appctx);
2002
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002003 strm->do_log = NULL;
2004 strm->res.flags |= CF_READ_DONTWAIT;
2005
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002006 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002007 LIST_ADDQ(&conf->agent->rt[tid].applets, &SPOE_APPCTX(appctx)->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002008 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &conf->agent->rt[tid].lock);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002009 HA_ATOMIC_ADD(&conf->agent->counters.applets, 1);
Emeric Brun5f77fef2017-05-29 15:26:51 +02002010
Emeric Brunc60def82017-09-27 14:59:38 +02002011 task_wakeup(SPOE_APPCTX(appctx)->task, TASK_WOKEN_INIT);
Willy Tarreau87787ac2017-08-28 16:22:54 +02002012 task_wakeup(strm->task, TASK_WOKEN_INIT);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002013 return appctx;
2014
2015 /* Error unrolling */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002016 out_free_sess:
2017 session_free(sess);
2018 out_free_spoe:
Christopher Faulet42bfa462017-01-04 14:14:19 +01002019 task_free(SPOE_APPCTX(appctx)->task);
2020 out_free_spoe_appctx:
Willy Tarreaubafbe012017-11-24 17:34:44 +01002021 pool_free(pool_head_spoe_appctx, SPOE_APPCTX(appctx));
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002022 out_free_appctx:
2023 appctx_free(appctx);
2024 out_error:
2025 return NULL;
2026}
2027
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002028static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002029spoe_queue_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002030{
2031 struct spoe_config *conf = FLT_CONF(ctx->filter);
2032 struct spoe_agent *agent = conf->agent;
2033 struct appctx *appctx;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002034 struct spoe_appctx *spoe_appctx;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002035
Christopher Fauleta1cda022016-12-21 08:58:06 +01002036 /* Check if we need to create a new SPOE applet or not. */
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002037 if (!eb_is_empty(&agent->rt[tid].idle_applets) &&
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002038 agent->rt[tid].processing < read_freq_ctr(&agent->rt[tid].processing_per_sec))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002039 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002040
2041 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauleta1cda022016-12-21 08:58:06 +01002042 " - try to create new SPOE appctx\n",
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002043 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
2044 ctx->strm);
2045
Christopher Fauleta1cda022016-12-21 08:58:06 +01002046 /* Do not try to create a new applet if there is no server up for the
2047 * agent's backend. */
2048 if (!agent->b.be->srv_act && !agent->b.be->srv_bck) {
2049 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2050 " - cannot create SPOE appctx: no server up\n",
2051 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2052 __FUNCTION__, ctx->strm);
2053 goto end;
2054 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002055
Christopher Fauleta1cda022016-12-21 08:58:06 +01002056 /* Do not try to create a new applet if we have reached the maximum of
2057 * connection per seconds */
Christopher Faulet48026722016-11-16 15:01:12 +01002058 if (agent->cps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002059 if (!freq_ctr_remain(&agent->rt[tid].conn_per_sec, agent->cps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002060 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2061 " - cannot create SPOE appctx: max CPS reached\n",
2062 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2063 __FUNCTION__, ctx->strm);
2064 goto end;
2065 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002066 }
2067
Christopher Faulet8ef75252017-02-20 22:56:03 +01002068 appctx = spoe_create_appctx(conf);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002069 if (appctx == NULL) {
2070 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2071 " - failed to create SPOE appctx\n",
2072 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2073 __FUNCTION__, ctx->strm);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02002074 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01002075 "SPOE: [%s] failed to create SPOE applet\n",
2076 agent->id);
2077
Christopher Fauleta1cda022016-12-21 08:58:06 +01002078 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002079 }
2080
Christopher Fauleta1cda022016-12-21 08:58:06 +01002081 /* Increase the per-process number of cumulated connections */
2082 if (agent->cps_max > 0)
Christopher Faulet24289f22017-09-25 14:48:02 +02002083 update_freq_ctr(&agent->rt[tid].conn_per_sec, 1);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002084
Christopher Fauleta1cda022016-12-21 08:58:06 +01002085 end:
2086 /* The only reason to return an error is when there is no applet */
Christopher Faulet24289f22017-09-25 14:48:02 +02002087 if (LIST_ISEMPTY(&agent->rt[tid].applets)) {
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002088 ctx->status_code = SPOE_CTX_ERR_RES;
2089 return -1;
2090 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002091
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002092 /* Add the SPOE context in the sending queue */
Christopher Faulet24289f22017-09-25 14:48:02 +02002093 LIST_ADDQ(&agent->rt[tid].sending_queue, &ctx->list);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002094 HA_ATOMIC_ADD(&agent->counters.nb_sending, 1);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002095 spoe_update_stat_time(&ctx->stats.tv_request, &ctx->stats.t_request);
2096 ctx->stats.tv_queue = now;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002097
2098 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002099 " - Add stream in sending queue"
Christopher Faulet68db0232018-04-06 11:34:12 +02002100 " - applets=%u - idles=%u - processing=%u\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002101 (int)now.tv_sec, (int)now.tv_usec, agent->id, __FUNCTION__,
Christopher Faulet68db0232018-04-06 11:34:12 +02002102 ctx->strm, agent->counters.applets, agent->counters.idles,
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002103 agent->rt[tid].processing);
Christopher Fauletf7a30922016-11-10 15:04:51 +01002104
Christopher Fauletb077cdc2018-01-24 16:37:57 +01002105 /* Finally try to wakeup an IDLE applet. */
2106 if (!eb_is_empty(&agent->rt[tid].idle_applets)) {
2107 struct eb32_node *node;
2108
2109 node = eb32_first(&agent->rt[tid].idle_applets);
2110 spoe_appctx = eb32_entry(node, struct spoe_appctx, node);
2111 if (node && spoe_appctx) {
2112 eb32_delete(&spoe_appctx->node);
2113 spoe_appctx->node.key++;
2114 eb32_insert(&agent->rt[tid].idle_applets, &spoe_appctx->node);
2115 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002116 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002117 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002118 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002119}
2120
2121/***************************************************************************
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002122 * Functions that encode SPOE messages
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002123 **************************************************************************/
Christopher Faulet10e37672017-09-21 16:38:22 +02002124/* Encode a SPOE message. Info in <ctx->frag_ctx>, if any, are used to handle
2125 * fragmented_content. If the next message can be processed, it returns 0. If
2126 * the message is too big, it returns -1.*/
2127static int
2128spoe_encode_message(struct stream *s, struct spoe_context *ctx,
2129 struct spoe_message *msg, int dir,
2130 char **buf, char *end)
2131{
2132 struct sample *smp;
2133 struct spoe_arg *arg;
2134 int ret;
2135
2136 if (msg->cond) {
2137 ret = acl_exec_cond(msg->cond, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2138 ret = acl_pass(ret);
2139 if (msg->cond->pol == ACL_COND_UNLESS)
2140 ret = !ret;
2141
2142 /* the rule does not match */
2143 if (!ret)
2144 goto next;
2145 }
2146
2147 /* Resume encoding of a SPOE argument */
2148 if (ctx->frag_ctx.curarg != NULL) {
2149 arg = ctx->frag_ctx.curarg;
2150 goto encode_argument;
2151 }
2152
2153 if (ctx->frag_ctx.curoff != UINT_MAX)
2154 goto encode_msg_payload;
2155
2156 /* Check if there is enough space for the message name and the
2157 * number of arguments. It implies <msg->id_len> is encoded on 2
2158 * bytes, at most (< 2288). */
2159 if (*buf + 2 + msg->id_len + 1 > end)
2160 goto too_big;
2161
2162 /* Encode the message name */
2163 if (spoe_encode_buffer(msg->id, msg->id_len, buf, end) == -1)
2164 goto too_big;
2165
2166 /* Set the number of arguments for this message */
2167 **buf = msg->nargs;
2168 (*buf)++;
2169
2170 ctx->frag_ctx.curoff = 0;
2171 encode_msg_payload:
2172
2173 /* Loop on arguments */
2174 list_for_each_entry(arg, &msg->args, list) {
2175 ctx->frag_ctx.curarg = arg;
2176 ctx->frag_ctx.curoff = UINT_MAX;
2177
2178 encode_argument:
2179 if (ctx->frag_ctx.curoff != UINT_MAX)
2180 goto encode_arg_value;
2181
2182 /* Encode the arguement name as a string. It can by NULL */
2183 if (spoe_encode_buffer(arg->name, arg->name_len, buf, end) == -1)
2184 goto too_big;
2185
2186 ctx->frag_ctx.curoff = 0;
2187 encode_arg_value:
2188
2189 /* Fetch the arguement value */
2190 smp = sample_process(s->be, s->sess, s, dir|SMP_OPT_FINAL, arg->expr, NULL);
2191 ret = spoe_encode_data(smp, &ctx->frag_ctx.curoff, buf, end);
2192 if (ret == -1 || ctx->frag_ctx.curoff)
2193 goto too_big;
2194 }
2195
2196 next:
2197 return 0;
2198
2199 too_big:
2200 return -1;
2201}
2202
Christopher Fauletc718b822017-09-21 16:50:56 +02002203/* Encode list of SPOE messages. Info in <ctx->frag_ctx>, if any, are used to
2204 * handle fragmented content. On success it returns 1. If an error occurred, -1
2205 * is returned. If nothing has been encoded, it returns 0 (this is only possible
2206 * for unfragmented payload). */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002207static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002208spoe_encode_messages(struct stream *s, struct spoe_context *ctx,
Christopher Fauletc718b822017-09-21 16:50:56 +02002209 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002210{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002211 struct spoe_config *conf = FLT_CONF(ctx->filter);
2212 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002213 struct spoe_message *msg;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002214 char *p, *end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002215
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002216 p = b_head(&ctx->buffer);
Christopher Faulet24289f22017-09-25 14:48:02 +02002217 end = p + agent->rt[tid].frame_size - FRAME_HDR_SIZE;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002218
Christopher Fauletc718b822017-09-21 16:50:56 +02002219 if (type == SPOE_MSGS_BY_EVENT) { /* Loop on messages by event */
2220 /* Resume encoding of a SPOE message */
2221 if (ctx->frag_ctx.curmsg != NULL) {
2222 msg = ctx->frag_ctx.curmsg;
2223 goto encode_evt_message;
2224 }
2225
2226 list_for_each_entry(msg, messages, by_evt) {
2227 ctx->frag_ctx.curmsg = msg;
2228 ctx->frag_ctx.curarg = NULL;
2229 ctx->frag_ctx.curoff = UINT_MAX;
2230
2231 encode_evt_message:
2232 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2233 goto too_big;
2234 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002235 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002236 else if (type == SPOE_MSGS_BY_GROUP) { /* Loop on messages by group */
2237 /* Resume encoding of a SPOE message */
2238 if (ctx->frag_ctx.curmsg != NULL) {
2239 msg = ctx->frag_ctx.curmsg;
2240 goto encode_grp_message;
2241 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002242
Christopher Fauletc718b822017-09-21 16:50:56 +02002243 list_for_each_entry(msg, messages, by_grp) {
2244 ctx->frag_ctx.curmsg = msg;
2245 ctx->frag_ctx.curarg = NULL;
2246 ctx->frag_ctx.curoff = UINT_MAX;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002247
Christopher Fauletc718b822017-09-21 16:50:56 +02002248 encode_grp_message:
2249 if (spoe_encode_message(s, ctx, msg, dir, &p, end) == -1)
2250 goto too_big;
2251 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002252 }
Christopher Fauletc718b822017-09-21 16:50:56 +02002253 else
2254 goto skip;
2255
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002256
Christopher Faulet57583e42017-09-04 15:41:09 +02002257 /* nothing has been encoded for an unfragmented payload */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002258 if (!(ctx->flags & SPOE_CTX_FL_FRAGMENTED) && p == b_head(&ctx->buffer))
Christopher Faulet57583e42017-09-04 15:41:09 +02002259 goto skip;
2260
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002261 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002262 " - encode %s messages - spoe_appctx=%p"
2263 "- max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002264 (int)now.tv_sec, (int)now.tv_usec,
2265 agent->id, __FUNCTION__, s,
2266 ((ctx->flags & SPOE_CTX_FL_FRAGMENTED) ? "last fragment of" : "unfragmented"),
Christopher Fauletfce747b2018-01-24 15:59:32 +01002267 ctx->spoe_appctx, (agent->rt[tid].frame_size - FRAME_HDR_SIZE),
Christopher Faulet45073512018-07-20 10:16:29 +02002268 p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002269
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002270 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002271 ctx->frag_ctx.curmsg = NULL;
2272 ctx->frag_ctx.curarg = NULL;
2273 ctx->frag_ctx.curoff = 0;
2274 ctx->frag_ctx.flags = SPOE_FRM_FL_FIN;
Christopher Faulet57583e42017-09-04 15:41:09 +02002275
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002276 return 1;
2277
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002278 too_big:
Christopher Fauletcecd8522017-02-24 22:11:21 +01002279 if (!(agent->flags & SPOE_FL_SND_FRAGMENTATION)) {
2280 ctx->status_code = SPOE_CTX_ERR_TOO_BIG;
2281 return -1;
2282 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002283
2284 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet8ef75252017-02-20 22:56:03 +01002285 " - encode fragmented messages - spoe_appctx=%p"
2286 " - curmsg=%p - curarg=%p - curoff=%u"
2287 " - max_size=%u - encoded=%ld\n",
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002288 (int)now.tv_sec, (int)now.tv_usec,
Christopher Fauletfce747b2018-01-24 15:59:32 +01002289 agent->id, __FUNCTION__, s, ctx->spoe_appctx,
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002290 ctx->frag_ctx.curmsg, ctx->frag_ctx.curarg, ctx->frag_ctx.curoff,
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002291 (agent->rt[tid].frame_size - FRAME_HDR_SIZE), p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002292
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002293 b_set_data(&ctx->buffer, p - b_head(&ctx->buffer));
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002294 ctx->flags |= SPOE_CTX_FL_FRAGMENTED;
2295 ctx->frag_ctx.flags &= ~SPOE_FRM_FL_FIN;
2296 return 1;
Christopher Faulet57583e42017-09-04 15:41:09 +02002297
2298 skip:
2299 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2300 " - skip the frame because nothing has been encoded\n",
2301 (int)now.tv_sec, (int)now.tv_usec,
2302 agent->id, __FUNCTION__, s);
2303 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002304}
2305
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002306
2307/***************************************************************************
2308 * Functions that handle SPOE actions
2309 **************************************************************************/
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002310/* Helper function to set a variable */
2311static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002312spoe_set_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002313 struct sample *smp)
2314{
2315 struct spoe_config *conf = FLT_CONF(ctx->filter);
2316 struct spoe_agent *agent = conf->agent;
2317 char varname[64];
2318
2319 memset(varname, 0, sizeof(varname));
2320 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2321 scope, agent->var_pfx, len, name);
Etienne Carriereaec89892017-12-14 09:36:40 +00002322 if (agent->flags & SPOE_FL_FORCE_SET_VAR)
2323 vars_set_by_name(varname, len, smp);
2324 else
2325 vars_set_by_name_ifexist(varname, len, smp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002326}
2327
2328/* Helper function to unset a variable */
2329static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002330spoe_unset_var(struct spoe_context *ctx, char *scope, char *name, int len,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002331 struct sample *smp)
2332{
2333 struct spoe_config *conf = FLT_CONF(ctx->filter);
2334 struct spoe_agent *agent = conf->agent;
2335 char varname[64];
2336
2337 memset(varname, 0, sizeof(varname));
2338 len = snprintf(varname, sizeof(varname), "%s.%s.%.*s",
2339 scope, agent->var_pfx, len, name);
2340 vars_unset_by_name_ifexist(varname, len, smp);
2341}
2342
2343
Christopher Faulet8ef75252017-02-20 22:56:03 +01002344static inline int
2345spoe_decode_action_set_var(struct stream *s, struct spoe_context *ctx,
2346 char **buf, char *end, int dir)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002347{
Christopher Faulet8ef75252017-02-20 22:56:03 +01002348 char *str, *scope, *p = *buf;
2349 struct sample smp;
2350 uint64_t sz;
2351 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002352
Christopher Faulet8ef75252017-02-20 22:56:03 +01002353 if (p + 2 >= end)
2354 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002355
Christopher Faulet8ef75252017-02-20 22:56:03 +01002356 /* SET-VAR requires 3 arguments */
2357 if (*p++ != 3)
2358 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002359
Christopher Faulet8ef75252017-02-20 22:56:03 +01002360 switch (*p++) {
2361 case SPOE_SCOPE_PROC: scope = "proc"; break;
2362 case SPOE_SCOPE_SESS: scope = "sess"; break;
2363 case SPOE_SCOPE_TXN : scope = "txn"; break;
2364 case SPOE_SCOPE_REQ : scope = "req"; break;
2365 case SPOE_SCOPE_RES : scope = "res"; break;
2366 default: goto skip;
2367 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002368
Christopher Faulet8ef75252017-02-20 22:56:03 +01002369 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2370 goto skip;
2371 memset(&smp, 0, sizeof(smp));
2372 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002373
Christopher Faulet8ef75252017-02-20 22:56:03 +01002374 if (spoe_decode_data(&p, end, &smp) == -1)
2375 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002376
Christopher Faulet8ef75252017-02-20 22:56:03 +01002377 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2378 " - set-var '%s.%s.%.*s'\n",
2379 (int)now.tv_sec, (int)now.tv_usec,
2380 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2381 __FUNCTION__, s, scope,
2382 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2383 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002384
Christopher Faulet8ef75252017-02-20 22:56:03 +01002385 spoe_set_var(ctx, scope, str, sz, &smp);
Christopher Fauletb5cff602016-11-24 14:53:22 +01002386
Christopher Faulet8ef75252017-02-20 22:56:03 +01002387 ret = (p - *buf);
2388 *buf = p;
2389 return ret;
2390 skip:
2391 return 0;
2392}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002393
Christopher Faulet8ef75252017-02-20 22:56:03 +01002394static inline int
2395spoe_decode_action_unset_var(struct stream *s, struct spoe_context *ctx,
2396 char **buf, char *end, int dir)
2397{
2398 char *str, *scope, *p = *buf;
2399 struct sample smp;
2400 uint64_t sz;
2401 int ret;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002402
Christopher Faulet8ef75252017-02-20 22:56:03 +01002403 if (p + 2 >= end)
2404 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002405
Christopher Faulet8ef75252017-02-20 22:56:03 +01002406 /* UNSET-VAR requires 2 arguments */
2407 if (*p++ != 2)
2408 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002409
Christopher Faulet8ef75252017-02-20 22:56:03 +01002410 switch (*p++) {
2411 case SPOE_SCOPE_PROC: scope = "proc"; break;
2412 case SPOE_SCOPE_SESS: scope = "sess"; break;
2413 case SPOE_SCOPE_TXN : scope = "txn"; break;
2414 case SPOE_SCOPE_REQ : scope = "req"; break;
2415 case SPOE_SCOPE_RES : scope = "res"; break;
2416 default: goto skip;
2417 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002418
Christopher Faulet8ef75252017-02-20 22:56:03 +01002419 if (spoe_decode_buffer(&p, end, &str, &sz) == -1)
2420 goto skip;
2421 memset(&smp, 0, sizeof(smp));
2422 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002423
Christopher Faulet8ef75252017-02-20 22:56:03 +01002424 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2425 " - unset-var '%s.%s.%.*s'\n",
2426 (int)now.tv_sec, (int)now.tv_usec,
2427 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->id,
2428 __FUNCTION__, s, scope,
2429 ((struct spoe_config *)FLT_CONF(ctx->filter))->agent->var_pfx,
2430 (int)sz, str);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002431
Christopher Faulet8ef75252017-02-20 22:56:03 +01002432 spoe_unset_var(ctx, scope, str, sz, &smp);
2433
2434 ret = (p - *buf);
2435 *buf = p;
2436 return ret;
2437 skip:
2438 return 0;
2439}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002440
Christopher Faulet8ef75252017-02-20 22:56:03 +01002441/* Process SPOE actions for a specific event. It returns 1 on success. If an
2442 * error occurred, 0 is returned. */
2443static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002444spoe_process_actions(struct stream *s, struct spoe_context *ctx, int dir)
Christopher Faulet8ef75252017-02-20 22:56:03 +01002445{
2446 char *p, *end;
2447 int ret;
2448
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002449 p = b_head(&ctx->buffer);
2450 end = p + b_data(&ctx->buffer);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002451
2452 while (p < end) {
2453 enum spoe_action_type type;
2454
2455 type = *p++;
2456 switch (type) {
2457 case SPOE_ACT_T_SET_VAR:
2458 ret = spoe_decode_action_set_var(s, ctx, &p, end, dir);
2459 if (!ret)
2460 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002461 break;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002462
Christopher Faulet8ef75252017-02-20 22:56:03 +01002463 case SPOE_ACT_T_UNSET_VAR:
2464 ret = spoe_decode_action_unset_var(s, ctx, &p, end, dir);
2465 if (!ret)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002466 goto skip;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002467 break;
2468
2469 default:
2470 goto skip;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002471 }
2472 }
2473
2474 return 1;
2475 skip:
2476 return 0;
2477}
2478
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002479/***************************************************************************
2480 * Functions that process SPOE events
2481 **************************************************************************/
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002482static void
2483spoe_update_stats(struct stream *s, struct spoe_agent *agent,
2484 struct spoe_context *ctx, int dir)
2485{
2486 if (!tv_iszero(&ctx->stats.tv_start)) {
2487 spoe_update_stat_time(&ctx->stats.tv_start, &ctx->stats.t_process);
2488 ctx->stats.t_total += ctx->stats.t_process;
2489 tv_zero(&ctx->stats.tv_request);
2490 tv_zero(&ctx->stats.tv_queue);
2491 tv_zero(&ctx->stats.tv_wait);
2492 tv_zero(&ctx->stats.tv_response);
2493 }
2494
2495 if (agent->var_t_process) {
2496 struct sample smp;
2497
2498 memset(&smp, 0, sizeof(smp));
2499 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2500 smp.data.u.sint = ctx->stats.t_process;
2501 smp.data.type = SMP_T_SINT;
2502
2503 spoe_set_var(ctx, "txn", agent->var_t_process,
2504 strlen(agent->var_t_process), &smp);
2505 }
2506
2507 if (agent->var_t_total) {
2508 struct sample smp;
2509
2510 memset(&smp, 0, sizeof(smp));
2511 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2512 smp.data.u.sint = ctx->stats.t_total;
2513 smp.data.type = SMP_T_SINT;
2514
2515 spoe_set_var(ctx, "txn", agent->var_t_total,
2516 strlen(agent->var_t_total), &smp);
2517 }
2518}
2519
2520static void
2521spoe_handle_processing_error(struct stream *s, struct spoe_agent *agent,
2522 struct spoe_context *ctx, int dir)
2523{
2524 if (agent->eps_max > 0)
2525 update_freq_ctr(&agent->rt[tid].err_per_sec, 1);
2526
2527 if (agent->var_on_error) {
2528 struct sample smp;
2529
2530 memset(&smp, 0, sizeof(smp));
2531 smp_set_owner(&smp, s->be, s->sess, s, dir|SMP_OPT_FINAL);
2532 smp.data.u.sint = ctx->status_code;
2533 smp.data.type = SMP_T_BOOL;
2534
2535 spoe_set_var(ctx, "txn", agent->var_on_error,
2536 strlen(agent->var_on_error), &smp);
2537 }
2538
2539 ctx->state = ((agent->flags & SPOE_FL_CONT_ON_ERR)
2540 ? SPOE_CTX_ST_READY
2541 : SPOE_CTX_ST_NONE);
2542}
2543
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002544static inline int
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002545spoe_start_processing(struct spoe_agent *agent, struct spoe_context *ctx, int dir)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002546{
Christopher Fauleta1cda022016-12-21 08:58:06 +01002547 /* If a process is already started for this SPOE context, retry
2548 * later. */
2549 if (ctx->flags & SPOE_CTX_FL_PROCESS)
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002550 return 0;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002551
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002552 agent->rt[tid].processing++;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002553 ctx->stats.tv_start = now;
2554 ctx->stats.tv_request = now;
2555 ctx->stats.t_request = -1;
2556 ctx->stats.t_queue = -1;
2557 ctx->stats.t_waiting = -1;
2558 ctx->stats.t_response = -1;
2559 ctx->stats.t_process = -1;
2560
2561 ctx->status_code = 0;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002562
Christopher Fauleta1cda022016-12-21 08:58:06 +01002563 /* Set the right flag to prevent request and response processing
2564 * in same time. */
2565 ctx->flags |= ((dir == SMP_OPT_DIR_REQ)
2566 ? SPOE_CTX_FL_REQ_PROCESS
2567 : SPOE_CTX_FL_RSP_PROCESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002568 return 1;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002569}
2570
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002571static inline void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002572spoe_stop_processing(struct spoe_agent *agent, struct spoe_context *ctx)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002573{
Christopher Fauletfce747b2018-01-24 15:59:32 +01002574 struct spoe_appctx *sa = ctx->spoe_appctx;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002575
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002576 if (!(ctx->flags & SPOE_CTX_FL_PROCESS))
2577 return;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002578 HA_ATOMIC_ADD(&agent->counters.nb_processed, 1);
Christopher Faulet879dca92018-03-23 11:53:24 +01002579 if (sa) {
2580 if (sa->frag_ctx.ctx == ctx) {
2581 sa->frag_ctx.ctx = NULL;
2582 spoe_wakeup_appctx(sa->owner);
2583 }
2584 else
2585 sa->cur_fpa--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002586 }
2587
Christopher Fauleta1cda022016-12-21 08:58:06 +01002588 /* Reset the flag to allow next processing */
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002589 agent->rt[tid].processing--;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002590 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002591
2592 /* Reset processing timer */
2593 ctx->process_exp = TICK_ETERNITY;
2594
Christopher Faulet8ef75252017-02-20 22:56:03 +01002595 spoe_release_buffer(&ctx->buffer, &ctx->buffer_wait);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002596
Christopher Fauletfce747b2018-01-24 15:59:32 +01002597 ctx->spoe_appctx = NULL;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002598 ctx->frag_ctx.curmsg = NULL;
2599 ctx->frag_ctx.curarg = NULL;
2600 ctx->frag_ctx.curoff = 0;
2601 ctx->frag_ctx.flags = 0;
2602
Christopher Fauleta1cda022016-12-21 08:58:06 +01002603 if (!LIST_ISEMPTY(&ctx->list)) {
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002604 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS)
Christopher Fauletebe13992018-04-26 11:33:44 +02002605 HA_ATOMIC_SUB(&agent->counters.nb_sending, 1);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002606 else
2607 HA_ATOMIC_SUB(&agent->counters.nb_waiting, 1);
2608
Christopher Fauleta1cda022016-12-21 08:58:06 +01002609 LIST_DEL(&ctx->list);
2610 LIST_INIT(&ctx->list);
2611 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002612}
2613
Christopher Faulet58d03682017-09-21 16:57:24 +02002614/* Process a list of SPOE messages. First, this functions will process messages
2615 * and send them to an agent in a NOTIFY frame. Then, it will wait a ACK frame
2616 * to process corresponding actions. During all the processing, it returns 0
2617 * and it returns 1 when the processing is finished. If an error occurred, -1
2618 * is returned. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002619static int
Christopher Faulet58d03682017-09-21 16:57:24 +02002620spoe_process_messages(struct stream *s, struct spoe_context *ctx,
2621 struct list *messages, int dir, int type)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002622{
Christopher Fauletf7a30922016-11-10 15:04:51 +01002623 struct spoe_config *conf = FLT_CONF(ctx->filter);
2624 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002625 int ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002626
2627 if (ctx->state == SPOE_CTX_ST_ERROR)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002628 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002629
2630 if (tick_is_expired(ctx->process_exp, now_ms) && ctx->state != SPOE_CTX_ST_DONE) {
2631 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002632 " - failed to process messages: timeout\n",
Christopher Fauletf7a30922016-11-10 15:04:51 +01002633 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002634 agent->id, __FUNCTION__, s);
Christopher Fauletb067b062017-01-04 16:39:11 +01002635 ctx->status_code = SPOE_CTX_ERR_TOUT;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002636 goto end;
Christopher Fauletf7a30922016-11-10 15:04:51 +01002637 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002638
2639 if (ctx->state == SPOE_CTX_ST_READY) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002640 if (agent->eps_max > 0) {
Christopher Faulet24289f22017-09-25 14:48:02 +02002641 if (!freq_ctr_remain(&agent->rt[tid].err_per_sec, agent->eps_max, 0)) {
Christopher Fauleta1cda022016-12-21 08:58:06 +01002642 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet58d03682017-09-21 16:57:24 +02002643 " - skip processing of messages: max EPS reached\n",
Christopher Fauleta1cda022016-12-21 08:58:06 +01002644 (int)now.tv_sec, (int)now.tv_usec,
Christopher Faulet58d03682017-09-21 16:57:24 +02002645 agent->id, __FUNCTION__, s);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002646 goto skip;
2647 }
2648 }
2649
Christopher Fauletf7a30922016-11-10 15:04:51 +01002650 if (!tick_isset(ctx->process_exp)) {
2651 ctx->process_exp = tick_add_ifset(now_ms, agent->timeout.processing);
2652 s->task->expire = tick_first((tick_is_expired(s->task->expire, now_ms) ? 0 : s->task->expire),
2653 ctx->process_exp);
2654 }
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002655 ret = spoe_start_processing(agent, ctx, dir);
Christopher Fauletb067b062017-01-04 16:39:11 +01002656 if (!ret)
2657 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002658
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002659 ctx->state = SPOE_CTX_ST_ENCODING_MSGS;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002660 /* fall through */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002661 }
2662
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002663 if (ctx->state == SPOE_CTX_ST_ENCODING_MSGS) {
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002664 if (!tv_iszero(&ctx->stats.tv_request))
2665 ctx->stats.tv_request = now;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002666 if (!spoe_acquire_buffer(&ctx->buffer, &ctx->buffer_wait))
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002667 goto out;
Christopher Faulet58d03682017-09-21 16:57:24 +02002668 ret = spoe_encode_messages(s, ctx, messages, dir, type);
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002669 if (ret < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002670 goto end;
Christopher Faulet57583e42017-09-04 15:41:09 +02002671 if (!ret)
2672 goto skip;
Christopher Faulet333694d2018-01-12 10:45:47 +01002673 if (spoe_queue_context(ctx) < 0)
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002674 goto end;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002675 ctx->state = SPOE_CTX_ST_SENDING_MSGS;
2676 }
2677
2678 if (ctx->state == SPOE_CTX_ST_SENDING_MSGS) {
Christopher Fauletfce747b2018-01-24 15:59:32 +01002679 if (ctx->spoe_appctx)
2680 spoe_wakeup_appctx(ctx->spoe_appctx->owner);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002681 ret = 0;
2682 goto out;
2683 }
2684
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01002685 if (ctx->state == SPOE_CTX_ST_WAITING_ACK) {
2686 ret = 0;
2687 goto out;
2688 }
2689
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002690 if (ctx->state == SPOE_CTX_ST_DONE) {
Christopher Faulet58d03682017-09-21 16:57:24 +02002691 spoe_process_actions(s, ctx, dir);
Christopher Faulet8ef75252017-02-20 22:56:03 +01002692 ret = 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002693 ctx->frame_id++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002694 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002695 spoe_update_stat_time(&ctx->stats.tv_response, &ctx->stats.t_response);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002696 goto end;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002697 }
2698
2699 out:
2700 return ret;
2701
Christopher Fauleta1cda022016-12-21 08:58:06 +01002702 skip:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002703 tv_zero(&ctx->stats.tv_start);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002704 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002705 spoe_stop_processing(agent, ctx);
2706 return 1;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002707
Christopher Fauleta1cda022016-12-21 08:58:06 +01002708 end:
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002709 spoe_update_stats(s, agent, ctx, dir);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002710 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002711 if (ctx->status_code) {
2712 HA_ATOMIC_ADD(&agent->counters.nb_errors, 1);
2713 spoe_handle_processing_error(s, agent, ctx, dir);
2714 ret = 1;
2715 }
Christopher Faulet58d03682017-09-21 16:57:24 +02002716 return ret;
2717}
2718
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002719/* Process a SPOE group, ie the list of messages attached to the group <grp>.
2720 * See spoe_process_message for details. */
2721static int
2722spoe_process_group(struct stream *s, struct spoe_context *ctx,
2723 struct spoe_group *group, int dir)
2724{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002725 struct spoe_config *conf = FLT_CONF(ctx->filter);
2726 struct spoe_agent *agent = conf->agent;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002727 int ret;
2728
2729 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
2730 " - ctx-state=%s - Process messages for group=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002731 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002732 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2733 group->id);
2734
2735 if (LIST_ISEMPTY(&group->messages))
2736 return 1;
2737
2738 ret = spoe_process_messages(s, ctx, &group->messages, dir, SPOE_MSGS_BY_GROUP);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002739 if (ret && ctx->stats.t_process != -1) {
2740 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002741 " - <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 +01002742 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002743 __FUNCTION__, s, group->id, s->uniq_id, ctx->status_code,
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002744 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002745 ctx->stats.t_response, ctx->stats.t_process,
2746 agent->counters.idles, agent->counters.applets,
2747 agent->counters.nb_sending, agent->counters.nb_waiting,
2748 agent->counters.nb_errors, agent->counters.nb_processed,
2749 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2750 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2751 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2752 "SPOE: [%s] <GROUP:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2753 agent->id, group->id, s->uniq_id, ctx->status_code,
2754 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2755 ctx->stats.t_response, ctx->stats.t_process,
2756 agent->counters.idles, agent->counters.applets,
2757 agent->counters.nb_sending, agent->counters.nb_waiting,
2758 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002759 }
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002760 return ret;
2761}
2762
Christopher Faulet58d03682017-09-21 16:57:24 +02002763/* Process a SPOE event, ie the list of messages attached to the event <ev>.
2764 * See spoe_process_message for details. */
2765static int
2766spoe_process_event(struct stream *s, struct spoe_context *ctx,
2767 enum spoe_event ev)
2768{
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002769 struct spoe_config *conf = FLT_CONF(ctx->filter);
2770 struct spoe_agent *agent = conf->agent;
Christopher Faulet58d03682017-09-21 16:57:24 +02002771 int dir, ret;
2772
2773 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Faulet344c4ab2017-09-22 10:20:13 +02002774 " - ctx-state=%s - Process messages for event=%s\n",
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002775 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Faulet58d03682017-09-21 16:57:24 +02002776 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
2777 spoe_event_str[ev]);
2778
2779 dir = ((ev < SPOE_EV_ON_SERVER_SESS) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES);
2780
2781 if (LIST_ISEMPTY(&(ctx->events[ev])))
2782 return 1;
2783
2784 ret = spoe_process_messages(s, ctx, &(ctx->events[ev]), dir, SPOE_MSGS_BY_EVENT);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002785 if (ret && ctx->stats.t_process != -1) {
2786 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002787 " - <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 +01002788 (int)now.tv_sec, (int)now.tv_usec, agent->id,
2789 __FUNCTION__, s, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2790 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02002791 ctx->stats.t_response, ctx->stats.t_process,
2792 agent->counters.idles, agent->counters.applets,
2793 agent->counters.nb_sending, agent->counters.nb_waiting,
2794 agent->counters.nb_errors, agent->counters.nb_processed,
2795 agent->rt[tid].processing, read_freq_ctr(&agent->rt[tid].processing_per_sec));
2796 if (ctx->status_code || !(conf->agent_fe.options2 & PR_O2_NOLOGNORM))
2797 send_log(&conf->agent_fe, (!ctx->status_code ? LOG_NOTICE : LOG_WARNING),
2798 "SPOE: [%s] <EVENT:%s> sid=%u st=%u %ld/%ld/%ld/%ld/%ld %u/%u %u/%u %llu/%llu\n",
2799 agent->id, spoe_event_str[ev], s->uniq_id, ctx->status_code,
2800 ctx->stats.t_request, ctx->stats.t_queue, ctx->stats.t_waiting,
2801 ctx->stats.t_response, ctx->stats.t_process,
2802 agent->counters.idles, agent->counters.applets,
2803 agent->counters.nb_sending, agent->counters.nb_waiting,
2804 agent->counters.nb_errors, agent->counters.nb_processed);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002805 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01002806 return ret;
2807}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002808
2809/***************************************************************************
2810 * Functions that create/destroy SPOE contexts
2811 **************************************************************************/
Christopher Fauleta1cda022016-12-21 08:58:06 +01002812static int
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002813spoe_acquire_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002814{
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002815 if (buf->size)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002816 return 1;
2817
Christopher Faulet4596fb72017-01-11 14:05:19 +01002818 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002819 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002820 LIST_DEL(&buffer_wait->list);
2821 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002822 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002823 }
2824
Christopher Faulet4596fb72017-01-11 14:05:19 +01002825 if (b_alloc_margin(buf, global.tune.reserved_bufs))
Christopher Fauleta1cda022016-12-21 08:58:06 +01002826 return 1;
2827
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002828 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002829 LIST_ADDQ(&buffer_wq, &buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002830 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002831 return 0;
2832}
2833
2834static void
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002835spoe_release_buffer(struct buffer *buf, struct buffer_wait *buffer_wait)
Christopher Fauleta1cda022016-12-21 08:58:06 +01002836{
Christopher Faulet4596fb72017-01-11 14:05:19 +01002837 if (!LIST_ISEMPTY(&buffer_wait->list)) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002838 HA_SPIN_LOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Faulet4596fb72017-01-11 14:05:19 +01002839 LIST_DEL(&buffer_wait->list);
2840 LIST_INIT(&buffer_wait->list);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002841 HA_SPIN_UNLOCK(BUF_WQ_LOCK, &buffer_wq_lock);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002842 }
2843
2844 /* Release the buffer if needed */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002845 if (buf->size) {
Christopher Faulet4596fb72017-01-11 14:05:19 +01002846 b_free(buf);
Olivier Houchard673867c2018-05-25 16:58:52 +02002847 offer_buffers(buffer_wait->target, tasks_run_queue);
Christopher Fauleta1cda022016-12-21 08:58:06 +01002848 }
2849}
2850
Christopher Faulet4596fb72017-01-11 14:05:19 +01002851static int
Christopher Faulet8ef75252017-02-20 22:56:03 +01002852spoe_wakeup_context(struct spoe_context *ctx)
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002853{
2854 task_wakeup(ctx->strm->task, TASK_WOKEN_MSG);
2855 return 1;
2856}
2857
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002858static struct spoe_context *
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002859spoe_create_context(struct stream *s, struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002860{
2861 struct spoe_config *conf = FLT_CONF(filter);
2862 struct spoe_context *ctx;
2863
Willy Tarreaubafbe012017-11-24 17:34:44 +01002864 ctx = pool_alloc_dirty(pool_head_spoe_ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002865 if (ctx == NULL) {
2866 return NULL;
2867 }
2868 memset(ctx, 0, sizeof(*ctx));
Christopher Fauletb067b062017-01-04 16:39:11 +01002869 ctx->filter = filter;
2870 ctx->state = SPOE_CTX_ST_NONE;
2871 ctx->status_code = SPOE_CTX_ERR_NONE;
2872 ctx->flags = 0;
Christopher Faulet11610f32017-09-21 10:23:10 +02002873 ctx->events = conf->agent->events;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02002874 ctx->groups = &conf->agent->groups;
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002875 ctx->buffer = BUF_NULL;
Christopher Fauleta73e59b2016-12-09 17:30:18 +01002876 LIST_INIT(&ctx->buffer_wait.list);
2877 ctx->buffer_wait.target = ctx;
Christopher Faulet8ef75252017-02-20 22:56:03 +01002878 ctx->buffer_wait.wakeup_cb = (int (*)(void *))spoe_wakeup_context;
Christopher Fauleta1cda022016-12-21 08:58:06 +01002879 LIST_INIT(&ctx->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002880
Christopher Fauletf7a30922016-11-10 15:04:51 +01002881 ctx->stream_id = 0;
2882 ctx->frame_id = 1;
2883 ctx->process_exp = TICK_ETERNITY;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002884
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002885 tv_zero(&ctx->stats.tv_start);
2886 tv_zero(&ctx->stats.tv_request);
2887 tv_zero(&ctx->stats.tv_queue);
2888 tv_zero(&ctx->stats.tv_wait);
2889 tv_zero(&ctx->stats.tv_response);
2890 ctx->stats.t_request = -1;
2891 ctx->stats.t_queue = -1;
2892 ctx->stats.t_waiting = -1;
2893 ctx->stats.t_response = -1;
2894 ctx->stats.t_process = -1;
2895 ctx->stats.t_total = 0;
2896
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002897 ctx->strm = s;
2898 ctx->state = SPOE_CTX_ST_READY;
2899 filter->ctx = ctx;
2900
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002901 return ctx;
2902}
2903
2904static void
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002905spoe_destroy_context(struct filter *filter)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002906{
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002907 struct spoe_config *conf = FLT_CONF(filter);
2908 struct spoe_context *ctx = filter->ctx;
2909
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002910 if (!ctx)
2911 return;
2912
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002913 spoe_stop_processing(conf->agent, ctx);
Willy Tarreaubafbe012017-11-24 17:34:44 +01002914 pool_free(pool_head_spoe_ctx, ctx);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01002915 filter->ctx = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002916}
2917
2918static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002919spoe_reset_context(struct spoe_context *ctx)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002920{
2921 ctx->state = SPOE_CTX_ST_READY;
Christopher Fauletf032c3e2017-02-17 15:18:35 +01002922 ctx->flags &= ~(SPOE_CTX_FL_PROCESS|SPOE_CTX_FL_FRAGMENTED);
Christopher Fauletb2dd1e02018-03-22 09:07:41 +01002923
2924 tv_zero(&ctx->stats.tv_start);
2925 tv_zero(&ctx->stats.tv_request);
2926 tv_zero(&ctx->stats.tv_queue);
2927 tv_zero(&ctx->stats.tv_wait);
2928 tv_zero(&ctx->stats.tv_response);
2929 ctx->stats.t_request = -1;
2930 ctx->stats.t_queue = -1;
2931 ctx->stats.t_waiting = -1;
2932 ctx->stats.t_response = -1;
2933 ctx->stats.t_process = -1;
2934 ctx->stats.t_total = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002935}
2936
2937
2938/***************************************************************************
2939 * Hooks that manage the filter lifecycle (init/check/deinit)
2940 **************************************************************************/
2941/* Signal handler: Do a soft stop, wakeup SPOE applet */
2942static void
Christopher Faulet8ef75252017-02-20 22:56:03 +01002943spoe_sig_stop(struct sig_handler *sh)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002944{
2945 struct proxy *p;
2946
Olivier Houchardfbc74e82017-11-24 16:54:05 +01002947 p = proxies_list;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002948 while (p) {
2949 struct flt_conf *fconf;
2950
2951 list_for_each_entry(fconf, &p->filter_configs, list) {
Christopher Faulet3b386a32017-02-23 10:17:15 +01002952 struct spoe_config *conf;
2953 struct spoe_agent *agent;
Christopher Faulet42bfa462017-01-04 14:14:19 +01002954 struct spoe_appctx *spoe_appctx;
Christopher Faulet24289f22017-09-25 14:48:02 +02002955 int i;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002956
Christopher Faulet3b386a32017-02-23 10:17:15 +01002957 if (fconf->id != spoe_filter_id)
2958 continue;
2959
2960 conf = fconf->conf;
2961 agent = conf->agent;
2962
Christopher Faulet24289f22017-09-25 14:48:02 +02002963 for (i = 0; i < global.nbthread; ++i) {
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002964 HA_SPIN_LOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02002965 list_for_each_entry(spoe_appctx, &agent->rt[i].applets, list)
2966 spoe_wakeup_appctx(spoe_appctx->owner);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01002967 HA_SPIN_UNLOCK(SPOE_APPLET_LOCK, &agent->rt[i].lock);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002968 }
2969 }
2970 p = p->next;
2971 }
2972}
2973
2974
2975/* Initialize the SPOE filter. Returns -1 on error, else 0. */
2976static int
2977spoe_init(struct proxy *px, struct flt_conf *fconf)
2978{
2979 struct spoe_config *conf = fconf->conf;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002980
Christopher Faulet7250b8f2018-03-26 17:19:01 +02002981 /* conf->agent_fe was already initialized during the config
2982 * parsing. Finish initialization. */
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002983 conf->agent_fe.last_change = now.tv_sec;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002984 conf->agent_fe.cap = PR_CAP_FE;
2985 conf->agent_fe.mode = PR_MODE_TCP;
2986 conf->agent_fe.maxconn = 0;
2987 conf->agent_fe.options2 |= PR_O2_INDEPSTR;
2988 conf->agent_fe.conn_retries = CONN_RETRIES;
2989 conf->agent_fe.accept = frontend_accept;
2990 conf->agent_fe.srv = NULL;
2991 conf->agent_fe.timeout.client = TICK_ETERNITY;
2992 conf->agent_fe.default_target = &spoe_applet.obj_type;
2993 conf->agent_fe.fe_req_ana = AN_REQ_SWITCHING_RULES;
2994
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002995 if (!sighandler_registered) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01002996 signal_register_fct(0, spoe_sig_stop, 0);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02002997 sighandler_registered = 1;
2998 }
2999
3000 return 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003001}
3002
3003/* Free ressources allocated by the SPOE filter. */
3004static void
3005spoe_deinit(struct proxy *px, struct flt_conf *fconf)
3006{
3007 struct spoe_config *conf = fconf->conf;
3008
3009 if (conf) {
3010 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003011
Christopher Faulet8ef75252017-02-20 22:56:03 +01003012 spoe_release_agent(agent);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003013 free(conf->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003014 free(conf);
3015 }
3016 fconf->conf = NULL;
3017}
3018
3019/* Check configuration of a SPOE filter for a specified proxy.
3020 * Return 1 on error, else 0. */
3021static int
3022spoe_check(struct proxy *px, struct flt_conf *fconf)
3023{
Christopher Faulet7ee86672017-09-19 11:08:28 +02003024 struct flt_conf *f;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003025 struct spoe_config *conf = fconf->conf;
3026 struct proxy *target;
3027
Christopher Faulet7ee86672017-09-19 11:08:28 +02003028 /* Check all SPOE filters for proxy <px> to be sure all SPOE agent names
3029 * are uniq */
3030 list_for_each_entry(f, &px->filter_configs, list) {
3031 struct spoe_config *c = f->conf;
3032
3033 /* This is not an SPOE filter */
3034 if (f->id != spoe_filter_id)
3035 continue;
3036 /* This is the current SPOE filter */
3037 if (f == fconf)
3038 continue;
3039
3040 /* Check engine Id. It should be uniq */
3041 if (!strcmp(conf->id, c->id)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003042 ha_alert("Proxy %s : duplicated name for SPOE engine '%s'.\n",
3043 px->id, conf->id);
Christopher Faulet7ee86672017-09-19 11:08:28 +02003044 return 1;
3045 }
3046 }
3047
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003048 target = proxy_be_by_name(conf->agent->b.name);
3049 if (target == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003050 ha_alert("Proxy %s : unknown backend '%s' used by SPOE agent '%s'"
3051 " declared at %s:%d.\n",
3052 px->id, conf->agent->b.name, conf->agent->id,
3053 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003054 return 1;
3055 }
3056 if (target->mode != PR_MODE_TCP) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003057 ha_alert("Proxy %s : backend '%s' used by SPOE agent '%s' declared"
3058 " at %s:%d does not support HTTP mode.\n",
3059 px->id, target->id, conf->agent->id,
3060 conf->agent->conf.file, conf->agent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003061 return 1;
3062 }
3063
3064 free(conf->agent->b.name);
3065 conf->agent->b.name = NULL;
3066 conf->agent->b.be = target;
3067 return 0;
3068}
3069
3070/**************************************************************************
3071 * Hooks attached to a stream
3072 *************************************************************************/
3073/* Called when a filter instance is created and attach to a stream. It creates
3074 * the context that will be used to process this stream. */
3075static int
3076spoe_start(struct stream *s, struct filter *filter)
3077{
Christopher Faulet72bcc472017-01-04 16:39:41 +01003078 struct spoe_config *conf = FLT_CONF(filter);
3079 struct spoe_agent *agent = conf->agent;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003080 struct spoe_context *ctx;
3081
3082 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
Christopher Faulet72bcc472017-01-04 16:39:41 +01003083 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003084 __FUNCTION__, s);
3085
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003086 if ((ctx = spoe_create_context(s, filter)) == NULL) {
Christopher Faulet72bcc472017-01-04 16:39:41 +01003087 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
3088 " - failed to create SPOE context\n",
3089 (int)now.tv_sec, (int)now.tv_usec, agent->id,
Christopher Fauletccbc3fd2017-09-15 11:51:18 +02003090 __FUNCTION__, s);
Christopher Faulet3b8e3492018-03-26 17:20:58 +02003091 send_log(&conf->agent_fe, LOG_EMERG,
Christopher Faulet72bcc472017-01-04 16:39:41 +01003092 "SPOE: [%s] failed to create SPOE context\n",
3093 agent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003094 return 0;
3095 }
3096
Christopher Faulet11610f32017-09-21 10:23:10 +02003097 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003098 filter->pre_analyzers |= AN_REQ_INSPECT_FE;
3099
Christopher Faulet11610f32017-09-21 10:23:10 +02003100 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003101 filter->pre_analyzers |= AN_REQ_INSPECT_BE;
3102
Christopher Faulet11610f32017-09-21 10:23:10 +02003103 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_TCP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003104 filter->pre_analyzers |= AN_RES_INSPECT;
3105
Christopher Faulet11610f32017-09-21 10:23:10 +02003106 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_FE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003107 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_FE;
3108
Christopher Faulet11610f32017-09-21 10:23:10 +02003109 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_REQ_BE]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003110 filter->pre_analyzers |= AN_REQ_HTTP_PROCESS_BE;
3111
Christopher Faulet11610f32017-09-21 10:23:10 +02003112 if (!LIST_ISEMPTY(&ctx->events[SPOE_EV_ON_HTTP_RSP]))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003113 filter->pre_analyzers |= AN_RES_HTTP_PROCESS_FE;
3114
3115 return 1;
3116}
3117
3118/* Called when a filter instance is detached from a stream. It release the
3119 * attached SPOE context. */
3120static void
3121spoe_stop(struct stream *s, struct filter *filter)
3122{
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003123 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p\n",
3124 (int)now.tv_sec, (int)now.tv_usec,
3125 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3126 __FUNCTION__, s);
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003127 spoe_destroy_context(filter);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003128}
3129
Christopher Fauletf7a30922016-11-10 15:04:51 +01003130
3131/*
3132 * Called when the stream is woken up because of expired timer.
3133 */
3134static void
3135spoe_check_timeouts(struct stream *s, struct filter *filter)
3136{
3137 struct spoe_context *ctx = filter->ctx;
3138
Christopher Fauletac580602018-03-20 16:09:20 +01003139 if (tick_is_expired(ctx->process_exp, now_ms))
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003140 s->pending_events |= TASK_WOKEN_MSG;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003141}
3142
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003143/* Called when we are ready to filter data on a channel */
3144static int
3145spoe_start_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3146{
3147 struct spoe_context *ctx = filter->ctx;
3148 int ret = 1;
3149
3150 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3151 " - ctx-flags=0x%08x\n",
3152 (int)now.tv_sec, (int)now.tv_usec,
3153 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3154 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3155
Christopher Fauletb067b062017-01-04 16:39:11 +01003156 if (ctx->state == SPOE_CTX_ST_NONE)
3157 goto out;
3158
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003159 if (!(chn->flags & CF_ISRESP)) {
3160 if (filter->pre_analyzers & AN_REQ_INSPECT_FE)
3161 chn->analysers |= AN_REQ_INSPECT_FE;
3162 if (filter->pre_analyzers & AN_REQ_INSPECT_BE)
3163 chn->analysers |= AN_REQ_INSPECT_BE;
3164
3165 if (ctx->flags & SPOE_CTX_FL_CLI_CONNECTED)
3166 goto out;
3167
3168 ctx->stream_id = s->uniq_id;
Christopher Faulet8ef75252017-02-20 22:56:03 +01003169 ret = spoe_process_event(s, ctx, SPOE_EV_ON_CLIENT_SESS);
Christopher Fauletb067b062017-01-04 16:39:11 +01003170 if (!ret)
3171 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003172 ctx->flags |= SPOE_CTX_FL_CLI_CONNECTED;
3173 }
3174 else {
3175 if (filter->pre_analyzers & SPOE_EV_ON_TCP_RSP)
3176 chn->analysers |= AN_RES_INSPECT;
3177
3178 if (ctx->flags & SPOE_CTX_FL_SRV_CONNECTED)
3179 goto out;
3180
Christopher Faulet8ef75252017-02-20 22:56:03 +01003181 ret = spoe_process_event(s, ctx, SPOE_EV_ON_SERVER_SESS);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003182 if (!ret) {
3183 channel_dont_read(chn);
3184 channel_dont_close(chn);
Christopher Fauletb067b062017-01-04 16:39:11 +01003185 goto out;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003186 }
Christopher Fauletb067b062017-01-04 16:39:11 +01003187 ctx->flags |= SPOE_CTX_FL_SRV_CONNECTED;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003188 }
3189
3190 out:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003191 return ret;
3192}
3193
3194/* Called before a processing happens on a given channel */
3195static int
3196spoe_chn_pre_analyze(struct stream *s, struct filter *filter,
3197 struct channel *chn, unsigned an_bit)
3198{
3199 struct spoe_context *ctx = filter->ctx;
3200 int ret = 1;
3201
3202 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3203 " - ctx-flags=0x%08x - ana=0x%08x\n",
3204 (int)now.tv_sec, (int)now.tv_usec,
3205 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3206 __FUNCTION__, s, spoe_ctx_state_str[ctx->state],
3207 ctx->flags, an_bit);
3208
Christopher Fauletb067b062017-01-04 16:39:11 +01003209 if (ctx->state == SPOE_CTX_ST_NONE)
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003210 goto out;
3211
3212 switch (an_bit) {
3213 case AN_REQ_INSPECT_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003214 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003215 break;
3216 case AN_REQ_INSPECT_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003217 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003218 break;
3219 case AN_RES_INSPECT:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003220 ret = spoe_process_event(s, ctx, SPOE_EV_ON_TCP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003221 break;
3222 case AN_REQ_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003223 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_FE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003224 break;
3225 case AN_REQ_HTTP_PROCESS_BE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003226 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_REQ_BE);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003227 break;
3228 case AN_RES_HTTP_PROCESS_FE:
Christopher Faulet8ef75252017-02-20 22:56:03 +01003229 ret = spoe_process_event(s, ctx, SPOE_EV_ON_HTTP_RSP);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003230 break;
3231 }
3232
3233 out:
Christopher Faulet9cdca972018-02-01 08:45:45 +01003234 if (!ret && (chn->flags & CF_ISRESP)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003235 channel_dont_read(chn);
3236 channel_dont_close(chn);
3237 }
3238 return ret;
3239}
3240
3241/* Called when the filtering on the channel ends. */
3242static int
3243spoe_end_analyze(struct stream *s, struct filter *filter, struct channel *chn)
3244{
3245 struct spoe_context *ctx = filter->ctx;
3246
3247 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p - ctx-state=%s"
3248 " - ctx-flags=0x%08x\n",
3249 (int)now.tv_sec, (int)now.tv_usec,
3250 ((struct spoe_config *)FLT_CONF(filter))->agent->id,
3251 __FUNCTION__, s, spoe_ctx_state_str[ctx->state], ctx->flags);
3252
3253 if (!(ctx->flags & SPOE_CTX_FL_PROCESS)) {
Christopher Faulet8ef75252017-02-20 22:56:03 +01003254 spoe_reset_context(ctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003255 }
3256
3257 return 1;
3258}
3259
3260/********************************************************************
3261 * Functions that manage the filter initialization
3262 ********************************************************************/
3263struct flt_ops spoe_ops = {
3264 /* Manage SPOE filter, called for each filter declaration */
3265 .init = spoe_init,
3266 .deinit = spoe_deinit,
3267 .check = spoe_check,
3268
3269 /* Handle start/stop of SPOE */
Christopher Fauletf7a30922016-11-10 15:04:51 +01003270 .attach = spoe_start,
3271 .detach = spoe_stop,
3272 .check_timeouts = spoe_check_timeouts,
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003273
3274 /* Handle channels activity */
3275 .channel_start_analyze = spoe_start_analyze,
3276 .channel_pre_analyze = spoe_chn_pre_analyze,
3277 .channel_end_analyze = spoe_end_analyze,
3278};
3279
3280
3281static int
3282cfg_parse_spoe_agent(const char *file, int linenum, char **args, int kwm)
3283{
3284 const char *err;
3285 int i, err_code = 0;
3286
3287 if ((cfg_scope == NULL && curengine != NULL) ||
3288 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003289 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003290 goto out;
3291
3292 if (!strcmp(args[0], "spoe-agent")) { /* new spoe-agent section */
3293 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003294 ha_alert("parsing [%s:%d] : missing name for spoe-agent section.\n",
3295 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003296 err_code |= ERR_ALERT | ERR_ABORT;
3297 goto out;
3298 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003299 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3300 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003301 goto out;
3302 }
3303
3304 err = invalid_char(args[1]);
3305 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003306 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3307 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003308 err_code |= ERR_ALERT | ERR_ABORT;
3309 goto out;
3310 }
3311
3312 if (curagent != NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003313 ha_alert("parsing [%s:%d] : another spoe-agent section previously defined.\n",
3314 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003315 err_code |= ERR_ALERT | ERR_ABORT;
3316 goto out;
3317 }
3318 if ((curagent = calloc(1, sizeof(*curagent))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003319 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003320 err_code |= ERR_ALERT | ERR_ABORT;
3321 goto out;
3322 }
3323
3324 curagent->id = strdup(args[1]);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003325
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003326 curagent->conf.file = strdup(file);
3327 curagent->conf.line = linenum;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003328
3329 curagent->timeout.hello = TICK_ETERNITY;
3330 curagent->timeout.idle = TICK_ETERNITY;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003331 curagent->timeout.processing = TICK_ETERNITY;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003332
3333 curagent->engine_id = NULL;
3334 curagent->var_pfx = NULL;
3335 curagent->var_on_error = NULL;
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003336 curagent->var_t_process = NULL;
3337 curagent->var_t_total = NULL;
Christopher Faulet24289f22017-09-25 14:48:02 +02003338 curagent->flags = (SPOE_FL_PIPELINING | SPOE_FL_SND_FRAGMENTATION);
3339 if (global.nbthread == 1)
3340 curagent->flags |= SPOE_FL_ASYNC;
Christopher Fauleta1cda022016-12-21 08:58:06 +01003341 curagent->cps_max = 0;
3342 curagent->eps_max = 0;
Christopher Faulet7aa0b2b2017-01-13 11:30:50 +01003343 curagent->max_frame_size = MAX_FRAME_SIZE;
Christopher Fauletb077cdc2018-01-24 16:37:57 +01003344 curagent->max_fpa = 20;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003345
3346 for (i = 0; i < SPOE_EV_EVENTS; ++i)
Christopher Faulet11610f32017-09-21 10:23:10 +02003347 LIST_INIT(&curagent->events[i]);
3348 LIST_INIT(&curagent->groups);
3349 LIST_INIT(&curagent->messages);
Christopher Fauleta1cda022016-12-21 08:58:06 +01003350
Christopher Faulet24289f22017-09-25 14:48:02 +02003351 if ((curagent->rt = calloc(global.nbthread, sizeof(*curagent->rt))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003352 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003353 err_code |= ERR_ALERT | ERR_ABORT;
3354 goto out;
3355 }
3356 for (i = 0; i < global.nbthread; ++i) {
3357 curagent->rt[i].frame_size = curagent->max_frame_size;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01003358 curagent->rt[i].processing = 0;
Christopher Faulet24289f22017-09-25 14:48:02 +02003359 LIST_INIT(&curagent->rt[i].applets);
3360 LIST_INIT(&curagent->rt[i].sending_queue);
3361 LIST_INIT(&curagent->rt[i].waiting_queue);
Christopher Faulet2a944ee2017-11-07 10:42:54 +01003362 HA_SPIN_INIT(&curagent->rt[i].lock);
Christopher Faulet24289f22017-09-25 14:48:02 +02003363 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003364 }
3365 else if (!strcmp(args[0], "use-backend")) {
3366 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003367 ha_alert("parsing [%s:%d] : '%s' expects a backend name.\n",
3368 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003369 err_code |= ERR_ALERT | ERR_FATAL;
3370 goto out;
3371 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003372 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003373 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003374 free(curagent->b.name);
3375 curagent->b.name = strdup(args[1]);
3376 }
3377 else if (!strcmp(args[0], "messages")) {
3378 int cur_arg = 1;
3379 while (*args[cur_arg]) {
Christopher Faulet11610f32017-09-21 10:23:10 +02003380 struct spoe_placeholder *ph = NULL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003381
Christopher Faulet11610f32017-09-21 10:23:10 +02003382 list_for_each_entry(ph, &curmphs, list) {
3383 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003384 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3385 file, linenum, args[cur_arg]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003386 err_code |= ERR_ALERT | ERR_FATAL;
3387 goto out;
3388 }
3389 }
3390
Christopher Faulet11610f32017-09-21 10:23:10 +02003391 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003392 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003393 err_code |= ERR_ALERT | ERR_ABORT;
3394 goto out;
3395 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003396 ph->id = strdup(args[cur_arg]);
3397 LIST_ADDQ(&curmphs, &ph->list);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003398 cur_arg++;
3399 }
3400 }
Christopher Faulet11610f32017-09-21 10:23:10 +02003401 else if (!strcmp(args[0], "groups")) {
3402 int cur_arg = 1;
3403 while (*args[cur_arg]) {
3404 struct spoe_placeholder *ph = NULL;
3405
3406 list_for_each_entry(ph, &curgphs, list) {
3407 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003408 ha_alert("parsing [%s:%d]: spoe-group '%s' already used.\n",
3409 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003410 err_code |= ERR_ALERT | ERR_FATAL;
3411 goto out;
3412 }
3413 }
3414
3415 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003416 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003417 err_code |= ERR_ALERT | ERR_ABORT;
3418 goto out;
3419 }
3420 ph->id = strdup(args[cur_arg]);
3421 LIST_ADDQ(&curgphs, &ph->list);
3422 cur_arg++;
3423 }
3424 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003425 else if (!strcmp(args[0], "timeout")) {
3426 unsigned int *tv = NULL;
3427 const char *res;
3428 unsigned timeout;
3429
3430 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003431 ha_alert("parsing [%s:%d] : 'timeout' expects 'hello', 'idle' and 'processing'.\n",
3432 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003433 err_code |= ERR_ALERT | ERR_FATAL;
3434 goto out;
3435 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003436 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3437 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003438 if (!strcmp(args[1], "hello"))
3439 tv = &curagent->timeout.hello;
3440 else if (!strcmp(args[1], "idle"))
3441 tv = &curagent->timeout.idle;
Christopher Fauletf7a30922016-11-10 15:04:51 +01003442 else if (!strcmp(args[1], "processing"))
3443 tv = &curagent->timeout.processing;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003444 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003445 ha_alert("parsing [%s:%d] : 'timeout' supports 'hello', 'idle' or 'processing' (got %s).\n",
3446 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003447 err_code |= ERR_ALERT | ERR_FATAL;
3448 goto out;
3449 }
3450 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003451 ha_alert("parsing [%s:%d] : 'timeout %s' expects an integer value (in milliseconds).\n",
3452 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003453 err_code |= ERR_ALERT | ERR_FATAL;
3454 goto out;
3455 }
3456 res = parse_time_err(args[2], &timeout, TIME_UNIT_MS);
3457 if (res) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003458 ha_alert("parsing [%s:%d] : unexpected character '%c' in 'timeout %s'.\n",
3459 file, linenum, *res, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003460 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003461 goto out;
3462 }
3463 *tv = MS_TO_TICKS(timeout);
3464 }
3465 else if (!strcmp(args[0], "option")) {
3466 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003467 ha_alert("parsing [%s:%d]: '%s' expects an option name.\n",
3468 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003469 err_code |= ERR_ALERT | ERR_FATAL;
3470 goto out;
3471 }
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003472
Christopher Faulet305c6072017-02-23 16:17:53 +01003473 if (!strcmp(args[1], "pipelining")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003474 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003475 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003476 if (kwm == 1)
3477 curagent->flags &= ~SPOE_FL_PIPELINING;
3478 else
3479 curagent->flags |= SPOE_FL_PIPELINING;
3480 goto out;
3481 }
3482 else if (!strcmp(args[1], "async")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003483 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet305c6072017-02-23 16:17:53 +01003484 goto out;
Christopher Faulet305c6072017-02-23 16:17:53 +01003485 if (kwm == 1)
3486 curagent->flags &= ~SPOE_FL_ASYNC;
Christopher Faulet24289f22017-09-25 14:48:02 +02003487 else {
3488 if (global.nbthread == 1)
3489 curagent->flags |= SPOE_FL_ASYNC;
3490 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003491 ha_warning("parsing [%s:%d] Async option is not supported with threads.\n",
3492 file, linenum);
Christopher Faulet24289f22017-09-25 14:48:02 +02003493 err_code |= ERR_WARN;
3494 }
3495 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003496 goto out;
3497 }
Christopher Fauletcecd8522017-02-24 22:11:21 +01003498 else if (!strcmp(args[1], "send-frag-payload")) {
3499 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3500 goto out;
3501 if (kwm == 1)
3502 curagent->flags &= ~SPOE_FL_SND_FRAGMENTATION;
3503 else
3504 curagent->flags |= SPOE_FL_SND_FRAGMENTATION;
3505 goto out;
3506 }
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003507 else if (!strcmp(args[1], "dontlog-normal")) {
3508 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3509 goto out;
3510 if (kwm == 1)
3511 curpxopts2 &= ~PR_O2_NOLOGNORM;
3512 else
3513 curpxopts2 |= PR_O2_NOLOGNORM;
Christopher Faulet799f5182018-04-26 11:36:34 +02003514 goto out;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02003515 }
Christopher Faulet305c6072017-02-23 16:17:53 +01003516
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003517 /* Following options does not support negation */
3518 if (kwm == 1) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003519 ha_alert("parsing [%s:%d]: negation is not supported for option '%s'.\n",
3520 file, linenum, args[1]);
Christopher Faulet6a2940c2017-02-23 15:06:26 +01003521 err_code |= ERR_ALERT | ERR_FATAL;
3522 goto out;
3523 }
3524
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003525 if (!strcmp(args[1], "var-prefix")) {
3526 char *tmp;
3527
3528 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003529 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3530 file, linenum, args[0],
3531 args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003532 err_code |= ERR_ALERT | ERR_FATAL;
3533 goto out;
3534 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003535 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3536 goto out;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003537 tmp = args[2];
3538 while (*tmp) {
3539 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003540 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003541 file, linenum, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003542 err_code |= ERR_ALERT | ERR_FATAL;
3543 goto out;
3544 }
3545 tmp++;
3546 }
3547 curagent->var_pfx = strdup(args[2]);
3548 }
Etienne Carriereaec89892017-12-14 09:36:40 +00003549 else if (!strcmp(args[1], "force-set-var")) {
3550 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3551 goto out;
3552 curagent->flags |= SPOE_FL_FORCE_SET_VAR;
3553 }
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003554 else if (!strcmp(args[1], "continue-on-error")) {
Christopher Fauletecc537a2017-02-23 22:52:39 +01003555 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003556 goto out;
Christopher Fauletea62c2a2016-11-14 10:54:21 +01003557 curagent->flags |= SPOE_FL_CONT_ON_ERR;
3558 }
Christopher Faulet985532d2016-11-16 15:36:19 +01003559 else if (!strcmp(args[1], "set-on-error")) {
3560 char *tmp;
3561
3562 if (!*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003563 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3564 file, linenum, args[0],
3565 args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003566 err_code |= ERR_ALERT | ERR_FATAL;
3567 goto out;
3568 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003569 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3570 goto out;
Christopher Faulet985532d2016-11-16 15:36:19 +01003571 tmp = args[2];
3572 while (*tmp) {
3573 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003574 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet767a84b2017-11-24 16:50:31 +01003575 file, linenum, args[0], args[1]);
Christopher Faulet985532d2016-11-16 15:36:19 +01003576 err_code |= ERR_ALERT | ERR_FATAL;
3577 goto out;
3578 }
3579 tmp++;
3580 }
3581 curagent->var_on_error = strdup(args[2]);
3582 }
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003583 else if (!strcmp(args[1], "set-process-time")) {
3584 char *tmp;
3585
3586 if (!*args[2]) {
3587 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3588 file, linenum, args[0],
3589 args[1]);
3590 err_code |= ERR_ALERT | ERR_FATAL;
3591 goto out;
3592 }
3593 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3594 goto out;
3595 tmp = args[2];
3596 while (*tmp) {
3597 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003598 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003599 file, linenum, args[0], args[1]);
3600 err_code |= ERR_ALERT | ERR_FATAL;
3601 goto out;
3602 }
3603 tmp++;
3604 }
3605 curagent->var_t_process = strdup(args[2]);
3606 }
3607 else if (!strcmp(args[1], "set-total-time")) {
3608 char *tmp;
3609
3610 if (!*args[2]) {
3611 ha_alert("parsing [%s:%d]: '%s %s' expects a value.\n",
3612 file, linenum, args[0],
3613 args[1]);
3614 err_code |= ERR_ALERT | ERR_FATAL;
3615 goto out;
3616 }
3617 if (alertif_too_many_args(2, file, linenum, args, &err_code))
3618 goto out;
3619 tmp = args[2];
3620 while (*tmp) {
3621 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
Thierry FOURNIER01a3f202018-05-10 16:41:26 +02003622 ha_alert("parsing [%s:%d]: '%s %s' only supports [a-zA-Z0-9_.] chars.\n",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01003623 file, linenum, args[0], args[1]);
3624 err_code |= ERR_ALERT | ERR_FATAL;
3625 goto out;
3626 }
3627 tmp++;
3628 }
3629 curagent->var_t_total = strdup(args[2]);
3630 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003631 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003632 ha_alert("parsing [%s:%d]: option '%s' is not supported.\n",
3633 file, linenum, args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003634 err_code |= ERR_ALERT | ERR_FATAL;
3635 goto out;
3636 }
Christopher Faulet48026722016-11-16 15:01:12 +01003637 }
3638 else if (!strcmp(args[0], "maxconnrate")) {
3639 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003640 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3641 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003642 err_code |= ERR_ALERT | ERR_FATAL;
3643 goto out;
3644 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003645 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003646 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003647 curagent->cps_max = atol(args[1]);
3648 }
3649 else if (!strcmp(args[0], "maxerrrate")) {
3650 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003651 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3652 file, linenum, args[0]);
Christopher Faulet48026722016-11-16 15:01:12 +01003653 err_code |= ERR_ALERT | ERR_FATAL;
3654 goto out;
3655 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003656 if (alertif_too_many_args(1, file, linenum, args, &err_code))
Christopher Faulet48026722016-11-16 15:01:12 +01003657 goto out;
Christopher Faulet48026722016-11-16 15:01:12 +01003658 curagent->eps_max = atol(args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003659 }
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003660 else if (!strcmp(args[0], "max-frame-size")) {
3661 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003662 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3663 file, linenum, args[0]);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003664 err_code |= ERR_ALERT | ERR_FATAL;
3665 goto out;
3666 }
3667 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3668 goto out;
3669 curagent->max_frame_size = atol(args[1]);
3670 if (curagent->max_frame_size < MIN_FRAME_SIZE ||
3671 curagent->max_frame_size > MAX_FRAME_SIZE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003672 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument in the range [%d, %d].\n",
3673 file, linenum, args[0], MIN_FRAME_SIZE, MAX_FRAME_SIZE);
Christopher Faulet2eca6b52017-02-27 09:40:34 +01003674 err_code |= ERR_ALERT | ERR_FATAL;
3675 goto out;
3676 }
3677 }
Christopher Faulete8ade382018-01-25 15:32:22 +01003678 else if (!strcmp(args[0], "max-waiting-frames")) {
3679 if (!*args[1]) {
3680 ha_alert("parsing [%s:%d] : '%s' expects an integer argument.\n",
3681 file, linenum, args[0]);
3682 err_code |= ERR_ALERT | ERR_FATAL;
3683 goto out;
3684 }
3685 if (alertif_too_many_args(1, file, linenum, args, &err_code))
3686 goto out;
3687 curagent->max_fpa = atol(args[1]);
3688 if (curagent->max_fpa < 1) {
3689 ha_alert("parsing [%s:%d] : '%s' expects a positive integer argument.\n",
3690 file, linenum, args[0]);
3691 err_code |= ERR_ALERT | ERR_FATAL;
3692 goto out;
3693 }
3694 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01003695 else if (!strcmp(args[0], "register-var-names")) {
3696 int cur_arg;
3697
3698 if (!*args[1]) {
3699 ha_alert("parsing [%s:%d] : '%s' expects one or more variable names.\n",
3700 file, linenum, args[0]);
3701 err_code |= ERR_ALERT | ERR_FATAL;
3702 goto out;
3703 }
3704 cur_arg = 1;
3705 while (*args[cur_arg]) {
3706 struct spoe_var_placeholder *vph;
3707
3708 if ((vph = calloc(1, sizeof(*vph))) == NULL) {
3709 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3710 err_code |= ERR_ALERT | ERR_ABORT;
3711 goto out;
3712 }
3713 if ((vph->name = strdup(args[cur_arg])) == NULL) {
3714 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
3715 err_code |= ERR_ALERT | ERR_ABORT;
3716 goto out;
3717 }
3718 LIST_ADDQ(&curvars, &vph->list);
3719 cur_arg++;
3720 }
3721 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02003722 else if (!strcmp(args[0], "log")) {
3723 char *errmsg = NULL;
3724
3725 if (!parse_logsrv(args, &curlogsrvs, (kwm == 1), &errmsg)) {
3726 ha_alert("parsing [%s:%d] : %s : %s\n", file, linenum, args[0], errmsg);
3727 err_code |= ERR_ALERT | ERR_FATAL;
3728 goto out;
3729 }
3730 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003731 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003732 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-agent section.\n",
3733 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003734 err_code |= ERR_ALERT | ERR_FATAL;
3735 goto out;
3736 }
3737 out:
3738 return err_code;
3739}
Christopher Faulet11610f32017-09-21 10:23:10 +02003740static int
3741cfg_parse_spoe_group(const char *file, int linenum, char **args, int kwm)
3742{
3743 struct spoe_group *grp;
3744 const char *err;
3745 int err_code = 0;
3746
3747 if ((cfg_scope == NULL && curengine != NULL) ||
3748 (cfg_scope != NULL && curengine == NULL) ||
3749 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
3750 goto out;
3751
3752 if (!strcmp(args[0], "spoe-group")) { /* new spoe-group section */
3753 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003754 ha_alert("parsing [%s:%d] : missing name for spoe-group section.\n",
3755 file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003756 err_code |= ERR_ALERT | ERR_ABORT;
3757 goto out;
3758 }
3759 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3760 err_code |= ERR_ABORT;
3761 goto out;
3762 }
3763
3764 err = invalid_char(args[1]);
3765 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003766 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3767 file, linenum, *err, args[0], args[1]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003768 err_code |= ERR_ALERT | ERR_ABORT;
3769 goto out;
3770 }
3771
3772 list_for_each_entry(grp, &curgrps, list) {
3773 if (!strcmp(grp->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003774 ha_alert("parsing [%s:%d]: spoe-group section '%s' has the same"
3775 " name as another one declared at %s:%d.\n",
3776 file, linenum, args[1], grp->conf.file, grp->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02003777 err_code |= ERR_ALERT | ERR_FATAL;
3778 goto out;
3779 }
3780 }
3781
3782 if ((curgrp = calloc(1, sizeof(*curgrp))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003783 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003784 err_code |= ERR_ALERT | ERR_ABORT;
3785 goto out;
3786 }
3787
3788 curgrp->id = strdup(args[1]);
3789 curgrp->conf.file = strdup(file);
3790 curgrp->conf.line = linenum;
3791 LIST_INIT(&curgrp->phs);
3792 LIST_INIT(&curgrp->messages);
3793 LIST_ADDQ(&curgrps, &curgrp->list);
3794 }
3795 else if (!strcmp(args[0], "messages")) {
3796 int cur_arg = 1;
3797 while (*args[cur_arg]) {
3798 struct spoe_placeholder *ph = NULL;
3799
3800 list_for_each_entry(ph, &curgrp->phs, list) {
3801 if (!strcmp(ph->id, args[cur_arg])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003802 ha_alert("parsing [%s:%d]: spoe-message '%s' already used.\n",
3803 file, linenum, args[cur_arg]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003804 err_code |= ERR_ALERT | ERR_FATAL;
3805 goto out;
3806 }
3807 }
3808
3809 if ((ph = calloc(1, sizeof(*ph))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003810 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Faulet11610f32017-09-21 10:23:10 +02003811 err_code |= ERR_ALERT | ERR_ABORT;
3812 goto out;
3813 }
3814 ph->id = strdup(args[cur_arg]);
3815 LIST_ADDQ(&curgrp->phs, &ph->list);
3816 cur_arg++;
3817 }
3818 }
3819 else if (*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003820 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-group section.\n",
3821 file, linenum, args[0]);
Christopher Faulet11610f32017-09-21 10:23:10 +02003822 err_code |= ERR_ALERT | ERR_FATAL;
3823 goto out;
3824 }
3825 out:
3826 return err_code;
3827}
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003828
3829static int
3830cfg_parse_spoe_message(const char *file, int linenum, char **args, int kwm)
3831{
3832 struct spoe_message *msg;
3833 struct spoe_arg *arg;
3834 const char *err;
3835 char *errmsg = NULL;
3836 int err_code = 0;
3837
3838 if ((cfg_scope == NULL && curengine != NULL) ||
3839 (cfg_scope != NULL && curengine == NULL) ||
Christopher Faulete1405e52017-09-19 10:35:35 +02003840 (curengine != NULL && cfg_scope != NULL && strcmp(curengine, cfg_scope)))
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003841 goto out;
3842
3843 if (!strcmp(args[0], "spoe-message")) { /* new spoe-message section */
3844 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003845 ha_alert("parsing [%s:%d] : missing name for spoe-message section.\n",
3846 file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003847 err_code |= ERR_ALERT | ERR_ABORT;
3848 goto out;
3849 }
Christopher Fauletecc537a2017-02-23 22:52:39 +01003850 if (alertif_too_many_args(1, file, linenum, args, &err_code)) {
3851 err_code |= ERR_ABORT;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003852 goto out;
3853 }
3854
3855 err = invalid_char(args[1]);
3856 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003857 ha_alert("parsing [%s:%d] : character '%c' is not permitted in '%s' name '%s'.\n",
3858 file, linenum, *err, args[0], args[1]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003859 err_code |= ERR_ALERT | ERR_ABORT;
3860 goto out;
3861 }
3862
3863 list_for_each_entry(msg, &curmsgs, list) {
3864 if (!strcmp(msg->id, args[1])) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003865 ha_alert("parsing [%s:%d]: spoe-message section '%s' has the same"
3866 " name as another one declared at %s:%d.\n",
3867 file, linenum, args[1], msg->conf.file, msg->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003868 err_code |= ERR_ALERT | ERR_FATAL;
3869 goto out;
3870 }
3871 }
3872
3873 if ((curmsg = calloc(1, sizeof(*curmsg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003874 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003875 err_code |= ERR_ALERT | ERR_ABORT;
3876 goto out;
3877 }
3878
3879 curmsg->id = strdup(args[1]);
3880 curmsg->id_len = strlen(curmsg->id);
3881 curmsg->event = SPOE_EV_NONE;
3882 curmsg->conf.file = strdup(file);
3883 curmsg->conf.line = linenum;
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003884 curmsg->nargs = 0;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003885 LIST_INIT(&curmsg->args);
Christopher Faulet57583e42017-09-04 15:41:09 +02003886 LIST_INIT(&curmsg->acls);
Christopher Faulet11610f32017-09-21 10:23:10 +02003887 LIST_INIT(&curmsg->by_evt);
3888 LIST_INIT(&curmsg->by_grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003889 LIST_ADDQ(&curmsgs, &curmsg->list);
3890 }
3891 else if (!strcmp(args[0], "args")) {
3892 int cur_arg = 1;
3893
3894 curproxy->conf.args.ctx = ARGC_SPOE;
3895 curproxy->conf.args.file = file;
3896 curproxy->conf.args.line = linenum;
3897 while (*args[cur_arg]) {
3898 char *delim = strchr(args[cur_arg], '=');
3899 int idx = 0;
3900
3901 if ((arg = calloc(1, sizeof(*arg))) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003902 ha_alert("parsing [%s:%d] : out of memory.\n", file, linenum);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003903 err_code |= ERR_ALERT | ERR_ABORT;
3904 goto out;
3905 }
3906
3907 if (!delim) {
3908 arg->name = NULL;
3909 arg->name_len = 0;
3910 delim = args[cur_arg];
3911 }
3912 else {
3913 arg->name = my_strndup(args[cur_arg], delim - args[cur_arg]);
3914 arg->name_len = delim - args[cur_arg];
3915 delim++;
3916 }
Christopher Fauletb0b42382017-02-23 22:41:09 +01003917 arg->expr = sample_parse_expr((char*[]){delim, NULL},
3918 &idx, file, linenum, &errmsg,
3919 &curproxy->conf.args);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003920 if (arg->expr == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003921 ha_alert("parsing [%s:%d] : '%s': %s.\n", file, linenum, args[0], errmsg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003922 err_code |= ERR_ALERT | ERR_FATAL;
3923 free(arg->name);
3924 free(arg);
3925 goto out;
3926 }
Christopher Fauletf51f5fa2017-01-19 10:01:12 +01003927 curmsg->nargs++;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003928 LIST_ADDQ(&curmsg->args, &arg->list);
3929 cur_arg++;
3930 }
3931 curproxy->conf.args.file = NULL;
3932 curproxy->conf.args.line = 0;
3933 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003934 else if (!strcmp(args[0], "acl")) {
3935 err = invalid_char(args[1]);
3936 if (err) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003937 ha_alert("parsing [%s:%d] : character '%c' is not permitted in acl name '%s'.\n",
3938 file, linenum, *err, args[1]);
Christopher Faulet57583e42017-09-04 15:41:09 +02003939 err_code |= ERR_ALERT | ERR_FATAL;
3940 goto out;
3941 }
3942 if (parse_acl((const char **)args + 1, &curmsg->acls, &errmsg, &curproxy->conf.args, file, linenum) == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003943 ha_alert("parsing [%s:%d] : error detected while parsing ACL '%s' : %s.\n",
3944 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003945 err_code |= ERR_ALERT | ERR_FATAL;
3946 goto out;
3947 }
3948 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003949 else if (!strcmp(args[0], "event")) {
3950 if (!*args[1]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003951 ha_alert("parsing [%s:%d] : missing event name.\n", file, linenum);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003952 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003953 goto out;
3954 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003955 /* if (alertif_too_many_args(1, file, linenum, args, &err_code)) */
3956 /* goto out; */
Christopher Fauletecc537a2017-02-23 22:52:39 +01003957
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003958 if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_CLIENT_SESS]))
3959 curmsg->event = SPOE_EV_ON_CLIENT_SESS;
3960 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_SERVER_SESS]))
3961 curmsg->event = SPOE_EV_ON_SERVER_SESS;
3962
3963 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_FE]))
3964 curmsg->event = SPOE_EV_ON_TCP_REQ_FE;
3965 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_REQ_BE]))
3966 curmsg->event = SPOE_EV_ON_TCP_REQ_BE;
3967 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_TCP_RSP]))
3968 curmsg->event = SPOE_EV_ON_TCP_RSP;
3969
3970 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_FE]))
3971 curmsg->event = SPOE_EV_ON_HTTP_REQ_FE;
3972 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_REQ_BE]))
3973 curmsg->event = SPOE_EV_ON_HTTP_REQ_BE;
3974 else if (!strcmp(args[1], spoe_event_str[SPOE_EV_ON_HTTP_RSP]))
3975 curmsg->event = SPOE_EV_ON_HTTP_RSP;
3976 else {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003977 ha_alert("parsing [%s:%d] : unkown event '%s'.\n",
3978 file, linenum, args[1]);
Christopher Fauletecc537a2017-02-23 22:52:39 +01003979 err_code |= ERR_ALERT | ERR_FATAL;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02003980 goto out;
3981 }
Christopher Faulet57583e42017-09-04 15:41:09 +02003982
3983 if (strcmp(args[2], "if") == 0 || strcmp(args[2], "unless") == 0) {
3984 struct acl_cond *cond;
3985
3986 cond = build_acl_cond(file, linenum, &curmsg->acls,
3987 curproxy, (const char **)args+2,
3988 &errmsg);
3989 if (cond == NULL) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003990 ha_alert("parsing [%s:%d] : error detected while "
3991 "parsing an 'event %s' condition : %s.\n",
3992 file, linenum, args[1], errmsg);
Christopher Faulet57583e42017-09-04 15:41:09 +02003993 err_code |= ERR_ALERT | ERR_FATAL;
3994 goto out;
3995 }
3996 curmsg->cond = cond;
3997 }
3998 else if (*args[2]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01003999 ha_alert("parsing [%s:%d]: 'event %s' expects either 'if' "
4000 "or 'unless' followed by a condition but found '%s'.\n",
4001 file, linenum, args[1], args[2]);
Christopher Faulet57583e42017-09-04 15:41:09 +02004002 err_code |= ERR_ALERT | ERR_FATAL;
4003 goto out;
4004 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004005 }
4006 else if (!*args[0]) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004007 ha_alert("parsing [%s:%d] : unknown keyword '%s' in spoe-message section.\n",
4008 file, linenum, args[0]);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004009 err_code |= ERR_ALERT | ERR_FATAL;
4010 goto out;
4011 }
4012 out:
4013 free(errmsg);
4014 return err_code;
4015}
4016
4017/* Return -1 on error, else 0 */
4018static int
4019parse_spoe_flt(char **args, int *cur_arg, struct proxy *px,
4020 struct flt_conf *fconf, char **err, void *private)
4021{
4022 struct list backup_sections;
4023 struct spoe_config *conf;
4024 struct spoe_message *msg, *msgback;
Christopher Faulet11610f32017-09-21 10:23:10 +02004025 struct spoe_group *grp, *grpback;
4026 struct spoe_placeholder *ph, *phback;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004027 struct spoe_var_placeholder *vph, *vphback;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004028 struct logsrv *logsrv, *logsrvback;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004029 char *file = NULL, *engine = NULL;
4030 int ret, pos = *cur_arg + 1;
4031
Christopher Faulet84c844e2018-03-23 14:37:14 +01004032 LIST_INIT(&curmsgs);
4033 LIST_INIT(&curgrps);
4034 LIST_INIT(&curmphs);
4035 LIST_INIT(&curgphs);
4036 LIST_INIT(&curvars);
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004037 LIST_INIT(&curlogsrvs);
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004038 curpxopts = 0;
4039 curpxopts2 = 0;
Christopher Faulet84c844e2018-03-23 14:37:14 +01004040
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004041 conf = calloc(1, sizeof(*conf));
4042 if (conf == NULL) {
4043 memprintf(err, "%s: out of memory", args[*cur_arg]);
4044 goto error;
4045 }
4046 conf->proxy = px;
4047
4048 while (*args[pos]) {
4049 if (!strcmp(args[pos], "config")) {
4050 if (!*args[pos+1]) {
4051 memprintf(err, "'%s' : '%s' option without value",
4052 args[*cur_arg], args[pos]);
4053 goto error;
4054 }
4055 file = args[pos+1];
4056 pos += 2;
4057 }
4058 else if (!strcmp(args[pos], "engine")) {
4059 if (!*args[pos+1]) {
4060 memprintf(err, "'%s' : '%s' option without value",
4061 args[*cur_arg], args[pos]);
4062 goto error;
4063 }
4064 engine = args[pos+1];
4065 pos += 2;
4066 }
4067 else {
4068 memprintf(err, "unknown keyword '%s'", args[pos]);
4069 goto error;
4070 }
4071 }
4072 if (file == NULL) {
4073 memprintf(err, "'%s' : missing config file", args[*cur_arg]);
4074 goto error;
4075 }
4076
4077 /* backup sections and register SPOE sections */
4078 LIST_INIT(&backup_sections);
4079 cfg_backup_sections(&backup_sections);
Christopher Faulet11610f32017-09-21 10:23:10 +02004080 cfg_register_section("spoe-agent", cfg_parse_spoe_agent, NULL);
4081 cfg_register_section("spoe-group", cfg_parse_spoe_group, NULL);
William Lallemandd2ff56d2017-10-16 11:06:50 +02004082 cfg_register_section("spoe-message", cfg_parse_spoe_message, NULL);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004083
4084 /* Parse SPOE filter configuration file */
4085 curengine = engine;
4086 curproxy = px;
4087 curagent = NULL;
4088 curmsg = NULL;
4089 ret = readcfgfile(file);
4090 curproxy = NULL;
4091
4092 /* unregister SPOE sections and restore previous sections */
4093 cfg_unregister_sections();
4094 cfg_restore_sections(&backup_sections);
4095
4096 if (ret == -1) {
4097 memprintf(err, "Could not open configuration file %s : %s",
4098 file, strerror(errno));
4099 goto error;
4100 }
4101 if (ret & (ERR_ABORT|ERR_FATAL)) {
4102 memprintf(err, "Error(s) found in configuration file %s", file);
4103 goto error;
4104 }
4105
4106 /* Check SPOE agent */
4107 if (curagent == NULL) {
4108 memprintf(err, "No SPOE agent found in file %s", file);
4109 goto error;
4110 }
4111 if (curagent->b.name == NULL) {
4112 memprintf(err, "No backend declared for SPOE agent '%s' declared at %s:%d",
4113 curagent->id, curagent->conf.file, curagent->conf.line);
4114 goto error;
4115 }
Christopher Fauletf7a30922016-11-10 15:04:51 +01004116 if (curagent->timeout.hello == TICK_ETERNITY ||
4117 curagent->timeout.idle == TICK_ETERNITY ||
Christopher Fauletf7a30922016-11-10 15:04:51 +01004118 curagent->timeout.processing == TICK_ETERNITY) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004119 ha_warning("Proxy '%s': missing timeouts for SPOE agent '%s' declare at %s:%d.\n"
4120 " | While not properly invalid, you will certainly encounter various problems\n"
4121 " | with such a configuration. To fix this, please ensure that all following\n"
4122 " | timeouts are set to a non-zero value: 'hello', 'idle', 'processing'.\n",
4123 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004124 }
4125 if (curagent->var_pfx == NULL) {
4126 char *tmp = curagent->id;
4127
4128 while (*tmp) {
4129 if (!isalnum(*tmp) && *tmp != '_' && *tmp != '.') {
4130 memprintf(err, "Invalid variable prefix '%s' for SPOE agent '%s' declared at %s:%d. "
4131 "Use 'option var-prefix' to set it. Only [a-zA-Z0-9_.] chars are supported.\n",
4132 curagent->id, curagent->id, curagent->conf.file, curagent->conf.line);
4133 goto error;
4134 }
4135 tmp++;
4136 }
4137 curagent->var_pfx = strdup(curagent->id);
4138 }
Christopher Fauleta1cda022016-12-21 08:58:06 +01004139 if (curagent->engine_id == NULL)
4140 curagent->engine_id = generate_pseudo_uuid();
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004141
Christopher Fauletb7426d12018-03-21 14:12:17 +01004142 if (curagent->var_on_error) {
4143 struct arg arg;
4144
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004145 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Fauletb7426d12018-03-21 14:12:17 +01004146 curagent->var_pfx, curagent->var_on_error);
4147
4148 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004149 arg.data.str.area = trash.area;
4150 arg.data.str.data = trash.data;
Christopher Fauletb7426d12018-03-21 14:12:17 +01004151 if (!vars_check_arg(&arg, err)) {
4152 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4153 curagent->id, curagent->var_pfx, curagent->var_on_error, *err);
4154 goto error;
4155 }
4156 }
4157
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004158 if (curagent->var_t_process) {
4159 struct arg arg;
4160
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004161 trash.data = snprintf(trash.area, trash.size, "txn.%s.%s",
Christopher Faulet36bda1c2018-03-22 09:08:20 +01004162 curagent->var_pfx, curagent->var_t_process);
4163
4164 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004165 arg.data.str.area = trash.area;
4166 arg.data.str.data = trash.data;
Christopher Faulet36bda1c2018-03-22 09:08:20 +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_t_process, *err);
4170 goto error;
4171 }
4172 }
4173
4174 if (curagent->var_t_total) {
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_total);
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 Faulet36bda1c2018-03-22 09:08:20 +01004183 if (!vars_check_arg(&arg, err)) {
4184 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4185 curagent->id, curagent->var_pfx, curagent->var_t_process, *err);
4186 goto error;
4187 }
4188 }
4189
Christopher Faulet11610f32017-09-21 10:23:10 +02004190 if (LIST_ISEMPTY(&curmphs) && LIST_ISEMPTY(&curgphs)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004191 ha_warning("Proxy '%s': No message/group used by SPOE agent '%s' declared at %s:%d.\n",
4192 px->id, curagent->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004193 goto finish;
4194 }
4195
Christopher Faulet11610f32017-09-21 10:23:10 +02004196 /* Replace placeholders by the corresponding messages for the SPOE
4197 * agent */
4198 list_for_each_entry(ph, &curmphs, list) {
4199 list_for_each_entry(msg, &curmsgs, list) {
Christopher Fauleta21b0642017-01-09 16:56:23 +01004200 struct spoe_arg *arg;
4201 unsigned int where;
4202
Christopher Faulet11610f32017-09-21 10:23:10 +02004203 if (!strcmp(msg->id, ph->id)) {
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004204 if ((px->cap & (PR_CAP_FE|PR_CAP_BE)) == (PR_CAP_FE|PR_CAP_BE)) {
4205 if (msg->event == SPOE_EV_ON_TCP_REQ_BE)
4206 msg->event = SPOE_EV_ON_TCP_REQ_FE;
4207 if (msg->event == SPOE_EV_ON_HTTP_REQ_BE)
4208 msg->event = SPOE_EV_ON_HTTP_REQ_FE;
4209 }
4210 if (!(px->cap & PR_CAP_FE) && (msg->event == SPOE_EV_ON_CLIENT_SESS ||
4211 msg->event == SPOE_EV_ON_TCP_REQ_FE ||
4212 msg->event == SPOE_EV_ON_HTTP_REQ_FE)) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004213 ha_warning("Proxy '%s': frontend event used on a backend proxy at %s:%d.\n",
4214 px->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004215 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004216 }
4217 if (msg->event == SPOE_EV_NONE) {
Christopher Faulet767a84b2017-11-24 16:50:31 +01004218 ha_warning("Proxy '%s': Ignore SPOE message '%s' without event at %s:%d.\n",
4219 px->id, msg->id, msg->conf.file, msg->conf.line);
Christopher Faulet11610f32017-09-21 10:23:10 +02004220 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004221 }
Christopher Fauleta21b0642017-01-09 16:56:23 +01004222
4223 where = 0;
4224 switch (msg->event) {
4225 case SPOE_EV_ON_CLIENT_SESS:
4226 where |= SMP_VAL_FE_CON_ACC;
4227 break;
4228
4229 case SPOE_EV_ON_TCP_REQ_FE:
4230 where |= SMP_VAL_FE_REQ_CNT;
4231 break;
4232
4233 case SPOE_EV_ON_HTTP_REQ_FE:
4234 where |= SMP_VAL_FE_HRQ_HDR;
4235 break;
4236
4237 case SPOE_EV_ON_TCP_REQ_BE:
4238 if (px->cap & PR_CAP_FE)
4239 where |= SMP_VAL_FE_REQ_CNT;
4240 if (px->cap & PR_CAP_BE)
4241 where |= SMP_VAL_BE_REQ_CNT;
4242 break;
4243
4244 case SPOE_EV_ON_HTTP_REQ_BE:
4245 if (px->cap & PR_CAP_FE)
4246 where |= SMP_VAL_FE_HRQ_HDR;
4247 if (px->cap & PR_CAP_BE)
4248 where |= SMP_VAL_BE_HRQ_HDR;
4249 break;
4250
4251 case SPOE_EV_ON_SERVER_SESS:
4252 where |= SMP_VAL_BE_SRV_CON;
4253 break;
4254
4255 case SPOE_EV_ON_TCP_RSP:
4256 if (px->cap & PR_CAP_FE)
4257 where |= SMP_VAL_FE_RES_CNT;
4258 if (px->cap & PR_CAP_BE)
4259 where |= SMP_VAL_BE_RES_CNT;
4260 break;
4261
4262 case SPOE_EV_ON_HTTP_RSP:
4263 if (px->cap & PR_CAP_FE)
4264 where |= SMP_VAL_FE_HRS_HDR;
4265 if (px->cap & PR_CAP_BE)
4266 where |= SMP_VAL_BE_HRS_HDR;
4267 break;
4268
4269 default:
4270 break;
4271 }
4272
4273 list_for_each_entry(arg, &msg->args, list) {
4274 if (!(arg->expr->fetch->val & where)) {
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004275 memprintf(err, "Ignore SPOE message '%s' at %s:%d: "
Christopher Fauleta21b0642017-01-09 16:56:23 +01004276 "some args extract information from '%s', "
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004277 "none of which is available here ('%s')",
4278 msg->id, msg->conf.file, msg->conf.line,
Christopher Fauleta21b0642017-01-09 16:56:23 +01004279 sample_ckp_names(arg->expr->fetch->use),
4280 sample_ckp_names(where));
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004281 goto error;
Christopher Fauleta21b0642017-01-09 16:56:23 +01004282 }
4283 }
4284
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004285 msg->agent = curagent;
Christopher Faulet11610f32017-09-21 10:23:10 +02004286 LIST_ADDQ(&curagent->events[msg->event], &msg->by_evt);
4287 goto next_mph;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004288 }
4289 }
4290 memprintf(err, "SPOE agent '%s' try to use undefined SPOE message '%s' at %s:%d",
Christopher Faulet11610f32017-09-21 10:23:10 +02004291 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004292 goto error;
Christopher Faulet11610f32017-09-21 10:23:10 +02004293 next_mph:
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004294 continue;
4295 }
4296
Christopher Faulet11610f32017-09-21 10:23:10 +02004297 /* Replace placeholders by the corresponding groups for the SPOE
4298 * agent */
4299 list_for_each_entry(ph, &curgphs, list) {
4300 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4301 if (!strcmp(grp->id, ph->id)) {
4302 grp->agent = curagent;
4303 LIST_DEL(&grp->list);
4304 LIST_ADDQ(&curagent->groups, &grp->list);
4305 goto next_aph;
4306 }
4307 }
4308 memprintf(err, "SPOE agent '%s' try to use undefined SPOE group '%s' at %s:%d",
4309 curagent->id, ph->id, curagent->conf.file, curagent->conf.line);
4310 goto error;
4311 next_aph:
4312 continue;
4313 }
4314
4315 /* Replace placeholders by the corresponding message for each SPOE
4316 * group of the SPOE agent */
4317 list_for_each_entry(grp, &curagent->groups, list) {
4318 list_for_each_entry_safe(ph, phback, &grp->phs, list) {
4319 list_for_each_entry(msg, &curmsgs, list) {
4320 if (!strcmp(msg->id, ph->id)) {
4321 if (msg->group != NULL) {
4322 memprintf(err, "SPOE message '%s' already belongs to "
4323 "the SPOE group '%s' declare at %s:%d",
4324 msg->id, msg->group->id,
4325 msg->group->conf.file,
4326 msg->group->conf.line);
4327 goto error;
4328 }
4329
4330 /* Scope for arguments are not checked for now. We will check
4331 * them only if a rule use the corresponding SPOE group. */
4332 msg->agent = curagent;
4333 msg->group = grp;
4334 LIST_DEL(&ph->list);
4335 LIST_ADDQ(&grp->messages, &msg->by_grp);
4336 goto next_mph_grp;
4337 }
4338 }
4339 memprintf(err, "SPOE group '%s' try to use undefined SPOE message '%s' at %s:%d",
4340 grp->id, ph->id, curagent->conf.file, curagent->conf.line);
4341 goto error;
4342 next_mph_grp:
4343 continue;
4344 }
4345 }
4346
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004347 finish:
Christopher Faulet11610f32017-09-21 10:23:10 +02004348 /* move curmsgs to the agent message list */
4349 curmsgs.n->p = &curagent->messages;
4350 curmsgs.p->n = &curagent->messages;
4351 curagent->messages = curmsgs;
4352 LIST_INIT(&curmsgs);
4353
Christopher Faulet7ee86672017-09-19 11:08:28 +02004354 conf->id = strdup(engine ? engine : curagent->id);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004355 conf->agent = curagent;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004356
4357 /* Start agent's proxy initialization here. It will be finished during
4358 * the filter init. */
4359 memset(&conf->agent_fe, 0, sizeof(conf->agent_fe));
4360 init_new_proxy(&conf->agent_fe);
4361 conf->agent_fe.id = conf->agent->id;
4362 conf->agent_fe.parent = conf->agent;
Christopher Faulet0e0f0852018-03-26 17:20:36 +02004363 conf->agent_fe.options |= curpxopts;
4364 conf->agent_fe.options2 |= curpxopts2;
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004365
4366 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4367 LIST_DEL(&logsrv->list);
4368 LIST_ADDQ(&conf->agent_fe.logsrvs, &logsrv->list);
4369 }
4370
Christopher Faulet11610f32017-09-21 10:23:10 +02004371 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4372 LIST_DEL(&ph->list);
4373 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004374 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004375 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4376 LIST_DEL(&ph->list);
4377 spoe_release_placeholder(ph);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004378 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004379 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4380 struct arg arg;
4381
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004382 trash.data = snprintf(trash.area, trash.size, "proc.%s.%s",
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004383 curagent->var_pfx, vph->name);
4384
4385 arg.type = ARGT_STR;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02004386 arg.data.str.area = trash.area;
4387 arg.data.str.data = trash.data;
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004388 if (!vars_check_arg(&arg, err)) {
4389 memprintf(err, "SPOE agent '%s': failed to register variable %s.%s (%s)",
4390 curagent->id, curagent->var_pfx, vph->name, *err);
4391 goto error;
4392 }
4393
4394 LIST_DEL(&vph->list);
4395 free(vph->name);
4396 free(vph);
4397 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004398 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4399 LIST_DEL(&grp->list);
4400 spoe_release_group(grp);
4401 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004402 *cur_arg = pos;
Christopher Faulet3b386a32017-02-23 10:17:15 +01004403 fconf->id = spoe_filter_id;
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004404 fconf->ops = &spoe_ops;
4405 fconf->conf = conf;
4406 return 0;
4407
4408 error:
Christopher Faulet8ef75252017-02-20 22:56:03 +01004409 spoe_release_agent(curagent);
Christopher Faulet11610f32017-09-21 10:23:10 +02004410 list_for_each_entry_safe(ph, phback, &curmphs, list) {
4411 LIST_DEL(&ph->list);
4412 spoe_release_placeholder(ph);
4413 }
4414 list_for_each_entry_safe(ph, phback, &curgphs, list) {
4415 LIST_DEL(&ph->list);
4416 spoe_release_placeholder(ph);
4417 }
Christopher Faulet336d3ef2017-12-22 10:00:55 +01004418 list_for_each_entry_safe(vph, vphback, &curvars, list) {
4419 LIST_DEL(&vph->list);
4420 free(vph->name);
4421 free(vph);
4422 }
Christopher Faulet11610f32017-09-21 10:23:10 +02004423 list_for_each_entry_safe(grp, grpback, &curgrps, list) {
4424 LIST_DEL(&grp->list);
4425 spoe_release_group(grp);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004426 }
4427 list_for_each_entry_safe(msg, msgback, &curmsgs, list) {
4428 LIST_DEL(&msg->list);
Christopher Faulet8ef75252017-02-20 22:56:03 +01004429 spoe_release_message(msg);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004430 }
Christopher Faulet7250b8f2018-03-26 17:19:01 +02004431 list_for_each_entry_safe(logsrv, logsrvback, &curlogsrvs, list) {
4432 LIST_DEL(&logsrv->list);
4433 free(logsrv);
4434 }
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004435 free(conf);
4436 return -1;
4437}
4438
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004439/* Send message of a SPOE group. This is the action_ptr callback of a rule
4440 * associated to a "send-spoe-group" action.
4441 *
4442 * It returns ACT_RET_CONT is processing is finished without error, it returns
4443 * ACT_RET_YIELD if the action is in progress. Otherwise it returns
4444 * ACT_RET_ERR. */
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004445static enum act_return
4446spoe_send_group(struct act_rule *rule, struct proxy *px,
4447 struct session *sess, struct stream *s, int flags)
4448{
4449 struct filter *filter;
4450 struct spoe_agent *agent = NULL;
4451 struct spoe_group *group = NULL;
4452 struct spoe_context *ctx = NULL;
4453 int ret, dir;
4454
4455 list_for_each_entry(filter, &s->strm_flt.filters, list) {
4456 if (filter->config == rule->arg.act.p[0]) {
4457 agent = rule->arg.act.p[2];
4458 group = rule->arg.act.p[3];
4459 ctx = filter->ctx;
4460 break;
4461 }
4462 }
4463 if (agent == NULL || group == NULL || ctx == NULL)
4464 return ACT_RET_ERR;
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004465 if (ctx->state == SPOE_CTX_ST_NONE)
4466 return ACT_RET_CONT;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004467
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004468 switch (rule->from) {
4469 case ACT_F_TCP_REQ_SES: dir = SMP_OPT_DIR_REQ; break;
4470 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
4471 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
4472 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
4473 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
4474 default:
4475 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4476 " - internal error while execute spoe-send-group\n",
4477 (int)now.tv_sec, (int)now.tv_usec, agent->id,
4478 __FUNCTION__, s);
4479 send_log(px, LOG_ERR, "SPOE: [%s] internal error while execute spoe-send-group\n",
4480 agent->id);
4481 return ACT_RET_CONT;
4482 }
4483
4484 ret = spoe_process_group(s, ctx, group, dir);
4485 if (ret == 1)
4486 return ACT_RET_CONT;
4487 else if (ret == 0) {
4488 if (flags & ACT_FLAG_FINAL) {
4489 SPOE_PRINTF(stderr, "%d.%06d [SPOE/%-15s] %s: stream=%p"
4490 " - failed to process group '%s': interrupted by caller\n",
4491 (int)now.tv_sec, (int)now.tv_usec,
4492 agent->id, __FUNCTION__, s, group->id);
4493 ctx->status_code = SPOE_CTX_ERR_INTERRUPT;
Christopher Faulet6f9ea4f2018-01-24 16:13:48 +01004494 spoe_stop_processing(agent, ctx);
Christopher Fauletcaf2fec2018-04-04 10:25:50 +02004495 spoe_handle_processing_error(s, agent, ctx, dir);
Christopher Faulet344c4ab2017-09-22 10:20:13 +02004496 return ACT_RET_CONT;
4497 }
4498 return ACT_RET_YIELD;
4499 }
4500 else
4501 return ACT_RET_ERR;
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004502}
4503
4504/* Check an "send-spoe-group" action. Here, we'll try to find the real SPOE
4505 * group associated to <rule>. The format of an rule using 'send-spoe-group'
4506 * action should be:
4507 *
4508 * (http|tcp)-(request|response) send-spoe-group <engine-id> <group-id>
4509 *
4510 * So, we'll loop on each configured SPOE filter for the proxy <px> to find the
4511 * SPOE engine matching <engine-id>. And then, we'll try to find the good group
4512 * matching <group-id>. Finally, we'll check all messages referenced by the SPOE
4513 * group.
4514 *
4515 * The function returns 1 in success case, otherwise, it returns 0 and err is
4516 * filled.
4517 */
4518static int
4519check_send_spoe_group(struct act_rule *rule, struct proxy *px, char **err)
4520{
4521 struct flt_conf *fconf;
4522 struct spoe_config *conf;
4523 struct spoe_agent *agent = NULL;
4524 struct spoe_group *group;
4525 struct spoe_message *msg;
4526 char *engine_id = rule->arg.act.p[0];
4527 char *group_id = rule->arg.act.p[1];
4528 unsigned int where = 0;
4529
4530 switch (rule->from) {
4531 case ACT_F_TCP_REQ_SES: where = SMP_VAL_FE_SES_ACC; break;
4532 case ACT_F_TCP_REQ_CNT: where = SMP_VAL_FE_REQ_CNT; break;
4533 case ACT_F_TCP_RES_CNT: where = SMP_VAL_BE_RES_CNT; break;
4534 case ACT_F_HTTP_REQ: where = SMP_VAL_FE_HRQ_HDR; break;
4535 case ACT_F_HTTP_RES: where = SMP_VAL_BE_HRS_HDR; break;
4536 default:
4537 memprintf(err,
4538 "internal error, unexpected rule->from=%d, please report this bug!",
4539 rule->from);
4540 goto error;
4541 }
4542
4543 /* Try to find the SPOE engine by checking all SPOE filters for proxy
4544 * <px> */
4545 list_for_each_entry(fconf, &px->filter_configs, list) {
4546 conf = fconf->conf;
4547
4548 /* This is not an SPOE filter */
4549 if (fconf->id != spoe_filter_id)
4550 continue;
4551
4552 /* This is the good engine */
4553 if (!strcmp(conf->id, engine_id)) {
4554 agent = conf->agent;
4555 break;
4556 }
4557 }
4558 if (agent == NULL) {
4559 memprintf(err, "unable to find SPOE engine '%s' used by the send-spoe-group '%s'",
4560 engine_id, group_id);
4561 goto error;
4562 }
4563
4564 /* Try to find the right group */
4565 list_for_each_entry(group, &agent->groups, list) {
4566 /* This is the good group */
4567 if (!strcmp(group->id, group_id))
4568 break;
4569 }
4570 if (&group->list == &agent->groups) {
4571 memprintf(err, "unable to find SPOE group '%s' into SPOE engine '%s' configuration",
4572 group_id, engine_id);
4573 goto error;
4574 }
4575
4576 /* Ok, we found the group, we need to check messages and their
4577 * arguments */
4578 list_for_each_entry(msg, &group->messages, by_grp) {
4579 struct spoe_arg *arg;
4580
4581 list_for_each_entry(arg, &msg->args, list) {
4582 if (!(arg->expr->fetch->val & where)) {
4583 memprintf(err, "Invalid SPOE message '%s' used by SPOE group '%s' at %s:%d: "
4584 "some args extract information from '%s',"
4585 "none of which is available here ('%s')",
4586 msg->id, group->id, msg->conf.file, msg->conf.line,
4587 sample_ckp_names(arg->expr->fetch->use),
4588 sample_ckp_names(where));
4589 goto error;
4590 }
4591 }
4592 }
4593
4594 free(engine_id);
4595 free(group_id);
4596 rule->arg.act.p[0] = fconf; /* Associate filter config with the rule */
4597 rule->arg.act.p[1] = conf; /* Associate SPOE config with the rule */
4598 rule->arg.act.p[2] = agent; /* Associate SPOE agent with the rule */
4599 rule->arg.act.p[3] = group; /* Associate SPOE group with the rule */
4600 return 1;
4601
4602 error:
4603 free(engine_id);
4604 free(group_id);
4605 return 0;
4606}
4607
4608/* Parse 'send-spoe-group' action following the format:
4609 *
4610 * ... send-spoe-group <engine-id> <group-id>
4611 *
4612 * It returns ACT_RET_PRS_ERR if fails and <err> is filled with an error
4613 * message. Otherwise, it returns ACT_RET_PRS_OK and parsing engine and group
4614 * ids are saved and used later, when the rule will be checked.
4615 */
4616static enum act_parse_ret
4617parse_send_spoe_group(const char **args, int *orig_arg, struct proxy *px,
4618 struct act_rule *rule, char **err)
4619{
4620 if (!*args[*orig_arg] || !*args[*orig_arg+1] ||
4621 (*args[*orig_arg+2] && strcmp(args[*orig_arg+2], "if") != 0 && strcmp(args[*orig_arg+2], "unless") != 0)) {
4622 memprintf(err, "expects 2 arguments: <engine-id> <group-id>");
4623 return ACT_RET_PRS_ERR;
4624 }
4625 rule->arg.act.p[0] = strdup(args[*orig_arg]); /* Copy the SPOE engine id */
4626 rule->arg.act.p[1] = strdup(args[*orig_arg+1]); /* Cope the SPOE group id */
4627
4628 (*orig_arg) += 2;
4629
4630 rule->action = ACT_CUSTOM;
4631 rule->action_ptr = spoe_send_group;
4632 rule->check_ptr = check_send_spoe_group;
4633 return ACT_RET_PRS_OK;
4634}
4635
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004636
4637/* Declare the filter parser for "spoe" keyword */
4638static struct flt_kw_list flt_kws = { "SPOE", { }, {
4639 { "spoe", parse_spoe_flt, NULL },
4640 { NULL, NULL, NULL },
4641 }
4642};
4643
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004644/* Delcate the action parser for "spoe-action" keyword */
4645static struct action_kw_list tcp_req_action_kws = { { }, {
4646 { "send-spoe-group", parse_send_spoe_group },
4647 { /* END */ },
4648 }
4649};
4650static struct action_kw_list tcp_res_action_kws = { { }, {
4651 { "send-spoe-group", parse_send_spoe_group },
4652 { /* END */ },
4653 }
4654};
4655static struct action_kw_list http_req_action_kws = { { }, {
4656 { "send-spoe-group", parse_send_spoe_group },
4657 { /* END */ },
4658 }
4659};
4660static struct action_kw_list http_res_action_kws = { { }, {
4661 { "send-spoe-group", parse_send_spoe_group },
4662 { /* END */ },
4663 }
4664};
4665
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004666__attribute__((constructor))
4667static void __spoe_init(void)
4668{
4669 flt_register_keywords(&flt_kws);
Christopher Faulet76c09ef2017-09-21 11:03:52 +02004670 tcp_req_cont_keywords_register(&tcp_req_action_kws);
4671 tcp_res_cont_keywords_register(&tcp_res_action_kws);
4672 http_req_keywords_register(&http_req_action_kws);
4673 http_res_keywords_register(&http_res_action_kws);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004674
Willy Tarreaubafbe012017-11-24 17:34:44 +01004675 pool_head_spoe_ctx = create_pool("spoe_ctx", sizeof(struct spoe_context), MEM_F_SHARED);
4676 pool_head_spoe_appctx = create_pool("spoe_appctx", sizeof(struct spoe_appctx), MEM_F_SHARED);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004677}
4678
4679__attribute__((destructor))
4680static void
4681__spoe_deinit(void)
4682{
Willy Tarreaubafbe012017-11-24 17:34:44 +01004683 pool_destroy(pool_head_spoe_ctx);
4684 pool_destroy(pool_head_spoe_appctx);
Christopher Fauletf7e4e7e2016-10-27 22:29:49 +02004685}